fix(types): align Definition with runtime; add rawHeaders, drop headers#2955
Merged
Conversation
2 tasks
mikicho
approved these changes
Apr 20, 2026
|
🎉 This PR is included in version 14.0.13 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Closed
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Definition type changes: research and rationale
Fixes #2926
Problem
Definitiontypedheadersas optional and did not includerawHeaders.headersproperty (it is never set), and response headers live onrawHeaders.before(def)in nock back) had to cast or extend the type to usedef.rawHeaders.Research
Where definitions come from
lib/recorder.js→generateRequestAndResponseObject, called from the unifiedrecordResponse()used by both ClientRequest and fetch interceptors.headerson DefinitiongenerateRequestAndResponseObjectreturns an object withrawHeaders,response,scope, etc., and does not setheaders.lib/scope.js(e.g. in the code path that callsdefine()/loadDefs) only usesnockDef.rawHeaders; there is no reference tonockDef.headersanywhere in the codebase.headerson Definition was dead; the type suggested a property that does not exist at runtime.Shape of
rawHeadersrecordResponse()buildsresonce for both interceptors as:rawHeadersas a Record, not astring[].scope.jsdoesconst rawHeaders = nockDef.rawHeaders || []and passes it to.reply(status, response, rawHeaders). The interceptor’sreply()usescommon.headersInputToRawArray(rawHeaders), which accepts array, Map, or plain object. So at runtime, an array form would still work (e.g. legacy fixtures), but nock’s recorder never produces it; the type reflects what nock actually records.Multi-value headers (
string | string[])Object.fromEntries(response.headers.entries())collapses duplicate keys to the last value, so recording only ever producesRecord<string, string>.set-cookie) as arrays.reply()accepts this viaReplyHeaderValue = string | string[], andcommon.addHeaderLine()explicitly handles array values. So the consumed shape can beRecord<string, string | string[]>.Changes made
Removed
headersfromDefinitionSo the type matches reality: nock never sets or reads it; only
rawHeadersexists.Added
rawHeaderstoDefinitionTyped as
Record<string, string | string[]>:string[]for the top-level type), matching what the recorder produces and whatdefine()uses.string | string[]so multi-value headers (e.g. set-cookie) in fixtures or hand-written defs are correctly typed.JSDoc
Clarifies that response headers live on
rawHeaders, that nock never sets/readsheaderson definitions, and that multi-value headers may bestring[]when definitions are loaded or hand-written.Result
before((def: Definition) => { ... def.rawHeaders ... })and similar code type-check without casts or custom extensions.define()/loadDefs()accept.