@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.
- package/package.json +1 -1
- package/src/index.ts +26 -6
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -43,7 +43,7 @@ class FetchGraphQLMesh {
|
|
|
43
43
|
return this._fetchGraphQlHeaders;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
public _fetchGraphQlHeaders: Header
|
|
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
|
|
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
|
-
|
|
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
|
}
|