Skip to content

Commit

Permalink
Allow input start with slash
Browse files Browse the repository at this point in the history
by replacing slashes at the start of the input
  • Loading branch information
jdavidferreira committed Feb 26, 2024
1 parent 58b5c4a commit a3decfb
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 14 deletions.
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
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();
});

0 comments on commit a3decfb

Please sign in to comment.