@smartive/datocms-utils 3.0.0 → 3.1.0

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/README.md CHANGED
@@ -44,7 +44,7 @@ Utilities for managing [DatoCMS cache tags](https://www.datocms.com/docs/content
44
44
  import { generateQueryId, parseXCacheTagsResponseHeader } from '@smartive/datocms-utils/cache-tags';
45
45
 
46
46
  // Generate a unique ID for a GraphQL query
47
- const queryId = generateQueryId(document, variables);
47
+ const queryId = generateQueryId(document, variables, headers);
48
48
 
49
49
  // Parse DatoCMS's X-Cache-Tags header
50
50
  const tags = parseXCacheTagsResponseHeader('tag-a tag-2 other-tag');
@@ -168,7 +168,7 @@ const provider = new RedisCacheTagsProvider({
168
168
  });
169
169
 
170
170
  // After making a DatoCMS query
171
- const queryId = generateQueryId(document, variables);
171
+ const queryId = generateQueryId(document, variables, request.headers);
172
172
  const cacheTags = parseXCacheTagsResponseHeader(response.headers['x-cache-tags']);
173
173
  await provider.storeQueryCacheTags(queryId, cacheTags);
174
174
 
@@ -9,10 +9,11 @@ import { type CacheTag } from './types.js';
9
9
  */
10
10
  export declare const parseXCacheTagsResponseHeader: (string?: null | string) => CacheTag[];
11
11
  /**
12
- * Generates a unique query ID based on the query document and its variables.
12
+ * Generates a unique query ID based on the query document, its variables, and optional HTTP headers.
13
13
  *
14
14
  * @param {DocumentNode} document Query document
15
- * @param {TVariables} variables Query variables
15
+ * @param {TVariables} variables Optional query variables
16
+ * @param {HeadersInit} headers Optional HTTP headers that might affect the query result (e.g., for authentication)
16
17
  * @returns Unique query ID
17
18
  */
18
- export declare const generateQueryId: <TVariables = unknown>(document: DocumentNode, variables?: TVariables) => string;
19
+ export declare const generateQueryId: <TVariables = unknown>(document: DocumentNode, variables?: TVariables, headers?: HeadersInit) => string;
@@ -9,16 +9,18 @@ import { createHash } from 'node:crypto';
9
9
  */
10
10
  export const parseXCacheTagsResponseHeader = (string) => (string?.split(' ') ?? []).map((tag) => tag);
11
11
  /**
12
- * Generates a unique query ID based on the query document and its variables.
12
+ * Generates a unique query ID based on the query document, its variables, and optional HTTP headers.
13
13
  *
14
14
  * @param {DocumentNode} document Query document
15
- * @param {TVariables} variables Query variables
15
+ * @param {TVariables} variables Optional query variables
16
+ * @param {HeadersInit} headers Optional HTTP headers that might affect the query result (e.g., for authentication)
16
17
  * @returns Unique query ID
17
18
  */
18
- export const generateQueryId = (document, variables) => {
19
+ export const generateQueryId = (document, variables, headers) => {
19
20
  return createHash('sha1')
20
21
  .update(print(document))
21
22
  .update(JSON.stringify(variables) || '')
23
+ .update(JSON.stringify(headers) || '')
22
24
  .digest('hex');
23
25
  };
24
26
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/cache-tags/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAqB,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,MAAsB,EAAE,EAAE,CACtE,CAAC,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAe,CAAC,CAAC;AAE3D;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAuB,QAAsB,EAAE,SAAsB,EAAU,EAAE;IAC9G,OAAO,UAAU,CAAC,MAAM,CAAC;SACtB,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;SACvB,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;SACvC,MAAM,CAAC,KAAK,CAAC,CAAC;AACnB,CAAC,CAAC"}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/cache-tags/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAqB,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,MAAsB,EAAE,EAAE,CACtE,CAAC,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAe,CAAC,CAAC;AAE3D;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,QAAsB,EACtB,SAAsB,EACtB,OAAqB,EACb,EAAE;IACV,OAAO,UAAU,CAAC,MAAM,CAAC;SACtB,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;SACvB,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;SACvC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;SACrC,MAAM,CAAC,KAAK,CAAC,CAAC;AACnB,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smartive/datocms-utils",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "A set of utilities and helpers to work with DatoCMS in a Next.js project.",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -13,15 +13,21 @@ export const parseXCacheTagsResponseHeader = (string?: null | string) =>
13
13
  (string?.split(' ') ?? []).map((tag) => tag as CacheTag);
14
14
 
15
15
  /**
16
- * Generates a unique query ID based on the query document and its variables.
16
+ * Generates a unique query ID based on the query document, its variables, and optional HTTP headers.
17
17
  *
18
18
  * @param {DocumentNode} document Query document
19
- * @param {TVariables} variables Query variables
19
+ * @param {TVariables} variables Optional query variables
20
+ * @param {HeadersInit} headers Optional HTTP headers that might affect the query result (e.g., for authentication)
20
21
  * @returns Unique query ID
21
22
  */
22
- export const generateQueryId = <TVariables = unknown>(document: DocumentNode, variables?: TVariables): string => {
23
+ export const generateQueryId = <TVariables = unknown>(
24
+ document: DocumentNode,
25
+ variables?: TVariables,
26
+ headers?: HeadersInit,
27
+ ): string => {
23
28
  return createHash('sha1')
24
29
  .update(print(document))
25
30
  .update(JSON.stringify(variables) || '')
31
+ .update(JSON.stringify(headers) || '')
26
32
  .digest('hex');
27
33
  };