Skip to content

Commit

Permalink
fix: prevent double option processing
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Jul 19, 2019
1 parent 7137a2d commit a81aa0b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"presystem-test": "npm run compile",
"system-test": "mocha build/system-test --timeout 40000",
"clean": "gts clean",
"compile": "tsc -p . && npm run fix",
"compile": "tsc -p .",
"fix": "gts fix",
"prepare": "npm run compile",
"pretest": "npm run compile",
Expand Down
10 changes: 9 additions & 1 deletion src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ export class Gaxios {
*/
async request<T = any>(opts: GaxiosOptions = {}): GaxiosPromise<T> {
opts = this.validateOpts(opts);
return this._request(opts);
}

/**
* Internal, retryable version of the `request` method.
* @param opts Set of HTTP options that will be used for this HTTP request.
*/
private async _request<T = any>(opts: GaxiosOptions = {}): GaxiosPromise<T> {
try {
let translatedResponse: GaxiosResponse<T>;
if (opts.adapter) {
Expand All @@ -107,7 +115,7 @@ export class Gaxios {
const {shouldRetry, config} = await getRetryConfig(e);
if (shouldRetry && config) {
err.config.retryConfig!.currentRetryAttempt = config.retryConfig!.currentRetryAttempt;
return this.request<T>(err.config);
return this._request<T>(err.config);
}
throw err;
}
Expand Down
22 changes: 21 additions & 1 deletion test/test.retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import assert from 'assert';
import nock from 'nock';

import {GaxiosError, GaxiosOptions, request} from '../src';
import {GaxiosError, GaxiosOptions, request, Gaxios} from '../src';

const assertRejects = require('assert-rejects');

Expand Down Expand Up @@ -138,6 +138,26 @@ describe('🛸 retry & exponential backoff', () => {
scope.done();
});

it('should retain the baseUrl on retry', async () => {
const body = {pumpkin: '🥧'};
const url = '/path';
const baseUrl = 'http://example.com';
const scope = nock(baseUrl)
.get(url)
.reply(500)
.get(url)
.reply(200, body);
const gaxios = new Gaxios({
baseUrl,
});
const res = await gaxios.request({
url,
retry: true,
});
assert.deepStrictEqual(res.data, body);
scope.done();
});

it('should not retry if retries set to 0', async () => {
const scope = nock(url)
.get('/')
Expand Down

0 comments on commit a81aa0b

Please sign in to comment.