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

fix: do not double prepend baseUrl on retry #142

Merged
merged 1 commit into from
Jul 19, 2019
Merged
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
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 () => {
JustinBeckwith marked this conversation as resolved.
Show resolved Hide resolved
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