{"id":19450491,"url":"https://github.com/mswjs/headers-polyfill","last_synced_at":"2025-04-06T12:10:21.085Z","repository":{"id":42704511,"uuid":"262254362","full_name":"mswjs/headers-polyfill","owner":"mswjs","description":"A Fetch API \"Headers\" polyfill and transformation library.","archived":false,"fork":false,"pushed_at":"2024-08-31T19:39:45.000Z","size":501,"stargazers_count":33,"open_issues_count":3,"forks_count":11,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-30T08:08:34.670Z","etag":null,"topics":["fetch","header","headers","http","https","polyfill","request","xmlhttprequest"],"latest_commit_sha":null,"homepage":"https://npm.im/headers-polyfill","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mswjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-08T07:19:23.000Z","updated_at":"2025-03-18T17:31:02.000Z","dependencies_parsed_at":"2023-02-08T08:00:35.438Z","dependency_job_id":"a3580e59-a72d-41c4-952e-7bb415f8d5ba","html_url":"https://github.com/mswjs/headers-polyfill","commit_stats":{"total_commits":119,"total_committers":10,"mean_commits":11.9,"dds":"0.18487394957983194","last_synced_commit":"9b42765f87cc4619eba6fdc59c2bbd2d6a4fe5bc"},"previous_names":["mswjs/headers-utils"],"tags_count":43,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mswjs%2Fheaders-polyfill","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mswjs%2Fheaders-polyfill/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mswjs%2Fheaders-polyfill/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mswjs%2Fheaders-polyfill/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mswjs","download_url":"https://codeload.github.com/mswjs/headers-polyfill/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247478323,"owners_count":20945266,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fetch","header","headers","http","https","polyfill","request","xmlhttprequest"],"created_at":"2024-11-10T16:38:01.149Z","updated_at":"2025-04-06T12:10:21.058Z","avatar_url":"https://github.com/mswjs.png","language":"TypeScript","readme":"[![Published version](https://img.shields.io/npm/v/headers-polyfill.svg)](https://www.npmjs.com/package/headers-polyfill)\n\n# `headers-polyfill`\n\nA `Headers` class polyfill and transformation library.\n\n## Motivation\n\nVarious 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.\n\n## Install\n\n```bash\nnpm install headers-polyfill\n```\n\n## Polyfill\n\nThis 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.\n\n```js\nimport { Headers } from 'headers-polyfill'\n\nconst headers = new Headers({\n  Accept: '*/*',\n  'Content-Type': 'application/json',\n})\n\nheaders.get('accept') // \"*/*\"\n```\n\n## Methods\n\nThe `Headers` polyfill instance supports the same methods as the standard `Headers` instance:\n\n- [`.has()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/has)\n- [`.get()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/get)\n- [`.set()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/set)\n- [`.append()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/append)\n- [`.delete()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/delete)\n- [`.forEach()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/forEach)\n- [`.getSetCookie()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/getSetCookie)\n\nAs well as the iterator methods:\n\n- [`.keys()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/keys)\n- [`.values()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/values)\n- [`.entries()`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/entries)\n\n## Transformations\n\n### `getRawHeaders()`\n\nReturns an object consisting of the header name/value pairs but preserving raw header names.\n\n```js\nconst headers = new Headers({\n  Accept: '*/*',\n  'Content-Type': 'application/json',\n})\n\nheaders.raw()\n// { \"Accept\": \"*/*\", \"Content-Type\": \"application/json\" }\n```\n\n### Headers ⭢ N\n\n- `headersToString: (h: Headers): string`\n\n```js\nimport { headersToString } from 'headers-polyfill'\n\nheadersToString(\n  new Headers({\n    connection: 'keep-alive',\n    'content-type': ['text/plain', 'image/png'],\n  })\n)\n// connetion: keep-alive\n// content-type: text/plain, image/png\n```\n\n- `headersToList: (h: Headers): Array\u003c[string, string | string[]]\u003e`\n\n```js\nimport { headersToList } from 'headers-polyfill'\n\nheadersToList(\n  new Headers({\n    connection: 'keep-alive',\n    'content-type': ['text/plain', 'image/png'],\n  })\n)\n// [['connection', 'keep-alive'], ['content-type', ['text/plain', 'image/png']]]\n```\n\n- `headersToObject: (h: Headers): Record\u003cstring, string | string[]\u003e`\n\n```js\nimport { headersToObject } from 'headers-polyfill'\n\nheadersToObject(\n  new Headers({\n    connection: 'keep-alive',\n    'content-type': ['text/plain', 'image/png'],\n  })\n)\n// { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }\n```\n\n### N ⭢ Headers\n\n- `stringToHeaders: (s: string): Headers`\n\n```js\nimport { stringToHeaders } from 'headers-polyfill'\n\n\nconst stringToHeaders(`\nconnection: keep-alive\ncontent-type: text/plain, image/png\n`)\n// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }\n```\n\n- `listToHeaders: (l: Array\u003c[string, string | string[]]\u003e): Headers`\n\n```js\nimport { listToHeaders } from 'headers-polyfill'\n\nlistToHeaders([\n  ['connection', 'keep-alive'],\n  ['content-type', ['text/plain', 'image/png']],\n])\n// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }\n```\n\n- `objectToHeaders: (o: Record\u003cstring, string | string[] | undefined\u003e): Headers`\n\n```js\nimport { objectToHeaders } from 'headers-polyfill'\n\nobjectToHeaders({\n  connection: 'keep-alive',\n  'content-type': ['text/plain', 'image/png'],\n})\n// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }\n```\n\n---\n\n## Utilities\n\n- `reduceHeadersObject: \u003cR\u003e(o: Record\u003cstring, string | string[]\u003e, reducer: (acc: R, name: string, value: string | string[]) =\u003e R) =\u003e R`\n\n```js\nimport { reduceHeadersObject } from 'headers-polyfill'\n\nreduceHeadersObject \u003c\n  HeadersObject \u003e\n  ({\n    Accept: '*/*',\n    'Content-Type': ['application/json', 'text/plain'],\n  },\n  (headers, name, value) =\u003e {\n    headers[name.toLowerCase()] = value\n    return headers\n  },\n  {})\n// { 'accept': '*/*', 'content-type': ['application/json', 'text/plain'] }\n```\n\n- `appendHeader: (o: Record\u003cstring, string | string[]\u003e, n: string, v: string | string[]): Record\u003cstring, string | string[]\u003e`\n\n```js\nimport { appendHeader } from 'headers-polyfill'\n\nappendHeader(\n  { 'content-type': 'application/json' },\n  'content-type',\n  'text/plain'\n)\n// { 'content-type': ['application/json', 'text/plain']}\n```\n\n- `flattenHeadersList: (l: Array\u003c[string, string | string[]]\u003e): Array\u003cstring, string\u003e`\n\n```js\nimport { flattenHeadersList } from 'headers-polyfill'\n\nflattenHeadersList([['content-type', ['text/plain', 'image/png']]])\n// ['content-type', 'text/plain, image/png']\n```\n\n- `flattenHeadersObject: (o: Record\u003cstring, string | string[]\u003e): Record\u003cstring, string\u003e`\n\n```js\nimport { flattenHeadersObject } from 'headers-polyfill'\n\nflattenHeadersObject({\n  'content-type': ['text/plain', 'image/png'],\n})\n// { 'content-type': 'text/plain, image/png' }\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmswjs%2Fheaders-polyfill","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmswjs%2Fheaders-polyfill","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmswjs%2Fheaders-polyfill/lists"}