Skip to content

Commit a3decfb

Browse files
Allow input start with slash
by replacing slashes at the start of the input
1 parent 58b5c4a commit a3decfb

File tree

4 files changed

+3
-14
lines changed

4 files changed

+3
-14
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Accepts any value supported by [`URLSearchParams()`](https://developer.mozilla.o
167167

168168
Type: `string | URL`
169169

170-
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.
170+
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.
171171

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

source/core/Ky.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,7 @@ export class Ky {
146146
}
147147

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

153151
if (!this._options.prefixUrl.endsWith('/')) {
154152
this._options.prefixUrl += '/';

source/types/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export type KyOptions = {
6666
searchParams?: SearchParamsOption;
6767

6868
/**
69-
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.
69+
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.
7070
7171
Useful when used with [`ky.extend()`](#kyextenddefaultoptions) to create niche-specific Ky-instances.
7272

test/prefix-url.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,5 @@ test('prefixUrl option', async t => {
2727
t.is(await ky('', {prefixUrl: `${server.url}/`}).text(), 'zebra');
2828
t.is(await ky('', {prefixUrl: new URL(server.url)}).text(), 'zebra');
2929

30-
t.throws(
31-
() => {
32-
void ky('/unicorn', {prefixUrl: `${server.url}/api`});
33-
},
34-
{
35-
message: '`input` must not begin with a slash when using `prefixUrl`',
36-
},
37-
);
38-
3930
await server.close();
4031
});

0 commit comments

Comments
 (0)