Package Exports
- headers-utils
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (headers-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
headers-utils
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
Headers ⭢ N
headersToString: (h: Headers): string
headersToString(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
// connetion: keep-alive
// content-type: text/plain, image/png
headersToList: (h: Headers): Array<[string, string | string[]]>
headersToList(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
// [['connection', 'keep-alive'], ['content-type', ['text/plain', 'image/png']]]
headersToObject: (h: Headers): Record<string, string | string[]>
headersToObject(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
// { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
N ⭢ Headers
stringToHeaders: (s: string): Headers
const stringToHeaders(`
connection: keep-alive
content-type: text/plain, image/png
`)
// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
listToHeaders: (l: Array<[string, string | string[]]>): Headers
listToHeaders([
['connection', 'keep-alive'],
['content-type', ['text/plain', 'image/png']],
])
// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
objectToHeaders: (o: Record<string, string | string[]>): Headers
objectToHeaders({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
Utilities
flattenHeadersList: (l: Array<[string, string | string[]]>): Array<string, string>
flattenHeadersList([['content-type', ['text/plain', 'image/png']]])
// ['content-type', 'text/plain; image/png']
flattenHeadersObject: (o: Record<string, string | string[]>): Record<string, string>
flattenHeadersObject({
'content-type': ['text/plain', 'image/png'],
})
// { 'content-type': 'text/plain; image/png' }