Skip to content

Commit

Permalink
Make types compatible with both DOM and Node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
alecmev committed Nov 12, 2023
1 parent d7b0abc commit 2f5f86e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion source/core/Ky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class Ky {
this._options.duplex = 'half';
}

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

if (this._options.searchParams) {
// eslint-disable-next-line unicorn/prevent-abbreviations
Expand Down
9 changes: 5 additions & 4 deletions source/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export type DownloadProgress = {
totalBytes: number;
};

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

/**
Custom Ky options
Expand Down Expand Up @@ -177,7 +178,7 @@ export type KyOptions = {
const json = await ky('https://example.com', {fetch}).json();
```
*/
fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
fetch?: (input: Input, init?: RequestInit) => Promise<Response>;
};

/**
Expand Down Expand Up @@ -251,8 +252,8 @@ Normalized options passed to the `fetch` call and the `beforeRequest` hooks.
*/
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`.
// Extended from `RequestInit`, but ensured to be set (not optional).
method: RequestInit['method'];
credentials: RequestInit['credentials'];
method: NonNullable<RequestInit['method']>;
credentials: NonNullable<RequestInit['credentials']>;

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

0 comments on commit 2f5f86e

Please sign in to comment.