From e09469b8a7578ba0c432c40c3b38b2464c308849 Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Wed, 31 Jan 2024 16:03:18 -0800 Subject: [PATCH] feat: Support URL objects --- src/common.ts | 12 ++++++++---- src/gaxios.ts | 10 +++++----- test/test.getch.ts | 7 +++++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/common.ts b/src/common.ts index de944c7c..b91bc97a 100644 --- a/src/common.ts +++ b/src/common.ts @@ -148,9 +148,12 @@ export interface GaxiosOptions { options: GaxiosOptions, defaultAdapter: (options: GaxiosOptions) => GaxiosPromise ) => GaxiosPromise; - url?: string; - baseUrl?: string; // deprecated - baseURL?: string; + url?: string | URL; + /** + * @deprecated + */ + baseUrl?: string; + baseURL?: string | URL; method?: | 'GET' | 'HEAD' @@ -396,7 +399,8 @@ export function defaultErrorRedactor(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); } diff --git a/src/gaxios.ts b/src/gaxios.ts index 09f7eb3a..00d34eb0 100644 --- a/src/gaxios.ts +++ b/src/gaxios.ts @@ -77,13 +77,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(/^\*\./, '.'); @@ -96,7 +96,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; @@ -232,7 +232,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; @@ -241,7 +241,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; } diff --git a/test/test.getch.ts b/test/test.getch.ts index b466ae01..3eead46a 100644 --- a/test/test.getch.ts +++ b/test/test.getch.ts @@ -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'});