From 868ba34042f778a615592fec60568543cd4a04ed Mon Sep 17 00:00:00 2001 From: Janne Liuhtonen Date: Sat, 20 Apr 2024 22:44:18 +0300 Subject: [PATCH 1/3] Fix request types for use with Node 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. --- source/types/request.ts | 46 +++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/source/types/request.ts b/source/types/request.ts index d56219a5..62637b8e 100644 --- a/source/types/request.ts +++ b/source/types/request.ts @@ -1,15 +1,49 @@ +type UndiciHeadersInit = + | string[][] + | Record + | Headers; + +type UndiciBodyInit = + | ArrayBuffer + | AsyncIterable + | Blob + | FormData + | Iterable + | 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; From 733c34ae2d81e7011b88e78a2cc52db345d40881 Mon Sep 17 00:00:00 2001 From: Janne Liuhtonen Date: Sun, 21 Apr 2024 12:46:23 +0300 Subject: [PATCH 2/3] Add comment about Undici request types Add link to DefinitelyTyped issue with missing exported globals. --- source/types/request.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/source/types/request.ts b/source/types/request.ts index 62637b8e..8496d1aa 100644 --- a/source/types/request.ts +++ b/source/types/request.ts @@ -1,3 +1,9 @@ +/** + * Undici types need to be here because they are not exported to globals by @types/node. + * See https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/69408 + * + * After the types are exported to globals, the Undici types can be removed from here. + */ type UndiciHeadersInit = | string[][] | Record From ebfc6f4dad3764086ab4be39ec48f7b83058b7dd Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 22 Apr 2024 13:14:32 +0700 Subject: [PATCH 3/3] Update request.ts --- source/types/request.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/source/types/request.ts b/source/types/request.ts index 8496d1aa..17e31086 100644 --- a/source/types/request.ts +++ b/source/types/request.ts @@ -1,9 +1,10 @@ -/** - * Undici types need to be here because they are not exported to globals by @types/node. - * See https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/69408 - * - * After the types are exported to globals, the Undici types can be removed from here. - */ +/* +Undici types need to be here because they are not exported to globals by @types/node. +See https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/69408 + +After the types are exported to globals, the Undici types can be removed from here. +*/ + type UndiciHeadersInit = | string[][] | Record