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