Skip to content

types(dispatcher): use OutgoingHttpHeaders for request headers#5067

Open
maruthang wants to merge 1 commit intonodejs:mainfrom
maruthang:types/issue-3840-dispatcher-headers
Open

types(dispatcher): use OutgoingHttpHeaders for request headers#5067
maruthang wants to merge 1 commit intonodejs:mainfrom
maruthang:types/issue-3840-dispatcher-headers

Conversation

@maruthang
Copy link
Copy Markdown
Contributor

This relates to...

Closes #3840

Rationale

DispatchOptions.headers (and ConnectOptions.headers, UpgradeOptions.headers, ProxyAgent.Options.headers, Socks5ProxyAgent.Options.headers) are used to set headers on outgoing requests, but the type union UndiciHeaders was built around IncomingHttpHeaders (server-side semantics). This prevented users from passing common outgoing values like { 'content-length': 42 } (numeric values), even though the runtime accepts them and coerces them to strings via template-literal interpolation in lib/core/request.js.

This change introduces an OutgoingHttpHeaders alias in types/header.d.ts (mirroring Node core's http.OutgoingHttpHeaders: Record<string, number | string | string[] | undefined>), and uses it to model UndiciHeaders and the headers option on the proxy agents. Response/incoming-side fields (ResponseData.headers, ConnectData.headers, etc.) continue to use IncomingHttpHeaders as before.

Because IncomingHttpHeaders is a subtype of the new OutgoingHttpHeaders, this is a non-breaking widening of the request-header types: any code that previously typechecked still typechecks.

Changes

Features

N/A

Bug Fixes

  • Add OutgoingHttpHeaders to types/header.d.ts and use it for outgoing-request headers options:
    • Dispatcher.UndiciHeaders (DispatchOptions, ConnectOptions, UpgradeOptions)
    • ProxyAgent.Options.headers
    • Socks5ProxyAgent.Options.headers
  • Update docs/docs/api/Dispatcher.md UndiciHeaders parameter docs to reflect OutgoingHttpHeaders semantics (matches the screenshot in Typing issue: headers in DispatchOptions should use OutgoingHttpHeaders #3840).
  • Add tsd regression tests in test/types/header.test-d.ts and test/types/dispatcher.test-d.ts covering numeric header values and Node core OutgoingHttpHeaders interop.

Breaking Changes and Deprecations

None. OutgoingHttpHeaders is a strict superset of IncomingHttpHeaders for the value union, so existing usages continue to typecheck.

Status

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typing issue: headers in DispatchOptions should use OutgoingHttpHeaders

1 participant