@serwist/cacheable-response 9.0.0-preview.2 → 9.0.0-preview.20
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/dist/index.d.ts +2 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -92
- package/package.json +9 -10
- package/src/index.ts +2 -15
- package/dist/CacheableResponse.d.ts +0 -44
- package/dist/CacheableResponse.d.ts.map +0 -1
- package/dist/CacheableResponsePlugin.d.ts +0 -27
- package/dist/CacheableResponsePlugin.d.ts.map +0 -1
- package/src/CacheableResponse.ts +0 -138
- package/src/CacheableResponsePlugin.ts +0 -46
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { CacheableResponsePlugin } from "./CacheableResponsePlugin.js";
|
|
4
|
-
export { CacheableResponse, CacheableResponsePlugin };
|
|
5
|
-
export type { CacheableResponseOptions };
|
|
1
|
+
export { CacheableResponse, CacheableResponsePlugin } from "@serwist/sw/plugins";
|
|
2
|
+
export type { CacheableResponseOptions } from "@serwist/sw/plugins";
|
|
6
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AACjF,YAAY,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,92 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class CacheableResponse {
|
|
4
|
-
_statuses;
|
|
5
|
-
_headers;
|
|
6
|
-
constructor(config = {}){
|
|
7
|
-
if (process.env.NODE_ENV !== "production") {
|
|
8
|
-
if (!(config.statuses || config.headers)) {
|
|
9
|
-
throw new SerwistError("statuses-or-headers-required", {
|
|
10
|
-
moduleName: "@serwist/cacheable-response",
|
|
11
|
-
className: "CacheableResponse",
|
|
12
|
-
funcName: "constructor"
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
if (config.statuses) {
|
|
16
|
-
assert.isArray(config.statuses, {
|
|
17
|
-
moduleName: "@serwist/cacheable-response",
|
|
18
|
-
className: "CacheableResponse",
|
|
19
|
-
funcName: "constructor",
|
|
20
|
-
paramName: "config.statuses"
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
if (config.headers) {
|
|
24
|
-
assert.isType(config.headers, "object", {
|
|
25
|
-
moduleName: "@serwist/cacheable-response",
|
|
26
|
-
className: "CacheableResponse",
|
|
27
|
-
funcName: "constructor",
|
|
28
|
-
paramName: "config.headers"
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
this._statuses = config.statuses;
|
|
33
|
-
this._headers = config.headers;
|
|
34
|
-
}
|
|
35
|
-
isResponseCacheable(response) {
|
|
36
|
-
if (process.env.NODE_ENV !== "production") {
|
|
37
|
-
assert.isInstance(response, Response, {
|
|
38
|
-
moduleName: "@serwist/cacheable-response",
|
|
39
|
-
className: "CacheableResponse",
|
|
40
|
-
funcName: "isResponseCacheable",
|
|
41
|
-
paramName: "response"
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
let cacheable = true;
|
|
45
|
-
if (this._statuses) {
|
|
46
|
-
cacheable = this._statuses.includes(response.status);
|
|
47
|
-
}
|
|
48
|
-
if (this._headers && cacheable) {
|
|
49
|
-
cacheable = Object.keys(this._headers).some((headerName)=>{
|
|
50
|
-
return response.headers.get(headerName) === this._headers[headerName];
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
if (process.env.NODE_ENV !== "production") {
|
|
54
|
-
if (!cacheable) {
|
|
55
|
-
logger.groupCollapsed(`The request for '${getFriendlyURL(response.url)}' returned a response that does not meet the criteria for being cached.`);
|
|
56
|
-
logger.groupCollapsed("View cacheability criteria here.");
|
|
57
|
-
logger.log(`Cacheable statuses: ${JSON.stringify(this._statuses)}`);
|
|
58
|
-
logger.log(`Cacheable headers: ${JSON.stringify(this._headers, null, 2)}`);
|
|
59
|
-
logger.groupEnd();
|
|
60
|
-
const logFriendlyHeaders = {};
|
|
61
|
-
response.headers.forEach((value, key)=>{
|
|
62
|
-
logFriendlyHeaders[key] = value;
|
|
63
|
-
});
|
|
64
|
-
logger.groupCollapsed("View response status and headers here.");
|
|
65
|
-
logger.log(`Response status: ${response.status}`);
|
|
66
|
-
logger.log(`Response headers: ${JSON.stringify(logFriendlyHeaders, null, 2)}`);
|
|
67
|
-
logger.groupEnd();
|
|
68
|
-
logger.groupCollapsed("View full response details here.");
|
|
69
|
-
logger.log(response.headers);
|
|
70
|
-
logger.log(response);
|
|
71
|
-
logger.groupEnd();
|
|
72
|
-
logger.groupEnd();
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return cacheable;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
class CacheableResponsePlugin {
|
|
80
|
-
_cacheableResponse;
|
|
81
|
-
constructor(config){
|
|
82
|
-
this._cacheableResponse = new CacheableResponse(config);
|
|
83
|
-
}
|
|
84
|
-
cacheWillUpdate = async ({ response })=>{
|
|
85
|
-
if (this._cacheableResponse.isResponseCacheable(response)) {
|
|
86
|
-
return response;
|
|
87
|
-
}
|
|
88
|
-
return null;
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export { CacheableResponse, CacheableResponsePlugin };
|
|
1
|
+
export { CacheableResponse, CacheableResponsePlugin } from '@serwist/sw/plugins';
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/cacheable-response",
|
|
3
|
-
"version": "9.0.0-preview.
|
|
3
|
+
"version": "9.0.0-preview.20",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "A module that takes a Response object and determines whether it's cacheable based on a specific configuration.",
|
|
6
6
|
"files": [
|
|
7
7
|
"src",
|
|
8
8
|
"dist"
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
],
|
|
17
17
|
"author": "Google's Web DevRel Team, Serwist's Team",
|
|
18
18
|
"license": "MIT",
|
|
19
|
-
"repository": "serwist/serwist",
|
|
20
|
-
"bugs": "https://
|
|
19
|
+
"repository": "https://gitlab.com/serwist/serwist",
|
|
20
|
+
"bugs": "https://gitlab.com/serwist/serwist/issues",
|
|
21
21
|
"homepage": "https://serwist.pages.dev",
|
|
22
22
|
"main": "./dist/index.js",
|
|
23
23
|
"types": "./dist/index.d.ts",
|
|
@@ -29,12 +29,12 @@
|
|
|
29
29
|
"./package.json": "./package.json"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@serwist/
|
|
32
|
+
"@serwist/sw": "9.0.0-preview.20"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"rollup": "4.
|
|
36
|
-
"typescript": "5.
|
|
37
|
-
"@serwist/constants": "9.0.0-preview.
|
|
35
|
+
"rollup": "4.13.0",
|
|
36
|
+
"typescript": "5.5.0-dev.20240323",
|
|
37
|
+
"@serwist/constants": "9.0.0-preview.20"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"typescript": ">=5.0.0"
|
|
@@ -45,8 +45,7 @@
|
|
|
45
45
|
}
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
|
-
"build": "cross-env NODE_ENV=production rollup --config rollup.config.js",
|
|
49
|
-
"dev": "rollup --config rollup.config.js --watch",
|
|
48
|
+
"build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
|
|
50
49
|
"lint": "biome lint ./src",
|
|
51
50
|
"typecheck": "tsc"
|
|
52
51
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,15 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { CacheableResponseOptions } from "./CacheableResponse.js";
|
|
10
|
-
import { CacheableResponse } from "./CacheableResponse.js";
|
|
11
|
-
import { CacheableResponsePlugin } from "./CacheableResponsePlugin.js";
|
|
12
|
-
|
|
13
|
-
export { CacheableResponse, CacheableResponsePlugin };
|
|
14
|
-
|
|
15
|
-
export type { CacheableResponseOptions };
|
|
1
|
+
export { CacheableResponse, CacheableResponsePlugin } from "@serwist/sw/plugins";
|
|
2
|
+
export type { CacheableResponseOptions } from "@serwist/sw/plugins";
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
export interface CacheableResponseOptions {
|
|
2
|
-
/**
|
|
3
|
-
* One or more status codes that a `Response` can have to be considered cacheable.
|
|
4
|
-
*/
|
|
5
|
-
statuses?: number[];
|
|
6
|
-
/**
|
|
7
|
-
* A mapping of header names and expected values that a `Response` can have and be
|
|
8
|
-
* considered cacheable. If multiple headers are provided, only one needs to be present.
|
|
9
|
-
*/
|
|
10
|
-
headers?: {
|
|
11
|
-
[headerName: string]: string;
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* This class allows you to set up rules determining what
|
|
16
|
-
* status codes and/or headers need to be present in order for a
|
|
17
|
-
* [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
18
|
-
* to be considered cacheable.
|
|
19
|
-
*/
|
|
20
|
-
export declare class CacheableResponse {
|
|
21
|
-
private readonly _statuses?;
|
|
22
|
-
private readonly _headers?;
|
|
23
|
-
/**
|
|
24
|
-
* To construct a new CacheableResponse instance you must provide at least
|
|
25
|
-
* one of the `config` properties.
|
|
26
|
-
*
|
|
27
|
-
* If both `statuses` and `headers` are specified, then both conditions must
|
|
28
|
-
* be met for the `Response` to be considered cacheable.
|
|
29
|
-
*
|
|
30
|
-
* @param config
|
|
31
|
-
*/
|
|
32
|
-
constructor(config?: CacheableResponseOptions);
|
|
33
|
-
/**
|
|
34
|
-
* Checks a response to see whether it's cacheable or not, based on this
|
|
35
|
-
* object's configuration.
|
|
36
|
-
*
|
|
37
|
-
* @param response The response whose cacheability is being
|
|
38
|
-
* checked.
|
|
39
|
-
* @returns `true` if the `Response` is cacheable, and `false`
|
|
40
|
-
* otherwise.
|
|
41
|
-
*/
|
|
42
|
-
isResponseCacheable(response: Response): boolean;
|
|
43
|
-
}
|
|
44
|
-
//# sourceMappingURL=CacheableResponse.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CacheableResponse.d.ts","sourceRoot":"","sources":["../src/CacheableResponse.ts"],"names":[],"mappings":"AAUA,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB;;;OAGG;IACH,OAAO,CAAC,EAAE;QAAE,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;CAC5C;AAED;;;;;GAKG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAuC;IAClE,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAsC;IAEhE;;;;;;;;OAQG;gBACS,MAAM,GAAE,wBAA6B;IAiCjD;;;;;;;;OAQG;IACH,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO;CAsDjD"}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { SerwistPlugin } from "@serwist/core";
|
|
2
|
-
import type { CacheableResponseOptions } from "./CacheableResponse.js";
|
|
3
|
-
/**
|
|
4
|
-
* A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
|
|
5
|
-
* easier to add in cacheability checks to requests made via Serwist's built-in
|
|
6
|
-
* strategies.
|
|
7
|
-
*/
|
|
8
|
-
export declare class CacheableResponsePlugin implements SerwistPlugin {
|
|
9
|
-
private readonly _cacheableResponse;
|
|
10
|
-
/**
|
|
11
|
-
* To construct a new CacheableResponsePlugin instance you must provide at
|
|
12
|
-
* least one of the `config` properties.
|
|
13
|
-
*
|
|
14
|
-
* If both `statuses` and `headers` are specified, then both conditions must
|
|
15
|
-
* be met for the `Response` to be considered cacheable.
|
|
16
|
-
*
|
|
17
|
-
* @param config
|
|
18
|
-
*/
|
|
19
|
-
constructor(config: CacheableResponseOptions);
|
|
20
|
-
/**
|
|
21
|
-
* @param options
|
|
22
|
-
* @returns
|
|
23
|
-
* @private
|
|
24
|
-
*/
|
|
25
|
-
cacheWillUpdate: SerwistPlugin["cacheWillUpdate"];
|
|
26
|
-
}
|
|
27
|
-
//# sourceMappingURL=CacheableResponsePlugin.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CacheableResponsePlugin.d.ts","sourceRoot":"","sources":["../src/CacheableResponsePlugin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAGvE;;;;GAIG;AACH,qBAAa,uBAAwB,YAAW,aAAa;IAC3D,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAoB;IAEvD;;;;;;;;OAQG;gBACS,MAAM,EAAE,wBAAwB;IAI5C;;;;OAIG;IACH,eAAe,EAAE,aAAa,CAAC,iBAAiB,CAAC,CAK/C;CACH"}
|
package/src/CacheableResponse.ts
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2018 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { assert, SerwistError, getFriendlyURL, logger } from "@serwist/core/internal";
|
|
10
|
-
|
|
11
|
-
export interface CacheableResponseOptions {
|
|
12
|
-
/**
|
|
13
|
-
* One or more status codes that a `Response` can have to be considered cacheable.
|
|
14
|
-
*/
|
|
15
|
-
statuses?: number[];
|
|
16
|
-
/**
|
|
17
|
-
* A mapping of header names and expected values that a `Response` can have and be
|
|
18
|
-
* considered cacheable. If multiple headers are provided, only one needs to be present.
|
|
19
|
-
*/
|
|
20
|
-
headers?: { [headerName: string]: string };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* This class allows you to set up rules determining what
|
|
25
|
-
* status codes and/or headers need to be present in order for a
|
|
26
|
-
* [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
27
|
-
* to be considered cacheable.
|
|
28
|
-
*/
|
|
29
|
-
export class CacheableResponse {
|
|
30
|
-
private readonly _statuses?: CacheableResponseOptions["statuses"];
|
|
31
|
-
private readonly _headers?: CacheableResponseOptions["headers"];
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* To construct a new CacheableResponse instance you must provide at least
|
|
35
|
-
* one of the `config` properties.
|
|
36
|
-
*
|
|
37
|
-
* If both `statuses` and `headers` are specified, then both conditions must
|
|
38
|
-
* be met for the `Response` to be considered cacheable.
|
|
39
|
-
*
|
|
40
|
-
* @param config
|
|
41
|
-
*/
|
|
42
|
-
constructor(config: CacheableResponseOptions = {}) {
|
|
43
|
-
if (process.env.NODE_ENV !== "production") {
|
|
44
|
-
if (!(config.statuses || config.headers)) {
|
|
45
|
-
throw new SerwistError("statuses-or-headers-required", {
|
|
46
|
-
moduleName: "@serwist/cacheable-response",
|
|
47
|
-
className: "CacheableResponse",
|
|
48
|
-
funcName: "constructor",
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (config.statuses) {
|
|
53
|
-
assert!.isArray(config.statuses, {
|
|
54
|
-
moduleName: "@serwist/cacheable-response",
|
|
55
|
-
className: "CacheableResponse",
|
|
56
|
-
funcName: "constructor",
|
|
57
|
-
paramName: "config.statuses",
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (config.headers) {
|
|
62
|
-
assert!.isType(config.headers, "object", {
|
|
63
|
-
moduleName: "@serwist/cacheable-response",
|
|
64
|
-
className: "CacheableResponse",
|
|
65
|
-
funcName: "constructor",
|
|
66
|
-
paramName: "config.headers",
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
this._statuses = config.statuses;
|
|
72
|
-
this._headers = config.headers;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Checks a response to see whether it's cacheable or not, based on this
|
|
77
|
-
* object's configuration.
|
|
78
|
-
*
|
|
79
|
-
* @param response The response whose cacheability is being
|
|
80
|
-
* checked.
|
|
81
|
-
* @returns `true` if the `Response` is cacheable, and `false`
|
|
82
|
-
* otherwise.
|
|
83
|
-
*/
|
|
84
|
-
isResponseCacheable(response: Response): boolean {
|
|
85
|
-
if (process.env.NODE_ENV !== "production") {
|
|
86
|
-
assert!.isInstance(response, Response, {
|
|
87
|
-
moduleName: "@serwist/cacheable-response",
|
|
88
|
-
className: "CacheableResponse",
|
|
89
|
-
funcName: "isResponseCacheable",
|
|
90
|
-
paramName: "response",
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
let cacheable = true;
|
|
95
|
-
|
|
96
|
-
if (this._statuses) {
|
|
97
|
-
cacheable = this._statuses.includes(response.status);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (this._headers && cacheable) {
|
|
101
|
-
cacheable = Object.keys(this._headers).some((headerName) => {
|
|
102
|
-
return response.headers.get(headerName) === this._headers![headerName];
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (process.env.NODE_ENV !== "production") {
|
|
107
|
-
if (!cacheable) {
|
|
108
|
-
logger.groupCollapsed(
|
|
109
|
-
`The request for '${getFriendlyURL(response.url)}' returned a response that does not meet the criteria for being cached.`,
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
logger.groupCollapsed("View cacheability criteria here.");
|
|
113
|
-
logger.log(`Cacheable statuses: ${JSON.stringify(this._statuses)}`);
|
|
114
|
-
logger.log(`Cacheable headers: ${JSON.stringify(this._headers, null, 2)}`);
|
|
115
|
-
logger.groupEnd();
|
|
116
|
-
|
|
117
|
-
const logFriendlyHeaders: { [key: string]: string } = {};
|
|
118
|
-
response.headers.forEach((value, key) => {
|
|
119
|
-
logFriendlyHeaders[key] = value;
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
logger.groupCollapsed("View response status and headers here.");
|
|
123
|
-
logger.log(`Response status: ${response.status}`);
|
|
124
|
-
logger.log(`Response headers: ${JSON.stringify(logFriendlyHeaders, null, 2)}`);
|
|
125
|
-
logger.groupEnd();
|
|
126
|
-
|
|
127
|
-
logger.groupCollapsed("View full response details here.");
|
|
128
|
-
logger.log(response.headers);
|
|
129
|
-
logger.log(response);
|
|
130
|
-
logger.groupEnd();
|
|
131
|
-
|
|
132
|
-
logger.groupEnd();
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return cacheable;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2018 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { SerwistPlugin } from "@serwist/core";
|
|
10
|
-
|
|
11
|
-
import type { CacheableResponseOptions } from "./CacheableResponse.js";
|
|
12
|
-
import { CacheableResponse } from "./CacheableResponse.js";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* A class implementing the `cacheWillUpdate` lifecycle callback. This makes it
|
|
16
|
-
* easier to add in cacheability checks to requests made via Serwist's built-in
|
|
17
|
-
* strategies.
|
|
18
|
-
*/
|
|
19
|
-
export class CacheableResponsePlugin implements SerwistPlugin {
|
|
20
|
-
private readonly _cacheableResponse: CacheableResponse;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* To construct a new CacheableResponsePlugin instance you must provide at
|
|
24
|
-
* least one of the `config` properties.
|
|
25
|
-
*
|
|
26
|
-
* If both `statuses` and `headers` are specified, then both conditions must
|
|
27
|
-
* be met for the `Response` to be considered cacheable.
|
|
28
|
-
*
|
|
29
|
-
* @param config
|
|
30
|
-
*/
|
|
31
|
-
constructor(config: CacheableResponseOptions) {
|
|
32
|
-
this._cacheableResponse = new CacheableResponse(config);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* @param options
|
|
37
|
-
* @returns
|
|
38
|
-
* @private
|
|
39
|
-
*/
|
|
40
|
-
cacheWillUpdate: SerwistPlugin["cacheWillUpdate"] = async ({ response }) => {
|
|
41
|
-
if (this._cacheableResponse.isResponseCacheable(response)) {
|
|
42
|
-
return response;
|
|
43
|
-
}
|
|
44
|
-
return null;
|
|
45
|
-
};
|
|
46
|
-
}
|