Skip to content
Myr

options.ts

This file

Path

source/utils/options.ts

61 lines · 2.4 KiB · TypeScript

Last changed by

RefactorSindre Sorhusbefore adoptiona5ed57b

History of this file

Take it

The walk stopped after 200 commits: lines older than 37fc250 point at it.

Fix hook state leaks and fetch option forwardingSindre Sorhusbefore adoption2df9b7e

line 1why, one step back
1import {kyOptionKeys, requestOptionsRegistry} from '../core/constants.js';

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

line 2why, one step back
2import type {SearchParamsOption} from '../types/options.js';

RefactorSindre Sorhusbefore adoptiona5ed57b

line 3why, one step back
3import {deletedParametersSymbol, type MarkedSearchParameters} from './merge.js';

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 4–9why, one step back
4
5export const findUnknownOptions = (6 options: Record<string, unknown>,7): Record<string, unknown> => {8 const unknownOptions: Record<string, unknown> = {};9

RefactorSindre Sorhusbefore adoptiona5ed57b

lines 10–11why, one step back
10 // `Object.keys()` skips inherited properties.
11 for (const key of Object.keys(options)) {

Fix hook state leaks and fetch option forwardingSindre Sorhusbefore adoption2df9b7e

lines 12–16why, one step back
12 // Forward every non-standard, non-Ky option to fetch().
13 // We intentionally do not check whether the key also exists on `Request`, because some runtimes14 // patch `Request.prototype` with fetch-only extensions. For example, Next.js adds `next`, and the15 // old `key in request` heuristic dropped it unless Ky kept a special-case allowlist.16 // Passing all non-standard keys makes that allowlist unnecessary and preserves future fetch extensions too.

TweaksSindre Sorhusbefore adoption3ded2aa

line 17why, one step back
17 // The registries are plain objects, so `Object.hasOwn()` is required: `in` would also match `Object.prototype` members such as `constructor` and `toString`, silently dropping those options.

Validate the retry options and forward every fetch-only extensionSindre Sorhusbefore adoptiond9774cf

line 18why, one step back
18 if (!Object.hasOwn(requestOptionsRegistry, key) && !Object.hasOwn(kyOptionKeys, key)) {

TweaksSindre Sorhusbefore adoption3ded2aa

lines 19–25why, one step back
19 // Defined instead of assigned, so a `__proto__` key becomes an own property of the init object rather than replacing its prototype, which would both drop the option and give `fetch()` an object that inherits from caller data.
20 Object.defineProperty(unknownOptions, key, {21 value: options[key],22 writable: true,23 enumerable: true,24 configurable: true,25 });

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 26–32why, one step back
26 }
27 }2829 return unknownOptions;30};3132export const hasSearchParameters = (search: SearchParamsOption): boolean => {

Count a compressed response as unknown, deep-copy context, and isolate `init` hook assignmentsSindre Sorhusbefore adoptiond89c8c8

line 33why, one step back
33 if (search === undefined) {

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

line 34why, one step back
34 return false;

Count a compressed response as unknown, deep-copy context, and isolate `init` hook assignmentsSindre Sorhusbefore adoptiond89c8c8

lines 35–36why, one step back
35 }
36

TweaksSindre Sorhusbefore adoption3ded2aa

line 37why, one step back
37 // Option merging already rejects `null`, but an `init` hook assigns straight onto the options object.

Count a compressed response as unknown, deep-copy context, and isolate `init` hook assignmentsSindre Sorhusbefore adoptiond89c8c8

lines 38–39why, one step back
38 if (search === null) {
39 throw new TypeError('The `searchParams` option must not be `null`. Use `undefined` to clear it.');

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 40–47why, one step back
40 }
4142 // The `typeof array` still gives "object", so we need different checking for array.43 if (Array.isArray(search)) {44 return search.length > 0;45 }4647 if (search instanceof URLSearchParams) {

RefactorSindre Sorhusbefore adoptiona5ed57b

line 48why, one step back
48 return search.size > 0 || Boolean((search as MarkedSearchParameters)[deletedParametersSymbol]?.size);

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 49–61why, one step back
49 }
5051 // Record52 if (typeof search === 'object') {53 return Object.keys(search).length > 0;54 }5556 if (typeof search === 'string') {57 return search.trim().length > 0;58 }5960 return Boolean(search);61};