4.0.3 • Published 1 year ago

headers-polyfill v4.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Published version

headers-polyfill

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

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:

Transformations

getRawHeaders()

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" }

Headers ⭢ N

  • 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
  • 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']]]
  • 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'] }

N ⭢ Headers

  • 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'] }
  • 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'] }
  • 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'] }

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'] }
  • 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']}
  • 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']
  • 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' }
@infinitebrahmanuniverse/nolb-hea@enes-center-demo/api-client@enes-center-demo/react@enes-center-demo2/api-client@enes-center-demo2/react@everything-registry/sub-chunk-1832supergoodsupergood-interceptorsbare-server-edu-testbare-server-edu-test1bare-server-eduu@dragonnaked311/twitter-scraper-edge@apihero/interceptors-js@apachedubbo/dubbo-node@avivasyuta/request-interceptor@admin-pkg/vite-plugin-msw@bufbuild/connect-node@catdevnull/twitter-scraper@center-inc/api-client@center-inc/react@charlietango/msw-fastify-middleware7x-networks-bare-server-node95thinterceptors95thmsw@axiona/http-server@letticdo/bare-server-node-tomphttp@iodigital/vite-plugin-msw@mattcosta7/msw@nicksrandall/vite-plugin-msw@microwavedev/twitter-scraper@s-ui/test-contract@quilted/polyfills@ssuf1998/interceptorsmswmsw-forkedmsw-tobilenmustard-connect-nodemind-agent-twitter-clientpage-withrop-twitter-clientrocket-swap-jsbridgerocket-swap-swsdk.claroidself-twitter-scrapernft-reactgoat-xglobal_components_assignment@suinesia/suinesiaswap-sdk@the-convocation/twitter-scraper@tidytiny/twitter-scraperagent-twitter-client-pocagent-twitter-client-saiagent-twitter-client-taelin-forkagent-twitter-client-updatedagent-twitter-client-with-proxyagent-twitter-modagent-twitter-summaryagent-x-clientagent-twitter-clientagent-twitter-client-cf-workersagent-twitter-client-grokkyapache-dubbo-nodebare-server-edubare-server-educationconnect-miniprogramdarinv-agent-twitter-clienteducational-bareeducational-br-srenhanced-mswjs-interceptors
4.0.3

1 year ago

4.0.2

2 years ago

3.2.2

2 years ago

3.3.0

2 years ago

3.2.5

2 years ago

3.2.4

2 years ago

3.2.3

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.10

3 years ago

3.0.8

3 years ago

3.0.9

3 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago