Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Redact After Retry Attempts #689

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,6 @@ export class GaxiosError<T = any> extends Error {
if (error && 'code' in error && error.code) {
this.code = error.code;
}

if (config.errorRedactor) {
config.errorRedactor({
config: this.config,
response: this.response,
});
}
}
}

Expand Down Expand Up @@ -496,7 +489,7 @@ 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 ||
Expand Down
4 changes: 4 additions & 0 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
* Perform an HTTP request with the given options.
* @param opts Set of HTTP options that will be used for this HTTP request.
*/
async request<T = any>(opts: GaxiosOptions = {}): GaxiosPromise<T> {

Check warning on line 138 in src/gaxios.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
let prepared = await this.#prepareRequest(opts);
prepared = await this.#applyRequestInterceptors(prepared);
return this.#applyResponseInterceptors(this._request(prepared));
Expand Down Expand Up @@ -177,7 +177,7 @@
* Internal, retryable version of the `request` method.
* @param opts Set of HTTP options that will be used for this HTTP request.
*/
protected async _request<T = any>(

Check warning on line 180 in src/gaxios.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
opts: GaxiosOptionsPrepared,
): GaxiosPromise<T> {
try {
Expand Down Expand Up @@ -229,6 +229,10 @@
return this._request<T>(opts);
}

if (opts.errorRedactor) {
opts.errorRedactor(err);
}

throw err;
}
}
Expand All @@ -236,7 +240,7 @@
private async getResponseData(
opts: GaxiosOptionsPrepared,
res: Response,
): Promise<any> {

Check warning on line 243 in src/gaxios.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (
opts.maxContentLength &&
res.headers.has('content-length') &&
Expand Down Expand Up @@ -577,7 +581,7 @@
*/
private async getResponseDataFromContentType(
response: Response,
): Promise<any> {

Check warning on line 584 in src/gaxios.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
let contentType = response.headers.get('Content-Type');
if (contentType === null) {
// Maintain existing functionality by calling text()
Expand Down
47 changes: 47 additions & 0 deletions test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,53 @@ describe('🎏 data handling', () => {
scope.done();
}
});

it('should redact after final retry', async () => {
const customURL = new URL(url);
customURL.searchParams.append('token', 'sensitive');
customURL.searchParams.append('client_secret', 'data');
customURL.searchParams.append('random', 'non-sensitive');

const data = {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: 'somesensitivedata',
unrelated: 'data',
client_secret: 'data',
};

let retryAttempted = false;
const config: GaxiosOptions = {
url: customURL,
method: 'POST',
data: new URLSearchParams(data),
retry: true,
retryConfig: {
httpMethodsToRetry: ['POST'],
onRetryAttempt: err => {
assert.deepStrictEqual(err.config.data, new URLSearchParams(data));
retryAttempted = true;
},
},
};

const scope = nock(url)
.post('/', data)
.query(() => true)
.reply(500)
.post('/', data)
.query(() => true)
.reply(204);

const gaxios = new Gaxios();

try {
await gaxios.request(config);

assert(retryAttempted);
} finally {
scope.done();
}
});
});

describe('🍂 defaults & instances', () => {
Expand Down