@01.software/sdk 0.1.5 → 0.1.6
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 +90 -7
- package/dist/{auth-DNaV-5wt.d.ts → auth-BIQrgvB-.d.cts} +12 -1
- package/dist/{auth-DMvniYSs.d.cts → auth-CRlgtFpv.d.ts} +12 -1
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.d.cts +2 -2
- package/dist/auth.d.ts +2 -2
- package/dist/auth.js.map +1 -1
- package/dist/index.cjs +815 -296
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +342 -46
- package/dist/index.d.ts +342 -46
- package/dist/index.js +815 -296
- package/dist/index.js.map +1 -1
- package/dist/{payload-types-BAZCcssT.d.cts → payload-types-XZDjUjRT.d.cts} +615 -11
- package/dist/{payload-types-BAZCcssT.d.ts → payload-types-XZDjUjRT.d.ts} +615 -11
- package/dist/{webhook-DdNkaI-f.d.cts → webhook-BkbRPVAd.d.cts} +2 -2
- package/dist/{webhook-CYJ0OPds.d.ts → webhook-CFVJAqOx.d.ts} +2 -2
- package/dist/webhook.cjs +6 -4
- package/dist/webhook.cjs.map +1 -1
- package/dist/webhook.d.cts +2 -2
- package/dist/webhook.d.ts +2 -2
- package/dist/webhook.js +6 -4
- package/dist/webhook.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -51,119 +51,8 @@ var __async = (__this, __arguments, generator) => {
|
|
|
51
51
|
});
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
-
// src/core/
|
|
55
|
-
|
|
56
|
-
constructor(api, collection) {
|
|
57
|
-
this.api = api;
|
|
58
|
-
this.collection = collection;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Find documents (list query)
|
|
62
|
-
* GET /api/{collection}
|
|
63
|
-
* @returns Payload CMS find response with docs array and pagination
|
|
64
|
-
*/
|
|
65
|
-
find(options) {
|
|
66
|
-
return __async(this, null, function* () {
|
|
67
|
-
return this.api.requestFind(
|
|
68
|
-
`/api/${String(this.collection)}`,
|
|
69
|
-
options
|
|
70
|
-
);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Find document by ID
|
|
75
|
-
* GET /api/{collection}/{id}
|
|
76
|
-
* @returns Document object directly (no wrapper)
|
|
77
|
-
*/
|
|
78
|
-
findById(id, options) {
|
|
79
|
-
return __async(this, null, function* () {
|
|
80
|
-
return this.api.requestFindById(
|
|
81
|
-
`/api/${String(this.collection)}/${String(id)}`,
|
|
82
|
-
options
|
|
83
|
-
);
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Create a new document
|
|
88
|
-
* POST /api/{collection}
|
|
89
|
-
* @returns Payload CMS mutation response with doc and message
|
|
90
|
-
*/
|
|
91
|
-
create(data) {
|
|
92
|
-
return __async(this, null, function* () {
|
|
93
|
-
return this.api.requestCreate(
|
|
94
|
-
`/api/${String(this.collection)}`,
|
|
95
|
-
data
|
|
96
|
-
);
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Update a document by ID
|
|
101
|
-
* PATCH /api/{collection}/{id}
|
|
102
|
-
* @returns Payload CMS mutation response with doc and message
|
|
103
|
-
*/
|
|
104
|
-
update(id, data) {
|
|
105
|
-
return __async(this, null, function* () {
|
|
106
|
-
return this.api.requestUpdate(
|
|
107
|
-
`/api/${String(this.collection)}/${String(id)}`,
|
|
108
|
-
data
|
|
109
|
-
);
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Count documents
|
|
114
|
-
* GET /api/{collection}/count
|
|
115
|
-
* @returns Count response with totalDocs
|
|
116
|
-
*/
|
|
117
|
-
count(options) {
|
|
118
|
-
return __async(this, null, function* () {
|
|
119
|
-
return this.api.requestCount(
|
|
120
|
-
`/api/${String(this.collection)}/count`,
|
|
121
|
-
options
|
|
122
|
-
);
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Update multiple documents (bulk update)
|
|
127
|
-
* PATCH /api/{collection}
|
|
128
|
-
* @returns Payload CMS find response with updated docs
|
|
129
|
-
*/
|
|
130
|
-
updateMany(where, data) {
|
|
131
|
-
return __async(this, null, function* () {
|
|
132
|
-
return this.api.requestUpdateMany(
|
|
133
|
-
`/api/${String(this.collection)}`,
|
|
134
|
-
{ where, data }
|
|
135
|
-
);
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Delete a document by ID
|
|
140
|
-
* DELETE /api/{collection}/{id}
|
|
141
|
-
* @returns Deleted document object directly (no wrapper)
|
|
142
|
-
*/
|
|
143
|
-
remove(id) {
|
|
144
|
-
return __async(this, null, function* () {
|
|
145
|
-
return this.api.requestDelete(
|
|
146
|
-
`/api/${String(this.collection)}/${String(id)}`
|
|
147
|
-
);
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Delete multiple documents (bulk delete)
|
|
152
|
-
* DELETE /api/{collection}
|
|
153
|
-
* @returns Payload CMS find response with deleted docs
|
|
154
|
-
*/
|
|
155
|
-
removeMany(where) {
|
|
156
|
-
return __async(this, null, function* () {
|
|
157
|
-
return this.api.requestDeleteMany(
|
|
158
|
-
`/api/${String(this.collection)}`,
|
|
159
|
-
{ where }
|
|
160
|
-
);
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
// src/core/collection/http-client.ts
|
|
166
|
-
import { stringify } from "qs-esm";
|
|
54
|
+
// src/core/internal/utils/index.ts
|
|
55
|
+
import { SignJWT, jwtVerify, decodeJwt } from "jose";
|
|
167
56
|
|
|
168
57
|
// src/core/internal/errors/index.ts
|
|
169
58
|
var SDKError = class _SDKError extends Error {
|
|
@@ -264,112 +153,6 @@ var createConfigError = (message, details, userMessage, suggestion) => new Confi
|
|
|
264
153
|
var createTimeoutError = (message, details, userMessage, suggestion) => new TimeoutError(message, details, userMessage, suggestion);
|
|
265
154
|
var createUsageLimitError = (message, usage, details, userMessage, suggestion) => new UsageLimitError(message, usage, details, userMessage, suggestion);
|
|
266
155
|
|
|
267
|
-
// src/core/collection/http-client.ts
|
|
268
|
-
var HttpClient = class {
|
|
269
|
-
constructor(clientKey, secretKey, baseUrl) {
|
|
270
|
-
if (!clientKey) {
|
|
271
|
-
throw createValidationError("clientKey is required.");
|
|
272
|
-
}
|
|
273
|
-
this.clientKey = clientKey;
|
|
274
|
-
this.secretKey = secretKey;
|
|
275
|
-
this.baseUrl = baseUrl;
|
|
276
|
-
this.defaultOptions = { clientKey, secretKey, baseUrl };
|
|
277
|
-
}
|
|
278
|
-
buildUrl(endpoint, options) {
|
|
279
|
-
if (!options) return endpoint;
|
|
280
|
-
const queryString = stringify(options, { addQueryPrefix: true });
|
|
281
|
-
return queryString ? `${endpoint}${queryString}` : endpoint;
|
|
282
|
-
}
|
|
283
|
-
assertJsonResponse(response) {
|
|
284
|
-
const contentType = response.headers.get("content-type");
|
|
285
|
-
if (!(contentType == null ? void 0 : contentType.includes("application/json"))) {
|
|
286
|
-
throw createApiError("Response is not in JSON format.", response.status, { contentType });
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Parse Payload CMS find response (list query)
|
|
291
|
-
* Returns native Payload response structure
|
|
292
|
-
*/
|
|
293
|
-
parseFindResponse(response) {
|
|
294
|
-
return __async(this, null, function* () {
|
|
295
|
-
var _a, _b;
|
|
296
|
-
const contentType = response.headers.get("content-type");
|
|
297
|
-
try {
|
|
298
|
-
this.assertJsonResponse(response);
|
|
299
|
-
const jsonData = yield response.json();
|
|
300
|
-
if (jsonData.docs === void 0) {
|
|
301
|
-
throw createApiError("Invalid find response.", response.status, { jsonData });
|
|
302
|
-
}
|
|
303
|
-
return {
|
|
304
|
-
docs: jsonData.docs,
|
|
305
|
-
totalDocs: jsonData.totalDocs || 0,
|
|
306
|
-
limit: jsonData.limit || 20,
|
|
307
|
-
totalPages: jsonData.totalPages || 0,
|
|
308
|
-
page: jsonData.page || 1,
|
|
309
|
-
pagingCounter: jsonData.pagingCounter || 1,
|
|
310
|
-
hasPrevPage: jsonData.hasPrevPage || false,
|
|
311
|
-
hasNextPage: jsonData.hasNextPage || false,
|
|
312
|
-
prevPage: (_a = jsonData.prevPage) != null ? _a : null,
|
|
313
|
-
nextPage: (_b = jsonData.nextPage) != null ? _b : null
|
|
314
|
-
};
|
|
315
|
-
} catch (error) {
|
|
316
|
-
throw createApiError("Failed to parse response.", response.status, {
|
|
317
|
-
contentType,
|
|
318
|
-
error: error instanceof Error ? error.message : error
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Parse Payload CMS mutation response (create/update)
|
|
325
|
-
* Returns native Payload response structure
|
|
326
|
-
*/
|
|
327
|
-
parseMutationResponse(response) {
|
|
328
|
-
return __async(this, null, function* () {
|
|
329
|
-
const contentType = response.headers.get("content-type");
|
|
330
|
-
try {
|
|
331
|
-
this.assertJsonResponse(response);
|
|
332
|
-
const jsonData = yield response.json();
|
|
333
|
-
if (jsonData.doc === void 0) {
|
|
334
|
-
throw createApiError("Invalid mutation response.", response.status, { jsonData });
|
|
335
|
-
}
|
|
336
|
-
return {
|
|
337
|
-
message: jsonData.message || "",
|
|
338
|
-
doc: jsonData.doc,
|
|
339
|
-
errors: jsonData.errors
|
|
340
|
-
};
|
|
341
|
-
} catch (error) {
|
|
342
|
-
throw createApiError("Failed to parse response.", response.status, {
|
|
343
|
-
contentType,
|
|
344
|
-
error: error instanceof Error ? error.message : error
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Parse Payload CMS document response (findById/delete)
|
|
351
|
-
* Returns document directly without wrapper
|
|
352
|
-
*/
|
|
353
|
-
parseDocumentResponse(response) {
|
|
354
|
-
return __async(this, null, function* () {
|
|
355
|
-
const contentType = response.headers.get("content-type");
|
|
356
|
-
try {
|
|
357
|
-
this.assertJsonResponse(response);
|
|
358
|
-
const jsonData = yield response.json();
|
|
359
|
-
return jsonData;
|
|
360
|
-
} catch (error) {
|
|
361
|
-
throw createApiError("Failed to parse response.", response.status, {
|
|
362
|
-
contentType,
|
|
363
|
-
error: error instanceof Error ? error.message : error
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
};
|
|
369
|
-
|
|
370
|
-
// src/core/internal/utils/index.ts
|
|
371
|
-
import { SignJWT, jwtVerify, decodeJwt } from "jose";
|
|
372
|
-
|
|
373
156
|
// src/core/client/types.ts
|
|
374
157
|
var API_URLS = {
|
|
375
158
|
local: "http://localhost:3000",
|
|
@@ -395,6 +178,7 @@ function resolveApiUrl(config) {
|
|
|
395
178
|
var DEFAULT_TIMEOUT = 3e4;
|
|
396
179
|
var DEFAULT_RETRYABLE_STATUSES = [408, 429, 500, 502, 503, 504];
|
|
397
180
|
var NON_RETRYABLE_STATUSES = [401, 403, 404, 422];
|
|
181
|
+
var SAFE_METHODS = ["GET", "HEAD", "OPTIONS"];
|
|
398
182
|
function createServerToken(clientKey, secretKey, expiresIn = "1h") {
|
|
399
183
|
return __async(this, null, function* () {
|
|
400
184
|
if (!clientKey || !secretKey) {
|
|
@@ -498,6 +282,7 @@ function _fetch(url, options) {
|
|
|
498
282
|
const _a = options || {}, {
|
|
499
283
|
clientKey,
|
|
500
284
|
secretKey,
|
|
285
|
+
customerToken,
|
|
501
286
|
timeout = DEFAULT_TIMEOUT,
|
|
502
287
|
baseUrl = API_URLS.production,
|
|
503
288
|
debug,
|
|
@@ -505,6 +290,7 @@ function _fetch(url, options) {
|
|
|
505
290
|
} = _a, requestInit = __objRest(_a, [
|
|
506
291
|
"clientKey",
|
|
507
292
|
"secretKey",
|
|
293
|
+
"customerToken",
|
|
508
294
|
"timeout",
|
|
509
295
|
"baseUrl",
|
|
510
296
|
"debug",
|
|
@@ -518,6 +304,8 @@ function _fetch(url, options) {
|
|
|
518
304
|
let authToken;
|
|
519
305
|
if (secretKey && clientKey) {
|
|
520
306
|
authToken = yield createServerToken(clientKey, secretKey);
|
|
307
|
+
} else if (customerToken) {
|
|
308
|
+
authToken = customerToken;
|
|
521
309
|
}
|
|
522
310
|
let lastError;
|
|
523
311
|
for (let attempt = 0; attempt <= retryConfig.maxRetries; attempt++) {
|
|
@@ -532,9 +320,14 @@ function _fetch(url, options) {
|
|
|
532
320
|
if (!headers.has("Content-Type") && requestInit.body) {
|
|
533
321
|
headers.set("Content-Type", "application/json");
|
|
534
322
|
}
|
|
323
|
+
const redactedHeaders = Object.fromEntries(headers.entries());
|
|
324
|
+
if (redactedHeaders["authorization"]) {
|
|
325
|
+
const token = redactedHeaders["authorization"];
|
|
326
|
+
redactedHeaders["authorization"] = token.length > 15 ? `${token.slice(0, 15)}...****` : "****";
|
|
327
|
+
}
|
|
535
328
|
debugLog(debug, "request", url, {
|
|
536
329
|
method: requestInit.method || "GET",
|
|
537
|
-
headers:
|
|
330
|
+
headers: redactedHeaders,
|
|
538
331
|
attempt: attempt + 1
|
|
539
332
|
});
|
|
540
333
|
const controller = new AbortController();
|
|
@@ -578,7 +371,8 @@ function _fetch(url, options) {
|
|
|
578
371
|
`Request failed (status: ${response.status})`,
|
|
579
372
|
getErrorSuggestion(response.status)
|
|
580
373
|
);
|
|
581
|
-
|
|
374
|
+
const method = (requestInit.method || "GET").toUpperCase();
|
|
375
|
+
if (attempt < retryConfig.maxRetries && SAFE_METHODS.includes(method) && retryConfig.retryableStatuses.includes(response.status)) {
|
|
582
376
|
lastError = error;
|
|
583
377
|
const retryDelay = retryConfig.retryDelay(attempt);
|
|
584
378
|
debugLog(debug, "error", `Retrying in ${retryDelay}ms...`, error);
|
|
@@ -590,6 +384,8 @@ function _fetch(url, options) {
|
|
|
590
384
|
return response;
|
|
591
385
|
} catch (error) {
|
|
592
386
|
debugLog(debug, "error", url, error);
|
|
387
|
+
const method = (requestInit.method || "GET").toUpperCase();
|
|
388
|
+
const isSafe = SAFE_METHODS.includes(method);
|
|
593
389
|
if (error instanceof Error && error.name === "AbortError") {
|
|
594
390
|
const timeoutError = createTimeoutError(
|
|
595
391
|
`Request timed out after ${timeout}ms.`,
|
|
@@ -597,7 +393,7 @@ function _fetch(url, options) {
|
|
|
597
393
|
"The request timed out.",
|
|
598
394
|
"Please check your network connection or try again later."
|
|
599
395
|
);
|
|
600
|
-
if (attempt < retryConfig.maxRetries) {
|
|
396
|
+
if (isSafe && attempt < retryConfig.maxRetries) {
|
|
601
397
|
lastError = timeoutError;
|
|
602
398
|
yield delay(retryConfig.retryDelay(attempt));
|
|
603
399
|
continue;
|
|
@@ -612,7 +408,7 @@ function _fetch(url, options) {
|
|
|
612
408
|
"Network connection failed.",
|
|
613
409
|
"Please check your internet connection and try again."
|
|
614
410
|
);
|
|
615
|
-
if (attempt < retryConfig.maxRetries) {
|
|
411
|
+
if (isSafe && attempt < retryConfig.maxRetries) {
|
|
616
412
|
lastError = networkError;
|
|
617
413
|
yield delay(retryConfig.retryDelay(attempt));
|
|
618
414
|
continue;
|
|
@@ -620,7 +416,7 @@ function _fetch(url, options) {
|
|
|
620
416
|
throw networkError;
|
|
621
417
|
}
|
|
622
418
|
if (error instanceof NetworkError || error instanceof TimeoutError) {
|
|
623
|
-
if (attempt < retryConfig.maxRetries && error.status && !NON_RETRYABLE_STATUSES.includes(error.status) && retryConfig.retryableStatuses.includes(error.status)) {
|
|
419
|
+
if (isSafe && attempt < retryConfig.maxRetries && error.status && !NON_RETRYABLE_STATUSES.includes(error.status) && retryConfig.retryableStatuses.includes(error.status)) {
|
|
624
420
|
lastError = error;
|
|
625
421
|
yield delay(retryConfig.retryDelay(attempt));
|
|
626
422
|
continue;
|
|
@@ -634,7 +430,7 @@ function _fetch(url, options) {
|
|
|
634
430
|
"An unknown error occurred.",
|
|
635
431
|
"Please try again later."
|
|
636
432
|
);
|
|
637
|
-
if (attempt < retryConfig.maxRetries) {
|
|
433
|
+
if (isSafe && attempt < retryConfig.maxRetries) {
|
|
638
434
|
lastError = unknownError;
|
|
639
435
|
yield delay(retryConfig.retryDelay(attempt));
|
|
640
436
|
continue;
|
|
@@ -642,14 +438,427 @@ function _fetch(url, options) {
|
|
|
642
438
|
throw unknownError;
|
|
643
439
|
}
|
|
644
440
|
}
|
|
645
|
-
throw lastError;
|
|
441
|
+
throw lastError != null ? lastError : new NetworkError("Request failed after retries");
|
|
646
442
|
});
|
|
647
443
|
}
|
|
648
444
|
|
|
445
|
+
// src/core/api/order-api.ts
|
|
446
|
+
var OrderApi = class {
|
|
447
|
+
constructor(options) {
|
|
448
|
+
if (!options.clientKey) {
|
|
449
|
+
throw createConfigError("clientKey is required for OrderApi.");
|
|
450
|
+
}
|
|
451
|
+
if (!options.secretKey) {
|
|
452
|
+
throw createConfigError("secretKey is required for OrderApi.");
|
|
453
|
+
}
|
|
454
|
+
this.clientKey = options.clientKey;
|
|
455
|
+
this.secretKey = options.secretKey;
|
|
456
|
+
this.baseUrl = options.baseUrl;
|
|
457
|
+
}
|
|
458
|
+
request(endpoint, body) {
|
|
459
|
+
return __async(this, null, function* () {
|
|
460
|
+
const response = yield _fetch(endpoint, {
|
|
461
|
+
method: "POST",
|
|
462
|
+
clientKey: this.clientKey,
|
|
463
|
+
secretKey: this.secretKey,
|
|
464
|
+
baseUrl: this.baseUrl,
|
|
465
|
+
body: JSON.stringify(body)
|
|
466
|
+
});
|
|
467
|
+
let data;
|
|
468
|
+
try {
|
|
469
|
+
data = yield response.json();
|
|
470
|
+
} catch (e) {
|
|
471
|
+
throw createApiError(
|
|
472
|
+
`Invalid JSON response from ${endpoint}`,
|
|
473
|
+
response.status,
|
|
474
|
+
void 0,
|
|
475
|
+
"Server returned an invalid response.",
|
|
476
|
+
"Check if the API endpoint is available."
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
if (data.error) {
|
|
480
|
+
const errorMessage = typeof data.error === "string" ? data.error : "Unknown API error";
|
|
481
|
+
throw createApiError(
|
|
482
|
+
errorMessage,
|
|
483
|
+
response.status,
|
|
484
|
+
data,
|
|
485
|
+
errorMessage,
|
|
486
|
+
"An error occurred while processing the request."
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
return data;
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
createOrder(params) {
|
|
493
|
+
return this.request("/api/orders/create", params);
|
|
494
|
+
}
|
|
495
|
+
updateOrder(params) {
|
|
496
|
+
return this.request("/api/orders/update", params);
|
|
497
|
+
}
|
|
498
|
+
getOrder(params) {
|
|
499
|
+
return this.request("/api/orders/get", params);
|
|
500
|
+
}
|
|
501
|
+
updateTransaction(params) {
|
|
502
|
+
return this.request("/api/transactions/update", params);
|
|
503
|
+
}
|
|
504
|
+
checkout(params) {
|
|
505
|
+
return this.request("/api/orders/checkout", params);
|
|
506
|
+
}
|
|
507
|
+
createFulfillment(params) {
|
|
508
|
+
return this.request("/api/orders/create-fulfillment", params);
|
|
509
|
+
}
|
|
510
|
+
returnWithRefund(params) {
|
|
511
|
+
return this.request("/api/returns/return-refund", params);
|
|
512
|
+
}
|
|
513
|
+
createReturn(params) {
|
|
514
|
+
return this.request("/api/returns/create", params);
|
|
515
|
+
}
|
|
516
|
+
updateReturn(params) {
|
|
517
|
+
return this.request("/api/returns/update", params);
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
// src/core/api/cart-api.ts
|
|
522
|
+
var CartApi = class {
|
|
523
|
+
constructor(options) {
|
|
524
|
+
if (!options.clientKey) {
|
|
525
|
+
throw createConfigError("clientKey is required for CartApi.");
|
|
526
|
+
}
|
|
527
|
+
if (!options.secretKey && !options.customerToken) {
|
|
528
|
+
throw createConfigError("Either secretKey or customerToken is required for CartApi.");
|
|
529
|
+
}
|
|
530
|
+
this.clientKey = options.clientKey;
|
|
531
|
+
this.secretKey = options.secretKey;
|
|
532
|
+
this.customerToken = options.customerToken;
|
|
533
|
+
this.baseUrl = options.baseUrl;
|
|
534
|
+
}
|
|
535
|
+
request(endpoint, body) {
|
|
536
|
+
return __async(this, null, function* () {
|
|
537
|
+
const token = typeof this.customerToken === "function" ? this.customerToken() : this.customerToken;
|
|
538
|
+
const response = yield _fetch(endpoint, {
|
|
539
|
+
method: "POST",
|
|
540
|
+
clientKey: this.clientKey,
|
|
541
|
+
secretKey: this.secretKey,
|
|
542
|
+
customerToken: token != null ? token : void 0,
|
|
543
|
+
baseUrl: this.baseUrl,
|
|
544
|
+
body: JSON.stringify(body)
|
|
545
|
+
});
|
|
546
|
+
let data;
|
|
547
|
+
try {
|
|
548
|
+
data = yield response.json();
|
|
549
|
+
} catch (e) {
|
|
550
|
+
throw createApiError(
|
|
551
|
+
`Invalid JSON response from ${endpoint}`,
|
|
552
|
+
response.status,
|
|
553
|
+
void 0,
|
|
554
|
+
"Server returned an invalid response.",
|
|
555
|
+
"Check if the API endpoint is available."
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
if (data.error) {
|
|
559
|
+
const errorMessage = typeof data.error === "string" ? data.error : "Unknown API error";
|
|
560
|
+
throw createApiError(
|
|
561
|
+
errorMessage,
|
|
562
|
+
response.status,
|
|
563
|
+
data,
|
|
564
|
+
errorMessage,
|
|
565
|
+
"An error occurred while processing the request."
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
return data;
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
addItem(params) {
|
|
572
|
+
return this.request("/api/carts/add-item", params);
|
|
573
|
+
}
|
|
574
|
+
updateItem(params) {
|
|
575
|
+
return this.request("/api/carts/update-item", params);
|
|
576
|
+
}
|
|
577
|
+
removeItem(params) {
|
|
578
|
+
return this.request("/api/carts/remove-item", params);
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
// src/core/api/product-api.ts
|
|
583
|
+
var ProductApi = class {
|
|
584
|
+
constructor(options) {
|
|
585
|
+
if (!options.clientKey) {
|
|
586
|
+
throw createConfigError("clientKey is required for ProductApi.");
|
|
587
|
+
}
|
|
588
|
+
if (!options.secretKey) {
|
|
589
|
+
throw createConfigError("secretKey is required for ProductApi.");
|
|
590
|
+
}
|
|
591
|
+
this.clientKey = options.clientKey;
|
|
592
|
+
this.secretKey = options.secretKey;
|
|
593
|
+
this.baseUrl = options.baseUrl;
|
|
594
|
+
}
|
|
595
|
+
request(endpoint, body) {
|
|
596
|
+
return __async(this, null, function* () {
|
|
597
|
+
const response = yield _fetch(endpoint, {
|
|
598
|
+
method: "POST",
|
|
599
|
+
clientKey: this.clientKey,
|
|
600
|
+
secretKey: this.secretKey,
|
|
601
|
+
baseUrl: this.baseUrl,
|
|
602
|
+
body: JSON.stringify(body)
|
|
603
|
+
});
|
|
604
|
+
let data;
|
|
605
|
+
try {
|
|
606
|
+
data = yield response.json();
|
|
607
|
+
} catch (e) {
|
|
608
|
+
throw createApiError(
|
|
609
|
+
`Invalid JSON response from ${endpoint}`,
|
|
610
|
+
response.status,
|
|
611
|
+
void 0,
|
|
612
|
+
"Server returned an invalid response.",
|
|
613
|
+
"Check if the API endpoint is available."
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
if (data.error) {
|
|
617
|
+
const errorMessage = typeof data.error === "string" ? data.error : "Unknown API error";
|
|
618
|
+
throw createApiError(
|
|
619
|
+
errorMessage,
|
|
620
|
+
response.status,
|
|
621
|
+
data,
|
|
622
|
+
errorMessage,
|
|
623
|
+
"An error occurred while processing the request."
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
return data;
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
stockCheck(params) {
|
|
630
|
+
return this.request("/api/products/stock-check", params);
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
// src/core/collection/query-builder.ts
|
|
635
|
+
var CollectionQueryBuilder = class {
|
|
636
|
+
constructor(api, collection) {
|
|
637
|
+
this.api = api;
|
|
638
|
+
this.collection = collection;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Find documents (list query)
|
|
642
|
+
* GET /api/{collection}
|
|
643
|
+
* @returns Payload CMS find response with docs array and pagination
|
|
644
|
+
*/
|
|
645
|
+
find(options) {
|
|
646
|
+
return __async(this, null, function* () {
|
|
647
|
+
return this.api.requestFind(
|
|
648
|
+
`/api/${String(this.collection)}`,
|
|
649
|
+
options
|
|
650
|
+
);
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Find document by ID
|
|
655
|
+
* GET /api/{collection}/{id}
|
|
656
|
+
* @returns Document object directly (no wrapper)
|
|
657
|
+
*/
|
|
658
|
+
findById(id, options) {
|
|
659
|
+
return __async(this, null, function* () {
|
|
660
|
+
return this.api.requestFindById(
|
|
661
|
+
`/api/${String(this.collection)}/${String(id)}`,
|
|
662
|
+
options
|
|
663
|
+
);
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Create a new document
|
|
668
|
+
* POST /api/{collection}
|
|
669
|
+
* @returns Payload CMS mutation response with doc and message
|
|
670
|
+
*/
|
|
671
|
+
create(data) {
|
|
672
|
+
return __async(this, null, function* () {
|
|
673
|
+
return this.api.requestCreate(
|
|
674
|
+
`/api/${String(this.collection)}`,
|
|
675
|
+
data
|
|
676
|
+
);
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Update a document by ID
|
|
681
|
+
* PATCH /api/{collection}/{id}
|
|
682
|
+
* @returns Payload CMS mutation response with doc and message
|
|
683
|
+
*/
|
|
684
|
+
update(id, data) {
|
|
685
|
+
return __async(this, null, function* () {
|
|
686
|
+
return this.api.requestUpdate(
|
|
687
|
+
`/api/${String(this.collection)}/${String(id)}`,
|
|
688
|
+
data
|
|
689
|
+
);
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Count documents
|
|
694
|
+
* GET /api/{collection}/count
|
|
695
|
+
* @returns Count response with totalDocs
|
|
696
|
+
*/
|
|
697
|
+
count(options) {
|
|
698
|
+
return __async(this, null, function* () {
|
|
699
|
+
return this.api.requestCount(
|
|
700
|
+
`/api/${String(this.collection)}/count`,
|
|
701
|
+
options
|
|
702
|
+
);
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* Update multiple documents (bulk update)
|
|
707
|
+
* PATCH /api/{collection}
|
|
708
|
+
* @returns Payload CMS find response with updated docs
|
|
709
|
+
*/
|
|
710
|
+
updateMany(where, data) {
|
|
711
|
+
return __async(this, null, function* () {
|
|
712
|
+
return this.api.requestUpdateMany(
|
|
713
|
+
`/api/${String(this.collection)}`,
|
|
714
|
+
{ where, data }
|
|
715
|
+
);
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
/**
|
|
719
|
+
* Delete a document by ID
|
|
720
|
+
* DELETE /api/{collection}/{id}
|
|
721
|
+
* @returns Deleted document object directly (no wrapper)
|
|
722
|
+
*/
|
|
723
|
+
remove(id) {
|
|
724
|
+
return __async(this, null, function* () {
|
|
725
|
+
return this.api.requestDelete(
|
|
726
|
+
`/api/${String(this.collection)}/${String(id)}`
|
|
727
|
+
);
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* Delete multiple documents (bulk delete)
|
|
732
|
+
* DELETE /api/{collection}
|
|
733
|
+
* @returns Payload CMS find response with deleted docs
|
|
734
|
+
*/
|
|
735
|
+
removeMany(where) {
|
|
736
|
+
return __async(this, null, function* () {
|
|
737
|
+
return this.api.requestDeleteMany(
|
|
738
|
+
`/api/${String(this.collection)}`,
|
|
739
|
+
{ where }
|
|
740
|
+
);
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
|
|
745
|
+
// src/core/collection/http-client.ts
|
|
746
|
+
import { stringify } from "qs-esm";
|
|
747
|
+
var HttpClient = class {
|
|
748
|
+
constructor(clientKey, secretKey, baseUrl, getCustomerToken) {
|
|
749
|
+
if (!clientKey) {
|
|
750
|
+
throw createValidationError("clientKey is required.");
|
|
751
|
+
}
|
|
752
|
+
this.clientKey = clientKey;
|
|
753
|
+
this.secretKey = secretKey;
|
|
754
|
+
this.baseUrl = baseUrl;
|
|
755
|
+
this.getCustomerToken = getCustomerToken;
|
|
756
|
+
}
|
|
757
|
+
get defaultOptions() {
|
|
758
|
+
var _a;
|
|
759
|
+
const opts = { clientKey: this.clientKey, secretKey: this.secretKey, baseUrl: this.baseUrl };
|
|
760
|
+
const token = (_a = this.getCustomerToken) == null ? void 0 : _a.call(this);
|
|
761
|
+
if (token) {
|
|
762
|
+
opts.customerToken = token;
|
|
763
|
+
}
|
|
764
|
+
return opts;
|
|
765
|
+
}
|
|
766
|
+
buildUrl(endpoint, options) {
|
|
767
|
+
if (!options) return endpoint;
|
|
768
|
+
const queryString = stringify(options, { addQueryPrefix: true });
|
|
769
|
+
return queryString ? `${endpoint}${queryString}` : endpoint;
|
|
770
|
+
}
|
|
771
|
+
assertJsonResponse(response) {
|
|
772
|
+
const contentType = response.headers.get("content-type");
|
|
773
|
+
if (!(contentType == null ? void 0 : contentType.includes("application/json"))) {
|
|
774
|
+
throw createApiError("Response is not in JSON format.", response.status, { contentType });
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
778
|
+
* Parse Payload CMS find response (list query)
|
|
779
|
+
* Returns native Payload response structure
|
|
780
|
+
*/
|
|
781
|
+
parseFindResponse(response) {
|
|
782
|
+
return __async(this, null, function* () {
|
|
783
|
+
var _a, _b;
|
|
784
|
+
const contentType = response.headers.get("content-type");
|
|
785
|
+
try {
|
|
786
|
+
this.assertJsonResponse(response);
|
|
787
|
+
const jsonData = yield response.json();
|
|
788
|
+
if (jsonData.docs === void 0) {
|
|
789
|
+
throw createApiError("Invalid find response.", response.status, { jsonData });
|
|
790
|
+
}
|
|
791
|
+
return {
|
|
792
|
+
docs: jsonData.docs,
|
|
793
|
+
totalDocs: jsonData.totalDocs || 0,
|
|
794
|
+
limit: jsonData.limit || 20,
|
|
795
|
+
totalPages: jsonData.totalPages || 0,
|
|
796
|
+
page: jsonData.page || 1,
|
|
797
|
+
pagingCounter: jsonData.pagingCounter || 1,
|
|
798
|
+
hasPrevPage: jsonData.hasPrevPage || false,
|
|
799
|
+
hasNextPage: jsonData.hasNextPage || false,
|
|
800
|
+
prevPage: (_a = jsonData.prevPage) != null ? _a : null,
|
|
801
|
+
nextPage: (_b = jsonData.nextPage) != null ? _b : null
|
|
802
|
+
};
|
|
803
|
+
} catch (error) {
|
|
804
|
+
throw createApiError("Failed to parse response.", response.status, {
|
|
805
|
+
contentType,
|
|
806
|
+
error: error instanceof Error ? error.message : error
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* Parse Payload CMS mutation response (create/update)
|
|
813
|
+
* Returns native Payload response structure
|
|
814
|
+
*/
|
|
815
|
+
parseMutationResponse(response) {
|
|
816
|
+
return __async(this, null, function* () {
|
|
817
|
+
const contentType = response.headers.get("content-type");
|
|
818
|
+
try {
|
|
819
|
+
this.assertJsonResponse(response);
|
|
820
|
+
const jsonData = yield response.json();
|
|
821
|
+
if (jsonData.doc === void 0) {
|
|
822
|
+
throw createApiError("Invalid mutation response.", response.status, { jsonData });
|
|
823
|
+
}
|
|
824
|
+
return {
|
|
825
|
+
message: jsonData.message || "",
|
|
826
|
+
doc: jsonData.doc,
|
|
827
|
+
errors: jsonData.errors
|
|
828
|
+
};
|
|
829
|
+
} catch (error) {
|
|
830
|
+
throw createApiError("Failed to parse response.", response.status, {
|
|
831
|
+
contentType,
|
|
832
|
+
error: error instanceof Error ? error.message : error
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Parse Payload CMS document response (findById/delete)
|
|
839
|
+
* Returns document directly without wrapper
|
|
840
|
+
*/
|
|
841
|
+
parseDocumentResponse(response) {
|
|
842
|
+
return __async(this, null, function* () {
|
|
843
|
+
const contentType = response.headers.get("content-type");
|
|
844
|
+
try {
|
|
845
|
+
this.assertJsonResponse(response);
|
|
846
|
+
const jsonData = yield response.json();
|
|
847
|
+
return jsonData;
|
|
848
|
+
} catch (error) {
|
|
849
|
+
throw createApiError("Failed to parse response.", response.status, {
|
|
850
|
+
contentType,
|
|
851
|
+
error: error instanceof Error ? error.message : error
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
};
|
|
857
|
+
|
|
649
858
|
// src/core/collection/collection-client.ts
|
|
650
859
|
var CollectionClient = class extends HttpClient {
|
|
651
|
-
constructor(clientKey, secretKey, baseUrl) {
|
|
652
|
-
super(clientKey, secretKey, baseUrl);
|
|
860
|
+
constructor(clientKey, secretKey, baseUrl, getCustomerToken) {
|
|
861
|
+
super(clientKey, secretKey, baseUrl, getCustomerToken);
|
|
653
862
|
}
|
|
654
863
|
from(collection) {
|
|
655
864
|
return new CollectionQueryBuilder(this, collection);
|
|
@@ -774,7 +983,17 @@ var COLLECTIONS = [
|
|
|
774
983
|
"order-products",
|
|
775
984
|
"returns",
|
|
776
985
|
"return-products",
|
|
986
|
+
"exchanges",
|
|
987
|
+
"exchange-products",
|
|
988
|
+
"fulfillments",
|
|
989
|
+
"fulfillment-items",
|
|
777
990
|
"transactions",
|
|
991
|
+
"customers",
|
|
992
|
+
"customer-addresses",
|
|
993
|
+
"carts",
|
|
994
|
+
"cart-items",
|
|
995
|
+
"discounts",
|
|
996
|
+
"shipping-policies",
|
|
778
997
|
"documents",
|
|
779
998
|
"document-categories",
|
|
780
999
|
"document-images",
|
|
@@ -792,12 +1011,205 @@ var COLLECTIONS = [
|
|
|
792
1011
|
"media"
|
|
793
1012
|
];
|
|
794
1013
|
|
|
1014
|
+
// src/core/customer/customer-auth.ts
|
|
1015
|
+
var DEFAULT_TIMEOUT2 = 15e3;
|
|
1016
|
+
var CustomerAuth = class {
|
|
1017
|
+
constructor(clientKey, baseUrl, options) {
|
|
1018
|
+
var _a;
|
|
1019
|
+
this.clientKey = clientKey;
|
|
1020
|
+
this.baseUrl = baseUrl;
|
|
1021
|
+
this.token = (_a = options == null ? void 0 : options.token) != null ? _a : null;
|
|
1022
|
+
this.onTokenChange = options == null ? void 0 : options.onTokenChange;
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* Register a new customer account
|
|
1026
|
+
*/
|
|
1027
|
+
register(data) {
|
|
1028
|
+
return __async(this, null, function* () {
|
|
1029
|
+
return this.requestJson("/api/customers/register", {
|
|
1030
|
+
method: "POST",
|
|
1031
|
+
body: JSON.stringify(data)
|
|
1032
|
+
});
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
/**
|
|
1036
|
+
* Login with email and password. Stores the token internally.
|
|
1037
|
+
*/
|
|
1038
|
+
login(data) {
|
|
1039
|
+
return __async(this, null, function* () {
|
|
1040
|
+
const result = yield this.requestJson("/api/customers/login", {
|
|
1041
|
+
method: "POST",
|
|
1042
|
+
body: JSON.stringify(data)
|
|
1043
|
+
});
|
|
1044
|
+
this.setToken(result.token);
|
|
1045
|
+
return result;
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
/**
|
|
1049
|
+
* Refresh the current token. Requires a valid (non-expired) token.
|
|
1050
|
+
*/
|
|
1051
|
+
refreshToken() {
|
|
1052
|
+
return __async(this, null, function* () {
|
|
1053
|
+
if (!this.token) throw new ApiError("Not authenticated", 401);
|
|
1054
|
+
const result = yield this.requestJson("/api/customers/refresh", {
|
|
1055
|
+
method: "POST",
|
|
1056
|
+
headers: { Authorization: `Bearer ${this.token}` }
|
|
1057
|
+
});
|
|
1058
|
+
this.setToken(result.token);
|
|
1059
|
+
return result;
|
|
1060
|
+
});
|
|
1061
|
+
}
|
|
1062
|
+
/**
|
|
1063
|
+
* Clear the stored token
|
|
1064
|
+
*/
|
|
1065
|
+
logout() {
|
|
1066
|
+
this.setToken(null);
|
|
1067
|
+
}
|
|
1068
|
+
/**
|
|
1069
|
+
* Get the current authenticated customer's profile
|
|
1070
|
+
*/
|
|
1071
|
+
me() {
|
|
1072
|
+
return __async(this, null, function* () {
|
|
1073
|
+
var _a;
|
|
1074
|
+
if (!this.token) return null;
|
|
1075
|
+
try {
|
|
1076
|
+
const data = yield this.requestJson("/api/customers/me", {
|
|
1077
|
+
method: "GET",
|
|
1078
|
+
headers: { Authorization: `Bearer ${this.token}` }
|
|
1079
|
+
});
|
|
1080
|
+
return (_a = data.customer) != null ? _a : null;
|
|
1081
|
+
} catch (error) {
|
|
1082
|
+
if (error instanceof ApiError && error.status === 401) {
|
|
1083
|
+
this.setToken(null);
|
|
1084
|
+
return null;
|
|
1085
|
+
}
|
|
1086
|
+
throw error;
|
|
1087
|
+
}
|
|
1088
|
+
});
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* Request a password reset email
|
|
1092
|
+
*/
|
|
1093
|
+
forgotPassword(email) {
|
|
1094
|
+
return __async(this, null, function* () {
|
|
1095
|
+
yield this.requestJson("/api/customers/forgot-password", {
|
|
1096
|
+
method: "POST",
|
|
1097
|
+
body: JSON.stringify({ email })
|
|
1098
|
+
});
|
|
1099
|
+
});
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
* Reset password using a token from the reset email
|
|
1103
|
+
*/
|
|
1104
|
+
resetPassword(token, password) {
|
|
1105
|
+
return __async(this, null, function* () {
|
|
1106
|
+
yield this.requestJson("/api/customers/reset-password", {
|
|
1107
|
+
method: "POST",
|
|
1108
|
+
body: JSON.stringify({ token, password })
|
|
1109
|
+
});
|
|
1110
|
+
});
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* Change the password of the currently authenticated customer
|
|
1114
|
+
*/
|
|
1115
|
+
changePassword(currentPassword, newPassword) {
|
|
1116
|
+
return __async(this, null, function* () {
|
|
1117
|
+
if (!this.token) throw new ApiError("Not authenticated", 401);
|
|
1118
|
+
yield this.requestJson("/api/customers/change-password", {
|
|
1119
|
+
method: "POST",
|
|
1120
|
+
headers: { Authorization: `Bearer ${this.token}` },
|
|
1121
|
+
body: JSON.stringify({ currentPassword, newPassword })
|
|
1122
|
+
});
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
/**
|
|
1126
|
+
* Verify email using the verification token
|
|
1127
|
+
*/
|
|
1128
|
+
verifyEmail(token) {
|
|
1129
|
+
return __async(this, null, function* () {
|
|
1130
|
+
yield this.requestJson("/api/customers/verify-email", {
|
|
1131
|
+
method: "POST",
|
|
1132
|
+
body: JSON.stringify({ token })
|
|
1133
|
+
});
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1136
|
+
/**
|
|
1137
|
+
* Get the current token (or null if not authenticated)
|
|
1138
|
+
*/
|
|
1139
|
+
getToken() {
|
|
1140
|
+
return this.token;
|
|
1141
|
+
}
|
|
1142
|
+
/**
|
|
1143
|
+
* Set the token manually (e.g. from SSR)
|
|
1144
|
+
*/
|
|
1145
|
+
setToken(token) {
|
|
1146
|
+
var _a;
|
|
1147
|
+
this.token = token;
|
|
1148
|
+
(_a = this.onTokenChange) == null ? void 0 : _a.call(this, token);
|
|
1149
|
+
}
|
|
1150
|
+
/**
|
|
1151
|
+
* Check if the customer is currently authenticated
|
|
1152
|
+
*/
|
|
1153
|
+
isAuthenticated() {
|
|
1154
|
+
return this.token !== null;
|
|
1155
|
+
}
|
|
1156
|
+
/**
|
|
1157
|
+
* Internal: make a request with timeout and error handling.
|
|
1158
|
+
* Auth endpoints don't retry — failures are final.
|
|
1159
|
+
*/
|
|
1160
|
+
requestJson(path, init) {
|
|
1161
|
+
return __async(this, null, function* () {
|
|
1162
|
+
const headers = new Headers(init.headers);
|
|
1163
|
+
headers.set("X-Client-Key", this.clientKey);
|
|
1164
|
+
if (!headers.has("Content-Type") && init.body) {
|
|
1165
|
+
headers.set("Content-Type", "application/json");
|
|
1166
|
+
}
|
|
1167
|
+
const controller = new AbortController();
|
|
1168
|
+
const timeoutId = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT2);
|
|
1169
|
+
let res;
|
|
1170
|
+
try {
|
|
1171
|
+
res = yield fetch(`${this.baseUrl}${path}`, __spreadProps(__spreadValues({}, init), {
|
|
1172
|
+
headers,
|
|
1173
|
+
signal: controller.signal
|
|
1174
|
+
}));
|
|
1175
|
+
} catch (error) {
|
|
1176
|
+
clearTimeout(timeoutId);
|
|
1177
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
1178
|
+
throw new TimeoutError(
|
|
1179
|
+
`Request timed out after ${DEFAULT_TIMEOUT2}ms`,
|
|
1180
|
+
{ url: path, timeout: DEFAULT_TIMEOUT2 }
|
|
1181
|
+
);
|
|
1182
|
+
}
|
|
1183
|
+
throw new NetworkError(
|
|
1184
|
+
error instanceof Error ? error.message : "Network request failed",
|
|
1185
|
+
void 0,
|
|
1186
|
+
{ url: path },
|
|
1187
|
+
"Network connection failed.",
|
|
1188
|
+
"Please check your internet connection and try again."
|
|
1189
|
+
);
|
|
1190
|
+
}
|
|
1191
|
+
clearTimeout(timeoutId);
|
|
1192
|
+
if (!res.ok) {
|
|
1193
|
+
const body = yield res.json().catch(() => ({}));
|
|
1194
|
+
throw new ApiError(
|
|
1195
|
+
body.error || `HTTP ${res.status}`,
|
|
1196
|
+
res.status,
|
|
1197
|
+
body.details,
|
|
1198
|
+
body.error
|
|
1199
|
+
);
|
|
1200
|
+
}
|
|
1201
|
+
return res.json();
|
|
1202
|
+
});
|
|
1203
|
+
}
|
|
1204
|
+
};
|
|
1205
|
+
|
|
795
1206
|
// src/core/query/get-query-client.ts
|
|
796
1207
|
import { isServer, QueryClient, defaultShouldDehydrateQuery } from "@tanstack/react-query";
|
|
797
1208
|
function makeQueryClient() {
|
|
798
1209
|
return new QueryClient({
|
|
799
1210
|
defaultOptions: {
|
|
800
1211
|
queries: {
|
|
1212
|
+
// SSR/RSC: server-fetched data doesn't auto-refetch; use refetch()/invalidate() to refresh
|
|
801
1213
|
staleTime: Number.POSITIVE_INFINITY,
|
|
802
1214
|
refetchOnWindowFocus: false
|
|
803
1215
|
},
|
|
@@ -838,11 +1250,24 @@ function collectionKeys(collection) {
|
|
|
838
1250
|
infinite: (options) => [collection, "infinite", options]
|
|
839
1251
|
};
|
|
840
1252
|
}
|
|
1253
|
+
var customerKeys = {
|
|
1254
|
+
all: ["customer"],
|
|
1255
|
+
me: () => ["customer", "me"]
|
|
1256
|
+
};
|
|
841
1257
|
var DEFAULT_PAGE_SIZE = 20;
|
|
842
1258
|
var QueryHooks = class {
|
|
843
|
-
constructor(queryClient, collectionClient) {
|
|
1259
|
+
constructor(queryClient, collectionClient, customerAuth) {
|
|
844
1260
|
this.queryClient = queryClient;
|
|
845
1261
|
this.collectionClient = collectionClient;
|
|
1262
|
+
this.customerAuth = customerAuth;
|
|
1263
|
+
}
|
|
1264
|
+
ensureCustomerAuth() {
|
|
1265
|
+
if (!this.customerAuth) {
|
|
1266
|
+
throw createConfigError(
|
|
1267
|
+
"Customer hooks require BrowserClient. Use createBrowserClient() instead of createServerClient()."
|
|
1268
|
+
);
|
|
1269
|
+
}
|
|
1270
|
+
return this.customerAuth;
|
|
846
1271
|
}
|
|
847
1272
|
// ===== useQuery =====
|
|
848
1273
|
useQuery(params, options) {
|
|
@@ -1034,6 +1459,124 @@ var QueryHooks = class {
|
|
|
1034
1459
|
);
|
|
1035
1460
|
}
|
|
1036
1461
|
}
|
|
1462
|
+
// ===== Customer Query Hooks =====
|
|
1463
|
+
useCustomerMe(options) {
|
|
1464
|
+
var _a, _b;
|
|
1465
|
+
return useQueryOriginal(__spreadProps(__spreadValues({
|
|
1466
|
+
queryKey: customerKeys.me(),
|
|
1467
|
+
queryFn: () => __async(this, null, function* () {
|
|
1468
|
+
return yield this.ensureCustomerAuth().me();
|
|
1469
|
+
})
|
|
1470
|
+
}, options), {
|
|
1471
|
+
enabled: ((_a = options == null ? void 0 : options.enabled) != null ? _a : true) && !!((_b = this.customerAuth) == null ? void 0 : _b.isAuthenticated())
|
|
1472
|
+
}));
|
|
1473
|
+
}
|
|
1474
|
+
useCustomerLogin(options) {
|
|
1475
|
+
return useMutationOriginal({
|
|
1476
|
+
mutationFn: (data) => __async(this, null, function* () {
|
|
1477
|
+
return yield this.ensureCustomerAuth().login(data);
|
|
1478
|
+
}),
|
|
1479
|
+
onSuccess: (data) => {
|
|
1480
|
+
var _a;
|
|
1481
|
+
this.queryClient.invalidateQueries({ queryKey: customerKeys.me() });
|
|
1482
|
+
(_a = options == null ? void 0 : options.onSuccess) == null ? void 0 : _a.call(options, data);
|
|
1483
|
+
},
|
|
1484
|
+
onError: options == null ? void 0 : options.onError,
|
|
1485
|
+
onSettled: options == null ? void 0 : options.onSettled
|
|
1486
|
+
});
|
|
1487
|
+
}
|
|
1488
|
+
useCustomerRegister(options) {
|
|
1489
|
+
return useMutationOriginal({
|
|
1490
|
+
mutationFn: (data) => __async(this, null, function* () {
|
|
1491
|
+
return yield this.ensureCustomerAuth().register(data);
|
|
1492
|
+
}),
|
|
1493
|
+
onSuccess: options == null ? void 0 : options.onSuccess,
|
|
1494
|
+
onError: options == null ? void 0 : options.onError,
|
|
1495
|
+
onSettled: options == null ? void 0 : options.onSettled
|
|
1496
|
+
});
|
|
1497
|
+
}
|
|
1498
|
+
useCustomerLogout(options) {
|
|
1499
|
+
return useMutationOriginal({
|
|
1500
|
+
mutationFn: () => __async(this, null, function* () {
|
|
1501
|
+
this.ensureCustomerAuth().logout();
|
|
1502
|
+
}),
|
|
1503
|
+
onSuccess: () => {
|
|
1504
|
+
var _a;
|
|
1505
|
+
this.queryClient.removeQueries({ queryKey: customerKeys.all });
|
|
1506
|
+
(_a = options == null ? void 0 : options.onSuccess) == null ? void 0 : _a.call(options);
|
|
1507
|
+
},
|
|
1508
|
+
onError: options == null ? void 0 : options.onError,
|
|
1509
|
+
onSettled: options == null ? void 0 : options.onSettled
|
|
1510
|
+
});
|
|
1511
|
+
}
|
|
1512
|
+
useCustomerForgotPassword(options) {
|
|
1513
|
+
return useMutationOriginal({
|
|
1514
|
+
mutationFn: (email) => __async(this, null, function* () {
|
|
1515
|
+
yield this.ensureCustomerAuth().forgotPassword(email);
|
|
1516
|
+
}),
|
|
1517
|
+
onSuccess: options == null ? void 0 : options.onSuccess,
|
|
1518
|
+
onError: options == null ? void 0 : options.onError,
|
|
1519
|
+
onSettled: options == null ? void 0 : options.onSettled
|
|
1520
|
+
});
|
|
1521
|
+
}
|
|
1522
|
+
useCustomerResetPassword(options) {
|
|
1523
|
+
return useMutationOriginal({
|
|
1524
|
+
mutationFn: (data) => __async(this, null, function* () {
|
|
1525
|
+
yield this.ensureCustomerAuth().resetPassword(data.token, data.password);
|
|
1526
|
+
}),
|
|
1527
|
+
onSuccess: options == null ? void 0 : options.onSuccess,
|
|
1528
|
+
onError: options == null ? void 0 : options.onError,
|
|
1529
|
+
onSettled: options == null ? void 0 : options.onSettled
|
|
1530
|
+
});
|
|
1531
|
+
}
|
|
1532
|
+
useCustomerVerifyEmail(options) {
|
|
1533
|
+
return useMutationOriginal({
|
|
1534
|
+
mutationFn: (token) => __async(this, null, function* () {
|
|
1535
|
+
yield this.ensureCustomerAuth().verifyEmail(token);
|
|
1536
|
+
}),
|
|
1537
|
+
onSuccess: () => {
|
|
1538
|
+
var _a;
|
|
1539
|
+
this.queryClient.invalidateQueries({ queryKey: customerKeys.me() });
|
|
1540
|
+
(_a = options == null ? void 0 : options.onSuccess) == null ? void 0 : _a.call(options);
|
|
1541
|
+
},
|
|
1542
|
+
onError: options == null ? void 0 : options.onError,
|
|
1543
|
+
onSettled: options == null ? void 0 : options.onSettled
|
|
1544
|
+
});
|
|
1545
|
+
}
|
|
1546
|
+
useCustomerRefreshToken(options) {
|
|
1547
|
+
return useMutationOriginal({
|
|
1548
|
+
mutationFn: () => __async(this, null, function* () {
|
|
1549
|
+
return yield this.ensureCustomerAuth().refreshToken();
|
|
1550
|
+
}),
|
|
1551
|
+
onSuccess: (data) => {
|
|
1552
|
+
var _a;
|
|
1553
|
+
this.queryClient.invalidateQueries({ queryKey: customerKeys.me() });
|
|
1554
|
+
(_a = options == null ? void 0 : options.onSuccess) == null ? void 0 : _a.call(options, data);
|
|
1555
|
+
},
|
|
1556
|
+
onError: options == null ? void 0 : options.onError,
|
|
1557
|
+
onSettled: options == null ? void 0 : options.onSettled
|
|
1558
|
+
});
|
|
1559
|
+
}
|
|
1560
|
+
useCustomerChangePassword(options) {
|
|
1561
|
+
return useMutationOriginal({
|
|
1562
|
+
mutationFn: (data) => __async(this, null, function* () {
|
|
1563
|
+
yield this.ensureCustomerAuth().changePassword(data.currentPassword, data.newPassword);
|
|
1564
|
+
}),
|
|
1565
|
+
onSuccess: options == null ? void 0 : options.onSuccess,
|
|
1566
|
+
onError: options == null ? void 0 : options.onError,
|
|
1567
|
+
onSettled: options == null ? void 0 : options.onSettled
|
|
1568
|
+
});
|
|
1569
|
+
}
|
|
1570
|
+
// ===== Customer Cache Utilities =====
|
|
1571
|
+
invalidateCustomerQueries() {
|
|
1572
|
+
return this.queryClient.invalidateQueries({ queryKey: customerKeys.all });
|
|
1573
|
+
}
|
|
1574
|
+
getCustomerData() {
|
|
1575
|
+
return this.queryClient.getQueryData(customerKeys.me());
|
|
1576
|
+
}
|
|
1577
|
+
setCustomerData(data) {
|
|
1578
|
+
this.queryClient.setQueryData(customerKeys.me(), data);
|
|
1579
|
+
}
|
|
1037
1580
|
};
|
|
1038
1581
|
|
|
1039
1582
|
// src/core/client/client.ts
|
|
@@ -1051,8 +1594,19 @@ var BrowserClient = class {
|
|
|
1051
1594
|
};
|
|
1052
1595
|
this.state = { metadata };
|
|
1053
1596
|
this.queryClient = getQueryClient();
|
|
1054
|
-
this.
|
|
1055
|
-
this.
|
|
1597
|
+
this.customer = new CustomerAuth(this.config.clientKey, this.baseUrl, options.customer);
|
|
1598
|
+
this.cart = new CartApi({
|
|
1599
|
+
clientKey: this.config.clientKey,
|
|
1600
|
+
customerToken: () => this.customer.getToken(),
|
|
1601
|
+
baseUrl: this.baseUrl
|
|
1602
|
+
});
|
|
1603
|
+
this.collections = new CollectionClient(
|
|
1604
|
+
this.config.clientKey,
|
|
1605
|
+
void 0,
|
|
1606
|
+
this.baseUrl,
|
|
1607
|
+
() => this.customer.getToken()
|
|
1608
|
+
);
|
|
1609
|
+
this.query = new QueryHooks(this.queryClient, this.collections, this.customer);
|
|
1056
1610
|
}
|
|
1057
1611
|
from(collection) {
|
|
1058
1612
|
return this.collections.from(collection);
|
|
@@ -1065,68 +1619,14 @@ function createBrowserClient(options) {
|
|
|
1065
1619
|
return new BrowserClient(options);
|
|
1066
1620
|
}
|
|
1067
1621
|
|
|
1068
|
-
// src/core/api/order-api.ts
|
|
1069
|
-
var OrderApi = class {
|
|
1070
|
-
constructor(options) {
|
|
1071
|
-
if (!options.clientKey) {
|
|
1072
|
-
throw createConfigError("clientKey is required for OrderApi.");
|
|
1073
|
-
}
|
|
1074
|
-
if (!options.secretKey) {
|
|
1075
|
-
throw createConfigError("secretKey is required for OrderApi.");
|
|
1076
|
-
}
|
|
1077
|
-
this.clientKey = options.clientKey;
|
|
1078
|
-
this.secretKey = options.secretKey;
|
|
1079
|
-
this.baseUrl = options.baseUrl;
|
|
1080
|
-
}
|
|
1081
|
-
request(endpoint, body) {
|
|
1082
|
-
return __async(this, null, function* () {
|
|
1083
|
-
const response = yield _fetch(endpoint, {
|
|
1084
|
-
method: "POST",
|
|
1085
|
-
clientKey: this.clientKey,
|
|
1086
|
-
secretKey: this.secretKey,
|
|
1087
|
-
baseUrl: this.baseUrl,
|
|
1088
|
-
body: JSON.stringify(body)
|
|
1089
|
-
});
|
|
1090
|
-
let data;
|
|
1091
|
-
try {
|
|
1092
|
-
data = yield response.json();
|
|
1093
|
-
} catch (e) {
|
|
1094
|
-
throw createApiError(
|
|
1095
|
-
`Invalid JSON response from ${endpoint}`,
|
|
1096
|
-
response.status,
|
|
1097
|
-
void 0,
|
|
1098
|
-
"Server returned an invalid response.",
|
|
1099
|
-
"Check if the API endpoint is available."
|
|
1100
|
-
);
|
|
1101
|
-
}
|
|
1102
|
-
if (data.error) {
|
|
1103
|
-
const errorMessage = typeof data.error === "string" ? data.error : "Unknown API error";
|
|
1104
|
-
throw createApiError(
|
|
1105
|
-
errorMessage,
|
|
1106
|
-
response.status,
|
|
1107
|
-
data,
|
|
1108
|
-
errorMessage,
|
|
1109
|
-
"An error occurred while processing the request."
|
|
1110
|
-
);
|
|
1111
|
-
}
|
|
1112
|
-
return data;
|
|
1113
|
-
});
|
|
1114
|
-
}
|
|
1115
|
-
createOrder(params) {
|
|
1116
|
-
return this.request("/api/orders/create", params);
|
|
1117
|
-
}
|
|
1118
|
-
updateOrder(params) {
|
|
1119
|
-
return this.request("/api/orders/update", params);
|
|
1120
|
-
}
|
|
1121
|
-
updateTransaction(params) {
|
|
1122
|
-
return this.request("/api/transactions/update", params);
|
|
1123
|
-
}
|
|
1124
|
-
};
|
|
1125
|
-
|
|
1126
1622
|
// src/core/client/client.server.ts
|
|
1127
1623
|
var ServerClient = class {
|
|
1128
1624
|
constructor(options) {
|
|
1129
|
-
|
|
1625
|
+
if (typeof window !== "undefined") {
|
|
1626
|
+
throw createConfigError(
|
|
1627
|
+
"ServerClient must not be used in a browser environment. This risks exposing your secretKey in client bundles. Use createBrowserClient() for browser code instead."
|
|
1628
|
+
);
|
|
1629
|
+
}
|
|
1130
1630
|
if (!options.clientKey) {
|
|
1131
1631
|
throw createConfigError("clientKey is required.");
|
|
1132
1632
|
}
|
|
@@ -1137,7 +1637,7 @@ var ServerClient = class {
|
|
|
1137
1637
|
this.baseUrl = resolveApiUrl(options);
|
|
1138
1638
|
const metadata = {
|
|
1139
1639
|
timestamp: Date.now(),
|
|
1140
|
-
userAgent:
|
|
1640
|
+
userAgent: "Node.js"
|
|
1141
1641
|
};
|
|
1142
1642
|
this.state = { metadata };
|
|
1143
1643
|
this.api = new OrderApi({
|
|
@@ -1145,6 +1645,16 @@ var ServerClient = class {
|
|
|
1145
1645
|
secretKey: this.config.secretKey,
|
|
1146
1646
|
baseUrl: this.baseUrl
|
|
1147
1647
|
});
|
|
1648
|
+
this.cart = new CartApi({
|
|
1649
|
+
clientKey: this.config.clientKey,
|
|
1650
|
+
secretKey: this.config.secretKey,
|
|
1651
|
+
baseUrl: this.baseUrl
|
|
1652
|
+
});
|
|
1653
|
+
this.product = new ProductApi({
|
|
1654
|
+
clientKey: this.config.clientKey,
|
|
1655
|
+
secretKey: this.config.secretKey,
|
|
1656
|
+
baseUrl: this.baseUrl
|
|
1657
|
+
});
|
|
1148
1658
|
this.collections = new CollectionClient(
|
|
1149
1659
|
this.config.clientKey,
|
|
1150
1660
|
this.config.secretKey,
|
|
@@ -1198,6 +1708,11 @@ function handleWebhook(request, handler, options) {
|
|
|
1198
1708
|
return __async(this, null, function* () {
|
|
1199
1709
|
try {
|
|
1200
1710
|
const rawBody = yield request.text();
|
|
1711
|
+
if (!(options == null ? void 0 : options.secret)) {
|
|
1712
|
+
console.warn(
|
|
1713
|
+
"[@01.software/sdk] Webhook signature verification is skipped because no secret was provided. Pass { secret } to handleWebhook() to enable signature verification."
|
|
1714
|
+
);
|
|
1715
|
+
}
|
|
1201
1716
|
if (options == null ? void 0 : options.secret) {
|
|
1202
1717
|
const signature = request.headers.get("x-webhook-signature") || "";
|
|
1203
1718
|
const valid = yield verifySignature(rawBody, options.secret, signature);
|
|
@@ -1223,10 +1738,7 @@ function handleWebhook(request, handler, options) {
|
|
|
1223
1738
|
} catch (error) {
|
|
1224
1739
|
console.error("Webhook processing error:", error);
|
|
1225
1740
|
return new Response(
|
|
1226
|
-
JSON.stringify({
|
|
1227
|
-
error: "Internal server error",
|
|
1228
|
-
message: error instanceof Error ? error.message : "Unknown error"
|
|
1229
|
-
}),
|
|
1741
|
+
JSON.stringify({ error: "Internal server error" }),
|
|
1230
1742
|
{ status: 500, headers: { "Content-Type": "application/json" } }
|
|
1231
1743
|
);
|
|
1232
1744
|
}
|
|
@@ -1243,10 +1755,13 @@ function createTypedWebhookHandler(collection, handler) {
|
|
|
1243
1755
|
|
|
1244
1756
|
// src/utils/order/generateOrderNumber.ts
|
|
1245
1757
|
var generateOrderNumber = () => {
|
|
1758
|
+
var _a;
|
|
1246
1759
|
const year = (/* @__PURE__ */ new Date()).getFullYear().toString().slice(-2);
|
|
1247
1760
|
const month = ((/* @__PURE__ */ new Date()).getMonth() + 1).toString().padStart(2, "0");
|
|
1248
1761
|
const day = (/* @__PURE__ */ new Date()).getDate().toString().padStart(2, "0");
|
|
1249
|
-
const
|
|
1762
|
+
const array = new Uint32Array(1);
|
|
1763
|
+
globalThis.crypto.getRandomValues(array);
|
|
1764
|
+
const random = (((_a = array[0]) != null ? _a : 0) % 1e6).toString().padStart(6, "0");
|
|
1250
1765
|
return `${year}${month}${day}${random}`;
|
|
1251
1766
|
};
|
|
1252
1767
|
|
|
@@ -1295,11 +1810,14 @@ export {
|
|
|
1295
1810
|
ApiError,
|
|
1296
1811
|
BrowserClient,
|
|
1297
1812
|
COLLECTIONS,
|
|
1813
|
+
CartApi,
|
|
1298
1814
|
CollectionClient,
|
|
1299
1815
|
CollectionQueryBuilder,
|
|
1300
1816
|
ConfigError,
|
|
1817
|
+
CustomerAuth,
|
|
1301
1818
|
NetworkError,
|
|
1302
1819
|
OrderApi,
|
|
1820
|
+
ProductApi,
|
|
1303
1821
|
QueryHooks,
|
|
1304
1822
|
RichTextContent,
|
|
1305
1823
|
ServerClient,
|
|
@@ -1312,6 +1830,7 @@ export {
|
|
|
1312
1830
|
createServerClient,
|
|
1313
1831
|
createServerToken,
|
|
1314
1832
|
createTypedWebhookHandler,
|
|
1833
|
+
customerKeys,
|
|
1315
1834
|
decodeServerToken,
|
|
1316
1835
|
formatOrderName,
|
|
1317
1836
|
generateOrderNumber,
|