Skip to content
Myr

is.ts

This file

Path

source/utils/is.ts

24 lines · 1.7 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.

Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc

line 1why, one step back
1// eslint-disable-next-line @typescript-eslint/no-restricted-types

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

line 2why, one step back
2export const isObject = (value: unknown): value is object => value !== null && typeof value === 'object';

Accept a cross-realm `beforeError` error and validate a `ky.retry({delay})`Sindre Sorhusbefore adoptionbd0e26f

line 3why, one step back
3

RefactorSindre Sorhusbefore adoptiona5ed57b

lines 4–9why, one step back
4// eslint-disable-next-line @typescript-eslint/no-restricted-types
5export const isNonArrayObject = (value: unknown): value is object => isObject(value) && !Array.isArray(value);67// `NaN` fails the comparison, so it is rejected too.8export const isNonNegativeNumber = (value: unknown): value is number => typeof value === 'number' && value >= 0;9

Accept a cross-realm `beforeError` error and validate a `ky.retry({delay})`Sindre Sorhusbefore adoptionbd0e26f

lines 10–14why, one step back
10const objectToString = Object.prototype.toString;
1112// An `Error` from another realm (an iframe, a worker, a `vm` context, or a duplicated dependency) fails `instanceof`, so the internal brand is checked as well, the same way the request and response checks do. `instanceof` stays first because it also accepts `DOMException`, which an `AbortSignal` reason is, and that does not carry the `[object Error]` brand.13export const isError = (value: unknown): value is Error =>14 value instanceof Error || objectToString.call(value) === '[object Error]';

RefactorSindre Sorhusbefore adoptiona5ed57b

lines 15–24why, one step back
15
16// A `Request` from another realm, such as one a hook built in an iframe, is accepted through its internal brand.17export const isRequest = (value: unknown): value is Request =>18 value instanceof globalThis.Request || objectToString.call(value) === '[object Request]';1920// Accepted custom responses are treated as full Responses throughout Ky.21// If a custom fetch returns one, it must behave like a Response for cloning,22// body consumption, `json()` decoration, and any enabled stream features.23export const isResponse = (value: unknown): value is Response =>24 value instanceof globalThis.Response || objectToString.call(value) === '[object Response]';