Skip to content

Commit e9dd764

Browse files
committed
feat: throw error when fetch is not available
1 parent ef22a1c commit e9dd764

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/client.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,22 @@ export interface ClientOptions {
2323
export class Client {
2424
private apiURL?: string
2525
private edgeURL?: string
26-
private fetch?: Fetcher
26+
private fetch: Fetcher
2727
private siteID: string
2828
private token: string
2929

3030
constructor({ apiURL, edgeURL, fetch, siteID, token }: ClientOptions) {
3131
this.apiURL = apiURL
3232
this.edgeURL = edgeURL
33-
this.fetch = fetch
33+
this.fetch = fetch ?? globalThis.fetch
3434
this.siteID = siteID
3535
this.token = token
36+
37+
if (!this.fetch) {
38+
throw new Error(
39+
'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.',
40+
)
41+
}
3642
}
3743

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

66-
const fetch = this.fetch ?? globalThis.fetch
67-
const res = await fetch(apiURL, { headers: apiHeaders, method })
72+
const res = await this.fetch(apiURL, { headers: apiHeaders, method })
6873

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

105-
const fetch = this.fetch ?? globalThis.fetch
106-
const res = await fetchAndRetry(fetch, url, options)
110+
const res = await fetchAndRetry(this.fetch, url, options)
107111

108112
if (res.status === 404 && method === HTTPMethod.GET) {
109113
return null

src/main.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,22 @@ describe(`getStore`, () => {
11211121
)
11221122
})
11231123

1124+
test('Throws when there is no `fetch` implementation available', async () => {
1125+
// @ts-expect-error Assigning a value that doesn't match the type.
1126+
globalThis.fetch = undefined
1127+
1128+
const context = {
1129+
siteID,
1130+
token: apiToken,
1131+
}
1132+
1133+
env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64')
1134+
1135+
expect(() => getStore('production')).toThrowError(
1136+
'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.',
1137+
)
1138+
})
1139+
11241140
test('URL-encodes the store name', async () => {
11251141
const mockStore = new MockFetch()
11261142
.get({

0 commit comments

Comments
 (0)