Skip to content

Commit

Permalink
feat: Support URL objects (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbankhead authored Feb 1, 2024
1 parent 16dac84 commit ef40c61
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,12 @@ export interface GaxiosOptions {
options: GaxiosOptions,
defaultAdapter: (options: GaxiosOptions) => GaxiosPromise<T>
) => GaxiosPromise<T>;
url?: string;
baseUrl?: string; // deprecated
baseURL?: string;
url?: string | URL;
/**
* @deprecated
*/
baseUrl?: string;
baseURL?: string | URL;
method?:
| 'GET'
| 'HEAD'
Expand Down Expand Up @@ -395,7 +398,8 @@ export function defaultErrorRedactor<T = any>(data: {
redactObject(data.config.body);

try {
const url = new URL(data.config.url || '');
const url = new URL('', data.config.url);

if (url.searchParams.has('token')) {
url.searchParams.set('token', REDACT);
}
Expand Down
10 changes: 5 additions & 5 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ function loadProxy() {

loadProxy();

function skipProxy(url: string) {
function skipProxy(url: string | URL) {
const noProxyEnv = process.env.NO_PROXY ?? process.env.no_proxy;
if (!noProxyEnv) {
return false;
}
const noProxyUrls = noProxyEnv.split(',');
const parsedURL = new URL(url);
const parsedURL = url instanceof URL ? url : new URL(url);
return !!noProxyUrls.find(url => {
if (url.startsWith('*.') || url.startsWith('.')) {
url = url.replace(/^\*\./, '.');
Expand All @@ -98,7 +98,7 @@ function skipProxy(url: string) {

// Figure out if we should be using a proxy. Only if it's required, load
// the https-proxy-agent module as it adds startup cost.
function getProxy(url: string) {
function getProxy(url: string | URL) {
// If there is a match between the no_proxy env variables and the url, then do not proxy
if (skipProxy(url)) {
return undefined;
Expand Down Expand Up @@ -239,7 +239,7 @@ export class Gaxios {
// baseUrl has been deprecated, remove in 2.0
const baseUrl = opts.baseUrl || opts.baseURL;
if (baseUrl) {
opts.url = baseUrl + opts.url;
opts.url = baseUrl.toString() + opts.url;
}

opts.paramsSerializer = opts.paramsSerializer || this.paramsSerializer;
Expand All @@ -248,7 +248,7 @@ export class Gaxios {
if (additionalQueryParams.startsWith('?')) {
additionalQueryParams = additionalQueryParams.slice(1);
}
const prefix = opts.url.includes('?') ? '&' : '?';
const prefix = opts.url.toString().includes('?') ? '&' : '?';
opts.url = opts.url + prefix + additionalQueryParams;
}

Expand Down
7 changes: 7 additions & 0 deletions test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ describe('🚙 error handling', () => {
});

describe('🥁 configuration options', () => {
it('should accept URL objects', async () => {
const scope = nock(url).get('/').reply(204);
const res = await request({url: new URL(url)});
scope.done();
assert.strictEqual(res.config.method, 'GET');
});

it('should use options passed into the constructor', async () => {
const scope = nock(url).head('/').reply(200);
const inst = new Gaxios({method: 'HEAD'});
Expand Down

0 comments on commit ef40c61

Please sign in to comment.