Make types compatible with both DOM and Node.js (#543)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Alec Mev · committed by GitHub · parent d7b0abc · Browse files · 4 files·+9 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+9 added−8 removed
- tests:+1 added−1 removed
- other:+8 added−7 removed
Provenance
- Commit
3cf5064- Tree
793b22c- Author
Alec Mev <alec@mev.earth>- Committer
GitHub <noreply@github.com>- Parent
d7b0abc
4 files changed+9 added−8 removed
modified:source/core/Ky.ts+1 added-1 removed
173 unmodified lines174 // @ts-expect-error - Types are outdated.175 this._options.duplex = 'half';176 }177 178- this.request = new globalThis.Request(this._input as RequestInfo, this._options as RequestInit);178+ this.request = new globalThis.Request(this._input, this._options);179 180 if (this._options.searchParams) {181 // eslint-disable-next-line unicorn/prevent-abbreviations182 const textSearchParams = typeof this._options.searchParams === 'string'181 unmodified linesmodified:source/types/options.ts+5 added-4 removed
20 unmodified lines21 */22 totalBytes: number;23 };24 25+// Not HeadersInit directly because @types/node doesn't export it25-export type KyHeadersInit = HeadersInit | Record<string, string | undefined>;26+export type KyHeadersInit = NonNullable<RequestInit['headers']> | Record<string, string | undefined>;27 28 /**29 Custom Ky options30 */146 unmodified lines177 178 const json = await ky('https://example.com', {fetch}).json();179 ```180 */180- fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>;181+ fetch?: (input: Input, init?: RequestInit) => Promise<Response>;182 };183 184 /**185 Each key from KyOptions is present and set to `true`.65 unmodified lines251 Normalized options passed to the `fetch` call and the `beforeRequest` hooks.252 */253 export interface NormalizedOptions extends RequestInit { // 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`.254 // Extended from `RequestInit`, but ensured to be set (not optional).254- method: RequestInit['method'];255- credentials: RequestInit['credentials'];255+ method: NonNullable<RequestInit['method']>;256+ credentials: NonNullable<RequestInit['credentials']>;257 258 // Extended from custom `KyOptions`, but ensured to be set (not optional).259 retry: RetryOptions;260 prefixUrl: string;4 unmodified linesmodified:source/utils/merge.ts+2 added-2 removed
10 unmodified lines1112131415151617171819202138 unmodified lines10 unmodified lines return deepMerge({}, ...sources);};
export const mergeHeaders = (source1: KyHeadersInit = {}, source2: KyHeadersInit = {}) => { const result = new globalThis.Headers(source1 as HeadersInit); const result = new globalThis.Headers(source1 as RequestInit['headers']); const isHeadersInstance = source2 instanceof globalThis.Headers; const source = new globalThis.Headers(source2 as HeadersInit); const source = new globalThis.Headers(source2 as RequestInit['headers']);
for (const [key, value] of source.entries()) { if ((isHeadersInstance && value === 'undefined') || value === undefined) { result.delete(key);38 unmodified linesmodified:test/headers.ts+1 added-1 removed
21 unmodified lines22 // Some old browsers throw for new Headers(undefined) or new Headers(null)23 // so we check that Ky never does that and passes an empty object instead.24 // See: https://github.com/sindresorhus/ky/issues/26025 globalThis.Headers = class Headers extends OriginalHeaders {26- constructor(headersInit?: HeadersInit | undefined) {26+ constructor(headersInit?: RequestInit['headers']) {27 t.deepEqual(headersInit, {});28 super(headersInit);29 }30 };406 unmodified lines