Skip to content

[POC] chore: use getHeaders #67

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/StorageClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { StorageBucketApi, StorageFileApi } from './lib'
import { Fetch } from './lib/fetch'
import { noopPromise } from './lib/helpers'

export class StorageClient extends StorageBucketApi {
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
super(url, headers, fetch)
constructor(
url: string,
getHeaders: () => Promise<{ [key: string]: string }> | { [key: string]: string } = noopPromise,
fetch?: Fetch
) {
super(url, getHeaders, fetch)
}

/**
Expand All @@ -12,6 +17,6 @@ export class StorageClient extends StorageBucketApi {
* @param id The bucket id to operate on.
*/
from(id: string): StorageFileApi {
return new StorageFileApi(this.url, this.headers, id, this.fetch)
return new StorageFileApi(this.url, this.getHeaders, id, this.fetch)
}
}
30 changes: 20 additions & 10 deletions src/lib/StorageBucketApi.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { DEFAULT_HEADERS } from './constants'
import { Fetch, get, post, put, remove } from './fetch'
import { resolveFetch } from './helpers'
import { noopPromise, resolveFetch } from './helpers'
import { Bucket } from './types'

export class StorageBucketApi {
protected url: string
protected headers: { [key: string]: string }
protected getHeaders: () => Promise<{ [key: string]: string }> | { [key: string]: string }
protected fetch: Fetch

constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
constructor(
url: string,
getHeaders: () => Promise<{ [key: string]: string }> | { [key: string]: string } = noopPromise,
fetch?: Fetch
) {
this.url = url
this.headers = { ...DEFAULT_HEADERS, ...headers }
this.getHeaders = async () => {
const headers = await Promise.resolve(getHeaders())

return { ...DEFAULT_HEADERS, ...headers }
}
this.fetch = resolveFetch(fetch)
}

Expand All @@ -19,7 +27,7 @@ export class StorageBucketApi {
*/
async listBuckets(): Promise<{ data: Bucket[] | null; error: Error | null }> {
try {
const data = await get(this.fetch, `${this.url}/bucket`, { headers: this.headers })
const data = await get(this.fetch, `${this.url}/bucket`, { headers: await this.getHeaders() })
return { data, error: null }
} catch (error) {
return { data: null, error }
Expand All @@ -33,7 +41,9 @@ export class StorageBucketApi {
*/
async getBucket(id: string): Promise<{ data: Bucket | null; error: Error | null }> {
try {
const data = await get(this.fetch, `${this.url}/bucket/${id}`, { headers: this.headers })
const data = await get(this.fetch, `${this.url}/bucket/${id}`, {
headers: await this.getHeaders(),
})
return { data, error: null }
} catch (error) {
return { data: null, error }
Expand All @@ -55,7 +65,7 @@ export class StorageBucketApi {
this.fetch,
`${this.url}/bucket`,
{ id, name: id, public: options.public },
{ headers: this.headers }
{ headers: await this.getHeaders() }
)
return { data: data.name, error: null }
} catch (error) {
Expand All @@ -77,7 +87,7 @@ export class StorageBucketApi {
this.fetch,
`${this.url}/bucket/${id}`,
{ id, name: id, public: options.public },
{ headers: this.headers }
{ headers: await this.getHeaders() }
)
return { data, error: null }
} catch (error) {
Expand All @@ -98,7 +108,7 @@ export class StorageBucketApi {
this.fetch,
`${this.url}/bucket/${id}/empty`,
{},
{ headers: this.headers }
{ headers: await this.getHeaders() }
)
return { data, error: null }
} catch (error) {
Expand All @@ -120,7 +130,7 @@ export class StorageBucketApi {
this.fetch,
`${this.url}/bucket/${id}`,
{},
{ headers: this.headers }
{ headers: await this.getHeaders() }
)
return { data, error: null }
} catch (error) {
Expand Down
28 changes: 14 additions & 14 deletions src/lib/StorageFileApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fetch, FetchParameters, get, post, remove } from './fetch'
import { resolveFetch } from './helpers'
import { noopPromise, resolveFetch } from './helpers'
import { FileObject, FileOptions, SearchOptions } from './types'

const DEFAULT_SEARCH_OPTIONS = {
Expand All @@ -19,18 +19,18 @@ const DEFAULT_FILE_OPTIONS: FileOptions = {

export class StorageFileApi {
protected url: string
protected headers: { [key: string]: string }
protected getHeaders: () => Promise<{ [key: string]: string }> | { [key: string]: string }
protected bucketId?: string
protected fetch: Fetch

constructor(
url: string,
headers: { [key: string]: string } = {},
getHeaders: () => Promise<{ [key: string]: string }> | { [key: string]: string } = noopPromise,
bucketId?: string,
fetch?: Fetch
) {
this.url = url
this.headers = headers
this.getHeaders = getHeaders
this.bucketId = bucketId
this.fetch = resolveFetch(fetch)
}
Expand Down Expand Up @@ -66,7 +66,7 @@ export class StorageFileApi {
let body
const options = { ...DEFAULT_FILE_OPTIONS, ...fileOptions }
const headers: Record<string, string> = {
...this.headers,
...(await this.getHeaders()),
...(method === 'POST' && { 'x-upsert': String(options.upsert as boolean) }),
}

Expand Down Expand Up @@ -175,7 +175,7 @@ export class StorageFileApi {
this.fetch,
`${this.url}/object/move`,
{ bucketId: this.bucketId, sourceKey: fromPath, destinationKey: toPath },
{ headers: this.headers }
{ headers: await this.getHeaders() }
)
return { data, error: null }
} catch (error) {
Expand All @@ -198,7 +198,7 @@ export class StorageFileApi {
this.fetch,
`${this.url}/object/copy`,
{ bucketId: this.bucketId, sourceKey: fromPath, destinationKey: toPath },
{ headers: this.headers }
{ headers: await this.getHeaders() }
)
return { data, error: null }
} catch (error) {
Expand Down Expand Up @@ -226,7 +226,7 @@ export class StorageFileApi {
this.fetch,
`${this.url}/object/sign/${_path}`,
{ expiresIn },
{ headers: this.headers }
{ headers: await this.getHeaders() }
)
const signedURL = `${this.url}${data.signedURL}`
data = { signedURL }
Expand Down Expand Up @@ -254,7 +254,7 @@ export class StorageFileApi {
this.fetch,
`${this.url}/object/sign/${this.bucketId}`,
{ expiresIn, paths },
{ headers: this.headers }
{ headers: await this.getHeaders() }
)
return {
data: data.map((datum: { signedURL: string }) => ({
Expand All @@ -277,7 +277,7 @@ export class StorageFileApi {
try {
const _path = this._getFinalPath(path)
const res = await get(this.fetch, `${this.url}/object/${_path}`, {
headers: this.headers,
headers: await this.getHeaders(),
noResolveJson: true,
})
const data = await res.blob()
Expand Down Expand Up @@ -320,7 +320,7 @@ export class StorageFileApi {
this.fetch,
`${this.url}/object/${this.bucketId}`,
{ prefixes: paths },
{ headers: this.headers }
{ headers: await this.getHeaders() }
)
return { data, error: null }
} catch (error) {
Expand All @@ -334,7 +334,7 @@ export class StorageFileApi {
*/
// async getMetadata(id: string): Promise<{ data: Metadata | null; error: Error | null }> {
// try {
// const data = await get(`${this.url}/metadata/${id}`, { headers: this.headers })
// const data = await get(`${this.url}/metadata/${id}`, { headers: await this.getHeaders() })
// return { data, error: null }
// } catch (error) {
// return { data: null, error }
Expand All @@ -351,7 +351,7 @@ export class StorageFileApi {
// meta: Metadata
// ): Promise<{ data: Metadata | null; error: Error | null }> {
// try {
// const data = await post(`${this.url}/metadata/${id}`, { ...meta }, { headers: this.headers })
// const data = await post(`${this.url}/metadata/${id}`, { ...meta }, { headers: await this.getHeaders() })
// return { data, error: null }
// } catch (error) {
// return { data: null, error }
Expand All @@ -375,7 +375,7 @@ export class StorageFileApi {
this.fetch,
`${this.url}/object/list/${this.bucketId}`,
body,
{ headers: this.headers },
{ headers: await this.getHeaders() },
parameters
)
return { data, error: null }
Expand Down
2 changes: 2 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export const resolveFetch = (customFetch?: Fetch): Fetch => {
}
return (...args) => _fetch(...args)
}

export const noopPromise = () => Promise.resolve({})
2 changes: 1 addition & 1 deletion test/storageApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const URL = 'http://localhost:8000/storage/v1'
const KEY =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTYwMzk2ODgzNCwiZXhwIjoyNTUwNjUzNjM0LCJhdWQiOiIiLCJzdWIiOiIzMTdlYWRjZS02MzFhLTQ0MjktYTBiYi1mMTlhN2E1MTdiNGEiLCJSb2xlIjoicG9zdGdyZXMifQ.pZobPtp6gDcX0UbzMmG3FHSlg4m4Q-22tKtGWalOrNo'

const storage = new StorageBucketApi(URL, { Authorization: `Bearer ${KEY}` })
const storage = new StorageBucketApi(URL, () => ({ Authorization: `Bearer ${KEY}` }))
const newBucketName = `my-new-bucket-${Date.now()}`

test('Build to succeed', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/storageFileApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const URL = 'http://localhost:8000/storage/v1'
const KEY =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTYwMzk2ODgzNCwiZXhwIjoyNTUwNjUzNjM0LCJhdWQiOiIiLCJzdWIiOiIzMTdlYWRjZS02MzFhLTQ0MjktYTBiYi1mMTlhN2E1MTdiNGEiLCJSb2xlIjoicG9zdGdyZXMifQ.pZobPtp6gDcX0UbzMmG3FHSlg4m4Q-22tKtGWalOrNo'

const storage = new StorageClient(URL, { Authorization: `Bearer ${KEY}` })
const storage = new StorageClient(URL, () => ({ Authorization: `Bearer ${KEY}` }))
const newBucketName = 'my-new-public-bucket'

test('get public URL', async () => {
Expand Down