diff --git a/package.json b/package.json index 88ca4cbc..3745731c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/gaxios.ts b/src/gaxios.ts index ccfa6ae3..5541e56a 100644 --- a/src/gaxios.ts +++ b/src/gaxios.ts @@ -84,6 +84,14 @@ export class Gaxios { */ async request(opts: GaxiosOptions = {}): GaxiosPromise { 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(opts: GaxiosOptions = {}): GaxiosPromise { try { let translatedResponse: GaxiosResponse; if (opts.adapter) { @@ -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(err.config); + return this._request(err.config); } throw err; } diff --git a/test/test.retry.ts b/test/test.retry.ts index b7d068b8..9d976978 100644 --- a/test/test.retry.ts +++ b/test/test.retry.ts @@ -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'); @@ -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('/')