Minor tweaks
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent 648e25d · Browse files · 5 files·+70 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+70 added−14 removed
- tests:+53 added−0 removed
- other:+17 added−14 removed
Provenance
- Commit
4b37c96- Tree
c739eee- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
648e25d
5 files changed+70 added−14 removed
modified:source/core/Ky.ts+15 added-12 removed
30 unmodified lines31 supportsRequestStreams,32 } from './constants.js';33 34 const maxErrorResponseBodySize = 10 * 1024 * 1024;35+const prefixUrlRenamedErrorMessage = 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl';36 37 const createTextDecoder = (contentType: string): TextDecoder => {38 const match = /;\s*charset\s*=\s*(?:"([^"]+)"|([^;,\s]+))/i.exec(contentType);39 const charset = match?.[1] ?? match?.[2];211 unmodified lines251 252 // eslint-disable-next-line complexity253 constructor(input: Input, options: Options = {}) {254 this.#input = input;255+ if (Object.hasOwn(options, 'prefixUrl')) {256+ throw new Error(prefixUrlRenamedErrorMessage);257+ }258 259 this.#options = {260 ...options,261 headers: mergeHeaders((this.#input as Request).headers, options.headers),20 unmodified lines282 throw new TypeError('`input` must be a string, URL, or Request');283 }284 285 if (typeof this.#input === 'string') {282- if ((this.#options as any).prefixUrl) {283- throw new Error('The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl');286+ if (this.#options.prefix) {287+ const normalizedPrefix = this.#options.prefix.replace(/\/+$/, '');288+ const normalizedInput = this.#input.replace(/^\/+/, '');289+ this.#input = `${normalizedPrefix}/${normalizedInput}`;290 }291 286- if (this.#options.prefix) {287- this.#options.prefix += this.#options.prefix.endsWith('/') ? '' : '/';292+ if (this.#options.baseUrl) {293+ let absoluteInput: URL | undefined;294+ try {295+ absoluteInput = new URL(this.#input);296+ } catch {}297 289- if (this.#input.startsWith('/')) {290- this.#input = this.#input.slice(1);298+ if (!absoluteInput) {299+ this.#input = new URL(this.#input, (new Request(this.#options.baseUrl)).url);300 }292-293- this.#input = this.#options.prefix + this.#input;301 }295-296- if (this.#options.baseUrl) {297- this.#input = new URL(this.#input, (new Request(this.#options.baseUrl ?? '')).url);298- }302 }303 304 if (supportsAbortController && supportsAbortSignal) {305 this.#userProvidedAbortSignal = this.#options.signal ?? (this.#input as Request).signal;477 unmodified linesmodified:source/types/options.ts+1 added-1 removed
124 unmodified lines125 126 *In most cases, you should use the `baseUrl` option instead, as it is more consistent with web standards. However, `prefix` is useful if you want origin-relative `input` URLs, such as `/users`, to be treated as if they were page-relative. In other words, the leading slash of the `input` will essentially be ignored, because the `prefix` will become part of the `input` before URL resolution happens.*127 128 Notes:129- - The `prefix` and `input` are joined with a slash `/`, which is deduplicated with any adjacent slashes already present in `prefix` or `input`.129+ - The `prefix` and `input` are joined with a slash `/`, and slashes are normalized at the join boundary by trimming trailing slashes from `prefix` and leading slashes from `input`.130 - After `prefix` and `input` are joined, the result is resolved against the `baseUrl` option, if present.131 132 @example133 ```279 unmodified linesmodified:test/base-url.ts+9 added
2 unmodified lines345678910114 unmodified lines161718192021222324252627282930313233343536373839408 unmodified lines2 unmodified linesimport {createHttpTestServer} from './helpers/create-http-test-server.js';
test('baseUrl option', async t => { const server = await createHttpTestServer(); const serverHost = new URL(server.url).host; server.get('/', (_request, response) => { response.end('/'); }); server.get('/foo', (_request, response) => {4 unmodified lines }); server.get('/foo/bar', (_request, response) => { response.end('/foo/bar'); }); server.get('/api/v1/users', (_request, response) => { response.end('/api/v1/users'); });
t.is( // @ts-expect-error {baseUrl: boolean} isn't officially supported await ky(`${server.url}/foo/bar`, {baseUrl: false}).text(), '/foo/bar', ); t.is(await ky(`${server.url}/foo/bar`, {baseUrl: '/api'}).text(), '/foo/bar'); t.is(await ky(`${server.url}/foo/bar`, {baseUrl: ''}).text(), '/foo/bar'); t.is(await ky(new URL(`${server.url}/foo/bar`), {baseUrl: ''}).text(), '/foo/bar'); t.is(await ky(new URL(`${server.url}/foo/bar`), {baseUrl: `${server.url}/api/`}).text(), '/foo/bar'); t.is(await ky(new Request(`${server.url}/foo/bar`), {baseUrl: `${server.url}/api/`}).text(), '/foo/bar'); t.is(await ky('foo/bar', {baseUrl: server.url}).text(), '/foo/bar'); t.is(await ky(`//${serverHost}/foo/bar`, {baseUrl: `${server.url}/api/`}).text(), '/foo/bar'); t.is(await ky('/users', {prefix: 'v1', baseUrl: `${server.url}/api/`}).text(), '/api/v1/users'); t.is(await ky('foo/bar', {baseUrl: new URL(server.url)}).text(), '/foo/bar'); t.is(await ky('/bar', {baseUrl: `${server.url}/foo/`}).text(), '/bar'); t.is(await ky('/bar', {baseUrl: `${server.url}/foo`}).text(), '/bar'); t.is(await ky('bar', {baseUrl: `${server.url}/foo/`}).text(), '/foo/bar');8 unmodified linesmodified:test/prefix.ts+44 added
26 unmodified lines2728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899026 unmodified lines t.is(await ky('foo/bar', {prefix: server.url}).text(), '/foo/bar'); t.is(await ky('foo/bar', {prefix: new URL(server.url)}).text(), '/foo/bar'); t.is(await ky('/bar', {prefix: `${server.url}/foo/`}).text(), '/foo/bar'); t.is(await ky('/bar', {prefix: `${server.url}/foo`}).text(), '/foo/bar'); t.is(await ky('//bar', {prefix: `${server.url}/foo`}).text(), '/foo/bar'); t.is(await ky('/bar', {prefix: `${server.url}/foo//`}).text(), '/foo/bar'); t.is(await ky('bar', {prefix: `${server.url}/foo/`}).text(), '/foo/bar'); t.is(await ky('bar', {prefix: `${server.url}/foo`}).text(), '/foo/bar'); t.is(await ky('bar', {prefix: new URL(`${server.url}/foo`)}).text(), '/foo/bar'); t.is(await ky('', {prefix: server.url}).text(), '/'); t.is(await ky('', {prefix: `${server.url}/`}).text(), '/'); t.is(await ky('', {prefix: new URL(server.url)}).text(), '/'); t.is( await ky( 'bar', Object.assign( Object.create({prefixUrl: `${server.url}/api`}), {prefix: `${server.url}/foo`}, ), ).text(), '/foo/bar', ); t.throws( () => { void ky(`${server.url}/unicorn`, {prefixUrl: ''}); }, { message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl', }, ); t.throws( () => { void ky(`${server.url}/unicorn`, {prefixUrl: undefined}); }, { message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl', }, );
t.throws( () => { void ky('/unicorn', {prefixUrl: `${server.url}/api`}); }, { message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl', }, ); t.throws( () => { void ky(new Request(`${server.url}/unicorn`), {prefixUrl: `${server.url}/api`}); }, { message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl', }, ); t.throws( () => { void ky(new URL(`${server.url}/unicorn`), {prefixUrl: `${server.url}/api`}); }, { message: 'The `prefixUrl` option has been renamed `prefix` in v2 and enhanced to allow slashes in input. See also the new `baseUrl` option for improved flexibility with standard URL resolution: https://github.com/sindresorhus/ky#baseurl', }, );});modified:readme.md+1 added-1 removed
218 unmodified lines219 //=> 'https://example.com/api/users'220 ```221 222 Notes:223- - The `prefix` and `input` are joined with a slash `/`, which is deduplicated with any adjacent slashes already present in `prefix` or `input`.223+ - The `prefix` and `input` are joined with a slash `/`, and slashes are normalized at the join boundary by trimming trailing slashes from `prefix` and leading slashes from `input`.224 - After `prefix` and `input` are joined, the result is resolved against the `baseUrl` option, if present.225 226 ##### retry227 1229 unmodified lines