Skip to content

Commit

Permalink
fix: prevent crash when using url option instead of uri (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored May 16, 2023
1 parent 8dba8ed commit 573c1bd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/http/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ function shouldRetry(err: any, attempt: number, options: any) {
// By default `retry.shouldRetry` doesn't retry on server errors so we add our own logic.

const isSafe = options.method === 'GET' || options.method === 'HEAD'
const isQuery = options.uri.startsWith('/data/query')
const uri = options.uri || options.url
const isQuery = uri.startsWith('/data/query')
const isRetriableResponse =
err.response &&
(err.response.statusCode === 429 ||
Expand Down
29 changes: 28 additions & 1 deletion test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@sanity/client'
import {of as observableOf} from 'rxjs'
import {filter} from 'rxjs/operators'
import {describe, expect, test} from 'vitest'
import {describe, expect, test, vi} from 'vitest'

const apiHost = 'api.sanity.url'
const defaultProjectId = 'bf1942'
Expand Down Expand Up @@ -335,6 +335,33 @@ describe('client', async () => {
})
expect(client.projects.getById('n1f7y')).rejects.toBeDefined()
})

test.each([429, 502, 503])('retries requests %d', async (code) => {
const userObj = {
role: null,
id: 'pabc123',
name: 'Mannen i Gata',
email: 'some@email.com',
}

for (let i = 0; i < 5; i++) {
nock(`https://${apiHost}`).get('/v2023-03-25/users/me').reply(code, {})
nock(`https://${apiHost}`).get('/v2023-03-25/users/me').reply(code, {})
nock(`https://${apiHost}`).get('/v2023-03-25/users/me').reply(code, {})
nock(`https://${apiHost}`).get('/v2023-03-25/users/me').reply(code, {})
nock(`https://${apiHost}`).get('/v2023-03-25/users/me').reply(200, userObj)
}

const fn = vi.fn().mockReturnValue(100)
const client = createClient({
apiVersion: '2023-03-25',
useProjectHostname: false,
apiHost: `https://${apiHost}`,
retryDelay: fn,
})
await expect(client.request({url: '/users/me'})).resolves.toEqual(userObj)
expect(fn).toHaveBeenCalledTimes(4)
})
})

describe('DATASETS', () => {
Expand Down

0 comments on commit 573c1bd

Please sign in to comment.