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

Allow input to start with slash by removing head slashes #561

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -72,6 +72,7 @@
"raw-body": "^2.5.2",
"tsx": "^4.7.0",
"typescript": "^5.2.2",
"undici-types": "^6.6.2",
Copy link
Owner

Choose a reason for hiding this comment

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

Where is this used?

Copy link
Author

Choose a reason for hiding this comment

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

It is used here:

import {type RequestInit as UndiciRequestInit} from 'undici-types';
.

I got a warning about the library not being found.

Copy link
Author

Choose a reason for hiding this comment

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

I removed the dependency since it is no needed from v1.2.2.

"xo": "^0.56.0"
},
"xo": {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
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
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
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
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
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();
});