Skip to content

Commit 1a951aa

Browse files
committed
refactor: rename fetcher to fetch
1 parent 99a0e4c commit 1a951aa

File tree

6 files changed

+53
-53
lines changed

6 files changed

+53
-53
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,20 @@ const store2 = getStore({
149149
assert.equal(await store2.get('my-key'), 'my value')
150150
```
151151

152-
### Custom fetcher
152+
### Custom `fetch`
153153

154154
The client uses [the web platform `fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to make HTTP
155155
calls. By default, it will use any globally-defined instance of `fetch`, but you can choose to provide your own.
156156

157-
You can do this by supplying a `fetcher` property to the `getStore` method.
157+
You can do this by supplying a `fetch` property to the `getStore` method.
158158

159159
```ts
160160
import { fetch } from 'whatwg-fetch'
161161

162162
import { getStore } from '@netlify/blobs'
163163

164164
const store = getStore({
165-
fetcher: fetch,
165+
fetch,
166166
name: 'my-store',
167167
})
168168

src/client.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ interface MakeStoreRequestOptions {
3535

3636
export class Client {
3737
private context?: Context
38-
private fetcher?: Fetcher
38+
private fetch?: Fetcher
3939

40-
constructor(context?: Context, fetcher?: Fetcher) {
40+
constructor(context?: Context, fetch?: Fetcher) {
4141
this.context = context ?? {}
42-
this.fetcher = fetcher
42+
this.fetch = fetch
4343
}
4444

4545
private static getEnvironmentContext() {
@@ -86,8 +86,8 @@ export class Client {
8686
context.siteID
8787
}/blobs/${encodedKey}?context=${storeName}`
8888
const headers = { authorization: `Bearer ${context.token}` }
89-
const fetcher = this.fetcher ?? globalThis.fetch
90-
const res = await fetcher(apiURL, { headers, method })
89+
const fetch = this.fetch ?? globalThis.fetch
90+
const res = await fetch(apiURL, { headers, method })
9191

9292
if (res.status !== 200) {
9393
throw new Error(`${method} operation has failed: API returned a ${res.status} response`)
@@ -123,8 +123,8 @@ export class Client {
123123
options.duplex = 'half'
124124
}
125125

126-
const fetcher = this.fetcher ?? globalThis.fetch
127-
const res = await fetchAndRetry(fetcher, url, options)
126+
const fetch = this.fetch ?? globalThis.fetch
127+
const res = await fetchAndRetry(fetch, url, options)
128128

129129
if (res.status === 404 && method === HTTPMethod.GET) {
130130
return null

src/main.test.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('get', () => {
7272
url: signedURL,
7373
})
7474

75-
globalThis.fetch = mockStore.fetcher
75+
globalThis.fetch = mockStore.fetch
7676

7777
const blobs = getStore({
7878
name: 'production',
@@ -104,7 +104,7 @@ describe('get', () => {
104104
url: signedURL,
105105
})
106106

107-
globalThis.fetch = mockStore.fetcher
107+
globalThis.fetch = mockStore.fetch
108108

109109
const blobs = getStore({
110110
name: 'production',
@@ -123,7 +123,7 @@ describe('get', () => {
123123
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`,
124124
})
125125

126-
globalThis.fetch = mockStore.fetcher
126+
globalThis.fetch = mockStore.fetch
127127

128128
const blobs = getStore({
129129
name: 'production',
@@ -149,7 +149,7 @@ describe('get', () => {
149149
url: signedURL,
150150
})
151151

152-
globalThis.fetch = mockStore.fetcher
152+
globalThis.fetch = mockStore.fetch
153153

154154
const blobs = getStore({
155155
name: 'production',
@@ -180,7 +180,7 @@ describe('get', () => {
180180
url: signedURL,
181181
})
182182

183-
globalThis.fetch = mockStore.fetcher
183+
globalThis.fetch = mockStore.fetch
184184

185185
const blobs = getStore({
186186
name: 'production',
@@ -207,7 +207,7 @@ describe('get', () => {
207207
url: `${edgeURL}/${siteID}/production/${key}`,
208208
})
209209

210-
globalThis.fetch = mockStore.fetcher
210+
globalThis.fetch = mockStore.fetch
211211

212212
const blobs = getStore({
213213
edgeURL,
@@ -232,7 +232,7 @@ describe('get', () => {
232232
url: `${edgeURL}/${siteID}/production/${key}`,
233233
})
234234

235-
globalThis.fetch = mockStore.fetcher
235+
globalThis.fetch = mockStore.fetch
236236

237237
const blobs = getStore({
238238
edgeURL,
@@ -252,7 +252,7 @@ describe('get', () => {
252252
url: `${edgeURL}/${siteID}/production/${key}`,
253253
})
254254

255-
globalThis.fetch = mockStore.fetcher
255+
globalThis.fetch = mockStore.fetch
256256

257257
const blobs = getStore({
258258
edgeURL,
@@ -292,7 +292,7 @@ describe('get', () => {
292292
url: `${edgeURL}/${siteID}/images/${key}`,
293293
})
294294

295-
globalThis.fetch = mockStore.fetcher
295+
globalThis.fetch = mockStore.fetch
296296

297297
for (let index = 0; index <= 1; index++) {
298298
const context = {
@@ -317,9 +317,9 @@ describe('get', () => {
317317
})
318318

319319
test('Throws when the instance is missing required configuration properties', async () => {
320-
const { fetcher } = new MockFetch()
320+
const { fetch } = new MockFetch()
321321

322-
globalThis.fetch = fetcher
322+
globalThis.fetch = fetch
323323

324324
const blobs1 = getStore('production')
325325

@@ -368,7 +368,7 @@ describe('set', () => {
368368
url: signedURL,
369369
})
370370

371-
globalThis.fetch = mockStore.fetcher
371+
globalThis.fetch = mockStore.fetch
372372

373373
const blobs = getStore({
374374
name: 'production',
@@ -400,7 +400,7 @@ describe('set', () => {
400400
url: signedURL,
401401
})
402402

403-
globalThis.fetch = mockStore.fetcher
403+
globalThis.fetch = mockStore.fetch
404404

405405
const blobs = getStore({
406406
name: 'production',
@@ -420,7 +420,7 @@ describe('set', () => {
420420
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`,
421421
})
422422

423-
globalThis.fetch = mockStore.fetcher
423+
globalThis.fetch = mockStore.fetch
424424

425425
const blobs = getStore({
426426
name: 'production',
@@ -474,7 +474,7 @@ describe('set', () => {
474474
url: signedURL,
475475
})
476476

477-
globalThis.fetch = mockStore.fetcher
477+
globalThis.fetch = mockStore.fetch
478478

479479
const blobs = getStore({
480480
name: 'production',
@@ -504,7 +504,7 @@ describe('set', () => {
504504
url: `${edgeURL}/${siteID}/production/${encodeURIComponent(complexKey)}`,
505505
})
506506

507-
globalThis.fetch = mockStore.fetcher
507+
globalThis.fetch = mockStore.fetch
508508

509509
const blobs = getStore({
510510
edgeURL,
@@ -527,7 +527,7 @@ describe('set', () => {
527527
url: `${edgeURL}/${siteID}/production/${key}`,
528528
})
529529

530-
globalThis.fetch = mockStore.fetcher
530+
globalThis.fetch = mockStore.fetch
531531

532532
const blobs = getStore({
533533
edgeURL,
@@ -570,7 +570,7 @@ describe('set', () => {
570570
url: `${edgeURL}/${siteID}/production/${key}`,
571571
})
572572

573-
globalThis.fetch = mockStore.fetcher
573+
globalThis.fetch = mockStore.fetch
574574

575575
const blobs = getStore({
576576
edgeURL,
@@ -586,9 +586,9 @@ describe('set', () => {
586586
})
587587

588588
test('Throws when the instance is missing required configuration properties', async () => {
589-
const { fetcher } = new MockFetch()
589+
const { fetch } = new MockFetch()
590590

591-
globalThis.fetch = fetcher
591+
globalThis.fetch = fetch
592592

593593
const blobs1 = getStore('production')
594594

@@ -626,7 +626,7 @@ describe('setJSON', () => {
626626
url: signedURL,
627627
})
628628

629-
globalThis.fetch = mockStore.fetcher
629+
globalThis.fetch = mockStore.fetch
630630

631631
const blobs = getStore({
632632
name: 'production',
@@ -649,7 +649,7 @@ describe('setJSON', () => {
649649
url: `${edgeURL}/${siteID}/production/${key}`,
650650
})
651651

652-
globalThis.fetch = mockStore.fetcher
652+
globalThis.fetch = mockStore.fetch
653653

654654
const blobs = getStore({
655655
edgeURL,
@@ -681,7 +681,7 @@ describe('setJSON', () => {
681681
url: signedURL,
682682
})
683683

684-
globalThis.fetch = mockStore.fetcher
684+
globalThis.fetch = mockStore.fetch
685685

686686
const blobs = getStore({
687687
name: 'production',
@@ -721,7 +721,7 @@ describe('delete', () => {
721721
url: signedURL,
722722
})
723723

724-
globalThis.fetch = mockStore.fetcher
724+
globalThis.fetch = mockStore.fetch
725725

726726
const blobs = getStore({
727727
name: 'production',
@@ -742,7 +742,7 @@ describe('delete', () => {
742742
url: `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`,
743743
})
744744

745-
globalThis.fetch = mockStore.fetcher
745+
globalThis.fetch = mockStore.fetch
746746

747747
const blobs = getStore({
748748
name: 'production',
@@ -765,7 +765,7 @@ describe('delete', () => {
765765
url: `${edgeURL}/${siteID}/production/${key}`,
766766
})
767767

768-
globalThis.fetch = mockStore.fetcher
768+
globalThis.fetch = mockStore.fetch
769769

770770
const blobs = getStore({
771771
edgeURL,
@@ -786,7 +786,7 @@ describe('delete', () => {
786786
url: `${edgeURL}/${siteID}/production/${key}`,
787787
})
788788

789-
globalThis.fetch = mockStore.fetcher
789+
globalThis.fetch = mockStore.fetch
790790

791791
const blobs = getStore({
792792
edgeURL,
@@ -804,9 +804,9 @@ describe('delete', () => {
804804
})
805805

806806
test('Throws when the instance is missing required configuration properties', async () => {
807-
const { fetcher } = new MockFetch()
807+
const { fetch } = new MockFetch()
808808

809-
globalThis.fetch = fetcher
809+
globalThis.fetch = fetch
810810

811811
const blobs1 = getStore('production')
812812

@@ -851,7 +851,7 @@ describe('Deploy scope', () => {
851851
url: `${edgeURL}/${siteID}/${deployID}/${key}`,
852852
})
853853

854-
globalThis.fetch = mockStore.fetcher
854+
globalThis.fetch = mockStore.fetch
855855

856856
const context = {
857857
edgeURL,
@@ -881,8 +881,8 @@ describe('Deploy scope', () => {
881881
})
882882
})
883883

884-
describe('Customer fetcher', () => {
885-
test('Uses a custom implementation of `fetch` if the `fetcher` parameter is supplied', async () => {
884+
describe('Custom `fetch`', () => {
885+
test('Uses a custom implementation of `fetch` if the `fetch` parameter is supplied', async () => {
886886
globalThis.fetch = () => {
887887
throw new Error('I should not be called')
888888
}
@@ -901,7 +901,7 @@ describe('Customer fetcher', () => {
901901

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

904-
const store = getStore({ fetcher: mockStore.fetcher, name: 'images' })
904+
const store = getStore({ fetch: mockStore.fetch, name: 'images' })
905905

906906
const string = await store.get(key)
907907
expect(string).toBe(value)

src/retry.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ const MAX_RETRY = 5
66
const RATE_LIMIT_HEADER = 'X-RateLimit-Reset'
77

88
export const fetchAndRetry = async (
9-
fetcher: Fetcher,
9+
fetch: Fetcher,
1010
url: string,
1111
options: RequestInit,
1212
attemptsLeft = MAX_RETRY,
1313
): ReturnType<typeof globalThis.fetch> => {
1414
try {
15-
const res = await fetcher(url, options)
15+
const res = await fetch(url, options)
1616

1717
if (attemptsLeft > 0 && (res.status === 429 || res.status >= 500)) {
1818
const delay = getDelay(res.headers.get(RATE_LIMIT_HEADER))
1919

2020
await sleep(delay)
2121

22-
return fetchAndRetry(fetcher, url, options, attemptsLeft - 1)
22+
return fetchAndRetry(fetch, url, options, attemptsLeft - 1)
2323
}
2424

2525
return res
@@ -32,7 +32,7 @@ export const fetchAndRetry = async (
3232

3333
await sleep(delay)
3434

35-
return fetchAndRetry(fetcher, url, options, attemptsLeft - 1)
35+
return fetchAndRetry(fetch, url, options, attemptsLeft - 1)
3636
}
3737
}
3838

src/store.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class Store {
139139

140140
interface GetStoreOptions extends Context {
141141
deployID?: string
142-
fetcher?: Fetcher
142+
fetch?: Fetcher
143143
name?: string
144144
}
145145

@@ -154,15 +154,15 @@ export const getStore: {
154154
}
155155

156156
if (typeof input.name === 'string') {
157-
const { fetcher, name, ...context } = input
158-
const client = new Client(context, fetcher)
157+
const { fetch, name, ...context } = input
158+
const client = new Client(context, fetch)
159159

160160
return new Store({ client, name })
161161
}
162162

163163
if (typeof input.deployID === 'string') {
164-
const { fetcher, deployID, ...context } = input
165-
const client = new Client(context, fetcher)
164+
const { fetch, deployID, ...context } = input
165+
const client = new Client(context, fetch)
166166

167167
return new Store({ client, name: deployID })
168168
}

0 commit comments

Comments
 (0)