Skip to content

Commit c2387e8

Browse files
pan93412SimenB
andauthored
fix: Allow “undefined“ as value in headers (#1929)
* fix: Allow “undefined“ as value in headers #1896 (comment) Signed-off-by: pan93412 <pan93412@gmail.com> * test: Add test for our IncomingHttpHeaders Co-Authored-By: Simen Bekkhus <sbekkhus91@gmail.com> Signed-off-by: pan93412 <pan93412@gmail.com> * chore: Upgrade TypeScript to 4.9.5 Signed-off-by: pan93412 <pan93412@gmail.com> --------- Signed-off-by: pan93412 <pan93412@gmail.com> Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
1 parent f73ec63 commit c2387e8

5 files changed

Lines changed: 31 additions & 7 deletions

File tree

docs/api/Dispatcher.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Returns: `void | Promise<ConnectData>` - Only returns a `Promise` if no `callbac
7474
#### Parameter: `ConnectData`
7575

7676
* **statusCode** `number`
77-
* **headers** `Record<string, string | string[]>`
77+
* **headers** `Record<string, string | string[] | undefined>`
7878
* **socket** `stream.Duplex`
7979
* **opaque** `unknown`
8080

@@ -383,7 +383,7 @@ Extends: [`RequestOptions`](#parameter-requestoptions)
383383
#### Parameter: PipelineHandlerData
384384

385385
* **statusCode** `number`
386-
* **headers** `Record<string, string | string[]>`
386+
* **headers** `Record<string, string | string[] | undefined>`
387387
* **opaque** `unknown`
388388
* **body** `stream.Readable`
389389
* **context** `object`
@@ -644,7 +644,7 @@ Returns: `void | Promise<StreamData>` - Only returns a `Promise` if no `callback
644644
#### Parameter: `StreamFactoryData`
645645

646646
* **statusCode** `number`
647-
* **headers** `Record<string, string | string[]>`
647+
* **headers** `Record<string, string | string[] | undefined>`
648648
* **opaque** `unknown`
649649
* **onInfo** `({statusCode: number, headers: Record<string, string | string[]>}) => void | null` (optional) - Default: `null` - Callback collecting all the info headers (HTTP 100-199) received.
650650

@@ -853,9 +853,9 @@ Emitted when dispatcher is no longer busy.
853853

854854
## Parameter: `UndiciHeaders`
855855

856-
* `Record<string, string | string[]> | string[] | null`
856+
* `Record<string, string | string[] | undefined> | string[] | null`
857857

858-
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<string, string | string[]>` (`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.
858+
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<string, string | string[] | undefined>` (`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.
859859

860860
Keys are lowercase and values are not modified.
861861

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"table": "^6.8.0",
9999
"tap": "^16.1.0",
100100
"tsd": "^0.25.0",
101-
"typescript": "^4.8.4",
101+
"typescript": "^4.9.5",
102102
"wait-on": "^6.0.0",
103103
"ws": "^8.11.0"
104104
},

test/types/dispatcher.test-d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IncomingHttpHeaders } from 'http'
12
import { Duplex, Readable, Writable } from 'stream'
23
import { expectAssignable, expectType } from 'tsd'
34
import { Dispatcher } from '../..'
@@ -9,11 +10,18 @@ expectAssignable<Dispatcher>(new Dispatcher())
910
{
1011
const dispatcher = new Dispatcher()
1112

13+
const nodeCoreHeaders = {
14+
authorization: undefined,
15+
['content-type']: 'application/json'
16+
} satisfies IncomingHttpHeaders;
17+
1218
// dispatch
1319
expectAssignable<boolean>(dispatcher.dispatch({ path: '', method: 'GET' }, {}))
1420
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET' }, {}))
21+
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: { authorization: undefined } }, {}))
1522
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: [] }, {}))
1623
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: {} }, {}))
24+
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: nodeCoreHeaders }, {}))
1725
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: null, reset: true }, {}))
1826
expectAssignable<boolean>(dispatcher.dispatch({ origin: new URL('http://localhost'), path: '', method: 'GET' }, {}))
1927

test/types/header.test-d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { IncomingHttpHeaders as CoreIncomingHttpHeaders } from "http";
2+
import { expectAssignable, expectNotAssignable } from "tsd";
3+
import { IncomingHttpHeaders } from "../../types/header";
4+
5+
const headers = {
6+
authorization: undefined,
7+
["content-type"]: "application/json",
8+
} satisfies CoreIncomingHttpHeaders;
9+
10+
expectAssignable<IncomingHttpHeaders>(headers);
11+
12+
// It is why we do not need to add ` | null` to `IncomingHttpHeaders`:
13+
expectNotAssignable<CoreIncomingHttpHeaders>({
14+
authorization: null,
15+
["content-type"]: "application/json",
16+
});

types/header.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/**
22
* The header type declaration of `undici`.
33
*/
4-
export type IncomingHttpHeaders = Record<string, string | string[]>;
4+
export type IncomingHttpHeaders = Record<string, string | string[] | undefined>;

0 commit comments

Comments
 (0)