Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve getPublicUrl #62

Merged
merged 10 commits into from
Jul 4, 2022
Merged
2 changes: 1 addition & 1 deletion src/lib/StorageBucketApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class StorageBucketApi {
/**
* Updates a new Storage bucket
*
* @param id A unique identifier for the bucket you are creating.
* @param id A unique identifier for the bucket you are updating.
*/
async updateBucket(
id: string,
Expand Down
32 changes: 4 additions & 28 deletions src/lib/StorageFileApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,37 +388,13 @@ export class StorageFileApi {
}

/**
* Retrieve URLs for assets in public buckets
* Retrieve URLs for assets in public buckets and encapsulates it in a return object
*
* @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
}
error: null
publicURL: string
}
| {
data: null
error: StorageError
publicURL: null
} {
try {
const _path = this._getFinalPath(path)
const publicURL = `${this.url}/object/public/${_path}`
const data = { publicURL }
return { data, error: null, publicURL }
} catch (error) {
if (isStorageError(error)) {
return { data: null, error, publicURL: null }
}

throw error
}
getPublicUrl(path: string): string {
const _path = this._getFinalPath(path)
return `${this.url}/object/public/${_path}`
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const resolveFetch = (customFetch?: Fetch): Fetch => {
if (customFetch) {
_fetch = customFetch
} else if (typeof fetch === 'undefined') {
_fetch = (crossFetch as unknown) as Fetch
_fetch = async (...args) => await (await import('cross-fetch')).fetch(...args)
} else {
_fetch = fetch
}
Expand Down
8 changes: 1 addition & 7 deletions test/__snapshots__/storageFileApi.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`get public URL 1`] = `
Object {
"publicURL": "http://localhost:8000/storage/v1/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"`;
exports[`get public URL 1`] = `"http://localhost:8000/storage/v1/object/public/my-new-public-bucket/profiles/myUniqueUserId/profile.png"`;
6 changes: 2 additions & 4 deletions test/storageFileApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ const KEY =
const storage = new StorageClient(URL, { Authorization: `Bearer ${KEY}` })
const newBucketName = 'my-new-public-bucket'

test('get public URL', async () => {
test('get public URL', () => {
const res = storage.from(newBucketName).getPublicUrl('profiles/myUniqueUserId/profile.png')
expect(res.error).toBeNull()
expect(res.data).toMatchSnapshot()
expect(res.publicURL).toMatchSnapshot()
expect(res).toMatchSnapshot()
})
2 changes: 1 addition & 1 deletion tsconfig.module.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"module": "ES2015",
"module": "ES2020",
"outDir": "dist/module"
}
}