Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"raw-body": "^2.5.2",
"tsx": "^4.7.0",
"typescript": "^5.2.2",
"undici-types": "^6.6.2",
"xo": "^0.56.0"
},
"xo": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Accepts any value supported by [`URLSearchParams()`](https://developer.mozilla.o

Type: `string | URL`

A prefix to prepend to the `input` URL when making the request. It can be any valid URL, either relative or absolute. A trailing slash `/` is optional and will be added automatically, if needed, when it is joined with `input`. Only takes effect when `input` is a string. The `input` argument cannot start with a slash `/` when using this option.
A prefix to prepend to the `input` URL when making the request. It can be any valid URL, either relative or absolute. A trailing slash `/` is optional and will be added automatically, if needed, when it is joined with `input`. Only takes effect when `input` is a string.

Useful when used with [`ky.extend()`](#kyextenddefaultoptions) to create niche-specific Ky-instances.

Expand Down
4 changes: 1 addition & 3 deletions source/core/Ky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ export class Ky {
}

if (this._options.prefixUrl && typeof this._input === 'string') {
if (this._input.startsWith('/')) {
throw new Error('`input` must not begin with a slash when using `prefixUrl`');
}
this._input = this._input.replace(/^\/+/, '');

if (!this._options.prefixUrl.endsWith('/')) {
this._options.prefixUrl += '/';
Expand Down
2 changes: 1 addition & 1 deletion source/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export type KyOptions = {
searchParams?: SearchParamsOption;

/**
A prefix to prepend to the `input` URL when making the request. It can be any valid URL, either relative or absolute. A trailing slash `/` is optional and will be added automatically, if needed, when it is joined with `input`. Only takes effect when `input` is a string. The `input` argument cannot start with a slash `/` when using this option.
A prefix to prepend to the `input` URL when making the request. It can be any valid URL, either relative or absolute. A trailing slash `/` is optional and will be added automatically, if needed, when it is joined with `input`. Only takes effect when `input` is a string.

Useful when used with [`ky.extend()`](#kyextenddefaultoptions) to create niche-specific Ky-instances.

Expand Down
7 changes: 1 addition & 6 deletions test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ defaultBrowsersTest('prefixUrl option', async (t: ExecutionContext, page: Page)
await page.goto(server.url);
await addKyScriptToPage(page);

await t.throwsAsync(
page.evaluate(async () => window.ky('/foo', {prefixUrl: '/'})),
{message: /`input` must not begin with a slash when using `prefixUrl`/},
);

const results = await page.evaluate(async (url: string) => Promise.all([
window.ky(`${url}/api/unicorn`).text(),
// @ts-expect-error unsupported {prefixUrl: null} type
Expand Down Expand Up @@ -362,8 +357,8 @@ defaultBrowsersTest('FormData with searchParams ("multipart/form-data" parser)',
});

server.post('/', async (request, response) => {
// @ts-expect-error
const [body, error] = await new Promise(resolve => {
// @ts-expect-error
const busboyInstance = busboy({headers: request.headers});

busboyInstance.on('error', (error: Error) => {
Expand Down
9 changes: 0 additions & 9 deletions test/prefix-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,5 @@ test('prefixUrl option', async t => {
t.is(await ky('', {prefixUrl: `${server.url}/`}).text(), 'zebra');
t.is(await ky('', {prefixUrl: new URL(server.url)}).text(), 'zebra');

t.throws(
() => {
void ky('/unicorn', {prefixUrl: `${server.url}/api`});
},
{
message: '`input` must not begin with a slash when using `prefixUrl`',
},
);

await server.close();
});