@envelop/response-cache 5.0.1 → 5.0.2-alpha-20230616190758-9cf98abb

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/plugin.js CHANGED
@@ -159,16 +159,9 @@ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache
159
159
  const processedResult = processResult(result);
160
160
  cache.invalidate(identifier.values());
161
161
  if (includeExtensionMetadata) {
162
- setResult({
163
- ...processedResult,
164
- extensions: {
165
- ...processedResult.extensions,
166
- responseCache: {
167
- ...processedResult.extensions?.responseCache,
168
- invalidatedEntities: Array.from(identifier.values()),
169
- },
170
- },
171
- });
162
+ setResult(resultWithMetadata(processedResult, {
163
+ invalidatedEntities: Array.from(identifier.values()),
164
+ }));
172
165
  }
173
166
  },
174
167
  };
@@ -184,16 +177,7 @@ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache
184
177
  const cachedResponse = (await cache.get(cacheKey));
185
178
  if (cachedResponse != null) {
186
179
  if (includeExtensionMetadata) {
187
- onExecuteParams.setResultAndStopExecution({
188
- ...cachedResponse,
189
- extensions: {
190
- ...cachedResponse.extensions,
191
- responseCache: {
192
- ...cachedResponse.extensions?.responseCache,
193
- hit: true,
194
- },
195
- },
196
- });
180
+ onExecuteParams.setResultAndStopExecution(resultWithMetadata(cachedResponse, { hit: true }));
197
181
  }
198
182
  else {
199
183
  onExecuteParams.setResultAndStopExecution(cachedResponse);
@@ -234,34 +218,13 @@ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache
234
218
  const finalTtl = currentTtl ?? globalTtl;
235
219
  if (finalTtl === 0) {
236
220
  if (includeExtensionMetadata) {
237
- setResult({
238
- ...processedResult,
239
- extensions: {
240
- ...processedResult.extensions,
241
- responseCache: {
242
- ...processedResult.extensions?.responseCache,
243
- hit: false,
244
- didCache: false,
245
- },
246
- },
247
- });
221
+ setResult(resultWithMetadata(processedResult, { hit: false, didCache: false }));
248
222
  }
249
223
  return;
250
224
  }
251
225
  cache.set(cacheKey, processedResult, identifier.values(), finalTtl);
252
226
  if (includeExtensionMetadata) {
253
- setResult({
254
- ...processedResult,
255
- extensions: {
256
- ...processedResult.extensions,
257
- responseCache: {
258
- ...processedResult.extensions?.responseCache,
259
- hit: false,
260
- didCache: true,
261
- ttl: finalTtl,
262
- },
263
- },
264
- });
227
+ setResult(resultWithMetadata(processedResult, { hit: false, didCache: true, ttl: finalTtl }));
265
228
  }
266
229
  },
267
230
  };
@@ -269,6 +232,18 @@ function useResponseCache({ cache = (0, in_memory_cache_js_1.createInMemoryCache
269
232
  };
270
233
  }
271
234
  exports.useResponseCache = useResponseCache;
235
+ function resultWithMetadata(result, metadata) {
236
+ return {
237
+ ...result,
238
+ extensions: {
239
+ ...result.extensions,
240
+ responseCache: {
241
+ ...result.extensions?.responseCache,
242
+ ...metadata,
243
+ },
244
+ },
245
+ };
246
+ }
272
247
  function calculateTtl(typeTtl, currentTtl) {
273
248
  if (typeof currentTtl === 'number') {
274
249
  return Math.min(currentTtl, typeTtl);
package/esm/plugin.js CHANGED
@@ -152,16 +152,9 @@ export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl
152
152
  const processedResult = processResult(result);
153
153
  cache.invalidate(identifier.values());
154
154
  if (includeExtensionMetadata) {
155
- setResult({
156
- ...processedResult,
157
- extensions: {
158
- ...processedResult.extensions,
159
- responseCache: {
160
- ...processedResult.extensions?.responseCache,
161
- invalidatedEntities: Array.from(identifier.values()),
162
- },
163
- },
164
- });
155
+ setResult(resultWithMetadata(processedResult, {
156
+ invalidatedEntities: Array.from(identifier.values()),
157
+ }));
165
158
  }
166
159
  },
167
160
  };
@@ -177,16 +170,7 @@ export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl
177
170
  const cachedResponse = (await cache.get(cacheKey));
178
171
  if (cachedResponse != null) {
179
172
  if (includeExtensionMetadata) {
180
- onExecuteParams.setResultAndStopExecution({
181
- ...cachedResponse,
182
- extensions: {
183
- ...cachedResponse.extensions,
184
- responseCache: {
185
- ...cachedResponse.extensions?.responseCache,
186
- hit: true,
187
- },
188
- },
189
- });
173
+ onExecuteParams.setResultAndStopExecution(resultWithMetadata(cachedResponse, { hit: true }));
190
174
  }
191
175
  else {
192
176
  onExecuteParams.setResultAndStopExecution(cachedResponse);
@@ -227,40 +211,31 @@ export function useResponseCache({ cache = createInMemoryCache(), ttl: globalTtl
227
211
  const finalTtl = currentTtl ?? globalTtl;
228
212
  if (finalTtl === 0) {
229
213
  if (includeExtensionMetadata) {
230
- setResult({
231
- ...processedResult,
232
- extensions: {
233
- ...processedResult.extensions,
234
- responseCache: {
235
- ...processedResult.extensions?.responseCache,
236
- hit: false,
237
- didCache: false,
238
- },
239
- },
240
- });
214
+ setResult(resultWithMetadata(processedResult, { hit: false, didCache: false }));
241
215
  }
242
216
  return;
243
217
  }
244
218
  cache.set(cacheKey, processedResult, identifier.values(), finalTtl);
245
219
  if (includeExtensionMetadata) {
246
- setResult({
247
- ...processedResult,
248
- extensions: {
249
- ...processedResult.extensions,
250
- responseCache: {
251
- ...processedResult.extensions?.responseCache,
252
- hit: false,
253
- didCache: true,
254
- ttl: finalTtl,
255
- },
256
- },
257
- });
220
+ setResult(resultWithMetadata(processedResult, { hit: false, didCache: true, ttl: finalTtl }));
258
221
  }
259
222
  },
260
223
  };
261
224
  },
262
225
  };
263
226
  }
227
+ function resultWithMetadata(result, metadata) {
228
+ return {
229
+ ...result,
230
+ extensions: {
231
+ ...result.extensions,
232
+ responseCache: {
233
+ ...result.extensions?.responseCache,
234
+ ...metadata,
235
+ },
236
+ },
237
+ };
238
+ }
264
239
  function calculateTtl(typeTtl, currentTtl) {
265
240
  if (typeof currentTtl === 'number') {
266
241
  return Math.min(currentTtl, typeTtl);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/response-cache",
3
- "version": "5.0.1",
3
+ "version": "5.0.2-alpha-20230616190758-9cf98abb",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "@envelop/core": "^4.0.0",
@@ -1,6 +1,6 @@
1
1
  import { ExecutionArgs } from 'graphql';
2
2
  import { ExecutionResult, Maybe, ObjMap, Plugin } from '@envelop/core';
3
- import type { Cache } from './cache.cjs';
3
+ import type { Cache, CacheEntityRecord } from './cache.cjs';
4
4
  /**
5
5
  * Function for building the response cache key based on the input parameters
6
6
  */
@@ -131,7 +131,7 @@ export type ResponseCacheExtensions = {
131
131
  didCache: true;
132
132
  ttl: number;
133
133
  } | {
134
- invalidatedEntities: string[];
134
+ invalidatedEntities: CacheEntityRecord[];
135
135
  };
136
136
  export type ResponseCacheExecutionResult = ExecutionResult<ObjMap<unknown>, {
137
137
  responseCache?: ResponseCacheExtensions;
@@ -1,6 +1,6 @@
1
1
  import { ExecutionArgs } from 'graphql';
2
2
  import { ExecutionResult, Maybe, ObjMap, Plugin } from '@envelop/core';
3
- import type { Cache } from './cache.js';
3
+ import type { Cache, CacheEntityRecord } from './cache.js';
4
4
  /**
5
5
  * Function for building the response cache key based on the input parameters
6
6
  */
@@ -131,7 +131,7 @@ export type ResponseCacheExtensions = {
131
131
  didCache: true;
132
132
  ttl: number;
133
133
  } | {
134
- invalidatedEntities: string[];
134
+ invalidatedEntities: CacheEntityRecord[];
135
135
  };
136
136
  export type ResponseCacheExecutionResult = ExecutionResult<ObjMap<unknown>, {
137
137
  responseCache?: ResponseCacheExtensions;