Skip to content

Commit

Permalink
Replace hardcoded request options list in favor of a registry of opti…
Browse files Browse the repository at this point in the history
…ons based on Typescript interfaces
  • Loading branch information
kdelmonte committed Oct 27, 2023
1 parent 9485d30 commit a34f99b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
34 changes: 18 additions & 16 deletions source/core/constants.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
};
5 changes: 5 additions & 0 deletions source/types/request.ts
Original file line number Diff line number Diff line change
@@ -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};
4 changes: 2 additions & 2 deletions source/utils/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {kyOptionKeys, requestOptions} from '../core/constants.js';
import {kyOptionKeys, requestOptionsRegistry} from '../core/constants.js';

export const findUnknownOptions = (
request: Request,
Expand All @@ -7,7 +7,7 @@ export const findUnknownOptions = (
const unknownOptions: Record<string, unknown> = {};

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];
}
}
Expand Down

0 comments on commit a34f99b

Please sign in to comment.