What is headers-polyfill?
The headers-polyfill npm package provides a way to manipulate HTTP headers, allowing you to create, read, and modify HTTP headers in a standardized manner. It is designed to mimic the Headers interface provided by the Fetch API, making it useful for environments where this API is not available or for projects that aim for consistency across different environments.
What are headers-polyfill's main functionalities?
Creating and appending headers
This feature allows you to create a new Headers object and append new headers to it. It's useful for setting up the headers required for an HTTP request.
{"const headers = new Headers();\nheaders.append('Content-Type', 'application/json');\nheaders.append('Authorization', 'Bearer your_token_here');"}
Reading header values
This feature enables you to read the value of a specific header by name. It's useful for inspecting headers received in an HTTP response.
{"const contentType = headers.get('Content-Type');"}
Checking for a header's existence
This feature allows you to check if a certain header exists within the Headers object. It's useful for conditional logic based on the presence of specific headers.
{"const hasContentType = headers.has('Content-Type');"}
Deleting a header
This feature enables you to remove a header from the Headers object. It's useful for modifying headers before sending an HTTP request or after processing an HTTP response.
{"headers.delete('Authorization');"}
Other packages similar to headers-polyfill
fetch-headers
Similar to headers-polyfill, fetch-headers provides an implementation of the Fetch API's Headers interface. It allows for manipulation of HTTP headers in environments where the Fetch API is not natively supported. The main difference lies in the specific implementation details and possibly the extent of compatibility with the Fetch API specification.
node-fetch
While primarily known for bringing window.fetch to Node.js, node-fetch also includes an implementation of the Headers interface. Compared to headers-polyfill, node-fetch offers a more comprehensive solution by also providing the ability to perform HTTP requests, making it a more feature-rich package for projects that require both fetching capabilities and headers manipulation.
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
yarn add headers-polyfill
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')
Methods
The Headers
polyfill instance supports the same methods as the standard Headers
instance:
As well as the iterator methods:
Custom methods
In addition, the polyfill instance has the following methods:
Returns the object of the normalized header name/value pairs.
const headers = new Headers({
Accept: '*/*',
'Content-Type': 'application/json',
})
headers.all()
Similar to the .all()
method, .raw()
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()
Transformations
headersToString: (h: Headers): string
import { headersToString } from 'headers-polyfill'
headersToString(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
headersToList: (h: Headers): Array<[string, string | string[]]>
import { headersToList } from 'headers-polyfill'
headersToList(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
headersToObject: (h: Headers): Record<string, string | string[]>
import { headersToObject } from 'headers-polyfill'
headersToObject(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
stringToHeaders: (s: string): Headers
import { stringToHeaders } from 'headers-polyfill'
const stringToHeaders(`
connection: keep-alive
content-type: text/plain, image/png
`)
listToHeaders: (l: Array<[string, string | string[]]>): Headers
import { listToHeaders } from 'headers-polyfill'
listToHeaders([
['connection', 'keep-alive'],
['content-type', ['text/plain', 'image/png']],
])
objectToHeaders: (o: Record<string, string | string[] | undefined>): Headers
import { objectToHeaders } from 'headers-polyfill'
objectToHeaders({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
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
},
{})
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'
)
flattenHeadersList: (l: Array<[string, string | string[]]>): Array<string, string>
import { flattenHeadersList } from 'headers-polyfill'
flattenHeadersList([['content-type', ['text/plain', 'image/png']]])
flattenHeadersObject: (o: Record<string, string | string[]>): Record<string, string>
import { flattenHeadersObject } from 'headers-polyfill'
flattenHeadersObject({
'content-type': ['text/plain', 'image/png'],
})