Skip to content
Myr

Fix support with environments that add support for custom `fetch` options (#536)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kelvin Del Monte · committed by GitHub · parent d372e2e · Browse files · 6 files+108 added−49 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+108 added−49 removed

  • tests:+13 added−0 removed
  • other:+95 added−49 removed

6 files+108 added−49 removed

modified:source/core/constants.ts+14 added-1 removed
modified:source/core/Ky.ts+5 added-2 removed
6 unmodified lines
7import {normalizeRequestMethod, normalizeRetryOptions} from '../utils/normalize.js';8import timeout, {type TimeoutOptions} from '../utils/timeout.js';9import delay from '../utils/delay.js';10import {type ObjectEntries} from '../utils/types.js';11import {findUnknownOptions} from '../utils/options.js';12import {13 maxSafeTimeout,14 responseTypes,15 stop,
278 unmodified lines
294 return result;295 }296 }297298 const nonRequestOptions = findUnknownOptions(this.request, this._options);299300 if (this._options.timeout === false) {298 return this._options.fetch(this.request.clone());301 return this._options.fetch(this.request.clone(), nonRequestOptions);302 }303301 return timeout(this.request.clone(), this.abortController, this._options as TimeoutOptions);304 return timeout(this.request.clone(), nonRequestOptions, this.abortController, this._options as TimeoutOptions);305 }306307 /* istanbul ignore next */308 protected _stream(response: Response, onDownloadProgress: Options['onDownloadProgress']) {
55 unmodified lines
modified:source/types/options.ts+58 added-45 removed
23 unmodified lines
2425export type KyHeadersInit = HeadersInit | Record<string, string | undefined>;2627/**28Options are the same as `window.fetch`, with some exceptions.28Custom Ky options29*/30export interface Options extends Omit<RequestInit, 'headers'> { // eslint-disable-line @typescript-eslint/consistent-type-definitions -- This must stay an interface so that it can be extended outside of Ky for use in `ky.create`.31 /**32 HTTP method used to make the request.3034 Internally, the standard methods (`GET`, `POST`, `PUT`, `PATCH`, `HEAD` and `DELETE`) are uppercased in order to avoid server errors due to case sensitivity.35 */36 method?: LiteralUnion<HttpMethod, string>;3731export type KyOptions = {32 /**39 HTTP headers used to make the request.4041 You can pass a `Headers` instance or a plain object.4243 You can remove a header with `.extend()` by passing the header with an `undefined` value.4445 @example46 ```47 import ky from 'ky';4849 const url = 'https://sindresorhus.com';5051 const original = ky.create({52 headers: {53 rainbow: 'rainbow',54 unicorn: 'unicorn'55 }56 });5758 const extended = original.extend({59 headers: {60 rainbow: undefined61 }62 });6364 const response = await extended(url).json();6566 console.log('rainbow' in response);67 //=> false6869 console.log('unicorn' in response);70 //=> true71 ```72 */73 headers?: KyHeadersInit;7475 /**33 Shortcut for sending JSON. Use this instead of the `body` option.3435 Accepts any plain object or value, which will be `JSON.stringify()`'d and sent in the body with the correct header set.36 */
140 unmodified lines
177 const json = await ky('https://example.com', {fetch}).json();178 ```179 */180 fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>;181};182183/**184Each key from KyOptions is present and set to `true`.185186This type is used for identifying and working with the known keys in KyOptions.187*/188export type KyOptionsRegistry = {[K in keyof KyOptions]-?: true};189190/**191Options are the same as `window.fetch`, except for the KyOptions192*/193export interface Options extends KyOptions, Omit<RequestInit, 'headers'> { // eslint-disable-line @typescript-eslint/consistent-type-definitions -- This must stay an interface so that it can be extended outside of Ky for use in `ky.create`.194 /**195 HTTP method used to make the request.196197 Internally, the standard methods (`GET`, `POST`, `PUT`, `PATCH`, `HEAD` and `DELETE`) are uppercased in order to avoid server errors due to case sensitivity.198 */199 method?: LiteralUnion<HttpMethod, string>;200201 /**202 HTTP headers used to make the request.203204 You can pass a `Headers` instance or a plain object.205206 You can remove a header with `.extend()` by passing the header with an `undefined` value.207208 @example209 ```210 import ky from 'ky';211212 const url = 'https://sindresorhus.com';213214 const original = ky.create({215 headers: {216 rainbow: 'rainbow',217 unicorn: 'unicorn'218 }219 });220221 const extended = original.extend({222 headers: {223 rainbow: undefined224 }225 });226227 const response = await extended(url).json();228229 console.log('rainbow' in response);230 //=> false231232 console.log('unicorn' in response);233 //=> true234 ```235 */236 headers?: KyHeadersInit;237}238239export type InternalOptions = Required<240Omit<Options, 'hooks' | 'retry'>,
23 unmodified lines
added:source/utils/options.ts+16 added
modified:source/utils/timeout.ts+2 added-1 removed
modified:test/fetch.ts+13 added