@01.software/sdk 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/const-BsO3aVX_.d.cts +19 -0
- package/dist/const-DZyvV9wU.d.ts +19 -0
- package/dist/index.cjs +285 -86
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +114 -196
- package/dist/index.d.ts +114 -196
- package/dist/index.js +285 -86
- package/dist/index.js.map +1 -1
- package/dist/{payload-types-ggU6BNuH.d.cts → payload-types-BsjHfeAF.d.cts} +68 -7
- package/dist/{payload-types-ggU6BNuH.d.ts → payload-types-BsjHfeAF.d.ts} +68 -7
- package/dist/realtime-DupPIYx-.d.cts +33 -0
- package/dist/realtime-DupPIYx-.d.ts +33 -0
- package/dist/realtime.cjs +263 -0
- package/dist/realtime.cjs.map +1 -0
- package/dist/realtime.d.cts +38 -0
- package/dist/realtime.d.ts +38 -0
- package/dist/realtime.js +241 -0
- package/dist/realtime.js.map +1 -0
- package/dist/ui/code-block.cjs +3 -1
- package/dist/ui/code-block.cjs.map +1 -1
- package/dist/ui/code-block.js +4 -2
- package/dist/ui/code-block.js.map +1 -1
- package/dist/ui/flow.cjs +282 -39
- package/dist/ui/flow.cjs.map +1 -1
- package/dist/ui/flow.d.cts +104 -3
- package/dist/ui/flow.d.ts +104 -3
- package/dist/ui/flow.js +270 -24
- package/dist/ui/flow.js.map +1 -1
- package/dist/ui/form.d.cts +1 -1
- package/dist/ui/form.d.ts +1 -1
- package/dist/ui/rich-text.cjs +8 -1
- package/dist/ui/rich-text.cjs.map +1 -1
- package/dist/ui/rich-text.d.cts +20 -1
- package/dist/ui/rich-text.d.ts +20 -1
- package/dist/ui/rich-text.js +8 -1
- package/dist/ui/rich-text.js.map +1 -1
- package/dist/ui/video.cjs +219 -0
- package/dist/ui/video.cjs.map +1 -0
- package/dist/ui/video.d.cts +96 -0
- package/dist/ui/video.d.ts +96 -0
- package/dist/ui/video.js +191 -0
- package/dist/ui/video.js.map +1 -0
- package/dist/video-DbLL8yuc.d.cts +85 -0
- package/dist/video-DbLL8yuc.d.ts +85 -0
- package/dist/webhook-BBWl8O2f.d.ts +20 -0
- package/dist/webhook-CkL56e65.d.cts +20 -0
- package/dist/webhook.cjs +8 -8
- package/dist/webhook.cjs.map +1 -1
- package/dist/webhook.d.cts +3 -2
- package/dist/webhook.d.ts +3 -2
- package/dist/webhook.js +8 -8
- package/dist/webhook.js.map +1 -1
- package/package.json +30 -4
- package/dist/webhook-B54a-HGd.d.ts +0 -35
- package/dist/webhook-DInps2xX.d.cts +0 -35
package/dist/index.js
CHANGED
|
@@ -135,7 +135,7 @@ function parseApiKey(apiKey) {
|
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
// src/core/internal/errors/index.ts
|
|
138
|
-
var SDKError = class
|
|
138
|
+
var SDKError = class extends Error {
|
|
139
139
|
constructor(code, message, status, details, userMessage, suggestion) {
|
|
140
140
|
super(message);
|
|
141
141
|
this.name = "SDKError";
|
|
@@ -145,7 +145,7 @@ var SDKError = class _SDKError extends Error {
|
|
|
145
145
|
this.userMessage = userMessage;
|
|
146
146
|
this.suggestion = suggestion;
|
|
147
147
|
if (Error.captureStackTrace) {
|
|
148
|
-
Error.captureStackTrace(this,
|
|
148
|
+
Error.captureStackTrace(this, new.target);
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
getUserMessage() {
|
|
@@ -261,9 +261,11 @@ var createUsageLimitError = (message, usage, details, userMessage, suggestion) =
|
|
|
261
261
|
|
|
262
262
|
// src/core/client/types.ts
|
|
263
263
|
function resolveApiUrl() {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
264
|
+
if (typeof process !== "undefined" && process.env) {
|
|
265
|
+
const envUrl = process.env.SOFTWARE_API_URL || process.env.NEXT_PUBLIC_SOFTWARE_API_URL;
|
|
266
|
+
if (envUrl) {
|
|
267
|
+
return envUrl.replace(/\/$/, "");
|
|
268
|
+
}
|
|
267
269
|
}
|
|
268
270
|
return "https://api.01.software";
|
|
269
271
|
}
|
|
@@ -284,10 +286,50 @@ function debugLog(debug, type, message, data) {
|
|
|
284
286
|
}
|
|
285
287
|
function getErrorSuggestion(status) {
|
|
286
288
|
if (status === 401) return "Please check your authentication credentials.";
|
|
289
|
+
if (status === 403) return "Access denied. Check your credentials or permissions.";
|
|
287
290
|
if (status === 404) return "The requested resource was not found.";
|
|
291
|
+
if (status === 422) return "The request data failed validation.";
|
|
288
292
|
if (status >= 500) return "A server error occurred. Please try again later.";
|
|
289
293
|
return void 0;
|
|
290
294
|
}
|
|
295
|
+
function parseErrorBody(response) {
|
|
296
|
+
return __async(this, null, function* () {
|
|
297
|
+
const fallback = {
|
|
298
|
+
errorMessage: `HTTP ${response.status}: ${response.statusText}`,
|
|
299
|
+
userMessage: `Request failed (status: ${response.status})`
|
|
300
|
+
};
|
|
301
|
+
try {
|
|
302
|
+
const body = yield response.json();
|
|
303
|
+
if (body.errors && Array.isArray(body.errors)) {
|
|
304
|
+
const details = body.errors.map(
|
|
305
|
+
(e) => e.field ? `${e.field}: ${e.message}` : e.message
|
|
306
|
+
).filter(Boolean).join("; ");
|
|
307
|
+
if (details) {
|
|
308
|
+
return {
|
|
309
|
+
errorMessage: `HTTP ${response.status}: ${details}`,
|
|
310
|
+
userMessage: details,
|
|
311
|
+
errors: body.errors
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (typeof body.error === "string") {
|
|
316
|
+
return {
|
|
317
|
+
errorMessage: `HTTP ${response.status}: ${body.error}`,
|
|
318
|
+
userMessage: body.error
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
if (body.message) {
|
|
322
|
+
return {
|
|
323
|
+
errorMessage: `HTTP ${response.status}: ${body.message}`,
|
|
324
|
+
userMessage: body.message
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
return fallback;
|
|
328
|
+
} catch (e) {
|
|
329
|
+
return fallback;
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
}
|
|
291
333
|
function delay(ms) {
|
|
292
334
|
return __async(this, null, function* () {
|
|
293
335
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
@@ -343,7 +385,7 @@ function httpFetch(url, options) {
|
|
|
343
385
|
const redactedHeaders = Object.fromEntries(headers.entries());
|
|
344
386
|
if (redactedHeaders["authorization"]) {
|
|
345
387
|
const token = redactedHeaders["authorization"];
|
|
346
|
-
redactedHeaders["authorization"] = token.length >
|
|
388
|
+
redactedHeaders["authorization"] = token.length > 20 ? `Bearer ...****${token.slice(-8)}` : "****";
|
|
347
389
|
}
|
|
348
390
|
debugLog(debug, "request", url, {
|
|
349
391
|
method: requestInit.method || "GET",
|
|
@@ -396,19 +438,26 @@ function httpFetch(url, options) {
|
|
|
396
438
|
}
|
|
397
439
|
}
|
|
398
440
|
if (NON_RETRYABLE_STATUSES.includes(response.status)) {
|
|
441
|
+
const parsed2 = yield parseErrorBody(response);
|
|
442
|
+
const details = __spreadValues({
|
|
443
|
+
url,
|
|
444
|
+
method: requestInit.method || "GET",
|
|
445
|
+
attempt: attempt + 1
|
|
446
|
+
}, parsed2.errors && { errors: parsed2.errors });
|
|
399
447
|
throw createNetworkError(
|
|
400
|
-
|
|
448
|
+
parsed2.errorMessage,
|
|
401
449
|
response.status,
|
|
402
|
-
|
|
403
|
-
|
|
450
|
+
details,
|
|
451
|
+
parsed2.userMessage,
|
|
404
452
|
getErrorSuggestion(response.status)
|
|
405
453
|
);
|
|
406
454
|
}
|
|
455
|
+
const parsed = yield parseErrorBody(response);
|
|
407
456
|
const error = createNetworkError(
|
|
408
|
-
|
|
457
|
+
parsed.errorMessage,
|
|
409
458
|
response.status,
|
|
410
459
|
{ url, method: requestInit.method || "GET", attempt: attempt + 1 },
|
|
411
|
-
|
|
460
|
+
parsed.userMessage,
|
|
412
461
|
getErrorSuggestion(response.status)
|
|
413
462
|
);
|
|
414
463
|
const method = (requestInit.method || "GET").toUpperCase();
|
|
@@ -482,6 +531,35 @@ function httpFetch(url, options) {
|
|
|
482
531
|
});
|
|
483
532
|
}
|
|
484
533
|
|
|
534
|
+
// src/core/api/parse-response.ts
|
|
535
|
+
function parseApiResponse(response, endpoint) {
|
|
536
|
+
return __async(this, null, function* () {
|
|
537
|
+
let data;
|
|
538
|
+
try {
|
|
539
|
+
data = yield response.json();
|
|
540
|
+
} catch (e) {
|
|
541
|
+
throw createApiError(
|
|
542
|
+
`Invalid JSON response from ${endpoint}`,
|
|
543
|
+
response.status,
|
|
544
|
+
void 0,
|
|
545
|
+
"Server returned an invalid response.",
|
|
546
|
+
"Check if the API endpoint is available."
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
if (data.error) {
|
|
550
|
+
const errorMessage = typeof data.error === "string" ? data.error : "Unknown API error";
|
|
551
|
+
throw createApiError(
|
|
552
|
+
errorMessage,
|
|
553
|
+
response.status,
|
|
554
|
+
data,
|
|
555
|
+
errorMessage,
|
|
556
|
+
"An error occurred while processing the request."
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
return data;
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
|
|
485
563
|
// src/core/api/base-api.ts
|
|
486
564
|
var BaseApi = class {
|
|
487
565
|
constructor(apiName, options) {
|
|
@@ -495,38 +573,17 @@ var BaseApi = class {
|
|
|
495
573
|
this.secretKey = options.secretKey;
|
|
496
574
|
this.baseUrl = options.baseUrl;
|
|
497
575
|
}
|
|
498
|
-
request(endpoint, body) {
|
|
576
|
+
request(endpoint, body, options) {
|
|
499
577
|
return __async(this, null, function* () {
|
|
500
|
-
|
|
501
|
-
|
|
578
|
+
var _a;
|
|
579
|
+
const method = (_a = options == null ? void 0 : options.method) != null ? _a : "POST";
|
|
580
|
+
const response = yield httpFetch(endpoint, __spreadValues(__spreadValues({
|
|
581
|
+
method,
|
|
502
582
|
clientKey: this.clientKey,
|
|
503
583
|
secretKey: this.secretKey,
|
|
504
|
-
baseUrl: this.baseUrl
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
let data;
|
|
508
|
-
try {
|
|
509
|
-
data = yield response.json();
|
|
510
|
-
} catch (e) {
|
|
511
|
-
throw createApiError(
|
|
512
|
-
`Invalid JSON response from ${endpoint}`,
|
|
513
|
-
response.status,
|
|
514
|
-
void 0,
|
|
515
|
-
"Server returned an invalid response.",
|
|
516
|
-
"Check if the API endpoint is available."
|
|
517
|
-
);
|
|
518
|
-
}
|
|
519
|
-
if (data.error) {
|
|
520
|
-
const errorMessage = typeof data.error === "string" ? data.error : "Unknown API error";
|
|
521
|
-
throw createApiError(
|
|
522
|
-
errorMessage,
|
|
523
|
-
response.status,
|
|
524
|
-
data,
|
|
525
|
-
errorMessage,
|
|
526
|
-
"An error occurred while processing the request."
|
|
527
|
-
);
|
|
528
|
-
}
|
|
529
|
-
return data;
|
|
584
|
+
baseUrl: this.baseUrl
|
|
585
|
+
}, body !== void 0 && { body: JSON.stringify(body) }), (options == null ? void 0 : options.headers) && { headers: options.headers }));
|
|
586
|
+
return parseApiResponse(response, endpoint);
|
|
530
587
|
});
|
|
531
588
|
}
|
|
532
589
|
};
|
|
@@ -616,29 +673,7 @@ var CartApi = class {
|
|
|
616
673
|
customerToken: token != null ? token : void 0,
|
|
617
674
|
baseUrl: this.baseUrl
|
|
618
675
|
}, token && this.onUnauthorized && { onUnauthorized: this.onUnauthorized }), body !== void 0 && { body: JSON.stringify(body) }));
|
|
619
|
-
|
|
620
|
-
try {
|
|
621
|
-
data = yield response.json();
|
|
622
|
-
} catch (e) {
|
|
623
|
-
throw createApiError(
|
|
624
|
-
`Invalid JSON response from ${endpoint}`,
|
|
625
|
-
response.status,
|
|
626
|
-
void 0,
|
|
627
|
-
"Server returned an invalid response.",
|
|
628
|
-
"Check if the API endpoint is available."
|
|
629
|
-
);
|
|
630
|
-
}
|
|
631
|
-
if (data.error) {
|
|
632
|
-
const errorMessage = typeof data.error === "string" ? data.error : "Unknown API error";
|
|
633
|
-
throw createApiError(
|
|
634
|
-
errorMessage,
|
|
635
|
-
response.status,
|
|
636
|
-
data,
|
|
637
|
-
errorMessage,
|
|
638
|
-
"An error occurred while processing the request."
|
|
639
|
-
);
|
|
640
|
-
}
|
|
641
|
-
return data;
|
|
676
|
+
return parseApiResponse(response, endpoint);
|
|
642
677
|
});
|
|
643
678
|
}
|
|
644
679
|
getCart(cartId) {
|
|
@@ -938,7 +973,7 @@ var HttpClient = class {
|
|
|
938
973
|
*/
|
|
939
974
|
parseFindResponse(response) {
|
|
940
975
|
return __async(this, null, function* () {
|
|
941
|
-
var _a, _b;
|
|
976
|
+
var _a, _b, _c, _d, _e, _f;
|
|
942
977
|
const contentType = response.headers.get("content-type");
|
|
943
978
|
try {
|
|
944
979
|
this.assertJsonResponse(response);
|
|
@@ -950,15 +985,15 @@ var HttpClient = class {
|
|
|
950
985
|
}
|
|
951
986
|
return {
|
|
952
987
|
docs: jsonData.docs,
|
|
953
|
-
totalDocs: jsonData.totalDocs
|
|
988
|
+
totalDocs: (_a = jsonData.totalDocs) != null ? _a : 0,
|
|
954
989
|
limit: jsonData.limit || 20,
|
|
955
|
-
totalPages: jsonData.totalPages
|
|
990
|
+
totalPages: (_b = jsonData.totalPages) != null ? _b : 0,
|
|
956
991
|
page: jsonData.page || 1,
|
|
957
992
|
pagingCounter: jsonData.pagingCounter || 1,
|
|
958
|
-
hasPrevPage: jsonData.hasPrevPage
|
|
959
|
-
hasNextPage: jsonData.hasNextPage
|
|
960
|
-
prevPage: (
|
|
961
|
-
nextPage: (
|
|
993
|
+
hasPrevPage: (_c = jsonData.hasPrevPage) != null ? _c : false,
|
|
994
|
+
hasNextPage: (_d = jsonData.hasNextPage) != null ? _d : false,
|
|
995
|
+
prevPage: (_e = jsonData.prevPage) != null ? _e : null,
|
|
996
|
+
nextPage: (_f = jsonData.nextPage) != null ? _f : null
|
|
962
997
|
};
|
|
963
998
|
} catch (error) {
|
|
964
999
|
if (error instanceof SDKError) throw error;
|
|
@@ -1203,6 +1238,7 @@ var COLLECTIONS = [
|
|
|
1203
1238
|
"document-categories",
|
|
1204
1239
|
"document-types",
|
|
1205
1240
|
"posts",
|
|
1241
|
+
"post-authors",
|
|
1206
1242
|
"post-categories",
|
|
1207
1243
|
"post-tags",
|
|
1208
1244
|
"playlists",
|
|
@@ -1229,23 +1265,33 @@ var COLLECTIONS = [
|
|
|
1229
1265
|
|
|
1230
1266
|
// src/core/customer/customer-auth.ts
|
|
1231
1267
|
var DEFAULT_TIMEOUT2 = 15e3;
|
|
1268
|
+
function safeGetItem(key) {
|
|
1269
|
+
try {
|
|
1270
|
+
return localStorage.getItem(key);
|
|
1271
|
+
} catch (e) {
|
|
1272
|
+
return null;
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1232
1275
|
var CustomerAuth = class {
|
|
1233
1276
|
constructor(clientKey, baseUrl, options) {
|
|
1234
1277
|
this.refreshPromise = null;
|
|
1235
|
-
var _a, _b
|
|
1278
|
+
var _a, _b;
|
|
1236
1279
|
this.clientKey = clientKey;
|
|
1237
1280
|
this.baseUrl = baseUrl;
|
|
1238
1281
|
const persist = (_a = options == null ? void 0 : options.persist) != null ? _a : true;
|
|
1239
1282
|
if (persist) {
|
|
1240
1283
|
const key = typeof persist === "string" ? persist : "customer-token";
|
|
1241
1284
|
const isBrowser = typeof window !== "undefined";
|
|
1242
|
-
this.token = isBrowser ? (
|
|
1285
|
+
this.token = isBrowser ? safeGetItem(key) : null;
|
|
1243
1286
|
this.onTokenChange = isBrowser ? (token) => {
|
|
1244
|
-
|
|
1245
|
-
|
|
1287
|
+
try {
|
|
1288
|
+
if (token) localStorage.setItem(key, token);
|
|
1289
|
+
else localStorage.removeItem(key);
|
|
1290
|
+
} catch (e) {
|
|
1291
|
+
}
|
|
1246
1292
|
} : void 0;
|
|
1247
1293
|
} else {
|
|
1248
|
-
this.token = (
|
|
1294
|
+
this.token = (_b = options == null ? void 0 : options.token) != null ? _b : null;
|
|
1249
1295
|
this.onTokenChange = options == null ? void 0 : options.onTokenChange;
|
|
1250
1296
|
}
|
|
1251
1297
|
}
|
|
@@ -2054,6 +2100,157 @@ function createServerClient(options) {
|
|
|
2054
2100
|
return new ServerClient(options);
|
|
2055
2101
|
}
|
|
2056
2102
|
|
|
2103
|
+
// src/core/query/realtime.ts
|
|
2104
|
+
var INITIAL_RECONNECT_DELAY = 1e3;
|
|
2105
|
+
var MAX_RECONNECT_DELAY = 3e4;
|
|
2106
|
+
var RECONNECT_BACKOFF_FACTOR = 2;
|
|
2107
|
+
var MAX_NO_TOKEN_RETRIES = 5;
|
|
2108
|
+
var RealtimeConnection = class {
|
|
2109
|
+
constructor(baseUrl, clientKey, getToken, collections) {
|
|
2110
|
+
this.baseUrl = baseUrl;
|
|
2111
|
+
this.clientKey = clientKey;
|
|
2112
|
+
this.getToken = getToken;
|
|
2113
|
+
this.collections = collections;
|
|
2114
|
+
this.abortController = null;
|
|
2115
|
+
this.reconnectAttempt = 0;
|
|
2116
|
+
this.noTokenAttempts = 0;
|
|
2117
|
+
this.reconnectTimer = null;
|
|
2118
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
2119
|
+
this._connected = false;
|
|
2120
|
+
}
|
|
2121
|
+
get connected() {
|
|
2122
|
+
return this._connected;
|
|
2123
|
+
}
|
|
2124
|
+
addListener(fn) {
|
|
2125
|
+
this.listeners.add(fn);
|
|
2126
|
+
return () => this.listeners.delete(fn);
|
|
2127
|
+
}
|
|
2128
|
+
connect() {
|
|
2129
|
+
if (this.abortController) return;
|
|
2130
|
+
this.abortController = new AbortController();
|
|
2131
|
+
this.startStream(this.abortController.signal);
|
|
2132
|
+
}
|
|
2133
|
+
disconnect() {
|
|
2134
|
+
this._connected = false;
|
|
2135
|
+
if (this.reconnectTimer) {
|
|
2136
|
+
clearTimeout(this.reconnectTimer);
|
|
2137
|
+
this.reconnectTimer = null;
|
|
2138
|
+
}
|
|
2139
|
+
if (this.abortController) {
|
|
2140
|
+
this.abortController.abort();
|
|
2141
|
+
this.abortController = null;
|
|
2142
|
+
}
|
|
2143
|
+
this.reconnectAttempt = 0;
|
|
2144
|
+
this.noTokenAttempts = 0;
|
|
2145
|
+
}
|
|
2146
|
+
startStream(signal) {
|
|
2147
|
+
return __async(this, null, function* () {
|
|
2148
|
+
var _a;
|
|
2149
|
+
const token = this.getToken();
|
|
2150
|
+
if (!token) {
|
|
2151
|
+
this.noTokenAttempts++;
|
|
2152
|
+
if (this.noTokenAttempts >= MAX_NO_TOKEN_RETRIES) {
|
|
2153
|
+
this._connected = false;
|
|
2154
|
+
this.abortController = null;
|
|
2155
|
+
return;
|
|
2156
|
+
}
|
|
2157
|
+
this.scheduleReconnect();
|
|
2158
|
+
return;
|
|
2159
|
+
}
|
|
2160
|
+
this.noTokenAttempts = 0;
|
|
2161
|
+
const params = ((_a = this.collections) == null ? void 0 : _a.length) ? `?collections=${this.collections.join(",")}` : "";
|
|
2162
|
+
const url = `${this.baseUrl}/api/events/stream${params}`;
|
|
2163
|
+
try {
|
|
2164
|
+
const response = yield fetch(url, {
|
|
2165
|
+
headers: {
|
|
2166
|
+
"X-Client-Key": this.clientKey,
|
|
2167
|
+
Authorization: `Bearer ${token}`
|
|
2168
|
+
},
|
|
2169
|
+
signal
|
|
2170
|
+
});
|
|
2171
|
+
if (!response.ok) {
|
|
2172
|
+
if (response.status === 401) {
|
|
2173
|
+
this.scheduleReconnect();
|
|
2174
|
+
return;
|
|
2175
|
+
}
|
|
2176
|
+
throw new Error(`SSE connection failed: ${response.status}`);
|
|
2177
|
+
}
|
|
2178
|
+
if (!response.body) {
|
|
2179
|
+
throw new Error("SSE response has no body");
|
|
2180
|
+
}
|
|
2181
|
+
this._connected = true;
|
|
2182
|
+
this.reconnectAttempt = 0;
|
|
2183
|
+
yield this.readStream(response.body, signal);
|
|
2184
|
+
} catch (e) {
|
|
2185
|
+
if (signal.aborted) return;
|
|
2186
|
+
this._connected = false;
|
|
2187
|
+
this.scheduleReconnect();
|
|
2188
|
+
}
|
|
2189
|
+
});
|
|
2190
|
+
}
|
|
2191
|
+
readStream(body, signal) {
|
|
2192
|
+
return __async(this, null, function* () {
|
|
2193
|
+
var _a;
|
|
2194
|
+
const reader = body.getReader();
|
|
2195
|
+
const decoder = new TextDecoder();
|
|
2196
|
+
let buffer = "";
|
|
2197
|
+
let currentEvent = "";
|
|
2198
|
+
let currentData = "";
|
|
2199
|
+
try {
|
|
2200
|
+
while (true) {
|
|
2201
|
+
const { done, value } = yield reader.read();
|
|
2202
|
+
if (done || signal.aborted) break;
|
|
2203
|
+
buffer += decoder.decode(value, { stream: true });
|
|
2204
|
+
const lines = buffer.split("\n");
|
|
2205
|
+
buffer = (_a = lines.pop()) != null ? _a : "";
|
|
2206
|
+
for (const line of lines) {
|
|
2207
|
+
if (line.startsWith("event: ")) {
|
|
2208
|
+
currentEvent = line.slice(7);
|
|
2209
|
+
} else if (line.startsWith("data: ")) {
|
|
2210
|
+
currentData += (currentData ? "\n" : "") + line.slice(6);
|
|
2211
|
+
} else if (line === "") {
|
|
2212
|
+
if (currentEvent === "collection:change" && currentData) {
|
|
2213
|
+
try {
|
|
2214
|
+
const event = JSON.parse(currentData);
|
|
2215
|
+
for (const listener of this.listeners) {
|
|
2216
|
+
try {
|
|
2217
|
+
listener(event);
|
|
2218
|
+
} catch (e) {
|
|
2219
|
+
}
|
|
2220
|
+
}
|
|
2221
|
+
} catch (e) {
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
currentEvent = "";
|
|
2225
|
+
currentData = "";
|
|
2226
|
+
}
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
} catch (e) {
|
|
2230
|
+
} finally {
|
|
2231
|
+
reader.releaseLock();
|
|
2232
|
+
this._connected = false;
|
|
2233
|
+
if (!signal.aborted) {
|
|
2234
|
+
this.scheduleReconnect();
|
|
2235
|
+
}
|
|
2236
|
+
}
|
|
2237
|
+
});
|
|
2238
|
+
}
|
|
2239
|
+
scheduleReconnect() {
|
|
2240
|
+
if (this.reconnectTimer) return;
|
|
2241
|
+
const delay2 = Math.min(
|
|
2242
|
+
INITIAL_RECONNECT_DELAY * Math.pow(RECONNECT_BACKOFF_FACTOR, this.reconnectAttempt),
|
|
2243
|
+
MAX_RECONNECT_DELAY
|
|
2244
|
+
);
|
|
2245
|
+
this.reconnectAttempt++;
|
|
2246
|
+
this.reconnectTimer = setTimeout(() => {
|
|
2247
|
+
this.reconnectTimer = null;
|
|
2248
|
+
this.abortController = new AbortController();
|
|
2249
|
+
this.startStream(this.abortController.signal);
|
|
2250
|
+
}, delay2);
|
|
2251
|
+
}
|
|
2252
|
+
};
|
|
2253
|
+
|
|
2057
2254
|
// src/core/webhook/index.ts
|
|
2058
2255
|
function isValidWebhookEvent(data) {
|
|
2059
2256
|
if (typeof data !== "object" || data === null) return false;
|
|
@@ -2062,22 +2259,22 @@ function isValidWebhookEvent(data) {
|
|
|
2062
2259
|
}
|
|
2063
2260
|
function verifySignature(payload, secret, signature) {
|
|
2064
2261
|
return __async(this, null, function* () {
|
|
2262
|
+
var _a;
|
|
2065
2263
|
const encoder = new TextEncoder();
|
|
2066
2264
|
const key = yield crypto.subtle.importKey(
|
|
2067
2265
|
"raw",
|
|
2068
2266
|
encoder.encode(secret),
|
|
2069
2267
|
{ name: "HMAC", hash: "SHA-256" },
|
|
2070
2268
|
false,
|
|
2071
|
-
["
|
|
2269
|
+
["verify"]
|
|
2072
2270
|
);
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
let result = expected.length !== signature.length ? 1 : 0;
|
|
2076
|
-
const len = Math.max(expected.length, signature.length);
|
|
2077
|
-
for (let i = 0; i < len; i++) {
|
|
2078
|
-
result |= (expected.charCodeAt(i) || 0) ^ (signature.charCodeAt(i) || 0);
|
|
2271
|
+
if (signature.length % 2 !== 0 || !/^[0-9a-fA-F]*$/.test(signature)) {
|
|
2272
|
+
return false;
|
|
2079
2273
|
}
|
|
2080
|
-
|
|
2274
|
+
const sigBytes = new Uint8Array(
|
|
2275
|
+
((_a = signature.match(/.{2}/g)) != null ? _a : []).map((byte) => parseInt(byte, 16))
|
|
2276
|
+
);
|
|
2277
|
+
return crypto.subtle.verify("HMAC", key, sigBytes, encoder.encode(payload));
|
|
2081
2278
|
});
|
|
2082
2279
|
}
|
|
2083
2280
|
function handleWebhook(request, handler, options) {
|
|
@@ -2199,9 +2396,10 @@ function getImagePlaceholderStyle(image, options) {
|
|
|
2199
2396
|
// src/utils/order/generateOrderNumber.ts
|
|
2200
2397
|
var generateOrderNumber = () => {
|
|
2201
2398
|
var _a;
|
|
2202
|
-
const
|
|
2203
|
-
const
|
|
2204
|
-
const
|
|
2399
|
+
const now = /* @__PURE__ */ new Date();
|
|
2400
|
+
const year = now.getFullYear().toString().slice(-2);
|
|
2401
|
+
const month = (now.getMonth() + 1).toString().padStart(2, "0");
|
|
2402
|
+
const day = now.getDate().toString().padStart(2, "0");
|
|
2205
2403
|
const array = new Uint32Array(1);
|
|
2206
2404
|
globalThis.crypto.getRandomValues(array);
|
|
2207
2405
|
const random = (((_a = array[0]) != null ? _a : 0) % 1e6).toString().padStart(6, "0");
|
|
@@ -2267,6 +2465,7 @@ export {
|
|
|
2267
2465
|
OrderApi,
|
|
2268
2466
|
ProductApi,
|
|
2269
2467
|
QueryHooks,
|
|
2468
|
+
RealtimeConnection,
|
|
2270
2469
|
SDKError,
|
|
2271
2470
|
ServerClient,
|
|
2272
2471
|
ServiceUnavailableError,
|