A Headers class polyfill and transformation library.
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.
Install
npm install headers-polyfill copy
Polyfill
This package exports the Headers class that polyfills the native window.Headers implementation. This allows you to construct and manage headers using the same API in non-browser environments.
import { Headers } from 'headers-polyfill'
const headers = new Headers ({
Accept: '*/*' ,
'Content-Type' : 'application/json' ,
})
headers. get ( 'accept' ) // "*/*" copy
Methods
The Headers polyfill instance supports the same methods as the standard Headers instance:
As well as the iterator methods:
Returns an object consisting of the header name/value pairs but preserving raw header names.
const headers = new Headers ({
Accept: '*/*' ,
'Content-Type' : 'application/json' ,
})
headers. raw ()
// { "Accept": "*/*", "Content-Type": "application/json" } copy
headersToString: (h: Headers): string
import { headersToString } from 'headers-polyfill'
headersToString (
new Headers ({
connection: 'keep-alive' ,
'content-type' : [ 'text/plain' , 'image/png' ],
})
)
// connetion: keep-alive
// content-type: text/plain, image/png copy
headersToList: (h: Headers): Array<[string, string | string[]]>
import { headersToList } from 'headers-polyfill'
headersToList (
new Headers ({
connection: 'keep-alive' ,
'content-type' : [ 'text/plain' , 'image/png' ],
})
)
// [['connection', 'keep-alive'], ['content-type', ['text/plain', 'image/png']]] copy
headersToObject: (h: Headers): Record<string, string | string[]>
import { headersToObject } from 'headers-polyfill'
headersToObject (
new Headers ({
connection: 'keep-alive' ,
'content-type' : [ 'text/plain' , 'image/png' ],
})
)
// { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] } copy
stringToHeaders: (s: string): Headers
import { stringToHeaders } from 'headers-polyfill'
const stringToHeaders( `
connection: keep-alive
content-type: text/plain, image/png
` )
// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] } copy
listToHeaders: (l: Array<[string, string | string[]]>): Headers
import { listToHeaders } from 'headers-polyfill'
listToHeaders ([
[ 'connection' , 'keep-alive' ],
[ 'content-type' , [ 'text/plain' , 'image/png' ]],
])
// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] } copy
objectToHeaders: (o: Record<string, string | string[] | undefined>): Headers
import { objectToHeaders } from 'headers-polyfill'
objectToHeaders ({
connection: 'keep-alive' ,
'content-type' : [ 'text/plain' , 'image/png' ],
})
// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] } copy
Utilities
reduceHeadersObject: <R>(o: Record<string, string | string[]>, reducer: (acc: R, name: string, value: string | string[]) => R) => R
import { reduceHeadersObject } from 'headers-polyfill'
reduceHeadersObject <
HeadersObject >
({
Accept: '*/*' ,
'Content-Type' : [ 'application/json' , 'text/plain' ],
},
(headers, name, value) => {
headers[name. toLowerCase ()] = value
return headers
},
{})
// { 'accept': '*/*', 'content-type': ['application/json', 'text/plain'] } copy
appendHeader: (o: Record<string, string | string[]>, n: string, v: string | string[]): Record<string, string | string[]>
import { appendHeader } from 'headers-polyfill'
appendHeader (
{ 'content-type' : 'application/json' },
'content-type' ,
'text/plain'
)
// { 'content-type': ['application/json', 'text/plain']} copy
flattenHeadersList: (l: Array<[string, string | string[]]>): Array<string, string>
import { flattenHeadersList } from 'headers-polyfill'
flattenHeadersList ([[ 'content-type' , [ 'text/plain' , 'image/png' ]]])
// ['content-type', 'text/plain, image/png'] copy
flattenHeadersObject: (o: Record<string, string | string[]>): Record<string, string>
import { flattenHeadersObject } from 'headers-polyfill'
flattenHeadersObject ({
'content-type' : [ 'text/plain' , 'image/png' ],
})
// { 'content-type': 'text/plain, image/png' } copy