@common-stack/server-stack 7.0.4-alpha.11 → 7.0.4-alpha.12
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/lib/plugins/invalidateCachePlugin.cjs +24 -15
- package/lib/plugins/invalidateCachePlugin.cjs.map +1 -1
- package/lib/plugins/invalidateCachePlugin.mjs +24 -15
- package/lib/plugins/invalidateCachePlugin.mjs.map +1 -1
- package/lib/plugins/responseCachePlugin.cjs +40 -42
- package/lib/plugins/responseCachePlugin.cjs.map +1 -1
- package/lib/plugins/responseCachePlugin.mjs +40 -42
- package/lib/plugins/responseCachePlugin.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var serverCore=require('@common-stack/server-core'),envConfig=require('../config/env-config.cjs');const invalidateCachePlugin = ({ cache: redisClient, invalidateCacheKeyGenerator, }) => ({
|
|
2
2
|
requestDidStart(requestContext) {
|
|
3
3
|
return {
|
|
4
4
|
willSendResponse: async (responseContext) => {
|
|
@@ -15,25 +15,34 @@
|
|
|
15
15
|
if (hasErrors || !queriesToInvalidate?.length || !isMutation) {
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
18
|
-
// this to get the
|
|
19
18
|
const nestedKeys = await Promise.all(queriesToInvalidate.map(async (query) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
// Build keys in order of specificity
|
|
20
|
+
const keys = [];
|
|
21
|
+
// Add user-specific key if user exists
|
|
22
|
+
if (user?.sub && tenantId) {
|
|
23
|
+
keys.push(`${envConfig.config.APP_NAME}:${tenantId}:${user.sub}:${query}:*`);
|
|
24
|
+
}
|
|
25
|
+
// Add tenant-specific key if tenant exists
|
|
26
|
+
if (tenantId) {
|
|
27
|
+
keys.push(`${envConfig.config.APP_NAME}:${tenantId}:${query}:*`);
|
|
28
|
+
}
|
|
29
|
+
// Add global key as fallback
|
|
30
|
+
keys.push(`${envConfig.config.APP_NAME}:${query}:*`);
|
|
31
|
+
// Allow custom key generation if provided
|
|
24
32
|
if (typeof invalidateCacheKeyGenerator === 'function') {
|
|
25
|
-
const generatedKey = invalidateCacheKeyGenerator(requestContext, responseContext,
|
|
33
|
+
const generatedKey = invalidateCacheKeyGenerator(requestContext, responseContext, keys[0]);
|
|
26
34
|
if (generatedKey) {
|
|
27
|
-
|
|
35
|
+
keys.unshift(generatedKey);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// Try keys in order until we find matches
|
|
39
|
+
for (const key of keys) {
|
|
40
|
+
const matchingKeys = await redisClient.keys(key);
|
|
41
|
+
if (matchingKeys.length) {
|
|
42
|
+
return matchingKeys;
|
|
28
43
|
}
|
|
29
44
|
}
|
|
30
|
-
|
|
31
|
-
if (privateKeys.length)
|
|
32
|
-
return privateKeys;
|
|
33
|
-
const tenantKeys = await redisClient.keys(defaultTenantKey);
|
|
34
|
-
if (tenantKeys.length)
|
|
35
|
-
return tenantKeys;
|
|
36
|
-
return redisClient.keys(defaultKey);
|
|
45
|
+
return [];
|
|
37
46
|
}));
|
|
38
47
|
const keys = nestedKeys.flat();
|
|
39
48
|
if (keys?.length) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"invalidateCachePlugin.cjs","sources":["../../src/plugins/invalidateCachePlugin.ts"],"sourcesContent":[null],"names":["extractTenantId","config"],"mappings":"+GAaO,MAAM,qBAAqB,GAAG,CAAC,EAClC,KAAK,EAAE,WAAW,EAClB,2BAA2B,GAI9B,MACI;AACG,IAAA,eAAe,CAAC,cAA4D,EAAA;QACxE,OAAO;AACH,YAAA,gBAAgB,EAAE,OAAO,eAA+D,KAAI;;gBAExF,IAAI,CAAC,cAAc,EAAE;oBACjB,OAAO;iBACV;AACD,gBAAA,IAAI;oBACA,MAAM,SAAS,GAAG,CAAC,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC;oBAClD,MAAM,EAAE,mBAAmB,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,cAAc,CAAC,YAAY,CAAC;oBACvE,MAAM,QAAQ,GAAGA,0BAAe,CAAC,GAAG,EAAE,sBAAsB,EAAE,SAAS,CAAC,CAAC;oBACzE,MAAM,CAAC,EAAE,SAAS,EAAE,CAAC,GAAG,eAAe,CAAC,QAAQ,CAAC,WAAwC,CAAC;AAC1F,oBAAA,MAAM,UAAU,GAAG,SAAS,KAAK,UAAU,CAAC;oBAC5C,IAAI,SAAS,IAAI,CAAC,mBAAmB,EAAE,MAAM,IAAI,CAAC,UAAU,EAAE;wBAC1D,OAAO;qBACV
|
|
1
|
+
{"version":3,"file":"invalidateCachePlugin.cjs","sources":["../../src/plugins/invalidateCachePlugin.ts"],"sourcesContent":[null],"names":["extractTenantId","config"],"mappings":"+GAaO,MAAM,qBAAqB,GAAG,CAAC,EAClC,KAAK,EAAE,WAAW,EAClB,2BAA2B,GAI9B,MACI;AACG,IAAA,eAAe,CAAC,cAA4D,EAAA;QACxE,OAAO;AACH,YAAA,gBAAgB,EAAE,OAAO,eAA+D,KAAI;;gBAExF,IAAI,CAAC,cAAc,EAAE;oBACjB,OAAO;iBACV;AACD,gBAAA,IAAI;oBACA,MAAM,SAAS,GAAG,CAAC,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC;oBAClD,MAAM,EAAE,mBAAmB,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,cAAc,CAAC,YAAY,CAAC;oBACvE,MAAM,QAAQ,GAAGA,0BAAe,CAAC,GAAG,EAAE,sBAAsB,EAAE,SAAS,CAAC,CAAC;oBACzE,MAAM,CAAC,EAAE,SAAS,EAAE,CAAC,GAAG,eAAe,CAAC,QAAQ,CAAC,WAAwC,CAAC;AAC1F,oBAAA,MAAM,UAAU,GAAG,SAAS,KAAK,UAAU,CAAC;oBAC5C,IAAI,SAAS,IAAI,CAAC,mBAAmB,EAAE,MAAM,IAAI,CAAC,UAAU,EAAE;wBAC1D,OAAO;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IAAI,GAAG,UAAU,CAAC,IAAI,EAAE,CAAC;AAC/B,oBAAA,IAAI,IAAI,EAAE,MAAM,EAAE;AACd,wBAAA,MAAM,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;qBAC/B;iBACJ;gBAAC,OAAO,CAAC,EAAE;AACR,oBAAA,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,yCAAyC,CAAC,CAAC;AACvE,oBAAA,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;iBAClC;aACJ;SACJ,CAAC;KACL;AACJ,CAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {extractTenantId}from'@common-stack/server-core';import {config}from'../config/env-config.mjs';const invalidateCachePlugin = ({ cache: redisClient, invalidateCacheKeyGenerator, }) => ({
|
|
2
2
|
requestDidStart(requestContext) {
|
|
3
3
|
return {
|
|
4
4
|
willSendResponse: async (responseContext) => {
|
|
@@ -15,25 +15,34 @@ import {config}from'../config/env-config.mjs';import {extractTenantId}from'@comm
|
|
|
15
15
|
if (hasErrors || !queriesToInvalidate?.length || !isMutation) {
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
18
|
-
// this to get the
|
|
19
18
|
const nestedKeys = await Promise.all(queriesToInvalidate.map(async (query) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
// Build keys in order of specificity
|
|
20
|
+
const keys = [];
|
|
21
|
+
// Add user-specific key if user exists
|
|
22
|
+
if (user?.sub && tenantId) {
|
|
23
|
+
keys.push(`${config.APP_NAME}:${tenantId}:${user.sub}:${query}:*`);
|
|
24
|
+
}
|
|
25
|
+
// Add tenant-specific key if tenant exists
|
|
26
|
+
if (tenantId) {
|
|
27
|
+
keys.push(`${config.APP_NAME}:${tenantId}:${query}:*`);
|
|
28
|
+
}
|
|
29
|
+
// Add global key as fallback
|
|
30
|
+
keys.push(`${config.APP_NAME}:${query}:*`);
|
|
31
|
+
// Allow custom key generation if provided
|
|
24
32
|
if (typeof invalidateCacheKeyGenerator === 'function') {
|
|
25
|
-
const generatedKey = invalidateCacheKeyGenerator(requestContext, responseContext,
|
|
33
|
+
const generatedKey = invalidateCacheKeyGenerator(requestContext, responseContext, keys[0]);
|
|
26
34
|
if (generatedKey) {
|
|
27
|
-
|
|
35
|
+
keys.unshift(generatedKey);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// Try keys in order until we find matches
|
|
39
|
+
for (const key of keys) {
|
|
40
|
+
const matchingKeys = await redisClient.keys(key);
|
|
41
|
+
if (matchingKeys.length) {
|
|
42
|
+
return matchingKeys;
|
|
28
43
|
}
|
|
29
44
|
}
|
|
30
|
-
|
|
31
|
-
if (privateKeys.length)
|
|
32
|
-
return privateKeys;
|
|
33
|
-
const tenantKeys = await redisClient.keys(defaultTenantKey);
|
|
34
|
-
if (tenantKeys.length)
|
|
35
|
-
return tenantKeys;
|
|
36
|
-
return redisClient.keys(defaultKey);
|
|
45
|
+
return [];
|
|
37
46
|
}));
|
|
38
47
|
const keys = nestedKeys.flat();
|
|
39
48
|
if (keys?.length) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"invalidateCachePlugin.mjs","sources":["../../src/plugins/invalidateCachePlugin.ts"],"sourcesContent":[null],"names":[],"mappings":"sGAaO,MAAM,qBAAqB,GAAG,CAAC,EAClC,KAAK,EAAE,WAAW,EAClB,2BAA2B,GAI9B,MACI;AACG,IAAA,eAAe,CAAC,cAA4D,EAAA;QACxE,OAAO;AACH,YAAA,gBAAgB,EAAE,OAAO,eAA+D,KAAI;;gBAExF,IAAI,CAAC,cAAc,EAAE;oBACjB,OAAO;iBACV;AACD,gBAAA,IAAI;oBACA,MAAM,SAAS,GAAG,CAAC,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC;oBAClD,MAAM,EAAE,mBAAmB,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,cAAc,CAAC,YAAY,C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AE;AACd,wBAAA,MAAM,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;qBAC/B;iBACJ;gBAAC,OAAO,CAAC,EAAE;AACR,oBAAA,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,yCAAyC,CAAC,CAAC;AACvE,oBAAA,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;iBAClC;aACJ;SACJ,CAAC;KACL;AACJ,CAAA"}
|
|
@@ -15,46 +15,44 @@ const isCacheable = (requestContext) => {
|
|
|
15
15
|
requestContext.overallCachePolicy.maxAge = cache.maxAge;
|
|
16
16
|
return cache.maxAge > 0;
|
|
17
17
|
};
|
|
18
|
-
const responseCachePlugin = ({ logger, cacheKeyGenerator }) => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
const responseCachePlugin = ({ logger, cacheKeyGenerator }) => cachePlugin({
|
|
19
|
+
sessionId: ({ contextValue }) => Promise.resolve(contextValue?.user?.sub ?? null),
|
|
20
|
+
generateCacheKey(requestContext) {
|
|
21
|
+
if (!isCacheable(requestContext)) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
const { request, contextValue, document, schema } = requestContext;
|
|
25
|
+
const { user, req } = contextValue;
|
|
26
|
+
const { query, variables } = request;
|
|
27
|
+
const cacheControlDirective = serverCore.getDirectiveArgsFromSchema({
|
|
28
|
+
schema,
|
|
29
|
+
document,
|
|
30
|
+
directiveName: serverCore.CACHE_CONTROL_DIRECTIVE,
|
|
31
|
+
});
|
|
32
|
+
const { scope } = cacheControlDirective ?? {};
|
|
33
|
+
const isPrivate = scope?.toLowerCase() === 'private';
|
|
34
|
+
const tenantId = serverCore.extractTenantId(req?.currentPageUriSegments?.authority);
|
|
35
|
+
const cacheKey = serverCore.generateQueryCacheKey({
|
|
36
|
+
query,
|
|
37
|
+
variables,
|
|
38
|
+
logger,
|
|
39
|
+
userId: isPrivate ? user?.sub || null : null,
|
|
40
|
+
tenantId,
|
|
41
|
+
});
|
|
42
|
+
try {
|
|
43
|
+
if (typeof cacheKeyGenerator === 'function') {
|
|
44
|
+
const generatedKey = cacheKeyGenerator(requestContext, cacheKey);
|
|
45
|
+
return generatedKey || cacheKey;
|
|
24
46
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
query,
|
|
38
|
-
variables,
|
|
39
|
-
logger,
|
|
40
|
-
userId: isPrivate ? user.sub : null,
|
|
41
|
-
tenantId,
|
|
42
|
-
});
|
|
43
|
-
try {
|
|
44
|
-
if (typeof cacheKeyGenerator === 'function') {
|
|
45
|
-
const generatedKey = cacheKeyGenerator(requestContext, cacheKey);
|
|
46
|
-
return generatedKey || cacheKey;
|
|
47
|
-
}
|
|
48
|
-
return cacheKey;
|
|
49
|
-
}
|
|
50
|
-
catch (e) {
|
|
51
|
-
// `this.logger` won't work here
|
|
52
|
-
console.warn('GenerateCacheKey Failed %s', e.message);
|
|
53
|
-
return cacheKey;
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
shouldWriteToCache: (ctx) =>
|
|
57
|
-
// Cache only successful responses
|
|
58
|
-
isCacheable(ctx) && _.isEmpty(ctx?.response?.body?.singleResult.errors),
|
|
59
|
-
});
|
|
60
|
-
};exports.responseCachePlugin=responseCachePlugin;//# sourceMappingURL=responseCachePlugin.cjs.map
|
|
47
|
+
return cacheKey;
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
// `this.logger` won't work here
|
|
51
|
+
console.warn('GenerateCacheKey Failed %s', e.message);
|
|
52
|
+
return cacheKey;
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
shouldWriteToCache: (ctx) =>
|
|
56
|
+
// Cache only successful responses
|
|
57
|
+
isCacheable(ctx) && _.isEmpty(ctx?.response?.body?.singleResult.errors),
|
|
58
|
+
});exports.responseCachePlugin=responseCachePlugin;//# sourceMappingURL=responseCachePlugin.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"responseCachePlugin.cjs","sources":["../../src/plugins/responseCachePlugin.ts"],"sourcesContent":[null],"names":["getDirectiveArgsFromSchema","CACHE_CONTROL_DIRECTIVE","extractTenantId","generateQueryCacheKey","isEmpty"],"mappings":"0JAaA,MAAM,WAAW,GAAI,iBAAyB,CAAC,OAAO,IAAI,iBAAiB,CAAC;AAO5E,MAAM,WAAW,GAAG,CAAC,cAA4D,KAAI;AACjF,IAAA,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC;IAC5C,MAAM,KAAK,GAAGA,qCAA0B,CAAC;QACrC,MAAM;QACN,QAAQ;AACR,QAAA,aAAa,EAAEC,kCAAuB;AACzC,KAAA,CAAC,CAAC;AACH,IAAA,IAAI,CAAC,KAAK;AAAE,QAAA,OAAO,KAAK,CAAC;IACzB,IAAI,KAAK,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE;AAC9B,QAAA,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC;KACxB;;IAED,cAAc,CAAC,kBAAkB,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;AACxD,IAAA,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;AAC5B,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"responseCachePlugin.cjs","sources":["../../src/plugins/responseCachePlugin.ts"],"sourcesContent":[null],"names":["getDirectiveArgsFromSchema","CACHE_CONTROL_DIRECTIVE","extractTenantId","generateQueryCacheKey","isEmpty"],"mappings":"0JAaA,MAAM,WAAW,GAAI,iBAAyB,CAAC,OAAO,IAAI,iBAAiB,CAAC;AAO5E,MAAM,WAAW,GAAG,CAAC,cAA4D,KAAI;AACjF,IAAA,MAAM,EAAE,QAAQ,EAAE,MAAM,EAA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AG,KAAK,EAAE,WAAW,EAAE,KAAK,SAAS,CAAC;QACrD,MAAM,QAAQ,GAAGC,0BAAe,CAAC,GAAG,EAAE,sBAAsB,EAAE,SAAS,CAAC,CAAC;QACzE,MAAM,QAAQ,GAAGC,gCAAqB,CAAC;YACnC,KAAK;YACL,SAAS;YACT,MAAM;AACN,YAAA,MAAM,EAAE,SAAS,GAAG,IAAI,EAAE,GAAG,IAAI,IAAI,GAAG,IAAI;YAC5C,QAAQ;AACX,SAAA,CAAC,CAAC;AACH,QAAA,IAAI;AACA,YAAA,IAAI,OAAO,iBAAiB,KAAK,UAAU,EAAE;gBACzC,MAAM,YAAY,GAAG,iBAAiB,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAC;gBACjE,OAAO,YAAY,IAAI,QAAQ,CAAC;aACnC;AACD,YAAA,OAAO,QAAQ,CAAC;SACnB;QAAC,OAAO,CAAC,EAAE;;YAER,OAAO,CAAC,IAAI,CAAC,4BAA4B,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC;AACtD,YAAA,OAAO,QAAQ,CAAC;SACnB;KACJ;AACD,IAAA,kBAAkB,EAAE,CAAC,GAAG;;AAEpB,IAAA,WAAW,CAAC,GAAG,CAAC,IAAIC,SAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,YAAY,CAAC,MAAM,CAAC;AAC5E,CAAA"}
|
|
@@ -15,46 +15,44 @@ const isCacheable = (requestContext) => {
|
|
|
15
15
|
requestContext.overallCachePolicy.maxAge = cache.maxAge;
|
|
16
16
|
return cache.maxAge > 0;
|
|
17
17
|
};
|
|
18
|
-
const responseCachePlugin = ({ logger, cacheKeyGenerator }) => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
const responseCachePlugin = ({ logger, cacheKeyGenerator }) => cachePlugin({
|
|
19
|
+
sessionId: ({ contextValue }) => Promise.resolve(contextValue?.user?.sub ?? null),
|
|
20
|
+
generateCacheKey(requestContext) {
|
|
21
|
+
if (!isCacheable(requestContext)) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
const { request, contextValue, document, schema } = requestContext;
|
|
25
|
+
const { user, req } = contextValue;
|
|
26
|
+
const { query, variables } = request;
|
|
27
|
+
const cacheControlDirective = getDirectiveArgsFromSchema({
|
|
28
|
+
schema,
|
|
29
|
+
document,
|
|
30
|
+
directiveName: CACHE_CONTROL_DIRECTIVE,
|
|
31
|
+
});
|
|
32
|
+
const { scope } = cacheControlDirective ?? {};
|
|
33
|
+
const isPrivate = scope?.toLowerCase() === 'private';
|
|
34
|
+
const tenantId = extractTenantId(req?.currentPageUriSegments?.authority);
|
|
35
|
+
const cacheKey = generateQueryCacheKey({
|
|
36
|
+
query,
|
|
37
|
+
variables,
|
|
38
|
+
logger,
|
|
39
|
+
userId: isPrivate ? user?.sub || null : null,
|
|
40
|
+
tenantId,
|
|
41
|
+
});
|
|
42
|
+
try {
|
|
43
|
+
if (typeof cacheKeyGenerator === 'function') {
|
|
44
|
+
const generatedKey = cacheKeyGenerator(requestContext, cacheKey);
|
|
45
|
+
return generatedKey || cacheKey;
|
|
24
46
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
query,
|
|
38
|
-
variables,
|
|
39
|
-
logger,
|
|
40
|
-
userId: isPrivate ? user.sub : null,
|
|
41
|
-
tenantId,
|
|
42
|
-
});
|
|
43
|
-
try {
|
|
44
|
-
if (typeof cacheKeyGenerator === 'function') {
|
|
45
|
-
const generatedKey = cacheKeyGenerator(requestContext, cacheKey);
|
|
46
|
-
return generatedKey || cacheKey;
|
|
47
|
-
}
|
|
48
|
-
return cacheKey;
|
|
49
|
-
}
|
|
50
|
-
catch (e) {
|
|
51
|
-
// `this.logger` won't work here
|
|
52
|
-
console.warn('GenerateCacheKey Failed %s', e.message);
|
|
53
|
-
return cacheKey;
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
shouldWriteToCache: (ctx) =>
|
|
57
|
-
// Cache only successful responses
|
|
58
|
-
isCacheable(ctx) && isEmpty(ctx?.response?.body?.singleResult.errors),
|
|
59
|
-
});
|
|
60
|
-
};export{responseCachePlugin};//# sourceMappingURL=responseCachePlugin.mjs.map
|
|
47
|
+
return cacheKey;
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
// `this.logger` won't work here
|
|
51
|
+
console.warn('GenerateCacheKey Failed %s', e.message);
|
|
52
|
+
return cacheKey;
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
shouldWriteToCache: (ctx) =>
|
|
56
|
+
// Cache only successful responses
|
|
57
|
+
isCacheable(ctx) && isEmpty(ctx?.response?.body?.singleResult.errors),
|
|
58
|
+
});export{responseCachePlugin};//# sourceMappingURL=responseCachePlugin.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"responseCachePlugin.mjs","sources":["../../src/plugins/responseCachePlugin.ts"],"sourcesContent":[null],"names":[],"mappings":"qOAaA,MAAM,WAAW,GAAI,iBAAyB,CAAC,OAAO,IAAI,iBAAiB,CAAC;AAO5E,MAAM,WAAW,GAAG,CAAC,cAA4D,KAAI;AACjF,IAAA,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC;IAC5C,MAAM,KAAK,GAAG,0BAA0B,CAAC;QACrC,MAAM;QACN,QAAQ;AACR,QAAA,aAAa,EAAE,uBAAuB;AACzC,KAAA,CAAC,CAAC;AACH,IAAA,IAAI,CAAC,KAAK;AAAE,QAAA,OAAO,KAAK,CAAC;IACzB,IAAI,KAAK,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE;AAC9B,QAAA,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC;KACxB;;IAED,cAAc,CAAC,kBAAkB,CA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-1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@common-stack/server-stack",
|
|
3
|
-
"version": "7.0.4-alpha.
|
|
3
|
+
"version": "7.0.4-alpha.12",
|
|
4
4
|
"description": "common core for higher packages to depend on",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "CDMBase LLC",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"publishConfig": {
|
|
89
89
|
"access": "public"
|
|
90
90
|
},
|
|
91
|
-
"gitHead": "
|
|
91
|
+
"gitHead": "dbc66a95c2e66a83402a7a40ddb8283282e7ee24",
|
|
92
92
|
"typescript": {
|
|
93
93
|
"definition": "lib/index.d.ts"
|
|
94
94
|
}
|