@reykjavik/webtools 0.1.27 → 0.1.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +24 -2
- package/esm/http.d.ts +11 -3
- package/esm/http.js +17 -11
- package/http.d.ts +11 -3
- package/http.js +19 -12
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,22 @@
|
|
|
4
4
|
|
|
5
5
|
- ... <!-- Add new lines here. -->
|
|
6
6
|
|
|
7
|
+
## 0.1.29
|
|
8
|
+
|
|
9
|
+
_2024-08-26_
|
|
10
|
+
|
|
11
|
+
- `@reykjavik/webtools/http`:
|
|
12
|
+
- feat: Add `cacheControlHeaders` helper that returns a `HeadersInit` object
|
|
13
|
+
- fix: `cacheControl` with `maxAge: 'unset'` didn't delete `X-Cache-Control`
|
|
14
|
+
in dev mode
|
|
15
|
+
|
|
16
|
+
## 0.1.28
|
|
17
|
+
|
|
18
|
+
_2024-08-26_
|
|
19
|
+
|
|
20
|
+
- `@reykjavik/webtools/http`:
|
|
21
|
+
- feat: `cacheControl` now also accepts `Map<string, string>` for headers
|
|
22
|
+
|
|
7
23
|
## 0.1.27
|
|
8
24
|
|
|
9
25
|
_2024-07-18_
|
package/README.md
CHANGED
|
@@ -20,6 +20,7 @@ bun add @reykjavik/webtools
|
|
|
20
20
|
- [HTTP Status Codes](#http-status-codes)
|
|
21
21
|
- [Types for HTTP Status code groups](#types-for-http-status-code-groups)
|
|
22
22
|
- [`cacheControl` helper](#cachecontrol-helper)
|
|
23
|
+
- [`cacheControlHeaders` helper](#cachecontrolheaders-helper)
|
|
23
24
|
- [Type `TTLConfig`](#type-ttlconfig)
|
|
24
25
|
- [`toSec` TTL helper](#tosec-ttl-helper)
|
|
25
26
|
- [`@reykjavik/webtools/async`](#reykjavikwebtoolsasync)
|
|
@@ -112,10 +113,10 @@ codes, are also available:
|
|
|
112
113
|
### `cacheControl` helper
|
|
113
114
|
|
|
114
115
|
**Syntax:**
|
|
115
|
-
`cacheConrol(response: ServerResponse | { res: ServerResponse }, ttlCfg: TTLConfig, eTag?: string|number): void`
|
|
116
|
+
`cacheConrol(response: ServerResponse | Response | Map<string, string> | { res: ServerResponse | Response }, ttlCfg: TTLConfig, eTag?: string|number): void`
|
|
116
117
|
|
|
117
118
|
Use this function to quickly set the `Cache-Control` header with a `max-age=`
|
|
118
|
-
on a HTTP response.
|
|
119
|
+
on a HTTP response (or a `Map` object representing response headers).
|
|
119
120
|
|
|
120
121
|
```js
|
|
121
122
|
import { cacheControl } from '@reykjavik/webtools/http';
|
|
@@ -131,6 +132,27 @@ The directives `private` and `immutable` are used by by default.
|
|
|
131
132
|
Use the optional `eTag` parameter if you intend to
|
|
132
133
|
[handle conditional requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Conditional_requests).
|
|
133
134
|
|
|
135
|
+
### `cacheControlHeaders` helper
|
|
136
|
+
|
|
137
|
+
**Syntax:**
|
|
138
|
+
`cacheControlHeaders(ttlCfg: TTLConfig, eTag?: string|number): Record<string, string>`
|
|
139
|
+
|
|
140
|
+
Similar to the [`cacheControl` helper](#cachecontrol-helper), but returns an
|
|
141
|
+
plain object with the headers for use in situations where `HeadersInit` object
|
|
142
|
+
are expected.
|
|
143
|
+
|
|
144
|
+
```js
|
|
145
|
+
import { cacheControlHeaders } from '@reykjavik/webtools/http';
|
|
146
|
+
|
|
147
|
+
const response = new Response('Hello, World!', {
|
|
148
|
+
headers: cacheControlHeaders('4h'),
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
|
|
154
|
+
```
|
|
155
|
+
|
|
134
156
|
#### Type `TTLConfig`
|
|
135
157
|
|
|
136
158
|
```js
|
package/esm/http.d.ts
CHANGED
|
@@ -168,15 +168,23 @@ type ServerResponseStub = Pick<ServerResponse, 'setHeader' | 'getHeader' | 'remo
|
|
|
168
168
|
headers?: Record<string, string | Array<string>>;
|
|
169
169
|
};
|
|
170
170
|
type ResponseStub = {
|
|
171
|
-
headers: Pick<Headers, 'set' | 'get' | 'delete'
|
|
171
|
+
headers: Pick<Headers, 'set' | 'get' | 'delete'>;
|
|
172
172
|
};
|
|
173
173
|
/**
|
|
174
174
|
* Use this function to quickly set the `Cache-Control` header with a `max-age=`
|
|
175
175
|
* on a HTTP response
|
|
176
176
|
*
|
|
177
|
-
* @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#
|
|
177
|
+
* @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#cachecontrol-helper
|
|
178
178
|
*/
|
|
179
|
-
export declare const cacheControl: (response: ServerResponseStub | ResponseStub | {
|
|
179
|
+
export declare const cacheControl: (response: ServerResponseStub | ResponseStub | Map<string, string> | {
|
|
180
180
|
res: ServerResponseStub | ResponseStub;
|
|
181
181
|
}, ttlCfg: TTLConfig, eTag?: string | number) => void;
|
|
182
|
+
/**
|
|
183
|
+
* Generates a Record with `Cache-Control` and `ETag` headers, for use in
|
|
184
|
+
* situations requiring a `HeadersInit` compatible object.
|
|
185
|
+
*
|
|
186
|
+
* Accepts the same arguments as `cacheControl()`.
|
|
187
|
+
* @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#cachecontrolheaders-helper
|
|
188
|
+
*/
|
|
189
|
+
export declare const cacheControlHeaders: (ttlCfg: TTLConfig, eTag?: string | number) => Record<string, string>;
|
|
182
190
|
export {};
|
package/esm/http.js
CHANGED
|
@@ -147,6 +147,9 @@ export const toSec = (ttl) => {
|
|
|
147
147
|
return Math.max(0, Math.round(ttl)) || 0;
|
|
148
148
|
};
|
|
149
149
|
const toRespnseStubHeaders = (response) => {
|
|
150
|
+
if (response instanceof Map) {
|
|
151
|
+
return response;
|
|
152
|
+
}
|
|
150
153
|
if ('headers' in response && !('setHeader' in response)) {
|
|
151
154
|
return response.headers;
|
|
152
155
|
}
|
|
@@ -159,15 +162,6 @@ const toRespnseStubHeaders = (response) => {
|
|
|
159
162
|
return val != null ? `${val}` : null;
|
|
160
163
|
},
|
|
161
164
|
set: (name, value) => response.setHeader(name, value),
|
|
162
|
-
append: (name, value) => {
|
|
163
|
-
const existing = response.getHeader(name);
|
|
164
|
-
if (existing) {
|
|
165
|
-
response.setHeader(name, `${existing}, ${value}`);
|
|
166
|
-
}
|
|
167
|
-
else {
|
|
168
|
-
response.setHeader(name, value);
|
|
169
|
-
}
|
|
170
|
-
},
|
|
171
165
|
delete: (name) => response.removeHeader(name),
|
|
172
166
|
};
|
|
173
167
|
};
|
|
@@ -194,7 +188,7 @@ const setCC = (response, cc) => {
|
|
|
194
188
|
* Use this function to quickly set the `Cache-Control` header with a `max-age=`
|
|
195
189
|
* on a HTTP response
|
|
196
190
|
*
|
|
197
|
-
* @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#
|
|
191
|
+
* @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#cachecontrol-helper
|
|
198
192
|
*/
|
|
199
193
|
// eslint-disable-next-line complexity
|
|
200
194
|
export const cacheControl = (response, ttlCfg, eTag) => {
|
|
@@ -215,7 +209,7 @@ export const cacheControl = (response, ttlCfg, eTag) => {
|
|
|
215
209
|
}
|
|
216
210
|
}
|
|
217
211
|
if (maxAge == null) {
|
|
218
|
-
|
|
212
|
+
setCC(response, undefined);
|
|
219
213
|
return;
|
|
220
214
|
}
|
|
221
215
|
maxAge = toSec(maxAge);
|
|
@@ -232,3 +226,15 @@ export const cacheControl = (response, ttlCfg, eTag) => {
|
|
|
232
226
|
setCC(response, `${scope}, max-age=${maxAge + sWR + sIE + stability}`);
|
|
233
227
|
eTag != null && toRespnseStubHeaders(response).set('ETag', String(eTag));
|
|
234
228
|
};
|
|
229
|
+
/**
|
|
230
|
+
* Generates a Record with `Cache-Control` and `ETag` headers, for use in
|
|
231
|
+
* situations requiring a `HeadersInit` compatible object.
|
|
232
|
+
*
|
|
233
|
+
* Accepts the same arguments as `cacheControl()`.
|
|
234
|
+
* @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#cachecontrolheaders-helper
|
|
235
|
+
*/
|
|
236
|
+
export const cacheControlHeaders = (ttlCfg, eTag) => {
|
|
237
|
+
const headers = new Map();
|
|
238
|
+
cacheControl(headers, ttlCfg, eTag);
|
|
239
|
+
return Object.fromEntries(headers);
|
|
240
|
+
};
|
package/http.d.ts
CHANGED
|
@@ -168,15 +168,23 @@ type ServerResponseStub = Pick<ServerResponse, 'setHeader' | 'getHeader' | 'remo
|
|
|
168
168
|
headers?: Record<string, string | Array<string>>;
|
|
169
169
|
};
|
|
170
170
|
type ResponseStub = {
|
|
171
|
-
headers: Pick<Headers, 'set' | 'get' | 'delete'
|
|
171
|
+
headers: Pick<Headers, 'set' | 'get' | 'delete'>;
|
|
172
172
|
};
|
|
173
173
|
/**
|
|
174
174
|
* Use this function to quickly set the `Cache-Control` header with a `max-age=`
|
|
175
175
|
* on a HTTP response
|
|
176
176
|
*
|
|
177
|
-
* @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#
|
|
177
|
+
* @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#cachecontrol-helper
|
|
178
178
|
*/
|
|
179
|
-
export declare const cacheControl: (response: ServerResponseStub | ResponseStub | {
|
|
179
|
+
export declare const cacheControl: (response: ServerResponseStub | ResponseStub | Map<string, string> | {
|
|
180
180
|
res: ServerResponseStub | ResponseStub;
|
|
181
181
|
}, ttlCfg: TTLConfig, eTag?: string | number) => void;
|
|
182
|
+
/**
|
|
183
|
+
* Generates a Record with `Cache-Control` and `ETag` headers, for use in
|
|
184
|
+
* situations requiring a `HeadersInit` compatible object.
|
|
185
|
+
*
|
|
186
|
+
* Accepts the same arguments as `cacheControl()`.
|
|
187
|
+
* @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#cachecontrolheaders-helper
|
|
188
|
+
*/
|
|
189
|
+
export declare const cacheControlHeaders: (ttlCfg: TTLConfig, eTag?: string | number) => Record<string, string>;
|
|
182
190
|
export {};
|
package/http.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HTTP_502_BadGateway = exports.HTTP_501_NotImplemented = exports.HTTP_500_InternalServerError = exports.HTTP_451_UnavailableForLegalReasons = exports.HTTP_431_RequestHeaderFieldsTooLarge = exports.HTTP_429_TooManyRequests = exports.HTTP_428_PreconditionRequired = exports.HTTP_426_UpgradeRequired = exports.HTTP_424_FailedDependency = exports.HTTP_423_Locked = exports.HTTP_422_UnprocessableContent = exports.HTTP_421_MisdirectedRequest = exports.HTTP_418_ImATeapot = exports.HTTP_417_ExpectationFailed = exports.HTTP_416_RangeNotSatisfiable = exports.HTTP_415_UnsupportedMediaType = exports.HTTP_414_URITooLong = exports.HTTP_413_PayloadTooLarge = exports.HTTP_412_PreconditionFailed = exports.HTTP_411_LengthRequired = exports.HTTP_410_Gone = exports.HTTP_409_Conflict = exports.HTTP_408_RequestTimeout = exports.HTTP_407_ProxyAuthenticationRequired = exports.HTTP_406_NotAcceptable = exports.HTTP_405_MethodNotAllowed = exports.HTTP_404_NotFound = exports.HTTP_403_Forbidden = exports.HTTP_401_Unauthorized = exports.HTTP_400_BadRequest = exports.HTTP_308_PermanentRedirect = exports.HTTP_307_TemporaryRedirect = exports.HTTP_304_NotModified = exports.HTTP_303_SeeOther = exports.HTTP_302_Found = exports.HTTP_301_MovedPermanently = exports.HTTP_226_IMUsed = exports.HTTP_208_AlreadyReported = exports.HTTP_207_MultiStatus = exports.HTTP_206_PartialContent = exports.HTTP_205_ResetContent = exports.HTTP_204_NoContent = exports.HTTP_203_NonAuthoritativeInformation = exports.HTTP_202_Accepted = exports.HTTP_201_Created = exports.HTTP_200_OK = exports.HTTP_103_EarlyHints = exports.HTTP_102_Processing = exports.HTTP_101_SwitchingProtocols = exports.HTTP_100_Continue = void 0;
|
|
4
|
-
exports.cacheControl = exports.toSec = exports.HTTP_511_NetworkAuthenticationRequired = exports.HTTP_510_NotExtended = exports.HTTP_508_LoopDetected = exports.HTTP_507_InsufficientStorage = exports.HTTP_506_VariantAlsoNegotiates = exports.HTTP_505_HTTPVersionNotSupported = exports.HTTP_504_GatewayTimeout = exports.HTTP_503_ServiceUnavailable = void 0;
|
|
4
|
+
exports.cacheControlHeaders = exports.cacheControl = exports.toSec = exports.HTTP_511_NetworkAuthenticationRequired = exports.HTTP_510_NotExtended = exports.HTTP_508_LoopDetected = exports.HTTP_507_InsufficientStorage = exports.HTTP_506_VariantAlsoNegotiates = exports.HTTP_505_HTTPVersionNotSupported = exports.HTTP_504_GatewayTimeout = exports.HTTP_503_ServiceUnavailable = void 0;
|
|
5
5
|
// INFORMATION
|
|
6
6
|
/** The client should continue the request or ignore the response if the request is already finished. */
|
|
7
7
|
exports.HTTP_100_Continue = 100;
|
|
@@ -152,6 +152,9 @@ const toSec = (ttl) => {
|
|
|
152
152
|
};
|
|
153
153
|
exports.toSec = toSec;
|
|
154
154
|
const toRespnseStubHeaders = (response) => {
|
|
155
|
+
if (response instanceof Map) {
|
|
156
|
+
return response;
|
|
157
|
+
}
|
|
155
158
|
if ('headers' in response && !('setHeader' in response)) {
|
|
156
159
|
return response.headers;
|
|
157
160
|
}
|
|
@@ -164,15 +167,6 @@ const toRespnseStubHeaders = (response) => {
|
|
|
164
167
|
return val != null ? `${val}` : null;
|
|
165
168
|
},
|
|
166
169
|
set: (name, value) => response.setHeader(name, value),
|
|
167
|
-
append: (name, value) => {
|
|
168
|
-
const existing = response.getHeader(name);
|
|
169
|
-
if (existing) {
|
|
170
|
-
response.setHeader(name, `${existing}, ${value}`);
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
response.setHeader(name, value);
|
|
174
|
-
}
|
|
175
|
-
},
|
|
176
170
|
delete: (name) => response.removeHeader(name),
|
|
177
171
|
};
|
|
178
172
|
};
|
|
@@ -199,7 +193,7 @@ const setCC = (response, cc) => {
|
|
|
199
193
|
* Use this function to quickly set the `Cache-Control` header with a `max-age=`
|
|
200
194
|
* on a HTTP response
|
|
201
195
|
*
|
|
202
|
-
* @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#
|
|
196
|
+
* @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#cachecontrol-helper
|
|
203
197
|
*/
|
|
204
198
|
// eslint-disable-next-line complexity
|
|
205
199
|
const cacheControl = (response, ttlCfg, eTag) => {
|
|
@@ -220,7 +214,7 @@ const cacheControl = (response, ttlCfg, eTag) => {
|
|
|
220
214
|
}
|
|
221
215
|
}
|
|
222
216
|
if (maxAge == null) {
|
|
223
|
-
|
|
217
|
+
setCC(response, undefined);
|
|
224
218
|
return;
|
|
225
219
|
}
|
|
226
220
|
maxAge = (0, exports.toSec)(maxAge);
|
|
@@ -238,3 +232,16 @@ const cacheControl = (response, ttlCfg, eTag) => {
|
|
|
238
232
|
eTag != null && toRespnseStubHeaders(response).set('ETag', String(eTag));
|
|
239
233
|
};
|
|
240
234
|
exports.cacheControl = cacheControl;
|
|
235
|
+
/**
|
|
236
|
+
* Generates a Record with `Cache-Control` and `ETag` headers, for use in
|
|
237
|
+
* situations requiring a `HeadersInit` compatible object.
|
|
238
|
+
*
|
|
239
|
+
* Accepts the same arguments as `cacheControl()`.
|
|
240
|
+
* @see https://github.com/reykjavikcity/webtools/blob/v0.1/README.md#cachecontrolheaders-helper
|
|
241
|
+
*/
|
|
242
|
+
const cacheControlHeaders = (ttlCfg, eTag) => {
|
|
243
|
+
const headers = new Map();
|
|
244
|
+
(0, exports.cacheControl)(headers, ttlCfg, eTag);
|
|
245
|
+
return Object.fromEntries(headers);
|
|
246
|
+
};
|
|
247
|
+
exports.cacheControlHeaders = cacheControlHeaders;
|
package/package.json
CHANGED