@adobe-commerce/fetch-graphql 1.0.0 → 1.0.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +26 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe-commerce/fetch-graphql",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "license": "SEE LICENSE IN LICENSE.md",
5
5
  "description": "A library to fetch GraphQL queries and mutations",
6
6
  "engines": {
package/src/index.ts CHANGED
@@ -43,7 +43,7 @@ class FetchGraphQLMesh {
43
43
  return this._fetchGraphQlHeaders;
44
44
  }
45
45
 
46
- public _fetchGraphQlHeaders: Header | undefined;
46
+ public _fetchGraphQlHeaders: Header = {};
47
47
 
48
48
  /**
49
49
  * Sets the GraphQL endpoint.
@@ -69,14 +69,34 @@ class FetchGraphQLMesh {
69
69
  * @param key - The key of the header.
70
70
  */
71
71
  public removeFetchGraphQlHeader(key: string) {
72
- delete this._fetchGraphQlHeaders?.[key];
72
+ delete this._fetchGraphQlHeaders[key];
73
73
  }
74
74
  /**
75
75
  * Sets the GraphQL headers.
76
- * @param header - The header object.
76
+ * @param header - The header object or a function that returns a header object.
77
+ * If a function is provided, it will be called with the previous headers.
78
+ * The returned object will be merged with the previous headers.
79
+ * @example
80
+ * ```js
81
+ * // set headers
82
+ * setFetchGraphQlHeaders({ test: 'test' });
83
+ *
84
+ * // merge with previous headers
85
+ * setFetchGraphQlHeaders((prev) => ({
86
+ * ...prev,
87
+ * test: 'test2',
88
+ * }));
89
+ * ```
77
90
  */
78
- public setFetchGraphQlHeaders(header: Header) {
79
- this._fetchGraphQlHeaders = { ...header };
91
+ public setFetchGraphQlHeaders(header: Header | ((prev: Header) => Header)) {
92
+ if (typeof header === 'function') {
93
+ this._fetchGraphQlHeaders = {
94
+ ...this._fetchGraphQlHeaders,
95
+ ...header(this._fetchGraphQlHeaders),
96
+ };
97
+ } else {
98
+ this._fetchGraphQlHeaders = { ...header };
99
+ }
80
100
  }
81
101
  /**
82
102
  * Fetches GraphQL data.
@@ -166,7 +186,7 @@ export class FetchGraphQL extends FetchGraphQLMesh {
166
186
  return (
167
187
  (this._endpoint
168
188
  ? this._fetchGraphQlHeaders
169
- : { ...this._fetchGraphQlHeaders, ...mesh.fetchGraphQlHeaders }) || {}
189
+ : { ...this._fetchGraphQlHeaders, ...mesh.fetchGraphQlHeaders })
170
190
  );
171
191
  }
172
192
  }