@shopkit/data-layer 1.2.0 → 1.4.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/dist/adapters/index.js +43 -644
- package/dist/adapters/index.js.map +1 -1
- package/dist/adapters/index.mjs +42 -643
- package/dist/adapters/index.mjs.map +1 -1
- package/dist/index.d.mts +33 -2
- package/dist/index.d.ts +33 -2
- package/dist/index.js +45 -644
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +43 -644
- package/dist/index.mjs.map +1 -1
- package/dist/models/index.d.mts +13 -0
- package/dist/models/index.d.ts +13 -0
- package/package.json +4 -2
package/dist/adapters/index.js
CHANGED
|
@@ -1,648 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
var
|
|
5
|
-
var MIN_RETRIES = 0;
|
|
6
|
-
var MAX_RETRIES = 3;
|
|
7
|
-
var GQL_API_ERROR = "An error occurred while fetching from the API. Review 'graphQLErrors' for details.";
|
|
8
|
-
var UNEXPECTED_CONTENT_TYPE_ERROR = "Response returned unexpected Content-Type:";
|
|
9
|
-
var NO_DATA_OR_ERRORS_ERROR = "An unknown error has occurred. The API did not return a data object or any errors in its response.";
|
|
10
|
-
var CONTENT_TYPES = {
|
|
11
|
-
json: "application/json",
|
|
12
|
-
multipart: "multipart/mixed"
|
|
13
|
-
};
|
|
14
|
-
var SDK_VARIANT_HEADER = "X-SDK-Variant";
|
|
15
|
-
var SDK_VERSION_HEADER = "X-SDK-Version";
|
|
16
|
-
var DEFAULT_SDK_VARIANT = "shopify-graphql-client";
|
|
17
|
-
var DEFAULT_CLIENT_VERSION = "1.4.1";
|
|
18
|
-
var RETRY_WAIT_TIME = 1e3;
|
|
19
|
-
var RETRIABLE_STATUS_CODES = [429, 503];
|
|
20
|
-
var DEFER_OPERATION_REGEX = /@(defer)\b/i;
|
|
21
|
-
var NEWLINE_SEPARATOR = "\r\n";
|
|
22
|
-
var BOUNDARY_HEADER_REGEX = /boundary="?([^=";]+)"?/i;
|
|
23
|
-
var HEADER_SEPARATOR = NEWLINE_SEPARATOR + NEWLINE_SEPARATOR;
|
|
24
|
-
|
|
25
|
-
// ../../node_modules/@shopify/graphql-client/dist/graphql-client/utilities.mjs
|
|
26
|
-
function formatErrorMessage(message, client = CLIENT) {
|
|
27
|
-
return message.startsWith(`${client}`) ? message : `${client}: ${message}`;
|
|
28
|
-
}
|
|
29
|
-
function getErrorMessage(error) {
|
|
30
|
-
return error instanceof Error ? error.message : JSON.stringify(error);
|
|
31
|
-
}
|
|
32
|
-
function getErrorCause(error) {
|
|
33
|
-
return error instanceof Error && error.cause ? error.cause : void 0;
|
|
34
|
-
}
|
|
35
|
-
function combineErrors(dataArray) {
|
|
36
|
-
return dataArray.flatMap(({ errors }) => {
|
|
37
|
-
return errors ?? [];
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
function validateRetries({ client, retries }) {
|
|
41
|
-
if (retries !== void 0 && (typeof retries !== "number" || retries < MIN_RETRIES || retries > MAX_RETRIES)) {
|
|
42
|
-
throw new Error(`${client}: The provided "retries" value (${retries}) is invalid - it cannot be less than ${MIN_RETRIES} or greater than ${MAX_RETRIES}`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
function getKeyValueIfValid(key, value) {
|
|
46
|
-
return value && (typeof value !== "object" || Array.isArray(value) || typeof value === "object" && Object.keys(value).length > 0) ? { [key]: value } : {};
|
|
47
|
-
}
|
|
48
|
-
function buildDataObjectByPath(path, data) {
|
|
49
|
-
if (path.length === 0) {
|
|
50
|
-
return data;
|
|
51
|
-
}
|
|
52
|
-
const key = path.pop();
|
|
53
|
-
const newData = {
|
|
54
|
-
[key]: data
|
|
55
|
-
};
|
|
56
|
-
if (path.length === 0) {
|
|
57
|
-
return newData;
|
|
58
|
-
}
|
|
59
|
-
return buildDataObjectByPath(path, newData);
|
|
60
|
-
}
|
|
61
|
-
function combineObjects(baseObject, newObject) {
|
|
62
|
-
return Object.keys(newObject || {}).reduce((acc, key) => {
|
|
63
|
-
if ((typeof newObject[key] === "object" || Array.isArray(newObject[key])) && baseObject[key]) {
|
|
64
|
-
acc[key] = combineObjects(baseObject[key], newObject[key]);
|
|
65
|
-
return acc;
|
|
66
|
-
}
|
|
67
|
-
acc[key] = newObject[key];
|
|
68
|
-
return acc;
|
|
69
|
-
}, Array.isArray(baseObject) ? [...baseObject] : { ...baseObject });
|
|
70
|
-
}
|
|
71
|
-
function buildCombinedDataObject([initialDatum, ...remainingData]) {
|
|
72
|
-
return remainingData.reduce(combineObjects, { ...initialDatum });
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// ../../node_modules/@shopify/graphql-client/dist/graphql-client/http-fetch.mjs
|
|
76
|
-
function generateHttpFetch({ clientLogger, customFetchApi = fetch, client = CLIENT, defaultRetryWaitTime = RETRY_WAIT_TIME, retriableCodes = RETRIABLE_STATUS_CODES }) {
|
|
77
|
-
const httpFetch = async (requestParams, count, maxRetries) => {
|
|
78
|
-
const nextCount = count + 1;
|
|
79
|
-
const maxTries = maxRetries + 1;
|
|
80
|
-
let response;
|
|
81
|
-
try {
|
|
82
|
-
response = await customFetchApi(...requestParams);
|
|
83
|
-
clientLogger({
|
|
84
|
-
type: "HTTP-Response",
|
|
85
|
-
content: {
|
|
86
|
-
requestParams,
|
|
87
|
-
response
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
if (!response.ok && retriableCodes.includes(response.status) && nextCount <= maxTries) {
|
|
91
|
-
throw new Error();
|
|
92
|
-
}
|
|
93
|
-
const deprecationNotice = response?.headers.get("X-Shopify-API-Deprecated-Reason") || "";
|
|
94
|
-
if (deprecationNotice) {
|
|
95
|
-
clientLogger({
|
|
96
|
-
type: "HTTP-Response-GraphQL-Deprecation-Notice",
|
|
97
|
-
content: {
|
|
98
|
-
requestParams,
|
|
99
|
-
deprecationNotice
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
return response;
|
|
104
|
-
} catch (error) {
|
|
105
|
-
if (nextCount <= maxTries) {
|
|
106
|
-
const retryAfter = response?.headers.get("Retry-After");
|
|
107
|
-
await sleep(retryAfter ? parseInt(retryAfter, 10) : defaultRetryWaitTime);
|
|
108
|
-
clientLogger({
|
|
109
|
-
type: "HTTP-Retry",
|
|
110
|
-
content: {
|
|
111
|
-
requestParams,
|
|
112
|
-
lastResponse: response,
|
|
113
|
-
retryAttempt: count,
|
|
114
|
-
maxRetries
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
return httpFetch(requestParams, nextCount, maxRetries);
|
|
118
|
-
}
|
|
119
|
-
throw new Error(formatErrorMessage(`${maxRetries > 0 ? `Attempted maximum number of ${maxRetries} network retries. Last message - ` : ""}${getErrorMessage(error)}`, client));
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
return httpFetch;
|
|
123
|
-
}
|
|
124
|
-
async function sleep(waitTime) {
|
|
125
|
-
return new Promise((resolve) => setTimeout(resolve, waitTime));
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// ../../node_modules/@shopify/graphql-client/dist/graphql-client/graphql-client.mjs
|
|
129
|
-
function createGraphQLClient({ headers, url, customFetchApi = fetch, retries = 0, logger: logger2 }) {
|
|
130
|
-
validateRetries({ client: CLIENT, retries });
|
|
131
|
-
const config = {
|
|
132
|
-
headers,
|
|
133
|
-
url,
|
|
134
|
-
retries
|
|
135
|
-
};
|
|
136
|
-
const clientLogger = generateClientLogger(logger2);
|
|
137
|
-
const httpFetch = generateHttpFetch({
|
|
138
|
-
customFetchApi,
|
|
139
|
-
clientLogger,
|
|
140
|
-
defaultRetryWaitTime: RETRY_WAIT_TIME
|
|
141
|
-
});
|
|
142
|
-
const fetchFn = generateFetch(httpFetch, config);
|
|
143
|
-
const request = generateRequest(fetchFn);
|
|
144
|
-
const requestStream = generateRequestStream(fetchFn);
|
|
145
|
-
return {
|
|
146
|
-
config,
|
|
147
|
-
fetch: fetchFn,
|
|
148
|
-
request,
|
|
149
|
-
requestStream
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
function generateClientLogger(logger2) {
|
|
153
|
-
return (logContent) => {
|
|
154
|
-
if (logger2) {
|
|
155
|
-
logger2(logContent);
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
async function processJSONResponse(response) {
|
|
160
|
-
const { errors, data, extensions } = await response.json();
|
|
161
|
-
return {
|
|
162
|
-
...getKeyValueIfValid("data", data),
|
|
163
|
-
...getKeyValueIfValid("extensions", extensions),
|
|
164
|
-
headers: response.headers,
|
|
165
|
-
...errors || !data ? {
|
|
166
|
-
errors: {
|
|
167
|
-
networkStatusCode: response.status,
|
|
168
|
-
message: formatErrorMessage(errors ? GQL_API_ERROR : NO_DATA_OR_ERRORS_ERROR),
|
|
169
|
-
...getKeyValueIfValid("graphQLErrors", errors),
|
|
170
|
-
response
|
|
171
|
-
}
|
|
172
|
-
} : {}
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
function generateFetch(httpFetch, { url, headers, retries }) {
|
|
176
|
-
return async (operation, options = {}) => {
|
|
177
|
-
const { variables, headers: overrideHeaders, url: overrideUrl, retries: overrideRetries, keepalive, signal } = options;
|
|
178
|
-
const body = JSON.stringify({
|
|
179
|
-
query: operation,
|
|
180
|
-
variables
|
|
181
|
-
});
|
|
182
|
-
validateRetries({ client: CLIENT, retries: overrideRetries });
|
|
183
|
-
const flatHeaders = Object.entries({
|
|
184
|
-
...headers,
|
|
185
|
-
...overrideHeaders
|
|
186
|
-
}).reduce((headers2, [key, value]) => {
|
|
187
|
-
headers2[key] = Array.isArray(value) ? value.join(", ") : value.toString();
|
|
188
|
-
return headers2;
|
|
189
|
-
}, {});
|
|
190
|
-
if (!flatHeaders[SDK_VARIANT_HEADER] && !flatHeaders[SDK_VERSION_HEADER]) {
|
|
191
|
-
flatHeaders[SDK_VARIANT_HEADER] = DEFAULT_SDK_VARIANT;
|
|
192
|
-
flatHeaders[SDK_VERSION_HEADER] = DEFAULT_CLIENT_VERSION;
|
|
193
|
-
}
|
|
194
|
-
const fetchParams = [
|
|
195
|
-
overrideUrl ?? url,
|
|
196
|
-
{
|
|
197
|
-
method: "POST",
|
|
198
|
-
headers: flatHeaders,
|
|
199
|
-
body,
|
|
200
|
-
signal,
|
|
201
|
-
keepalive
|
|
202
|
-
}
|
|
203
|
-
];
|
|
204
|
-
return httpFetch(fetchParams, 1, overrideRetries ?? retries);
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
function generateRequest(fetchFn) {
|
|
208
|
-
return async (...props) => {
|
|
209
|
-
if (DEFER_OPERATION_REGEX.test(props[0])) {
|
|
210
|
-
throw new Error(formatErrorMessage("This operation will result in a streamable response - use requestStream() instead."));
|
|
211
|
-
}
|
|
212
|
-
let response = null;
|
|
213
|
-
try {
|
|
214
|
-
response = await fetchFn(...props);
|
|
215
|
-
const { status, statusText } = response;
|
|
216
|
-
const contentType = response.headers.get("content-type") || "";
|
|
217
|
-
if (!response.ok) {
|
|
218
|
-
return {
|
|
219
|
-
errors: {
|
|
220
|
-
networkStatusCode: status,
|
|
221
|
-
message: formatErrorMessage(statusText),
|
|
222
|
-
response
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
if (!contentType.includes(CONTENT_TYPES.json)) {
|
|
227
|
-
return {
|
|
228
|
-
errors: {
|
|
229
|
-
networkStatusCode: status,
|
|
230
|
-
message: formatErrorMessage(`${UNEXPECTED_CONTENT_TYPE_ERROR} ${contentType}`),
|
|
231
|
-
response
|
|
232
|
-
}
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
return await processJSONResponse(response);
|
|
236
|
-
} catch (error) {
|
|
237
|
-
return {
|
|
238
|
-
errors: {
|
|
239
|
-
message: getErrorMessage(error),
|
|
240
|
-
...response == null ? {} : {
|
|
241
|
-
networkStatusCode: response.status,
|
|
242
|
-
response
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
async function* getStreamBodyIterator(response) {
|
|
250
|
-
const decoder = new TextDecoder();
|
|
251
|
-
if (response.body[Symbol.asyncIterator]) {
|
|
252
|
-
for await (const chunk of response.body) {
|
|
253
|
-
yield decoder.decode(chunk);
|
|
254
|
-
}
|
|
255
|
-
} else {
|
|
256
|
-
const reader = response.body.getReader();
|
|
257
|
-
let readResult;
|
|
258
|
-
try {
|
|
259
|
-
while (!(readResult = await reader.read()).done) {
|
|
260
|
-
yield decoder.decode(readResult.value);
|
|
261
|
-
}
|
|
262
|
-
} finally {
|
|
263
|
-
reader.cancel();
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
function readStreamChunk(streamBodyIterator, boundary) {
|
|
268
|
-
return {
|
|
269
|
-
async *[Symbol.asyncIterator]() {
|
|
270
|
-
try {
|
|
271
|
-
let buffer = "";
|
|
272
|
-
for await (const textChunk of streamBodyIterator) {
|
|
273
|
-
buffer += textChunk;
|
|
274
|
-
if (buffer.indexOf(boundary) > -1) {
|
|
275
|
-
const lastBoundaryIndex = buffer.lastIndexOf(boundary);
|
|
276
|
-
const fullResponses = buffer.slice(0, lastBoundaryIndex);
|
|
277
|
-
const chunkBodies = fullResponses.split(boundary).filter((chunk) => chunk.trim().length > 0).map((chunk) => {
|
|
278
|
-
const body = chunk.slice(chunk.indexOf(HEADER_SEPARATOR) + HEADER_SEPARATOR.length).trim();
|
|
279
|
-
return body;
|
|
280
|
-
});
|
|
281
|
-
if (chunkBodies.length > 0) {
|
|
282
|
-
yield chunkBodies;
|
|
283
|
-
}
|
|
284
|
-
buffer = buffer.slice(lastBoundaryIndex + boundary.length);
|
|
285
|
-
if (buffer.trim() === `--`) {
|
|
286
|
-
buffer = "";
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
} catch (error) {
|
|
291
|
-
throw new Error(`Error occured while processing stream payload - ${getErrorMessage(error)}`);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
};
|
|
295
|
-
}
|
|
296
|
-
function createJsonResponseAsyncIterator(response) {
|
|
297
|
-
return {
|
|
298
|
-
async *[Symbol.asyncIterator]() {
|
|
299
|
-
const processedResponse = await processJSONResponse(response);
|
|
300
|
-
yield {
|
|
301
|
-
...processedResponse,
|
|
302
|
-
hasNext: false
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
};
|
|
306
|
-
}
|
|
307
|
-
function getResponseDataFromChunkBodies(chunkBodies) {
|
|
308
|
-
return chunkBodies.map((value) => {
|
|
309
|
-
try {
|
|
310
|
-
return JSON.parse(value);
|
|
311
|
-
} catch (error) {
|
|
312
|
-
throw new Error(`Error in parsing multipart response - ${getErrorMessage(error)}`);
|
|
313
|
-
}
|
|
314
|
-
}).map((payload) => {
|
|
315
|
-
const { data, incremental, hasNext, extensions, errors } = payload;
|
|
316
|
-
if (!incremental) {
|
|
317
|
-
return {
|
|
318
|
-
data: data || {},
|
|
319
|
-
...getKeyValueIfValid("errors", errors),
|
|
320
|
-
...getKeyValueIfValid("extensions", extensions),
|
|
321
|
-
hasNext
|
|
322
|
-
};
|
|
323
|
-
}
|
|
324
|
-
const incrementalArray = incremental.map(({ data: data2, path, errors: errors2 }) => {
|
|
325
|
-
return {
|
|
326
|
-
data: data2 && path ? buildDataObjectByPath(path, data2) : {},
|
|
327
|
-
...getKeyValueIfValid("errors", errors2)
|
|
328
|
-
};
|
|
329
|
-
});
|
|
330
|
-
return {
|
|
331
|
-
data: incrementalArray.length === 1 ? incrementalArray[0].data : buildCombinedDataObject([
|
|
332
|
-
...incrementalArray.map(({ data: data2 }) => data2)
|
|
333
|
-
]),
|
|
334
|
-
...getKeyValueIfValid("errors", combineErrors(incrementalArray)),
|
|
335
|
-
hasNext
|
|
336
|
-
};
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
function validateResponseData(responseErrors, combinedData) {
|
|
340
|
-
if (responseErrors.length > 0) {
|
|
341
|
-
throw new Error(GQL_API_ERROR, {
|
|
342
|
-
cause: {
|
|
343
|
-
graphQLErrors: responseErrors
|
|
344
|
-
}
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
if (Object.keys(combinedData).length === 0) {
|
|
348
|
-
throw new Error(NO_DATA_OR_ERRORS_ERROR);
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
function createMultipartResponseAsyncInterator(response, responseContentType) {
|
|
352
|
-
const boundaryHeader = (responseContentType ?? "").match(BOUNDARY_HEADER_REGEX);
|
|
353
|
-
const boundary = `--${boundaryHeader ? boundaryHeader[1] : "-"}`;
|
|
354
|
-
if (!response.body?.getReader && !response.body?.[Symbol.asyncIterator]) {
|
|
355
|
-
throw new Error("API multipart response did not return an iterable body", {
|
|
356
|
-
cause: response
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
const streamBodyIterator = getStreamBodyIterator(response);
|
|
360
|
-
let combinedData = {};
|
|
361
|
-
let responseExtensions;
|
|
362
|
-
return {
|
|
363
|
-
async *[Symbol.asyncIterator]() {
|
|
364
|
-
try {
|
|
365
|
-
let streamHasNext = true;
|
|
366
|
-
for await (const chunkBodies of readStreamChunk(streamBodyIterator, boundary)) {
|
|
367
|
-
const responseData = getResponseDataFromChunkBodies(chunkBodies);
|
|
368
|
-
responseExtensions = responseData.find((datum) => datum.extensions)?.extensions ?? responseExtensions;
|
|
369
|
-
const responseErrors = combineErrors(responseData);
|
|
370
|
-
combinedData = buildCombinedDataObject([
|
|
371
|
-
combinedData,
|
|
372
|
-
...responseData.map(({ data }) => data)
|
|
373
|
-
]);
|
|
374
|
-
streamHasNext = responseData.slice(-1)[0].hasNext;
|
|
375
|
-
validateResponseData(responseErrors, combinedData);
|
|
376
|
-
yield {
|
|
377
|
-
...getKeyValueIfValid("data", combinedData),
|
|
378
|
-
...getKeyValueIfValid("extensions", responseExtensions),
|
|
379
|
-
hasNext: streamHasNext
|
|
380
|
-
};
|
|
381
|
-
}
|
|
382
|
-
if (streamHasNext) {
|
|
383
|
-
throw new Error(`Response stream terminated unexpectedly`);
|
|
384
|
-
}
|
|
385
|
-
} catch (error) {
|
|
386
|
-
const cause = getErrorCause(error);
|
|
387
|
-
yield {
|
|
388
|
-
...getKeyValueIfValid("data", combinedData),
|
|
389
|
-
...getKeyValueIfValid("extensions", responseExtensions),
|
|
390
|
-
errors: {
|
|
391
|
-
message: formatErrorMessage(getErrorMessage(error)),
|
|
392
|
-
networkStatusCode: response.status,
|
|
393
|
-
...getKeyValueIfValid("graphQLErrors", cause?.graphQLErrors),
|
|
394
|
-
response
|
|
395
|
-
},
|
|
396
|
-
hasNext: false
|
|
397
|
-
};
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
};
|
|
401
|
-
}
|
|
402
|
-
function generateRequestStream(fetchFn) {
|
|
403
|
-
return async (...props) => {
|
|
404
|
-
if (!DEFER_OPERATION_REGEX.test(props[0])) {
|
|
405
|
-
throw new Error(formatErrorMessage("This operation does not result in a streamable response - use request() instead."));
|
|
406
|
-
}
|
|
407
|
-
try {
|
|
408
|
-
const response = await fetchFn(...props);
|
|
409
|
-
const { statusText } = response;
|
|
410
|
-
if (!response.ok) {
|
|
411
|
-
throw new Error(statusText, { cause: response });
|
|
412
|
-
}
|
|
413
|
-
const responseContentType = response.headers.get("content-type") || "";
|
|
414
|
-
switch (true) {
|
|
415
|
-
case responseContentType.includes(CONTENT_TYPES.json):
|
|
416
|
-
return createJsonResponseAsyncIterator(response);
|
|
417
|
-
case responseContentType.includes(CONTENT_TYPES.multipart):
|
|
418
|
-
return createMultipartResponseAsyncInterator(response, responseContentType);
|
|
419
|
-
default:
|
|
420
|
-
throw new Error(`${UNEXPECTED_CONTENT_TYPE_ERROR} ${responseContentType}`, { cause: response });
|
|
421
|
-
}
|
|
422
|
-
} catch (error) {
|
|
423
|
-
return {
|
|
424
|
-
async *[Symbol.asyncIterator]() {
|
|
425
|
-
const response = getErrorCause(error);
|
|
426
|
-
yield {
|
|
427
|
-
errors: {
|
|
428
|
-
message: formatErrorMessage(getErrorMessage(error)),
|
|
429
|
-
...getKeyValueIfValid("networkStatusCode", response?.status),
|
|
430
|
-
...getKeyValueIfValid("response", response)
|
|
431
|
-
},
|
|
432
|
-
hasNext: false
|
|
433
|
-
};
|
|
434
|
-
}
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
// ../../node_modules/@shopify/graphql-client/dist/api-client-utilities/validations.mjs
|
|
441
|
-
function validateDomainAndGetStoreUrl({ client, storeDomain }) {
|
|
442
|
-
try {
|
|
443
|
-
if (!storeDomain || typeof storeDomain !== "string") {
|
|
444
|
-
throw new Error();
|
|
445
|
-
}
|
|
446
|
-
const trimmedDomain = storeDomain.trim();
|
|
447
|
-
const protocolUrl = trimmedDomain.match(/^https?:/) ? trimmedDomain : `https://${trimmedDomain}`;
|
|
448
|
-
const url = new URL(protocolUrl);
|
|
449
|
-
url.protocol = "https";
|
|
450
|
-
return url.origin;
|
|
451
|
-
} catch (error) {
|
|
452
|
-
throw new Error(`${client}: a valid store domain ("${storeDomain}") must be provided`, { cause: error });
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
function validateApiVersion({ client, currentSupportedApiVersions, apiVersion, logger: logger2 }) {
|
|
456
|
-
const versionError = `${client}: the provided apiVersion ("${apiVersion}")`;
|
|
457
|
-
const supportedVersion = `Currently supported API versions: ${currentSupportedApiVersions.join(", ")}`;
|
|
458
|
-
if (!apiVersion || typeof apiVersion !== "string") {
|
|
459
|
-
throw new Error(`${versionError} is invalid. ${supportedVersion}`);
|
|
460
|
-
}
|
|
461
|
-
const trimmedApiVersion = apiVersion.trim();
|
|
462
|
-
if (!currentSupportedApiVersions.includes(trimmedApiVersion)) {
|
|
463
|
-
if (logger2) {
|
|
464
|
-
logger2({
|
|
465
|
-
type: "Unsupported_Api_Version",
|
|
466
|
-
content: {
|
|
467
|
-
apiVersion,
|
|
468
|
-
supportedApiVersions: currentSupportedApiVersions
|
|
469
|
-
}
|
|
470
|
-
});
|
|
471
|
-
} else {
|
|
472
|
-
console.warn(`${versionError} is likely deprecated or not supported. ${supportedVersion}`);
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
// ../../node_modules/@shopify/graphql-client/dist/api-client-utilities/api-versions.mjs
|
|
478
|
-
function getQuarterMonth(quarter) {
|
|
479
|
-
const month = quarter * 3 - 2;
|
|
480
|
-
return month === 10 ? month : `0${month}`;
|
|
481
|
-
}
|
|
482
|
-
function getPrevousVersion(year, quarter, nQuarter) {
|
|
483
|
-
const versionQuarter = quarter - nQuarter;
|
|
484
|
-
if (versionQuarter <= 0) {
|
|
485
|
-
return `${year - 1}-${getQuarterMonth(versionQuarter + 4)}`;
|
|
486
|
-
}
|
|
487
|
-
return `${year}-${getQuarterMonth(versionQuarter)}`;
|
|
488
|
-
}
|
|
489
|
-
function getCurrentApiVersion() {
|
|
490
|
-
const date = /* @__PURE__ */ new Date();
|
|
491
|
-
const month = date.getUTCMonth();
|
|
492
|
-
const year = date.getUTCFullYear();
|
|
493
|
-
const quarter = Math.floor(month / 3 + 1);
|
|
494
|
-
return {
|
|
495
|
-
year,
|
|
496
|
-
quarter,
|
|
497
|
-
version: `${year}-${getQuarterMonth(quarter)}`
|
|
498
|
-
};
|
|
499
|
-
}
|
|
500
|
-
function getCurrentSupportedApiVersions() {
|
|
501
|
-
const { year, quarter, version: currentVersion } = getCurrentApiVersion();
|
|
502
|
-
const nextVersion = quarter === 4 ? `${year + 1}-01` : `${year}-${getQuarterMonth(quarter + 1)}`;
|
|
503
|
-
return [
|
|
504
|
-
getPrevousVersion(year, quarter, 3),
|
|
505
|
-
getPrevousVersion(year, quarter, 2),
|
|
506
|
-
getPrevousVersion(year, quarter, 1),
|
|
507
|
-
currentVersion,
|
|
508
|
-
nextVersion,
|
|
509
|
-
"unstable"
|
|
510
|
-
];
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
// ../../node_modules/@shopify/graphql-client/dist/api-client-utilities/utilities.mjs
|
|
514
|
-
function generateGetHeaders(config) {
|
|
515
|
-
return (customHeaders) => {
|
|
516
|
-
return { ...customHeaders ?? {}, ...config.headers };
|
|
517
|
-
};
|
|
518
|
-
}
|
|
519
|
-
function generateGetGQLClientParams({ getHeaders, getApiUrl }) {
|
|
520
|
-
return (operation, options) => {
|
|
521
|
-
const props = [operation];
|
|
522
|
-
if (options && Object.keys(options).length > 0) {
|
|
523
|
-
const { variables, apiVersion: propApiVersion, headers, retries, signal } = options;
|
|
524
|
-
props.push({
|
|
525
|
-
...variables ? { variables } : {},
|
|
526
|
-
...headers ? { headers: getHeaders(headers) } : {},
|
|
527
|
-
...propApiVersion ? { url: getApiUrl(propApiVersion) } : {},
|
|
528
|
-
...retries ? { retries } : {},
|
|
529
|
-
...signal ? { signal } : {}
|
|
530
|
-
});
|
|
531
|
-
}
|
|
532
|
-
return props;
|
|
533
|
-
};
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
// ../../node_modules/@shopify/storefront-api-client/dist/constants.mjs
|
|
537
|
-
var DEFAULT_CONTENT_TYPE = "application/json";
|
|
538
|
-
var DEFAULT_SDK_VARIANT2 = "storefront-api-client";
|
|
539
|
-
var DEFAULT_CLIENT_VERSION2 = "1.0.9";
|
|
540
|
-
var PUBLIC_ACCESS_TOKEN_HEADER = "X-Shopify-Storefront-Access-Token";
|
|
541
|
-
var PRIVATE_ACCESS_TOKEN_HEADER = "Shopify-Storefront-Private-Token";
|
|
542
|
-
var SDK_VARIANT_HEADER2 = "X-SDK-Variant";
|
|
543
|
-
var SDK_VERSION_HEADER2 = "X-SDK-Version";
|
|
544
|
-
var SDK_VARIANT_SOURCE_HEADER = "X-SDK-Variant-Source";
|
|
545
|
-
var CLIENT2 = "Storefront API Client";
|
|
546
|
-
|
|
547
|
-
// ../../node_modules/@shopify/storefront-api-client/dist/validations.mjs
|
|
548
|
-
function validatePrivateAccessTokenUsage(privateAccessToken) {
|
|
549
|
-
if (privateAccessToken && typeof window !== "undefined") {
|
|
550
|
-
throw new Error(`${CLIENT2}: private access tokens and headers should only be used in a server-to-server implementation. Use the public API access token in nonserver environments.`);
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
function validateRequiredAccessTokens(publicAccessToken, privateAccessToken) {
|
|
554
|
-
if (!publicAccessToken && !privateAccessToken) {
|
|
555
|
-
throw new Error(`${CLIENT2}: a public or private access token must be provided`);
|
|
556
|
-
}
|
|
557
|
-
if (publicAccessToken && privateAccessToken) {
|
|
558
|
-
throw new Error(`${CLIENT2}: only provide either a public or private access token`);
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
// ../../node_modules/@shopify/storefront-api-client/dist/storefront-api-client.mjs
|
|
563
|
-
function createStorefrontApiClient({ storeDomain, apiVersion, publicAccessToken, privateAccessToken, clientName, retries = 0, customFetchApi, logger: logger2 }) {
|
|
564
|
-
const currentSupportedApiVersions = getCurrentSupportedApiVersions();
|
|
565
|
-
const storeUrl = validateDomainAndGetStoreUrl({
|
|
566
|
-
client: CLIENT2,
|
|
567
|
-
storeDomain
|
|
568
|
-
});
|
|
569
|
-
const baseApiVersionValidationParams = {
|
|
570
|
-
client: CLIENT2,
|
|
571
|
-
currentSupportedApiVersions,
|
|
572
|
-
logger: logger2
|
|
573
|
-
};
|
|
574
|
-
validateApiVersion({ ...baseApiVersionValidationParams, apiVersion });
|
|
575
|
-
validateRequiredAccessTokens(publicAccessToken, privateAccessToken);
|
|
576
|
-
validatePrivateAccessTokenUsage(privateAccessToken);
|
|
577
|
-
const apiUrlFormatter = generateApiUrlFormatter(storeUrl, apiVersion, baseApiVersionValidationParams);
|
|
578
|
-
const config = {
|
|
579
|
-
storeDomain: storeUrl,
|
|
580
|
-
apiVersion,
|
|
581
|
-
...publicAccessToken ? { publicAccessToken } : {
|
|
582
|
-
privateAccessToken
|
|
583
|
-
},
|
|
584
|
-
headers: {
|
|
585
|
-
"Content-Type": DEFAULT_CONTENT_TYPE,
|
|
586
|
-
Accept: DEFAULT_CONTENT_TYPE,
|
|
587
|
-
[SDK_VARIANT_HEADER2]: DEFAULT_SDK_VARIANT2,
|
|
588
|
-
[SDK_VERSION_HEADER2]: DEFAULT_CLIENT_VERSION2,
|
|
589
|
-
...clientName ? { [SDK_VARIANT_SOURCE_HEADER]: clientName } : {},
|
|
590
|
-
...publicAccessToken ? { [PUBLIC_ACCESS_TOKEN_HEADER]: publicAccessToken } : { [PRIVATE_ACCESS_TOKEN_HEADER]: privateAccessToken }
|
|
591
|
-
},
|
|
592
|
-
apiUrl: apiUrlFormatter(),
|
|
593
|
-
clientName
|
|
594
|
-
};
|
|
595
|
-
const graphqlClient = createGraphQLClient({
|
|
596
|
-
headers: config.headers,
|
|
597
|
-
url: config.apiUrl,
|
|
598
|
-
retries,
|
|
599
|
-
customFetchApi,
|
|
600
|
-
logger: logger2
|
|
601
|
-
});
|
|
602
|
-
const getHeaders = generateGetHeaders(config);
|
|
603
|
-
const getApiUrl = generateGetApiUrl(config, apiUrlFormatter);
|
|
604
|
-
const getGQLClientParams = generateGetGQLClientParams({
|
|
605
|
-
getHeaders,
|
|
606
|
-
getApiUrl
|
|
607
|
-
});
|
|
608
|
-
const client = {
|
|
609
|
-
config,
|
|
610
|
-
getHeaders,
|
|
611
|
-
getApiUrl,
|
|
612
|
-
fetch: (...props) => {
|
|
613
|
-
return graphqlClient.fetch(...getGQLClientParams(...props));
|
|
614
|
-
},
|
|
615
|
-
request: (...props) => {
|
|
616
|
-
return graphqlClient.request(...getGQLClientParams(...props));
|
|
617
|
-
},
|
|
618
|
-
requestStream: (...props) => {
|
|
619
|
-
return graphqlClient.requestStream(...getGQLClientParams(...props));
|
|
620
|
-
}
|
|
621
|
-
};
|
|
622
|
-
return Object.freeze(client);
|
|
623
|
-
}
|
|
624
|
-
function generateApiUrlFormatter(storeUrl, defaultApiVersion, baseApiVersionValidationParams) {
|
|
625
|
-
return (apiVersion) => {
|
|
626
|
-
if (apiVersion) {
|
|
627
|
-
validateApiVersion({
|
|
628
|
-
...baseApiVersionValidationParams,
|
|
629
|
-
apiVersion
|
|
630
|
-
});
|
|
631
|
-
}
|
|
632
|
-
const urlApiVersion = (apiVersion ?? defaultApiVersion).trim();
|
|
633
|
-
return `${storeUrl}/api/${urlApiVersion}/graphql.json`;
|
|
634
|
-
};
|
|
635
|
-
}
|
|
636
|
-
function generateGetApiUrl(config, apiUrlFormatter) {
|
|
637
|
-
return (propApiVersion) => {
|
|
638
|
-
return propApiVersion ? apiUrlFormatter(propApiVersion) : config.apiUrl;
|
|
639
|
-
};
|
|
640
|
-
}
|
|
3
|
+
var storefrontApiClient = require('@shopify/storefront-api-client');
|
|
4
|
+
var cacheTags = require('@shopkit/core/cache-tags');
|
|
641
5
|
|
|
642
6
|
// src/adapters/shopify/client.ts
|
|
643
7
|
function createShopifyStorefrontClient(config) {
|
|
644
8
|
const { domain, storefrontAccessToken, apiVersion = "2025-04" } = config;
|
|
645
|
-
const nativeClient = createStorefrontApiClient({
|
|
9
|
+
const nativeClient = storefrontApiClient.createStorefrontApiClient({
|
|
646
10
|
storeDomain: domain,
|
|
647
11
|
apiVersion,
|
|
648
12
|
publicAccessToken: storefrontAccessToken
|
|
@@ -806,6 +170,12 @@ function formatResponse(params) {
|
|
|
806
170
|
};
|
|
807
171
|
}
|
|
808
172
|
|
|
173
|
+
// src/core/request-timeout.ts
|
|
174
|
+
var DEFAULT_REQUEST_TIMEOUT_MS = 1e4;
|
|
175
|
+
function resolveRequestSignal(options, defaultMs = DEFAULT_REQUEST_TIMEOUT_MS) {
|
|
176
|
+
return options?.signal ?? AbortSignal.timeout(options?.timeoutMs ?? defaultMs);
|
|
177
|
+
}
|
|
178
|
+
|
|
809
179
|
// src/core/client.ts
|
|
810
180
|
var NotImplementedError = class extends Error {
|
|
811
181
|
constructor(adapterName, methodName) {
|
|
@@ -1157,7 +527,10 @@ var BaseCommerceClient = class {
|
|
|
1157
527
|
async fetchRequest(params) {
|
|
1158
528
|
try {
|
|
1159
529
|
const { url, ...fetchOptions } = params;
|
|
1160
|
-
const response = await fetch(url,
|
|
530
|
+
const response = await fetch(url, {
|
|
531
|
+
...fetchOptions,
|
|
532
|
+
signal: resolveRequestSignal(fetchOptions)
|
|
533
|
+
});
|
|
1161
534
|
const data = await response.json();
|
|
1162
535
|
if (response.ok) {
|
|
1163
536
|
return formatResponse({
|
|
@@ -4289,7 +3662,7 @@ var getCommonHeaders = (config) => {
|
|
|
4289
3662
|
// src/adapters/custom/services/base.ts
|
|
4290
3663
|
var BaseCustomService = class {
|
|
4291
3664
|
constructor(baseUrl, config, logger2) {
|
|
4292
|
-
this.timeout =
|
|
3665
|
+
this.timeout = DEFAULT_REQUEST_TIMEOUT_MS;
|
|
4293
3666
|
this.baseUrl = baseUrl;
|
|
4294
3667
|
this.config = config;
|
|
4295
3668
|
this.logger = logger2 || createDomainLogger("custom", "custom", { enabled: false });
|
|
@@ -4326,8 +3699,15 @@ var BaseCustomService = class {
|
|
|
4326
3699
|
}
|
|
4327
3700
|
const requestOptions = {
|
|
4328
3701
|
method,
|
|
4329
|
-
headers
|
|
3702
|
+
headers,
|
|
3703
|
+
// Bound the upstream wait — see core/request-timeout.ts for the policy and
|
|
3704
|
+
// rationale (Bblunt prod memory-leak postmortem, Jun 2026).
|
|
3705
|
+
signal: resolveRequestSignal(options, this.timeout),
|
|
3706
|
+
...options?.cache ? { cache: options.cache } : {}
|
|
4330
3707
|
};
|
|
3708
|
+
if (options?.next) {
|
|
3709
|
+
requestOptions.next = options.next;
|
|
3710
|
+
}
|
|
4331
3711
|
if (data && method !== "GET") {
|
|
4332
3712
|
requestOptions.body = JSON.stringify(data);
|
|
4333
3713
|
}
|
|
@@ -4595,7 +3975,15 @@ var ProductService2 = class extends BaseCustomService {
|
|
|
4595
3975
|
PRODUCT_ENDPOINTS.getCollections(options?.version),
|
|
4596
3976
|
"GET",
|
|
4597
3977
|
void 0,
|
|
4598
|
-
{
|
|
3978
|
+
{
|
|
3979
|
+
params: queryParams,
|
|
3980
|
+
...options || {},
|
|
3981
|
+
// Broad collections tag so a catalog-wide change can revalidate the list.
|
|
3982
|
+
next: {
|
|
3983
|
+
...options?.next || {},
|
|
3984
|
+
tags: [cacheTags.CacheTags.collections(), ...options?.next?.tags || []]
|
|
3985
|
+
}
|
|
3986
|
+
}
|
|
4599
3987
|
);
|
|
4600
3988
|
if (!result || !result.collections) {
|
|
4601
3989
|
return formatResponse({
|
|
@@ -4645,7 +4033,18 @@ var ProductService2 = class extends BaseCustomService {
|
|
|
4645
4033
|
const apiUrl = handle ? PRODUCT_ENDPOINTS.getCollectionByHandle(options?.version, handle) : PRODUCT_ENDPOINTS.getCollectionById(options?.version, id);
|
|
4646
4034
|
const result = await this.request(apiUrl, "GET", void 0, {
|
|
4647
4035
|
params: queryParams,
|
|
4648
|
-
...options || {}
|
|
4036
|
+
...options || {},
|
|
4037
|
+
// Tag the collection read so a webhook can revalidateTag() it, refreshing
|
|
4038
|
+
// the collection page AND every grid built from it. GET only — Next caches
|
|
4039
|
+
// GET; the handle keys the entity. See @shopkit/core/cache-tags.
|
|
4040
|
+
next: {
|
|
4041
|
+
...options?.next || {},
|
|
4042
|
+
tags: [
|
|
4043
|
+
cacheTags.CacheTags.collections(),
|
|
4044
|
+
...handle ? [cacheTags.CacheTags.collection(handle)] : [],
|
|
4045
|
+
...options?.next?.tags || []
|
|
4046
|
+
]
|
|
4047
|
+
}
|
|
4649
4048
|
});
|
|
4650
4049
|
if (!result) {
|
|
4651
4050
|
return formatResponse({
|