Utilities for working with a Headers
instance.
Motivation
Various request issuing libraries utilize a different format of headers. This library chooses the Headers
instance as the middle-ground between server and client, and provides functions to convert that instance to primitives and vice-versa.
Getting started
$ npm install headers-utils
headersToString(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
headersToList(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
headersToObject(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
const stringToHeaders(`
connection: keep-alive
content-type: text/plain, image/png
`)
listToHeaders([
['connection', 'keep-alive'],
['content-type', ['text/plain', 'image/png']],
])
objectToHeaders({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
Utilities
reduceHeadersObject <
HeadersObject >
({
Accept: '*/*',
'Content-Type': ['application/json', 'text/plain'],
},
(headers, name, value) => {
headers[name.toLowerCase()] = value
return headers
},
{})
appendHeader(
{ 'content-type': 'application/json' },
'content-type',
'text/plain'
)
flattenHeadersList([['content-type', ['text/plain', 'image/png']]])
flattenHeadersObject({
'content-type': ['text/plain', 'image/png'],
})