From c2387e8071e4ed7e32b512ee6f78375ac5c227b8 Mon Sep 17 00:00:00 2001 From: pan93412 Date: Wed, 15 Feb 2023 16:48:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Allow=20=E2=80=9Cundefined=E2=80=9C=20as?= =?UTF-8?q?=20value=20in=20headers=20(#1929)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Allow “undefined“ as value in headers https://github.com/nodejs/undici/pull/1896#discussion_r1104301293 Signed-off-by: pan93412 * test: Add test for our IncomingHttpHeaders Co-Authored-By: Simen Bekkhus Signed-off-by: pan93412 * chore: Upgrade TypeScript to 4.9.5 Signed-off-by: pan93412 --------- Signed-off-by: pan93412 Co-authored-by: Simen Bekkhus --- docs/api/Dispatcher.md | 10 +++++----- package.json | 2 +- test/types/dispatcher.test-d.ts | 8 ++++++++ test/types/header.test-d.ts | 16 ++++++++++++++++ types/header.d.ts | 2 +- 5 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 test/types/header.test-d.ts diff --git a/docs/api/Dispatcher.md b/docs/api/Dispatcher.md index 25c980d612b..fa8ae2dd6fc 100644 --- a/docs/api/Dispatcher.md +++ b/docs/api/Dispatcher.md @@ -74,7 +74,7 @@ Returns: `void | Promise` - Only returns a `Promise` if no `callbac #### Parameter: `ConnectData` * **statusCode** `number` -* **headers** `Record` +* **headers** `Record` * **socket** `stream.Duplex` * **opaque** `unknown` @@ -383,7 +383,7 @@ Extends: [`RequestOptions`](#parameter-requestoptions) #### Parameter: PipelineHandlerData * **statusCode** `number` -* **headers** `Record` +* **headers** `Record` * **opaque** `unknown` * **body** `stream.Readable` * **context** `object` @@ -644,7 +644,7 @@ Returns: `void | Promise` - Only returns a `Promise` if no `callback #### Parameter: `StreamFactoryData` * **statusCode** `number` -* **headers** `Record` +* **headers** `Record` * **opaque** `unknown` * **onInfo** `({statusCode: number, headers: Record}) => void | null` (optional) - Default: `null` - Callback collecting all the info headers (HTTP 100-199) received. @@ -853,9 +853,9 @@ Emitted when dispatcher is no longer busy. ## Parameter: `UndiciHeaders` -* `Record | string[] | null` +* `Record | string[] | null` -Header arguments such as `options.headers` in [`Client.dispatch`](Client.md#clientdispatchoptions-handlers) can be specified in two forms; either as an object specified by the `Record` (`IncomingHttpHeaders`) type, or an array of strings. An array representation of a header list must have an even length or an `InvalidArgumentError` will be thrown. +Header arguments such as `options.headers` in [`Client.dispatch`](Client.md#clientdispatchoptions-handlers) can be specified in two forms; either as an object specified by the `Record` (`IncomingHttpHeaders`) type, or an array of strings. An array representation of a header list must have an even length or an `InvalidArgumentError` will be thrown. Keys are lowercase and values are not modified. diff --git a/package.json b/package.json index c664a9af9b3..e3c2d12ba9f 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,7 @@ "table": "^6.8.0", "tap": "^16.1.0", "tsd": "^0.25.0", - "typescript": "^4.8.4", + "typescript": "^4.9.5", "wait-on": "^6.0.0", "ws": "^8.11.0" }, diff --git a/test/types/dispatcher.test-d.ts b/test/types/dispatcher.test-d.ts index 94c66ee3abc..2f31b91c987 100644 --- a/test/types/dispatcher.test-d.ts +++ b/test/types/dispatcher.test-d.ts @@ -1,3 +1,4 @@ +import { IncomingHttpHeaders } from 'http' import { Duplex, Readable, Writable } from 'stream' import { expectAssignable, expectType } from 'tsd' import { Dispatcher } from '../..' @@ -9,11 +10,18 @@ expectAssignable(new Dispatcher()) { const dispatcher = new Dispatcher() + const nodeCoreHeaders = { + authorization: undefined, + ['content-type']: 'application/json' + } satisfies IncomingHttpHeaders; + // dispatch expectAssignable(dispatcher.dispatch({ path: '', method: 'GET' }, {})) expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET' }, {})) + expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: { authorization: undefined } }, {})) expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: [] }, {})) expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: {} }, {})) + expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: nodeCoreHeaders }, {})) expectAssignable(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: null, reset: true }, {})) expectAssignable(dispatcher.dispatch({ origin: new URL('http://localhost'), path: '', method: 'GET' }, {})) diff --git a/test/types/header.test-d.ts b/test/types/header.test-d.ts new file mode 100644 index 00000000000..38ac9f6afdc --- /dev/null +++ b/test/types/header.test-d.ts @@ -0,0 +1,16 @@ +import { IncomingHttpHeaders as CoreIncomingHttpHeaders } from "http"; +import { expectAssignable, expectNotAssignable } from "tsd"; +import { IncomingHttpHeaders } from "../../types/header"; + +const headers = { + authorization: undefined, + ["content-type"]: "application/json", +} satisfies CoreIncomingHttpHeaders; + +expectAssignable(headers); + +// It is why we do not need to add ` | null` to `IncomingHttpHeaders`: +expectNotAssignable({ + authorization: null, + ["content-type"]: "application/json", +}); diff --git a/types/header.d.ts b/types/header.d.ts index 3fd9483e27b..bfdb3296d4d 100644 --- a/types/header.d.ts +++ b/types/header.d.ts @@ -1,4 +1,4 @@ /** * The header type declaration of `undici`. */ -export type IncomingHttpHeaders = Record; +export type IncomingHttpHeaders = Record;