Skip to content

Commit fd57ac1

Browse files
committed
Build headers with esbuild
1 parent 1cc0f5f commit fd57ac1

File tree

6 files changed

+316
-41
lines changed

6 files changed

+316
-41
lines changed

packages/headers/.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore test files within the src directory
2+
src/**/*.test.ts

packages/headers/README.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# headers
22

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.
44

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.
66

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:
118

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!
1316

1417
## Installation
1518

@@ -46,6 +49,9 @@ headers.get('Accept'); // 'text/html,text/plain;q=0.9,text/*;q=0.8'
4649
// Accept-Encoding
4750
headers.acceptEncoding = 'gzip, deflate;q=0.8';
4851

52+
headers.acceptEncoding.encodings; // [ 'gzip', 'deflate' ]
53+
Object.fromEntries(headers.acceptEncoding.entries()); // { 'gzip': 1, 'deflate': 0.8 }
54+
4955
headers.acceptEncoding.accepts('gzip'); // true
5056
headers.acceptEncoding.accepts('br'); // false
5157

@@ -206,7 +212,7 @@ let headers = new Headers({
206212
});
207213

208214
console.log(`${headers}`);
209-
// Content-Type: text/html
215+
// Content-Type: application/json
210216
// Accept-Language: en-US,en;q=0.9
211217
```
212218

@@ -307,6 +313,23 @@ header.sMaxage; // 3600
307313

308314
// Alternative init style
309315
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
310333
```
311334

312335
### Content-Disposition

packages/headers/package.json

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,57 @@
1212
"homepage": "https://github.com/mjackson/remix-the-web/tree/main/packages/headers#readme",
1313
"files": [
1414
"dist",
15+
"src",
1516
"LICENSE",
1617
"README.md"
1718
],
1819
"type": "module",
20+
"main": "./dist/headers.cjs",
21+
"module": "./dist/headers.js",
1922
"types": "./dist/headers.d.ts",
20-
"main": "./dist/headers.js",
2123
"exports": {
2224
".": {
23-
"module-sync": {
24-
"types": "./dist/headers.d.ts",
25-
"default": "./dist/headers.js"
26-
},
27-
"import": {
28-
"types": "./dist/headers.d.ts",
29-
"default": "./dist/headers.js"
30-
},
31-
"require": {
32-
"types": "./dist/headers.d.cts",
33-
"default": "./dist/headers.cjs"
34-
},
35-
"default": {
36-
"types": "./dist/headers.d.ts",
37-
"default": "./dist/headers.js"
38-
}
25+
"types": "./dist/headers.d.ts",
26+
"import": "./dist/headers.js",
27+
"require": "./dist/headers.cjs",
28+
"default": "./dist/headers.js"
3929
},
4030
"./package.json": "./package.json"
4131
},
4232
"devDependencies": {
4333
"@types/node": "^20.14.10",
44-
"tsup": "^8.3.5"
34+
"esbuild": "^0.20.0"
4535
},
4636
"scripts": {
47-
"build": "tsup",
37+
"clean": "rm -rf dist",
38+
"build:types": "tsc --project tsconfig.build.json",
39+
"build:esm": "esbuild src/headers.ts --bundle --outfile=dist/headers.js --format=esm --platform=neutral --minify --sourcemap",
40+
"build:cjs": "esbuild src/headers.ts --bundle --outfile=dist/headers.cjs --format=cjs --platform=node --minify --sourcemap",
41+
"build": "pnpm run clean && pnpm run build:types && pnpm run build:esm && pnpm run build:cjs",
4842
"test": "node --experimental-strip-types --disable-warning=ExperimentalWarning --test ./src/**/*.test.ts",
49-
"prepare": "pnpm run build"
43+
"prepublishOnly": "pnpm run build"
5044
},
5145
"keywords": [
5246
"fetch",
5347
"http",
5448
"header",
55-
"headers"
49+
"headers",
50+
"http-headers",
51+
"request-headers",
52+
"response-headers",
53+
"content-negotiation",
54+
"cookies",
55+
"set-cookie",
56+
"cache-control",
57+
"content-type",
58+
"accept",
59+
"accept-encoding",
60+
"accept-language",
61+
"content-disposition",
62+
"if-none-match",
63+
"etag",
64+
"user-agent",
65+
"host",
66+
"last-modified"
5667
]
5768
}

packages/headers/tsconfig.build.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"declaration": true,
5+
"emitDeclarationOnly": true,
6+
"declarationMap": true,
7+
"outDir": "./dist"
8+
},
9+
"include": ["src"]
10+
}

packages/headers/tsup.config.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)