10000 feat!: add `getStore` method by eduardoboucas · Pull Request #58 · netlify/blobs · GitHub
[go: up one dir, main page]

Skip to content

feat!: add getStore method #58

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 19 commits into from
Oct 17, 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
refactor: rename fetcher to fetch
  • Loading branch information
eduardoboucas committed Oct 17, 2023
commit 1a951aa3dd257051224b2abcb13e905fe37d1199
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,20 @@ const store2 = getStore({
assert.equal(await store2.get('my-key'), 'my value')
```

### Custom fetcher
### Custom `fetch`

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

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

```ts
import { fetch } from 'whatwg-fetch'

import { getStore } from '@netlify/blobs'

const store = getStore({
fetcher: fetch,
fetch,
name: 'my-store',
})

Expand Down
14 changes: 7 additions & 7 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ interface MakeStoreRequestOptions {

export class Client {
private context?: Context
private fetcher?: Fetcher
private fetch?: Fetcher

constructor(context?: Context, fetcher?: Fetcher) {
constructor(context?: Context, fetch?: Fetcher) {
this.context = context ?? {}
this.fetcher = fetcher
this.fetch = fetch
}

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

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

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

if (res.status === 404 && method === HTTPMethod.GET) {
return null
Expand Down
66 changes: 33 additions & 33 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('get', () => {
url: signedURL,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
name: 'production',
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('get', () => {
url: signedURL,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

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

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
name: 'production',
Expand All @@ -149,7 +149,7 @@ describe('get', () => {
url: signedURL,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
name: 'production',
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('get', () => {
url: signedURL,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

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

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
edgeURL,
Expand All @@ -232,7 +232,7 @@ describe('get', () => {
url: `${edgeURL}/${siteID}/production/${key}`,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
edgeURL,
Expand All @@ -252,7 +252,7 @@ describe('get', () => {
url: `${edgeURL}/${siteID}/production/${key}`,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
edgeURL,
Expand Down Expand Up @@ -292,7 +292,7 @@ describe('get', () => {
url: `${edgeURL}/${siteID}/images/${key}`,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

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

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

globalThis.fetch = fetcher
globalThis.fetch = fetch

< 97AE /td>
const blobs1 = getStore('production')

Expand Down Expand Up @@ -368,7 +368,7 @@ describe('set', () => {
url: signedURL,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
name: 'production',
Expand Down Expand Up @@ -400,7 +400,7 @@ describe('set', () => {
url: signedURL,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

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

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
name: 'production',
Expand Down Expand Up @@ -474,7 +474,7 @@ describe('set', () => {
url: signedURL,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
name: 'production',
Expand Down Expand Up @@ -504,7 +504,7 @@ describe('set', () => {
url: `${edgeURL}/${siteID}/production/${encodeURIComponent(complexKey)}`,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
edgeURL,
Expand All @@ -527,7 +527,7 @@ describe('set', () => {
url: `${edgeURL}/${siteID}/production/${key}`,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
edgeURL,
Expand Down Expand Up @@ -570,7 +570,7 @@ describe('set', () => {
url: `${edgeURL}/${siteID}/production/${key}`,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
edgeURL,
Expand All @@ -586,9 +586,9 @@ describe('set', () => {
})

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

globalThis.fetch = fetcher
globalThis.fetch = fetch

const blobs1 = getStore('production')

Expand Down Expand Up @@ -626,7 +626,7 @@ describe('setJSON', () => {
url: signedURL,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

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

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
edgeURL,
Expand Down Expand Up @@ -681,7 +681,7 @@ describe('setJSON', () => {
url: signedURL,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
name: 'production',
Expand Down Expand Up @@ -721,7 +721,7 @@ describe('delete', () => {
url: signedURL,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

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

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

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

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
edgeURL,
Expand All @@ -786,7 +786,7 @@ describe('delete', () => {
url: `${edgeURL}/${siteID}/production/${key}`,
})

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const blobs = getStore({
edgeURL,
Expand All @@ -804,9 +804,9 @@ describe('delete', () => {
})

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

globalThis.fetch = fetcher
globalThis.fetch = fetch

const blobs1 = getStore('production')

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

globalThis.fetch = mockStore.fetcher
globalThis.fetch = mockStore.fetch

const context = {
edgeURL,
Expand Down Expand Up @@ -881,8 +881,8 @@ describe('Deploy scope', () => {
})
})

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

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

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

const string = await store.get(key)
expect(string).toBe(value)
Expand Down
8 changes: 4 additions & 4 deletions src/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ const MAX_RETRY = 5
const RATE_LIMIT_HEADER = 'X-RateLimit-Reset'

export const fetchAndRetry = async (
fetcher: Fetcher,
fetch: Fetcher,
url: string,
options: RequestInit,
attemptsLeft = MAX_RETRY,
): ReturnType<typeof globalThis.fetch> => {
try {
const res = await fetcher(url, options)
const res = await fetch(url, options)

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

await sleep(delay)

return fetchAndRetry(fetcher, url, options, attemptsLeft - 1)
return fetchAndRetry(fetch, url, options, attemptsLeft - 1)
}

return res
Expand All @@ -32,7 +32,7 @@ export const fetchAndRetry = async (

await sleep(delay)

return fetchAndRetry(fetcher, url, options, attemptsLeft - 1)
return fetchAndRetry(fetch, url, options, attemptsLeft - 1)
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Store {

interface GetStoreOptions extends Context {
deployID?: string
fetcher?: Fetcher
fetch?: Fetcher
name?: string
}

Expand All @@ -154,15 +154,15 @@ export const getStore: {
}

if (typeof input.name === 'string') {
const { fetcher, name, ...context } = input
const client = new Client(context, fetcher)
const { fetch, name, ...context } = input
const client = new Client(context, fetch)

return new Store({ client, name })
}

if (typeof input.deployID === 'string') {
const { fetcher, deployID, ...context } = input
const client = new Client(context, fetcher)
const { fetch, deployID, ...context } = input
const client = new Client(context, fetch)

return new Store({ client, name: deployID })
}
Expand Down
Loading
0