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: Allow prefixUrl to work #370

Merged
merged 3 commits into from
Jul 19, 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
40 changes: 29 additions & 11 deletions src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,33 @@ export function createFetch(got: Got): GotFetch {
const globalCache = new Map();

return async (input, opts) => {
const url = new URL(typeof input === 'string' ? input : input.url);
let url = input.toString();
let searchParams: URLSearchParams = new URLSearchParams();

try {
const urlObj = new URL(
typeof input === "string"
? input
: input.url
);

searchParams = new URLSearchParams(urlObj.searchParams);
urlObj.search = "";

url = urlObj.href;
} catch (e) {
// Ignore url parsing failure, likely prefixUrl is being used
// When using prefixUrl, the path cannot start with a "/"
if (url.startsWith("/")) {
url = url.substring(1);
}

if (url.includes("?")) {
const [path, search] = url.split("?");
url = path;
searchParams = new URLSearchParams(search);
}
}
const request: RequestInit = typeof input === 'object' ? input : opts || {};

if (request.mode === 'no-cors' || request.mode === 'same-origin' || request.mode === 'navigate') {
Expand All @@ -31,18 +57,10 @@ export function createFetch(got: Got): GotFetch {
throw new TypeError(format('request.headers must be plain object: %j', request.headers));
}

// got does not merge base searchParams with the url's searchParams
// but it does merge searchParams options
// so we clone the url's searchParams
// we also clear the url's search to work around this bug
// https://github.com/sindresorhus/got/issues/1188
const searchParams = new URLSearchParams(url.searchParams);
url.search = '';

const gotOpts: GotOptions = {
// url needs to be stringified to support UNIX domain sockets, and
// For more info see https://github.com/alexghr/got-fetch/pull/8
url: url.toString(),
url,
searchParams,
followRedirect: true,
throwHttpErrors: false,
Expand Down Expand Up @@ -107,7 +125,7 @@ export function createFetch(got: Got): GotFetch {
// using Array.prototype.at would've been nice but it's not
// supported by anything below Node 16.8
? r.redirectUrls[r.redirectUrls.length - 1]
: url.href,
: r.url,
});
});
}
Expand Down
42 changes: 34 additions & 8 deletions src/test/fetch.request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import got from 'got';
import { URLSearchParams } from 'url';

import { createFetch } from '../lib/fetch';
import { Interceptor, intercept, assert200, url } from './util';
import { Interceptor, intercept, assert200, url, ORIGIN } from './util';

describe('fetch request', () => {
let interceptor: Interceptor;
Expand Down Expand Up @@ -37,6 +37,18 @@ describe('fetch request', () => {
const fetch = createFetch(got);
await assert200(fetch(url('/foo')));
});

it('allows prefixUrl', async () => {
expect.assertions(1);
interceptor.intercept('/foo', 'get').reply(200);

const prefixedClient = got.extend({
prefixUrl: ORIGIN
})

const fetch = createFetch(prefixedClient);
await assert200(fetch('/foo'));
});
});

describe('querystring', () => {
Expand All @@ -48,13 +60,27 @@ describe('fetch request', () => {
await assert200(fetch(url('/', { foo: '123', bar: '456' })));
});

it('merges query string parameters', async () => {
expect.assertions(1);
interceptor.intercept('/', 'get').query({ foo: '123', bar: '456' }).reply(200);
});

const fetch = createFetch(got.extend({ searchParams: { bar: '456' } }));
await assert200(fetch(url('/', { foo: '123' })));
});
it('merges query string parameters', async () => {
expect.assertions(1);
interceptor.intercept('/', 'get').query({ foo: '123', bar: '456' }).reply(200);

const fetch = createFetch(got.extend({ searchParams: { bar: '456' } }));
await assert200(fetch(url('/', { foo: '123' })));
});

it('merges query string parameters with prefixUrl', async () => {
expect.assertions(1);
interceptor.intercept('/foo', 'get').query({ foo: '123', bar: '456' }).reply(200);

const prefixedClient = got.extend({
prefixUrl: ORIGIN,
searchParams: { bar: '456' }
})

const fetch = createFetch(prefixedClient);
await assert200(fetch('/foo?foo=123'));
});

describe('headers', () => {
Expand Down Expand Up @@ -175,7 +201,7 @@ describe('fetch request', () => {

});

it.only("sends own content-type header", async () => {
it("sends own content-type header", async () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only meant none of the other tests in this file ran

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️ thanks for fixing this

expect.assertions(1);

// as set by the client
Expand Down
2 changes: 1 addition & 1 deletion src/test/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import nock from 'nock';
import { URL } from 'url';

const ORIGIN = 'https://example.com';
export const ORIGIN = 'https://example.com';

export type Interceptor = nock.Scope;
export function intercept(): Interceptor {
Expand Down