diff --git a/readme.md b/readme.md index 7ec0aa6e..c7e1114e 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/source/core/Ky.ts b/source/core/Ky.ts index 9147191a..407b3786 100644 --- a/source/core/Ky.ts +++ b/source/core/Ky.ts @@ -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 += '/'; diff --git a/source/types/options.ts b/source/types/options.ts index d69555f1..892dc0bf 100644 --- a/source/types/options.ts +++ b/source/types/options.ts @@ -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. diff --git a/test/browser.ts b/test/browser.ts index e8766dcf..47fc1b06 100644 --- a/test/browser.ts +++ b/test/browser.ts @@ -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 @@ -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) => { diff --git a/test/prefix-url.ts b/test/prefix-url.ts index e09650b4..6787165c 100644 --- a/test/prefix-url.ts +++ b/test/prefix-url.ts @@ -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(); });