From 7138ccb89a5bd622887ee321b967a864cdec3e85 Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Fri, 17 May 2024 13:45:43 -0700 Subject: [PATCH] chore: more merge resolutions --- src/common.ts | 16 +++++----------- test/test.getch.ts | 22 ++++++++++++++-------- test/test.retry.ts | 13 ++++++++----- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/common.ts b/src/common.ts index 9db49887..e55c0d54 100644 --- a/src/common.ts +++ b/src/common.ts @@ -94,8 +94,7 @@ export class GaxiosError extends Error { try { this.response.data = translateData( this.config.responseType, - // workaround for `node-fetch`'s `.data` deprecation... - this.response?.bodyUsed ? this.response?.data : undefined + this.response?.data ); } catch { // best effort - don't throw an error within an error @@ -424,17 +423,12 @@ export function defaultErrorRedactor< } function redactObject(obj: T | null) { - if (!obj) { + if (!obj || typeof obj !== 'object') { return; - } else if ( - obj instanceof FormData || - obj instanceof URLSearchParams || - // support `node-fetch` FormData/URLSearchParams - ('forEach' in obj && 'set' in obj) - ) { - (obj as FormData | URLSearchParams).forEach((_, key) => { + } else if (obj instanceof FormData || obj instanceof URLSearchParams) { + obj.forEach((_, key) => { if (['grant_type', 'assertion'].includes(key) || /secret/.test(key)) { - (obj as FormData | URLSearchParams).set(key, REDACT); + obj.set(key, REDACT); } }); } else { diff --git a/test/test.getch.ts b/test/test.getch.ts index 02032042..0f90593c 100644 --- a/test/test.getch.ts +++ b/test/test.getch.ts @@ -120,7 +120,7 @@ describe('🚙 error handling', () => { ); assert(error.response); - assert.equal(error.response.data, notJSON); + assert.equal(error.response.data, '.'); }); it('should support `instanceof` for GaxiosErrors of the same version', () => { @@ -191,12 +191,17 @@ describe('🥁 configuration options', () => { it('should support redirects by default', async () => { const body = {hello: '🌎'}; - const scopes = [ - nock(url).get('/foo').reply(200, body), - nock(url).get('/').reply(302, undefined, {location: '/foo'}), - ]; - const res = await request({url}); - scopes.forEach(x => x.done()); + const url = new URL('https://example.com/foo/'); + + nock.enableNetConnect(); + const scope = nock(url.origin) + .get('/foo/') + .reply(302, undefined, {location: '/redirect/'}) + .get('/redirect/') + .reply(200, body); + + const res = await request(url); + scope.done(); assert.deepStrictEqual(res.data, body); assert.strictEqual(res.url, `${url}/foo`); }); @@ -317,7 +322,8 @@ describe('🥁 configuration options', () => { assert.deepStrictEqual(res.data, {}); }); - describe('proxying', () => { + // TODO: Should update with `fetch` compatible proxy agent first + describe.skip('proxying', () => { const url = 'https://domain.example.com/with-path'; const proxy = 'https://fake.proxy/'; let gaxios: Gaxios; diff --git a/test/test.retry.ts b/test/test.retry.ts index e2801d69..51b17fa7 100644 --- a/test/test.retry.ts +++ b/test/test.retry.ts @@ -93,9 +93,12 @@ describe('🛸 retry & exponential backoff', () => { it('should not retry if user aborted request', async () => { const ac = new AbortController(); + + // Note, no redirect target as it shouldn't be reached + nock(url).get('/').reply(302, undefined, {location: '/foo'}); + const config: GaxiosOptions = { - method: 'GET', - url: 'https://google.com', + url, signal: ac.signal, retryConfig: {retry: 10, noResponseRetries: 10}, }; @@ -104,10 +107,10 @@ describe('🛸 retry & exponential backoff', () => { try { await req; throw Error('unreachable'); - // eslint-disable-next-line @typescript-eslint/no-explicit-any - } catch (err: any) { + } catch (err) { + assert(err instanceof GaxiosError); assert(err.config); - assert.strictEqual(err.config.retryConfig.currentRetryAttempt, 0); + assert.strictEqual(err.config.retryConfig?.currentRetryAttempt, 0); } });