From 3f90ab0faa993e83585caea05f8be654b1149c4a Mon Sep 17 00:00:00 2001 From: Dain Cilke Date: Fri, 17 Jan 2025 14:31:49 +0100 Subject: [PATCH] feat(request): add flag to disable logging api warnings (#925) * feat(request): add flag to disable logging api warnings * feat(client): only log x-sanity-warnings once --- src/defineCreateClient.ts | 8 +++++--- src/http/request.ts | 23 +++++++++++++++-------- test/warnings.test.ts | 23 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/defineCreateClient.ts b/src/defineCreateClient.ts index e79070de..ca7914a1 100644 --- a/src/defineCreateClient.ts +++ b/src/defineCreateClient.ts @@ -38,10 +38,11 @@ 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, @@ -49,6 +50,7 @@ export default function defineCreateClientExports< } as Any), config, ) + } return {requester: defaultRequester, createClient} } diff --git a/src/http/request.ts b/src/http/request.ts index 450dfeba..789ee9d9 100644 --- a/src/http/request.ts +++ b/src/http/request.ts @@ -17,13 +17,20 @@ 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 = {} + 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 */ @@ -31,7 +38,7 @@ export function defineHttpRequest(envMiddleware: Middlewares): Requester { return getIt([ retry({shouldRetry}), ...envMiddleware, - printWarnings, + printWarnings(), jsonRequest(), jsonResponse(), progress(), diff --git a/test/warnings.test.ts b/test/warnings.test.ts index 03eba4bc..ee7f33ae 100644 --- a/test/warnings.test.ts +++ b/test/warnings.test.ts @@ -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) + }) })