@graphql-mesh/plugin-http-cache 0.105.26 → 0.105.27
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/cjs/index.js +12 -4
- package/esm/index.js +12 -4
- package/package.json +1 -1
package/cjs/index.js
CHANGED
|
@@ -6,6 +6,7 @@ const http_cache_semantics_1 = tslib_1.__importDefault(require("http-cache-seman
|
|
|
6
6
|
const utils_1 = require("@graphql-mesh/utils");
|
|
7
7
|
const fetch_1 = require("@whatwg-node/fetch");
|
|
8
8
|
const promise_helpers_1 = require("@whatwg-node/promise-helpers");
|
|
9
|
+
const cacheHeaders = ['Cache-Control', 'Expires', 'ETag', 'Last-Modified', 'Vary'];
|
|
9
10
|
function useHTTPCache({ cache, matches, ignores, logger, }) {
|
|
10
11
|
if (!cache) {
|
|
11
12
|
throw new Error('HTTP Cache plugin requires a cache instance');
|
|
@@ -50,14 +51,14 @@ function useHTTPCache({ cache, matches, ignores, logger, }) {
|
|
|
50
51
|
logger ||= yoga.logger;
|
|
51
52
|
},
|
|
52
53
|
onFetch({ url, options, setOptions, context, endResponse }) {
|
|
53
|
-
if (shouldSkip(url) || typeof options
|
|
54
|
+
if (shouldSkip(url) || typeof options?.body === 'object') {
|
|
54
55
|
pluginLogger?.debug(`Skipping cache for ${url}`);
|
|
55
56
|
return;
|
|
56
57
|
}
|
|
57
|
-
const cacheKey = `http-cache-${url}-${options
|
|
58
|
+
const cacheKey = `http-cache-${url}-${options?.method}-${options?.body}`;
|
|
58
59
|
const policyRequest = {
|
|
59
60
|
url,
|
|
60
|
-
headers: (0, utils_1.getHeadersObj)(options
|
|
61
|
+
headers: (0, utils_1.getHeadersObj)(options?.headers || {}),
|
|
61
62
|
};
|
|
62
63
|
return (0, promise_helpers_1.handleMaybePromise)(() => cache.get(cacheKey), function handleCacheEntry(cacheEntry) {
|
|
63
64
|
let policy;
|
|
@@ -78,13 +79,20 @@ function useHTTPCache({ cache, matches, ignores, logger, }) {
|
|
|
78
79
|
else if (policy?.revalidationHeaders) {
|
|
79
80
|
pluginLogger?.debug(`Cache will be revalidated for ${url}`);
|
|
80
81
|
setOptions({
|
|
81
|
-
...options,
|
|
82
|
+
...(options || {}),
|
|
82
83
|
// @ts-expect-error - Headers type mismatch
|
|
83
84
|
headers: policy.revalidationHeaders(policyRequest),
|
|
84
85
|
});
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
88
|
return function handleResponse({ response, setResponse }) {
|
|
89
|
+
const cacheControlHeader = response.headers.get('Cache-Control');
|
|
90
|
+
if (cacheHeaders.every(header => !response.headers.has(header)) ||
|
|
91
|
+
cacheControlHeader?.includes('no-store') ||
|
|
92
|
+
cacheControlHeader?.includes('max-age=0') ||
|
|
93
|
+
cacheControlHeader?.includes('s-maxage=0')) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
88
96
|
let body$;
|
|
89
97
|
const policyResponse = {
|
|
90
98
|
status: response.status,
|
package/esm/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import CachePolicy from 'http-cache-semantics';
|
|
|
2
2
|
import { getHeadersObj } from '@graphql-mesh/utils';
|
|
3
3
|
import { Response as DefaultResponseCtor, URLPattern as DefaultURLPatternCtor, } from '@whatwg-node/fetch';
|
|
4
4
|
import { handleMaybePromise } from '@whatwg-node/promise-helpers';
|
|
5
|
+
const cacheHeaders = ['Cache-Control', 'Expires', 'ETag', 'Last-Modified', 'Vary'];
|
|
5
6
|
export default function useHTTPCache({ cache, matches, ignores, logger, }) {
|
|
6
7
|
if (!cache) {
|
|
7
8
|
throw new Error('HTTP Cache plugin requires a cache instance');
|
|
@@ -46,14 +47,14 @@ export default function useHTTPCache({ cache, matches, ignores, logger, }) {
|
|
|
46
47
|
logger ||= yoga.logger;
|
|
47
48
|
},
|
|
48
49
|
onFetch({ url, options, setOptions, context, endResponse }) {
|
|
49
|
-
if (shouldSkip(url) || typeof options
|
|
50
|
+
if (shouldSkip(url) || typeof options?.body === 'object') {
|
|
50
51
|
pluginLogger?.debug(`Skipping cache for ${url}`);
|
|
51
52
|
return;
|
|
52
53
|
}
|
|
53
|
-
const cacheKey = `http-cache-${url}-${options
|
|
54
|
+
const cacheKey = `http-cache-${url}-${options?.method}-${options?.body}`;
|
|
54
55
|
const policyRequest = {
|
|
55
56
|
url,
|
|
56
|
-
headers: getHeadersObj(options
|
|
57
|
+
headers: getHeadersObj(options?.headers || {}),
|
|
57
58
|
};
|
|
58
59
|
return handleMaybePromise(() => cache.get(cacheKey), function handleCacheEntry(cacheEntry) {
|
|
59
60
|
let policy;
|
|
@@ -74,13 +75,20 @@ export default function useHTTPCache({ cache, matches, ignores, logger, }) {
|
|
|
74
75
|
else if (policy?.revalidationHeaders) {
|
|
75
76
|
pluginLogger?.debug(`Cache will be revalidated for ${url}`);
|
|
76
77
|
setOptions({
|
|
77
|
-
...options,
|
|
78
|
+
...(options || {}),
|
|
78
79
|
// @ts-expect-error - Headers type mismatch
|
|
79
80
|
headers: policy.revalidationHeaders(policyRequest),
|
|
80
81
|
});
|
|
81
82
|
}
|
|
82
83
|
}
|
|
83
84
|
return function handleResponse({ response, setResponse }) {
|
|
85
|
+
const cacheControlHeader = response.headers.get('Cache-Control');
|
|
86
|
+
if (cacheHeaders.every(header => !response.headers.has(header)) ||
|
|
87
|
+
cacheControlHeader?.includes('no-store') ||
|
|
88
|
+
cacheControlHeader?.includes('max-age=0') ||
|
|
89
|
+
cacheControlHeader?.includes('s-maxage=0')) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
84
92
|
let body$;
|
|
85
93
|
const policyResponse = {
|
|
86
94
|
status: response.status,
|