Skip to content
Myr

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

5 files+70 added−14 removed

modified:source/core/Ky.ts+15 added-12 removed
30 unmodified lines
31 supportsRequestStreams,32} from './constants.js';3334const maxErrorResponseBodySize = 10 * 1024 * 1024;35const 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';3637const createTextDecoder = (contentType: string): TextDecoder => {38 const match = /;\s*charset\s*=\s*(?:"([^"]+)"|([^;,\s]+))/i.exec(contentType);39 const charset = match?.[1] ?? match?.[2];
211 unmodified lines
251252 // 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 }258259 this.#options = {260 ...options,261 headers: mergeHeaders((this.#input as Request).headers, options.headers),
20 unmodified lines
282 throw new TypeError('`input` must be a string, URL, or Request');283 }284285 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 }291286 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 {}297289 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 }292293 this.#input = this.#options.prefix + this.#input;301 }295296 if (this.#options.baseUrl) {297 this.#input = new URL(this.#input, (new Request(this.#options.baseUrl ?? '')).url);298 }302 }303304 if (supportsAbortController && supportsAbortSignal) {305 this.#userProvidedAbortSignal = this.#options.signal ?? (this.#input as Request).signal;
477 unmodified lines
modified:source/types/options.ts+1 added-1 removed
124 unmodified lines
125126 *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.*127128 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.131132 @example133 ```
279 unmodified lines
modified:test/base-url.ts+9 added
modified:test/prefix.ts+44 added
modified:readme.md+1 added-1 removed
218 unmodified lines
219//=> 'https://example.com/api/users'220```221222Notes: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.225226##### retry227
1229 unmodified lines