Skip to content

Commit

Permalink
feat(request): add flag to disable logging api warnings (#925)
Browse files Browse the repository at this point in the history
* feat(request): add flag to disable logging api warnings

* feat(client): only log x-sanity-warnings once
  • Loading branch information
dcilke authored Jan 17, 2025
1 parent bbe4cfe commit 3f90ab0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/defineCreateClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ export default function defineCreateClientExports<
// Set the http client to use for requests, and its environment specific middleware
const defaultRequester = defineHttpRequest(envMiddleware)

const createClient = (config: ClientConfigType) =>
new ClassConstructor(
const createClient = (config: ClientConfigType) => {
const clientRequester = defineHttpRequest(envMiddleware)
return new ClassConstructor(
(options, requester) =>
(requester || defaultRequester)({
(requester || clientRequester)({
maxRedirects: 0,
maxRetries: config.maxRetries,
retryDelay: config.retryDelay,
...options,
} as Any),
config,
)
}

return {requester: defaultRequester, createClient}
}
23 changes: 15 additions & 8 deletions src/http/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,28 @@ const httpError = {
},
}

const printWarnings = {
onResponse: (res: Any) => {
const warn = res.headers['x-sanity-warning']
const warnings = Array.isArray(warn) ? warn : [warn]
warnings.filter(Boolean).forEach((msg) => console.warn(msg)) // eslint-disable-line no-console
return res
},
function printWarnings() {
const seen: Record<string, boolean> = {}
return {
onResponse: (res: Any) => {
const warn = res.headers['x-sanity-warning']
const warnings = Array.isArray(warn) ? warn : [warn]
for (const msg of warnings) {
if (!msg || seen[msg]) continue
seen[msg] = true
console.warn(msg) // eslint-disable-line no-console
}
return res
},
}
}

/** @internal */
export function defineHttpRequest(envMiddleware: Middlewares): Requester {
return getIt([
retry({shouldRetry}),
...envMiddleware,
printWarnings,
printWarnings(),
jsonRequest(),
jsonResponse(),
progress(),
Expand Down
23 changes: 23 additions & 0 deletions test/warnings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,27 @@ describe('Client config warnings', async () => {
await createClient({projectId: 'abc123', useCdn: true, apiVersion: '1'}).users.getById('me')
expect(warn).toHaveBeenCalledWith('Friction endures')
})

test.skipIf(isEdge)('only warns once', async () => {
expect.assertions(2)

const {default: nock} = await import('nock')

nock('https://abc123.api.sanity.io')
.get('/v1/users/me')
.times(2)
.reply(200, {}, {'X-Sanity-Warning': 'Friction endures'})

const client = createClient({
projectId: 'abc123',
useCdn: true,
apiVersion: '1',
})

await client.users.getById('me')
await client.users.getById('me')

expect(warn).toHaveBeenCalledWith('Friction endures')
expect(warn).toHaveBeenCalledTimes(1)
})
})

0 comments on commit 3f90ab0

Please sign in to comment.