MCPcopy Create free account
hub / github.com/nock/nock / headersFieldNamesToLowerCase

Function headersFieldNamesToLowerCase

lib/common.js:118–141  ·  view source on GitHub ↗

* Return a new object with all field names of the headers lower-cased. * * Duplicates throw an error.

(headers, throwOnDuplicate)

Source from the content-addressed store, hash-verified

116 * Duplicates throw an error.
117 */
118function headersFieldNamesToLowerCase(headers, throwOnDuplicate) {
119 if (!isPlainObject(headers)) {
120 throw Error('Headers must be provided as an object')
121 }
122
123 const lowerCaseHeaders = {}
124 Object.entries(headers).forEach(([fieldName, fieldValue]) => {
125 const key = fieldName.toLowerCase()
126 if (lowerCaseHeaders[key] !== undefined) {
127 if (throwOnDuplicate) {
128 throw Error(
129 `Failed to convert header keys to lower case due to field name conflict: ${key}`,
130 )
131 } else {
132 debug(
133 `Duplicate header provided in request: ${key}. Only the last value can be matched.`,
134 )
135 }
136 }
137 lowerCaseHeaders[key] = fieldValue
138 })
139
140 return lowerCaseHeaders
141}
142
143const headersFieldsArrayToLowerCase = headers => [
144 ...new Set(headers.map(fieldName => fieldName.toLowerCase())),

Callers

nothing calls this directly

Calls 1

isPlainObjectFunction · 0.85

Tested by

no test coverage detected