|
1 | 1 | # headers
|
2 | 2 |
|
3 |
| -`headers` is a toolkit for working with HTTP headers in JavaScript. |
| 3 | +Tired of manually parsing and stringifying HTTP header values in JavaScript? `headers` supercharges the standard `Headers` interface, providing a robust toolkit for effortless and type-safe header manipulation. |
4 | 4 |
|
5 |
| -HTTP headers contain a wealth of information: |
| 5 | +HTTP headers are packed with critical information—from content negotiation and caching directives to authentication tokens and file metadata. While the native `Headers` API provides a basic string-based interface, it leaves the complexities of parsing specific header formats (like `Accept`, `Content-Type`, or `Set-Cookie`) entirely up to you. |
6 | 6 |
|
7 |
| -- Who is sending this request? |
8 |
| -- What's in the payload and how is it encoded? |
9 |
| -- What is the filename of this file upload? |
10 |
| -- and much more! |
| 7 | +`headers` solves this by offering: |
11 | 8 |
|
12 |
| -The [built-in JavaScript `Headers` interface](https://developer.mozilla.org/en-US/docs/Web/API/Headers) accepts and gives you strings for everything, which you're probably used to parsing and stringifying manually as needed. This library aims to give you a more fluent interface for all of this information. Similar to how the DOM gives you programmatic access to HTML documents, `headers` gives you access to HTTP headers. |
| 9 | +- **Type-Safe Accessors:** Interact with complex header values (e.g., media types, quality factors, cookie attributes) through strongly-typed properties and methods, eliminating guesswork and manual parsing. |
| 10 | +- **Automatic Parsing & Stringification:** The library intelligently handles the parsing of raw header strings into structured objects and stringifies your structured data back into spec-compliant header values. |
| 11 | +- **Fluent Interface:** Enjoy a more expressive and developer-friendly API for reading and writing header information. |
| 12 | +- **Drop-in Enhancement:** As a subclass of the standard `Headers` object, it can be used anywhere a `Headers` object is expected, providing progressive enhancement to your existing code. |
| 13 | +- **Individual Header Utilities:** For fine-grained control, use standalone utility classes for specific headers, perfect for scenarios outside of a full `Headers` object. |
| 14 | + |
| 15 | +Unlock a more powerful and elegant way to work with HTTP headers in your JavaScript and TypeScript projects! |
13 | 16 |
|
14 | 17 | ## Installation
|
15 | 18 |
|
@@ -46,6 +49,9 @@ headers.get('Accept'); // 'text/html,text/plain;q=0.9,text/*;q=0.8'
|
46 | 49 | // Accept-Encoding
|
47 | 50 | headers.acceptEncoding = 'gzip, deflate;q=0.8';
|
48 | 51 |
|
| 52 | +headers.acceptEncoding.encodings; // [ 'gzip', 'deflate' ] |
| 53 | +Object.fromEntries(headers.acceptEncoding.entries()); // { 'gzip': 1, 'deflate': 0.8 } |
| 54 | + |
49 | 55 | headers.acceptEncoding.accepts('gzip'); // true
|
50 | 56 | headers.acceptEncoding.accepts('br'); // false
|
51 | 57 |
|
@@ -206,7 +212,7 @@ let headers = new Headers({
|
206 | 212 | });
|
207 | 213 |
|
208 | 214 | console.log(`${headers}`);
|
209 |
| -// Content-Type: text/html |
| 215 | +// Content-Type: application/json |
210 | 216 | // Accept-Language: en-US,en;q=0.9
|
211 | 217 | ```
|
212 | 218 |
|
@@ -307,6 +313,23 @@ header.sMaxage; // 3600
|
307 | 313 |
|
308 | 314 | // Alternative init style
|
309 | 315 | let header = new CacheControl({ public: true, maxAge: 3600 });
|
| 316 | + |
| 317 | +// Full set of supported properties |
| 318 | +header.public; // true/false |
| 319 | +header.private; // true/false |
| 320 | +header.noCache; // true/false |
| 321 | +header.noStore; // true/false |
| 322 | +header.noTransform; // true/false |
| 323 | +header.mustRevalidate; // true/false |
| 324 | +header.proxyRevalidate; // true/false |
| 325 | +header.maxAge; // number |
| 326 | +header.sMaxage; // number |
| 327 | +header.minFresh; // number |
| 328 | +header.maxStale; // number |
| 329 | +header.onlyIfCached; // true/false |
| 330 | +header.immutable; // true/false |
| 331 | +header.staleWhileRevalidate; // number |
| 332 | +header.staleIfError; // number |
310 | 333 | ```
|
311 | 334 |
|
312 | 335 | ### Content-Disposition
|
|
0 commit comments