8000 feat: encode store name + check for `fetch` by eduardoboucas · Pull Request #73 · netlify/blobs · GitHub
[go: up one dir, main page]

Skip to content
8000

feat: encode store name + check for fetch #73

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

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: throw error when fetch is not available
  • Loading branch information
eduardoboucas committed Oct 19, 2023
commit e9dd7642c163bb52f7e170c68871b044c761f8d6
16 changes: 10 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ export interface ClientOptions {
export class Client {
private apiURL?: string
private edgeURL?: string
private fetch?: Fetcher
private fetch: Fetcher
private siteID: string
private token: string

constructor({ apiURL, edgeURL, fetch, siteID, token }: ClientOptions) {
this.apiURL = apiURL
this.edgeURL = edgeURL
this.fetch = fetch
this.fetch = fetch ?? globalThis.fetch
this.siteID = siteID
this.token = token

if (!this.fetch) {
throw new Error(
'Netlify Blobs could not find a `fetch` client in the global scope. You can either update your runtime to a version that includes `fetch` (like Node.js 18.0.0 or above), or you can supply your own implementation using the `fetch` property.',
)
}
}

private async getFinalRequest(storeName: string, key: string, method: string, metadata?: Metadata) {
Expand Down Expand Up @@ -63,8 +69,7 @@ export class Client {
apiHeaders[METADATA_HEADER_EXTERNAL] = encodedMetadata
}

const fetch = this.fetch ?? globalThis.fetch
const res = await fetch(apiURL, { headers: apiHeaders, method })
const res = await this.fetch(apiURL, { headers: apiHeaders, method })

if (res.status !== 200) {
throw new Error(`${method} operation has failed: API returned a ${res.status} response`)
Expand Down Expand Up @@ -102,8 +107,7 @@ export class Client {
options.duplex = 'half'
}

const fetch = this.fetch ?? globalThis.fetch
const res = await fetchAndRetry(fetch, url, options)
const res = await fetchAndRetry(this.fetch, url, options)

if (res.status === 404 && method === HTTPMethod.GET) {
return null
Expand Down
16 changes: 16 additions & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,22 @@ describe(`getStore`, () => {
)
})

test('Throws when there is no `fetch` implementation available', async () => {
// @ts-expect-error Assigning a value that doesn't match the type.
globalThis.fetch = undefined

const context = {
siteID,
token: apiToken,
}

env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64')

expect(() => getStore('production')).toThrowError(
'Netlify Blobs could not find a `fetch` client in the global scope. You can either update your runtime to a version that includes `fetch` (like Node.js 18.0.0 or above), or you can supply your own implementation using the `fetch` property.',
)
})

test('URL-encodes the store name', async () => {
const mockStore = new MockFetch()
.get({
Expand Down
0