diff --git a/README.md b/README.md index 27e081c..874e441 100644 --- a/README.md +++ b/README.md @@ -156,20 +156,24 @@ const storageClient = new StorageClient(STORAGE_URL, { const { data, error } = await storageClient.from('public-bucket').getPublicUrl('path/to/file') ``` +- Check if a file exists: + + ```js + const { data, error } = await storageClient.from('bucket').exists('path/to/file') + // data will be true if the file exists, false otherwise + ``` + ### Error Handling Supplying `.throwOnError()` will throw errors instead of returning them as a property on the response object. - ```js - try { - const { data } = await storageClient - .from('bucket') - .throwOnError() - .download('path/to/file') - } catch (error) { - console.error(error) - } - ``` +```js +try { + const { data } = await storageClient.from('bucket').throwOnError().download('path/to/file') +} catch (error) { + console.error(error) +} +``` ## Sponsors diff --git a/src/packages/StorageFileApi.ts b/src/packages/StorageFileApi.ts index a8e2819..4479ca4 100644 --- a/src/packages/StorageFileApi.ts +++ b/src/packages/StorageFileApi.ts @@ -1,4 +1,4 @@ -import { isStorageError, StorageError, StorageUnknownError } from '../lib/errors' +import { isStorageError, StorageError, StorageUnknownError, StorageApiError } from '../lib/errors' import { Fetch, get, head, post, put, remove } from '../lib/fetch' import { recursiveToCamel, resolveFetch } from '../lib/helpers' import { @@ -604,11 +604,19 @@ export default class StorageFileApi { if (this.shouldThrowOnError) { throw error } - if (isStorageError(error) && error instanceof StorageUnknownError) { - const originalError = (error.originalError as unknown) as { status: number } + if (isStorageError(error)) { + // Check for both StorageApiError (which has status directly) and StorageUnknownError (with originalError) + let status: number | undefined + + if (error instanceof StorageApiError) { + status = error.status + } else if (error instanceof StorageUnknownError) { + const originalError = (error.originalError as unknown) as { status: number } + status = originalError?.status + } - if ([400, 404].includes(originalError?.status)) { - return { data: false, error } + if (status && [400, 404].includes(status)) { + return { data: false, error: null } } }