diff --git a/src/lib/StorageFileApi.ts b/src/lib/StorageFileApi.ts index 5e2655c..418b9c7 100644 --- a/src/lib/StorageFileApi.ts +++ b/src/lib/StorageFileApi.ts @@ -178,6 +178,29 @@ export class StorageFileApi { } } + /** + * Retrieve URLs for assets in public buckets + * + * @param path The file path to be downloaded, including the path and file name. For example `folder/image.png`. + */ + getPublicUrl( + path: string + ): { + data: { publicURL: string } | null + error: Error | null + publicURL: string | null + } { + try { + const _path = this._getFinalPath(path) + let publicURL = `/object/public/${_path}` + const data = { publicURL } + publicURL = `${this.url}${publicURL}` + return { data, error: null, publicURL } + } catch (error) { + return { data: null, error, publicURL: null } + } + } + /** * Deletes files within the same bucket * diff --git a/test/__snapshots__/storageFileApi.test.ts.snap b/test/__snapshots__/storageFileApi.test.ts.snap new file mode 100644 index 0000000..2707736 --- /dev/null +++ b/test/__snapshots__/storageFileApi.test.ts.snap @@ -0,0 +1,9 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`get public URL 1`] = ` +Object { + "publicURL": "/object/public/my-new-public-bucket/profiles/myUniqueUserId/profile.png", +} +`; + +exports[`get public URL 2`] = `"http://localhost:8000/storage/v1/object/public/my-new-public-bucket/profiles/myUniqueUserId/profile.png"`; diff --git a/test/storageFileApi.test.ts b/test/storageFileApi.test.ts new file mode 100644 index 0000000..09fb954 --- /dev/null +++ b/test/storageFileApi.test.ts @@ -0,0 +1,16 @@ +import { SupabaseStorageClient } from '../src/index' + +// TODO: need to setup storage-api server for this test +const URL = 'http://localhost:8000/storage/v1' +const KEY = + 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTYwMzk2ODgzNCwiZXhwIjoyNTUwNjUzNjM0LCJhdWQiOiIiLCJzdWIiOiIzMTdlYWRjZS02MzFhLTQ0MjktYTBiYi1mMTlhN2E1MTdiNGEiLCJSb2xlIjoicG9zdGdyZXMifQ.pZobPtp6gDcX0UbzMmG3FHSlg4m4Q-22tKtGWalOrNo' + +const storage = new SupabaseStorageClient(URL, { Authorization: `Bearer ${KEY}` }) +const newBucketName = 'my-new-public-bucket' + +test('get public URL', async () => { + const res = storage.from(newBucketName).getPublicUrl('profiles/myUniqueUserId/profile.png') + expect(res.error).toBeNull() + expect(res.data).toMatchSnapshot() + expect(res.publicURL).toMatchSnapshot() +})