From 9560921b6bbde0d8cae486ebb9896d5ad4199cd0 Mon Sep 17 00:00:00 2001 From: Kelvin Del Monte Date: Fri, 27 Oct 2023 17:14:11 -0400 Subject: [PATCH] Replace hardcoded request options list in favor of a registry of options based on Typescript interfaces --- source/core/constants.ts | 34 ++++++++++++++++++---------------- source/types/request.ts | 5 +++++ source/utils/options.ts | 4 ++-- 3 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 source/types/request.ts diff --git a/source/core/constants.ts b/source/core/constants.ts index 088ff58d..0f1fcdbd 100644 --- a/source/core/constants.ts +++ b/source/core/constants.ts @@ -1,5 +1,6 @@ import type {Expect, Equal} from '@type-challenges/utils'; import {type HttpMethod, type KyOptionsRegistry} from '../types/options.js'; +import {type RequestInitRegistry} from '../types/request.js'; export const supportsRequestStreams = (() => { let duplexAccessed = false; @@ -59,19 +60,20 @@ export const kyOptionKeys: KyOptionsRegistry = { fetch: true, }; -export const requestOptions = new Set([ - 'options', - 'method', - 'headers', - 'body', - 'mode', - 'credentials', - 'cache', - 'redirect', - 'referrer', - 'referrerPolicy', - 'integrity', - 'keepalive', - 'signal', - 'priority', -]); +export const requestOptionsRegistry: RequestInitRegistry = { + method: true, + headers: true, + body: true, + mode: true, + credentials: true, + cache: true, + redirect: true, + referrer: true, + referrerPolicy: true, + integrity: true, + keepalive: true, + signal: true, + window: true, + dispatcher: true, + duplex: true, +}; diff --git a/source/types/request.ts b/source/types/request.ts new file mode 100644 index 00000000..15fea38c --- /dev/null +++ b/source/types/request.ts @@ -0,0 +1,5 @@ +import {type RequestInit as UndiciRequestInit} from 'undici-types'; + +type CombinedRequestInit = globalThis.RequestInit & UndiciRequestInit; + +export type RequestInitRegistry = {[K in keyof CombinedRequestInit]-?: true}; diff --git a/source/utils/options.ts b/source/utils/options.ts index 95ae52cc..4968f2bf 100644 --- a/source/utils/options.ts +++ b/source/utils/options.ts @@ -1,4 +1,4 @@ -import {kyOptionKeys, requestOptions} from '../core/constants.js'; +import {kyOptionKeys, requestOptionsRegistry} from '../core/constants.js'; export const findUnknownOptions = ( request: Request, @@ -7,7 +7,7 @@ export const findUnknownOptions = ( const unknownOptions: Record = {}; for (const key in options) { - if (!requestOptions.has(key) && !(key in kyOptionKeys) && !(key in request)) { + if (!(key in requestOptionsRegistry) && !(key in kyOptionKeys) && !(key in request)) { unknownOptions[key] = options[key]; } }