The `parse-prefer-header` library provides a lightweight utility for parsing the HTTP `Prefer` header, as defined in RFC7240. Currently at version 1.0.0, this package focuses on accurately transforming the header's comma-separated tokens and their optional parameters into a user-friendly JavaScript object. It handles various intricacies like token normalization (e.g., `respond-async` to `respondAsync`) and correctly interprets quoted values. Its core functionality is to map preference tokens to either `true` (for preferences without explicit values) or their parsed string value. Given its stable 1.0.0 version and specific parsing scope, its release cadence is effectively stable with no new major versions anticipated for such a focused utility. Key differentiators include its strict adherence to RFC7240 and its minimal API surface for a common, yet often complex, HTTP header.
npm install parse-prefer-headerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates parsing various HTTP Prefer header string formats, including single strings, arrays, values with parameters, and quoted values, and shows how the output is structured as a JavaScript object.
For CommonJS, use `const parsePreferHeader = require('parse-prefer-header');`. For ESM, ensure your build setup correctly handles CJS module interoperability, or consider a tool like Rollup/Webpack to bundle. In Node.js ESM, `import parsePreferHeader from 'parse-prefer-header';` generally works for CJS default exports.Always access preferences using their camelCase equivalent. For example, `parsed.respondAsync` instead of `parsed['respond-async']`. If a preference has parameters, the parameters themselves are treated as properties of the main preference object.
If your environment provides `Prefer` headers as an array (e.g., from `req.headers['prefer']` which often consolidates multiple identical headers into an array), pass the array directly. Otherwise, pass the single header string.
No direct fix, but users should be aware of the package's age. For critical applications, consider vendoring the code or evaluating if a more actively maintained HTTP header parsing utility could be adapted.
For CommonJS, use `const parsePreferHeader = require('parse-prefer-header');`. For ESM in Node.js, use `import parsePreferHeader from 'parse-prefer-header';` which leverages Node's CJS default export interop. Do not use named imports from this package.If in a Node.js ESM file, use `import parsePreferHeader from 'parse-prefer-header';`. If in a browser, ensure your bundler correctly handles CommonJS modules. If you need a direct CJS `require` in Node.js ESM, you can explicitly create a `require` function using `import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);`.Verify that `parsePreferHeader` is correctly imported and is indeed a function. Check the input string for valid RFC7240 syntax. Always check for the existence of properties on the returned object before accessing them, e.g., `if (preferences.respondAsync) { ... }`.No dependency data recorded yet.