Skip to content

Commit

Permalink
Fix request types for use with Node
Browse files Browse the repository at this point in the history
When using Ky with Node, TypeScript will run into compilation error regarding types required by `UndiciRequestInit`. This is because `@types/node` does not yet provide these types, and they are not imported from `undici-types` either. The latter would not even be possible to keep Ky usable with browsers.

This happened to work in the project, because `@sindresorhus/tsconfig` includes `DOM` in the libs section. In most Node applications, the DOM lib should not be there and clashes with Node types. One way to circumvent this is to add `skipLibCheck: true` to `tsconfig.json`, but this is not a good solution.

This commit adds the needed types until `@types/node` imports the missing types and adds them to globals. After that, all "Undici" types from the request types should be removed.
  • Loading branch information
jliuhtonen committed Apr 20, 2024
1 parent 2d56baf commit 868ba34
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions source/types/request.ts
@@ -1,15 +1,49 @@
type UndiciHeadersInit =
| string[][]
| Record<string, string | readonly string[]>
| Headers;

type UndiciBodyInit =
| ArrayBuffer
| AsyncIterable<Uint8Array>
| Blob
| FormData
| Iterable<Uint8Array>
| NodeJS.ArrayBufferView
| URLSearchParams
// eslint-disable-next-line @typescript-eslint/ban-types
| null
| string;

type UndiciRequestRedirect = 'error' | 'follow' | 'manual';

type UndiciRequestCredentials = 'omit' | 'include' | 'same-origin';

type UndiciReferrerPolicy =
| ''
| 'no-referrer'
| 'no-referrer-when-downgrade'
| 'origin'
| 'origin-when-cross-origin'
| 'same-origin'
| 'strict-origin'
| 'strict-origin-when-cross-origin'
| 'unsafe-url';

type UndiciRequestMode = 'cors' | 'navigate' | 'no-cors' | 'same-origin';

type UndiciRequestInit = {
method?: string;
keepalive?: boolean;
headers?: HeadersInit;
body?: BodyInit;
redirect?: RequestRedirect;
headers?: UndiciHeadersInit;
body?: UndiciBodyInit;
redirect?: UndiciRequestRedirect;
integrity?: string;
signal?: AbortSignal | undefined;
credentials?: RequestCredentials;
mode?: RequestMode;
credentials?: UndiciRequestCredentials;
mode?: UndiciRequestMode;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
referrerPolicy?: UndiciReferrerPolicy;
window?: undefined;
dispatcher?: unknown;
duplex?: unknown;
Expand Down

0 comments on commit 868ba34

Please sign in to comment.