Skip to content

Commit

Permalink
chore: more merge resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
d-goog committed May 17, 2024
1 parent 10d557a commit 7138ccb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
16 changes: 5 additions & 11 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ export class GaxiosError<T = any> 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
Expand Down Expand Up @@ -424,17 +423,12 @@ export function defaultErrorRedactor<
}

function redactObject<T extends O['data'] | R>(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 {
Expand Down
22 changes: 14 additions & 8 deletions test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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`);
});
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 8 additions & 5 deletions test/test.retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -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);
}
});

Expand Down

0 comments on commit 7138ccb

Please sign in to comment.