Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow “undefined“ as value in headers #1929

Merged
merged 3 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/api/Dispatcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Returns: `void | Promise<ConnectData>` - Only returns a `Promise` if no `callbac
#### Parameter: `ConnectData`

* **statusCode** `number`
* **headers** `Record<string, string | string[]>`
* **headers** `Record<string, string | string[] | undefined>`
* **socket** `stream.Duplex`
* **opaque** `unknown`

Expand Down Expand Up @@ -383,7 +383,7 @@ Extends: [`RequestOptions`](#parameter-requestoptions)
#### Parameter: PipelineHandlerData

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

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

Expand Down Expand Up @@ -853,9 +853,9 @@ Emitted when dispatcher is no longer busy.

## Parameter: `UndiciHeaders`

* `Record<string, string | string[]> | string[] | null`
* `Record<string, string | string[] | undefined> | 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<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.
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.

Keys are lowercase and values are not modified.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
8 changes: 8 additions & 0 deletions test/types/dispatcher.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IncomingHttpHeaders } from 'http'
import { Duplex, Readable, Writable } from 'stream'
import { expectAssignable, expectType } from 'tsd'
import { Dispatcher } from '../..'
Expand All @@ -9,11 +10,18 @@ expectAssignable<Dispatcher>(new Dispatcher())
{
const dispatcher = new Dispatcher()

const nodeCoreHeaders = {
authorization: undefined,
['content-type']: 'application/json'
} satisfies IncomingHttpHeaders;

// dispatch
expectAssignable<boolean>(dispatcher.dispatch({ path: '', method: 'GET' }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET' }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: { authorization: undefined } }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: [] }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: {} }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: nodeCoreHeaders }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: '', path: '', method: 'GET', headers: null, reset: true }, {}))
expectAssignable<boolean>(dispatcher.dispatch({ origin: new URL('http://localhost'), path: '', method: 'GET' }, {}))

Expand Down
16 changes: 16 additions & 0 deletions test/types/header.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<IncomingHttpHeaders>(headers);

// It is why we do not need to add ` | null` to `IncomingHttpHeaders`:
expectNotAssignable<CoreIncomingHttpHeaders>({
authorization: null,
["content-type"]: "application/json",
});
2 changes: 1 addition & 1 deletion types/header.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* The header type declaration of `undici`.
*/
export type IncomingHttpHeaders = Record<string, string | string[]>;
export type IncomingHttpHeaders = Record<string, string | string[] | undefined>;