Spaces:
Running
Running
File size: 5,580 Bytes
d4b85c0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
[](https://www.npmjs.com/package/headers-polyfill)
# `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`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) instance as the middle-ground between server and client, and provides functions to convert that instance to primitives and vice-versa.
## Install
```bash
npm install headers-polyfill
```
## Polyfill
This package exports the `Headers` class that polyfills the native [`window.Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) implementation. This allows you to construct and manage headers using the same API in non-browser environments.
```js
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:
- [`.has()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/has)
- [`.get()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/get)
- [`.set()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/set)
- [`.append()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/append)
- [`.delete()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/delete)
- [`.forEach()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/forEach)
- [`.getSetCookie()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/getSetCookie)
As well as the iterator methods:
- [`.keys()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/keys)
- [`.values()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/values)
- [`.entries()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/entries)
## Transformations
### `getRawHeaders()`
Returns an object consisting of the header name/value pairs but preserving raw header names.
```js
const headers = new Headers({
Accept: '*/*',
'Content-Type': 'application/json',
})
headers.raw()
// { "Accept": "*/*", "Content-Type": "application/json" }
```
### Headers ⭢ N
- `headersToString: (h: Headers): string`
```js
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[]]>`
```js
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[]>`
```js
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`
```js
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`
```js
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`
```js
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`
```js
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[]>`
```js
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>`
```js
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>`
```js
import { flattenHeadersObject } from 'headers-polyfill'
flattenHeadersObject({
'content-type': ['text/plain', 'image/png'],
})
// { 'content-type': 'text/plain, image/png' }
```
|