@reykjavik/webtools 0.1.28 → 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 +9 -0
- package/README.md +22 -0
- package/esm/http.d.ts +9 -1
- package/esm/http.js +14 -2
- package/http.d.ts +9 -1
- package/http.js +16 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,15 @@
|
|
|
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
|
+
|
|
7
16
|
## 0.1.28
|
|
8
17
|
|
|
9
18
|
_2024-08-26_
|
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)
|
|
@@ -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
|
@@ -174,9 +174,17 @@ type ResponseStub = {
|
|
|
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
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
|
@@ -188,7 +188,7 @@ const setCC = (response, cc) => {
|
|
|
188
188
|
* Use this function to quickly set the `Cache-Control` header with a `max-age=`
|
|
189
189
|
* on a HTTP response
|
|
190
190
|
*
|
|
191
|
-
* @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
|
|
192
192
|
*/
|
|
193
193
|
// eslint-disable-next-line complexity
|
|
194
194
|
export const cacheControl = (response, ttlCfg, eTag) => {
|
|
@@ -209,7 +209,7 @@ export const cacheControl = (response, ttlCfg, eTag) => {
|
|
|
209
209
|
}
|
|
210
210
|
}
|
|
211
211
|
if (maxAge == null) {
|
|
212
|
-
|
|
212
|
+
setCC(response, undefined);
|
|
213
213
|
return;
|
|
214
214
|
}
|
|
215
215
|
maxAge = toSec(maxAge);
|
|
@@ -226,3 +226,15 @@ export const cacheControl = (response, ttlCfg, eTag) => {
|
|
|
226
226
|
setCC(response, `${scope}, max-age=${maxAge + sWR + sIE + stability}`);
|
|
227
227
|
eTag != null && toRespnseStubHeaders(response).set('ETag', String(eTag));
|
|
228
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
|
@@ -174,9 +174,17 @@ type ResponseStub = {
|
|
|
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
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;
|
|
@@ -193,7 +193,7 @@ const setCC = (response, cc) => {
|
|
|
193
193
|
* Use this function to quickly set the `Cache-Control` header with a `max-age=`
|
|
194
194
|
* on a HTTP response
|
|
195
195
|
*
|
|
196
|
-
* @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
|
|
197
197
|
*/
|
|
198
198
|
// eslint-disable-next-line complexity
|
|
199
199
|
const cacheControl = (response, ttlCfg, eTag) => {
|
|
@@ -214,7 +214,7 @@ const cacheControl = (response, ttlCfg, eTag) => {
|
|
|
214
214
|
}
|
|
215
215
|
}
|
|
216
216
|
if (maxAge == null) {
|
|
217
|
-
|
|
217
|
+
setCC(response, undefined);
|
|
218
218
|
return;
|
|
219
219
|
}
|
|
220
220
|
maxAge = (0, exports.toSec)(maxAge);
|
|
@@ -232,3 +232,16 @@ const cacheControl = (response, ttlCfg, eTag) => {
|
|
|
232
232
|
eTag != null && toRespnseStubHeaders(response).set('ETag', String(eTag));
|
|
233
233
|
};
|
|
234
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