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

feat: Support URL objects #598

Merged
merged 2 commits into from
Feb 1, 2024
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
12 changes: 8 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@
}

export interface Headers {
[index: string]: any;

Check warning on line 121 in src/common.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
}
export type GaxiosPromise<T = any> = Promise<GaxiosResponse<T>>;

Check warning on line 123 in src/common.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

export interface GaxiosXMLHttpRequest {
responseURL: string;
}

export interface GaxiosResponse<T = any> {

Check warning on line 129 in src/common.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
config: GaxiosOptions;
data: T;
status: number;
Expand All @@ -143,13 +143,16 @@
* Optional method to override making the actual HTTP request. Useful
* for writing tests.
*/
adapter?: <T = any>(

Check warning on line 146 in src/common.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
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 All @@ -161,8 +164,8 @@
| 'TRACE'
| 'PATCH';
headers?: Headers;
data?: any;

Check warning on line 167 in src/common.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
body?: any;

Check warning on line 168 in src/common.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
/**
* The maximum size of the http response content in bytes allowed.
*/
Expand All @@ -172,13 +175,13 @@
*/
maxRedirects?: number;
follow?: number;
params?: any;

Check warning on line 178 in src/common.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
paramsSerializer?: (params: {[index: string]: string | number}) => string;
timeout?: number;
/**
* @deprecated ignored
*/
onUploadProgress?: (progressEvent: any) => void;

Check warning on line 184 in src/common.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
responseType?:
| 'arraybuffer'
| 'blob'
Expand All @@ -192,7 +195,7 @@
retry?: boolean;
// Should be instance of https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
// interface. Left as 'any' due to incompatibility between spec and abort-controller.
signal?: any;

Check warning on line 198 in src/common.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
size?: number;
/**
* Implementation of `fetch` to use when making the API call. By default,
Expand Down Expand Up @@ -224,7 +227,7 @@
*
* @experimental
*/
export type RedactableGaxiosResponse<T = any> = Pick<

Check warning on line 230 in src/common.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
GaxiosResponse<T>,
'config' | 'data' | 'headers'
>;
Expand Down Expand Up @@ -395,7 +398,8 @@
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
Loading