Skip to content

Commit

Permalink
fix: drop requirement on URL/combine url and params (#338)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: parameters in `url` and parameters provided via params will now be combined.
  • Loading branch information
JustinBeckwith authored Sep 14, 2020
1 parent aefaad1 commit e166bc6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
21 changes: 6 additions & 15 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,12 @@ import {getRetryConfig} from './retry';
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable node/no-unsupported-features/node-builtins */

const URL = hasURL() ? window.URL : url.URL;
const fetch = hasFetch() ? window.fetch : nodeFetch;

function hasWindow() {
return typeof window !== 'undefined' && !!window;
}

function hasURL() {
return hasWindow() && !!window.URL;
}

function hasFetch() {
return hasWindow() && !!window.fetch;
}
Expand Down Expand Up @@ -174,20 +169,16 @@ export class Gaxios {
opts.url = baseUrl + opts.url;
}

const parsedUrl = new URL(opts.url);
opts.url = `${parsedUrl.origin}${parsedUrl.pathname}`;
opts.params = extend(
qs.parse(parsedUrl.search.substr(1)), // removes leading ?
opts.params
);

opts.paramsSerializer = opts.paramsSerializer || this.paramsSerializer;
if (opts.params) {
parsedUrl.search = opts.paramsSerializer(opts.params);
let additionalQueryParams = opts.paramsSerializer(opts.params);
if (additionalQueryParams.startsWith('?')) {
additionalQueryParams = additionalQueryParams.slice(1);
}
const prefix = opts.url.includes('?') ? '&' : '?';
opts.url = opts.url + prefix + additionalQueryParams;
}

opts.url = parsedUrl.href;

if (typeof options.maxContentLength === 'number') {
opts.size = options.maxContentLength;
}
Expand Down
19 changes: 15 additions & 4 deletions test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,24 @@ describe('🥁 configuration options', () => {
scope.done();
});

it('should preserve the original querystring', async () => {
const path = '/?robot';
const opts = {url: `${url}${path}`};
const scope = nock(url).get(path).reply(200, {});
const res = await request(opts);
assert.strictEqual(res.status, 200);
assert.strictEqual(res.config.url, url + path);
scope.done();
});

it('should encode parameters from the params option', async () => {
const opts = {url, params: {james: 'kirk', montgomery: 'scott'}};
const path = '/?james=kirk&montgomery=scott';
const qs = '?james=kirk&montgomery=scott';
const path = `/${qs}`;
const scope = nock(url).get(path).reply(200, {});
const res = await request(opts);
assert.strictEqual(res.status, 200);
assert.strictEqual(res.config.url, url + path);
assert.strictEqual(res.config.url, url + qs);
scope.done();
});

Expand All @@ -184,7 +195,7 @@ describe('🥁 configuration options', () => {
url: `${url}/?james=beckwith&montgomery=scott`,
params: {james: 'kirk'},
};
const path = '/?james=kirk&montgomery=scott';
const path = '/?james=beckwith&montgomery=scott&james=kirk';
const scope = nock(url).get(path).reply(200, {});
const res = await request(opts);
assert.strictEqual(res.status, 200);
Expand All @@ -206,7 +217,7 @@ describe('🥁 configuration options', () => {
const scope = nock(url).get(`/${qs}`).reply(200, {});
const res = await request(opts);
assert.strictEqual(res.status, 200);
assert.strictEqual(res.config.url, `${url}/${qs}`);
assert.strictEqual(res.config.url, url + qs);
scope.done();
});

Expand Down

0 comments on commit e166bc6

Please sign in to comment.