Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
headers-utils
Advanced tools
Readme
headers-utils
A Headers
class polyfill and transformation library.
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.
$ npm install headers-utils
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-utils'
const headers = new Headers({
Accept: '*/*',
'Content-Type': 'application/json',
})
headers.get('accept') // "*/*"
The Headers
polyfill instance supports the same methods as the standard Headers
instance:
As well as the iterator methods:
In addition, the polyfill instance has the following methods:
.all()
Returns the object of the normalized header name/value pairs.
const headers = new Headers({
Accept: '*/*',
'Content-Type': 'application/json',
})
headers.all()
// { "accept": "*/*", "content-type": "application/json" }
.raw()
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()
// { "Accept": "*/*", "Content-Type": "application/json" }
headersToString: (h: Headers): string
import { headersToString } from 'headers-utils'
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[]]>
import { headersToList } from 'headers-utils'
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[]>
import { headersToObject } from 'headers-utils'
headersToObject(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
// { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
stringToHeaders: (s: string): Headers
import { stringToHeaders } from 'headers-utils'
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
import { listToHeaders } from 'headers-utils'
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[] | undefined>): Headers
import { objectToHeaders } from 'headers-utils'
objectToHeaders({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
reduceHeadersObject: <R>(o: Record<string, string | string[]>, reducer: (acc: R, name: string, value: string | string[]) => R) => R
import { reduceHeadersObject } from 'headers-utils'
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'] }
appendHeader: (o: Record<string, string | string[]>, n: string, v: string | string[]): Record<string, string | string[]>
import { appendHeader } from 'headers-utils'
appendHeader(
{ 'content-type': 'application/json' },
'content-type',
'text/plain'
)
// { 'content-type': ['application/json', 'text/plain']}
flattenHeadersList: (l: Array<[string, string | string[]]>): Array<string, string>
import { flattenHeadersList } from 'headers-utils'
flattenHeadersList([['content-type', ['text/plain', 'image/png']]])
// ['content-type', 'text/plain; image/png']
flattenHeadersObject: (o: Record<string, string | string[]>): Record<string, string>
import { flattenHeadersObject } from 'headers-utils'
flattenHeadersObject({
'content-type': ['text/plain', 'image/png'],
})
// { 'content-type': 'text/plain; image/png' }
FAQs
A Headers class polyfill and transformation library.
The npm package headers-utils receives a total of 314,423 weekly downloads. As such, headers-utils popularity was classified as popular.
We found that headers-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.