Skip to content

Commit 3cf5064

Browse files
authored
Make types compatible with both DOM and Node.js (#543)
1 parent d7b0abc commit 3cf5064

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

source/core/Ky.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export class Ky {
175175
this._options.duplex = 'half';
176176
}
177177

178-
this.request = new globalThis.Request(this._input as RequestInfo, this._options as RequestInit);
178+
this.request = new globalThis.Request(this._input, this._options);
179179

180180
if (this._options.searchParams) {
181181
// eslint-disable-next-line unicorn/prevent-abbreviations

source/types/options.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export type DownloadProgress = {
2222
totalBytes: number;
2323
};
2424

25-
export type KyHeadersInit = HeadersInit | Record<string, string | undefined>;
25+
// Not HeadersInit directly because @types/node doesn't export it
26+
export type KyHeadersInit = NonNullable<RequestInit['headers']> | Record<string, string | undefined>;
2627

2728
/**
2829
Custom Ky options
@@ -177,7 +178,7 @@ export type KyOptions = {
177178
const json = await ky('https://example.com', {fetch}).json();
178179
```
179180
*/
180-
fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
181+
fetch?: (input: Input, init?: RequestInit) => Promise<Response>;
181182
};
182183

183184
/**
@@ -251,8 +252,8 @@ Normalized options passed to the `fetch` call and the `beforeRequest` hooks.
251252
*/
252253
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`.
253254
// 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']>;
256257

257258
// Extended from custom `KyOptions`, but ensured to be set (not optional).
258259
retry: RetryOptions;

source/utils/merge.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export const validateAndMerge = (...sources: Array<Partial<Options> | undefined>
1212
};
1313

1414
export const mergeHeaders = (source1: KyHeadersInit = {}, source2: KyHeadersInit = {}) => {
15-
const result = new globalThis.Headers(source1 as HeadersInit);
15+
const result = new globalThis.Headers(source1 as RequestInit['headers']);
1616
const isHeadersInstance = source2 instanceof globalThis.Headers;
17-
const source = new globalThis.Headers(source2 as HeadersInit);
17+
const source = new globalThis.Headers(source2 as RequestInit['headers']);
1818

1919
for (const [key, value] of source.entries()) {
2020
if ((isHeadersInstance && value === 'undefined') || value === undefined) {

test/headers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test.serial('works with nullish headers even in old browsers', async t => {
2323
// so we check that Ky never does that and passes an empty object instead.
2424
// See: https://github.com/sindresorhus/ky/issues/260
2525
globalThis.Headers = class Headers extends OriginalHeaders {
26-
constructor(headersInit?: HeadersInit | undefined) {
26+
constructor(headersInit?: RequestInit['headers']) {
2727
t.deepEqual(headersInit, {});
2828
super(headersInit);
2929
}

0 commit comments

Comments
 (0)