From f988c4685e701244b25828c3655f8da0ee8a687a Mon Sep 17 00:00:00 2001 From: Soheil Nazari Date: Sun, 27 Aug 2023 17:54:58 +0200 Subject: [PATCH] Added new response type --- src/lib/types.ts | 27 +++++++++ src/packages/StorageBucketApi.ts | 39 ++----------- src/packages/StorageFileApi.ts | 97 +++----------------------------- 3 files changed, 40 insertions(+), 123 deletions(-) diff --git a/src/lib/types.ts b/src/lib/types.ts index fbccbb9..1d48cbf 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,3 +1,5 @@ +import { StorageError } from './errors' + export interface Bucket { id: string name: string @@ -109,3 +111,28 @@ export interface TransformOptions { */ format?: 'origin' } + +type StorageUploadResponseData = + | { path: string } + | { message: string } + | { signedUrl: string } + | { + signedUrl: string + token: string + path: string + } + | { + error: string | null + path: string | null + signedUrl: string + }[] + +export type StorageUploadResponse = + | { + data: StorageUploadResponseData + error: null + } + | { + data: null + error: StorageError + } diff --git a/src/packages/StorageBucketApi.ts b/src/packages/StorageBucketApi.ts index 3a36b82..0c4ee1b 100644 --- a/src/packages/StorageBucketApi.ts +++ b/src/packages/StorageBucketApi.ts @@ -2,7 +2,7 @@ import { DEFAULT_HEADERS } from '../lib/constants' import { isStorageError, StorageError } from '../lib/errors' import { Fetch, get, post, put, remove } from '../lib/fetch' import { resolveFetch } from '../lib/helpers' -import { Bucket } from '../lib/types' +import { Bucket, StorageUploadResponse } from '../lib/types' export default class StorageBucketApi { protected url: string @@ -143,16 +143,7 @@ export default class StorageBucketApi { fileSizeLimit?: number | string | null allowedMimeTypes?: string[] | null } - ): Promise< - | { - data: { message: string } - error: null - } - | { - data: null - error: StorageError - } - > { + ): Promise { try { const data = await put( this.fetch, @@ -181,18 +172,7 @@ export default class StorageBucketApi { * * @param id The unique identifier of the bucket you would like to empty. */ - async emptyBucket( - id: string - ): Promise< - | { - data: { message: string } - error: null - } - | { - data: null - error: StorageError - } - > { + async emptyBucket(id: string): Promise { try { const data = await post( this.fetch, @@ -216,18 +196,7 @@ export default class StorageBucketApi { * * @param id The unique identifier of the bucket you would like to delete. */ - async deleteBucket( - id: string - ): Promise< - | { - data: { message: string } - error: null - } - | { - data: null - error: StorageError - } - > { + async deleteBucket(id: string): Promise { try { const data = await remove( this.fetch, diff --git a/src/packages/StorageFileApi.ts b/src/packages/StorageFileApi.ts index 978350f..f21aa77 100644 --- a/src/packages/StorageFileApi.ts +++ b/src/packages/StorageFileApi.ts @@ -7,6 +7,7 @@ import { SearchOptions, FetchParameters, TransformOptions, + StorageUploadResponse, } from '../lib/types' const DEFAULT_SEARCH_OPTIONS = { @@ -66,16 +67,7 @@ export default class StorageFileApi { path: string, fileBody: FileBody, fileOptions?: FileOptions - ): Promise< - | { - data: { path: string } - error: null - } - | { - data: null - error: StorageError - } - > { + ): Promise { try { let body const options = { ...DEFAULT_FILE_OPTIONS, ...fileOptions } @@ -134,16 +126,7 @@ export default class StorageFileApi { path: string, fileBody: FileBody, fileOptions?: FileOptions - ): Promise< - | { - data: { path: string } - error: null - } - | { - data: null - error: StorageError - } - > { + ): Promise { return this.uploadOrUpdate('POST', path, fileBody, fileOptions) } @@ -216,18 +199,7 @@ export default class StorageFileApi { * They are valid for 2 hours. * @param path The file path, including the current file name. For example `folder/image.png`. */ - async createSignedUploadUrl( - path: string - ): Promise< - | { - data: { signedUrl: string; token: string; path: string } - error: null - } - | { - data: null - error: StorageError - } - > { + async createSignedUploadUrl(path: string): Promise { try { let _path = this._getFinalPath(path) @@ -276,16 +248,7 @@ export default class StorageFileApi { | URLSearchParams | string, fileOptions?: FileOptions - ): Promise< - | { - data: { path: string } - error: null - } - | { - data: null - error: StorageError - } - > { + ): Promise { return this.uploadOrUpdate('PUT', path, fileBody, fileOptions) } @@ -295,19 +258,7 @@ export default class StorageFileApi { * @param fromPath The original file path, including the current file name. For example `folder/image.png`. * @param toPath The new file path, including the new file name. For example `folder/image-new.png`. */ - async move( - fromPath: string, - toPath: string - ): Promise< - | { - data: { message: string } - error: null - } - | { - data: null - error: StorageError - } - > { + async move(fromPath: string, toPath: string): Promise { try { const data = await post( this.fetch, @@ -331,19 +282,7 @@ export default class StorageFileApi { * @param fromPath The original file path, including the current file name. For example `folder/image.png`. * @param toPath The new file path, including the new file name. For example `folder/image-copy.png`. */ - async copy( - fromPath: string, - toPath: string - ): Promise< - | { - data: { path: string } - error: null - } - | { - data: null - error: StorageError - } - > { + async copy(fromPath: string, toPath: string): Promise { try { const data = await post( this.fetch, @@ -373,16 +312,7 @@ export default class StorageFileApi { path: string, expiresIn: number, options?: { download?: string | boolean; transform?: TransformOptions } - ): Promise< - | { - data: { signedUrl: string } - error: null - } - | { - data: null - error: StorageError - } - > { + ): Promise { try { let _path = this._getFinalPath(path) @@ -418,16 +348,7 @@ export default class StorageFileApi { paths: string[], expiresIn: number, options?: { download: string | boolean } - ): Promise< - | { - data: { error: string | null; path: string | null; signedUrl: string }[] - error: null - } - | { - data: null - error: StorageError - } - > { + ): Promise { try { const data = await post( this.fetch,