@01.software/sdk 0.3.0 → 0.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/README.md +5 -2
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/const-BpirbGBD.d.cts +19 -0
- package/dist/const-qZSQiSSC.d.ts +19 -0
- package/dist/index.cjs +228 -78
- 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 +228 -78
- package/dist/index.js.map +1 -1
- package/dist/{payload-types-ggU6BNuH.d.cts → payload-types-CZiaL4Wr.d.cts} +6 -5
- package/dist/{payload-types-ggU6BNuH.d.ts → payload-types-CZiaL4Wr.d.ts} +6 -5
- package/dist/realtime-DupPIYx-.d.cts +33 -0
- package/dist/realtime-DupPIYx-.d.ts +33 -0
- package/dist/realtime.cjs +261 -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 +239 -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 +18 -3
- package/dist/ui/flow.cjs.map +1 -1
- package/dist/ui/flow.js +18 -3
- 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/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-3iL9OEyq.d.cts +20 -0
- package/dist/webhook-DuTqrH9x.d.ts +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 +29 -4
- package/dist/webhook-B54a-HGd.d.ts +0 -35
- package/dist/webhook-DInps2xX.d.cts +0 -35
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@ npm install @01.software/sdk
|
|
|
10
10
|
pnpm add @01.software/sdk
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
+
|
|
13
14
|
## Features
|
|
14
15
|
|
|
15
16
|
- Full TypeScript type inference
|
|
@@ -47,6 +48,7 @@ import { Image } from '@01.software/sdk/ui/image'
|
|
|
47
48
|
import { FormRenderer } from '@01.software/sdk/ui/form'
|
|
48
49
|
import { CodeBlock } from '@01.software/sdk/ui/code-block'
|
|
49
50
|
import { FlowRenderer } from '@01.software/sdk/ui/flow'
|
|
51
|
+
import { VideoPlayer } from '@01.software/sdk/ui/video'
|
|
50
52
|
```
|
|
51
53
|
|
|
52
54
|
## Getting Started
|
|
@@ -79,11 +81,12 @@ const client = createServerClient({
|
|
|
79
81
|
|
|
80
82
|
// Create order (server only)
|
|
81
83
|
const order = await client.api.createOrder({
|
|
82
|
-
paymentId: 'pay_123',
|
|
83
84
|
orderNumber: generateOrderNumber(),
|
|
84
|
-
email: 'user@example.com',
|
|
85
|
+
customerSnapshot: { email: 'user@example.com' },
|
|
86
|
+
shippingAddress: { recipientName: 'John', phone: '010-1234-5678', postalCode: '12345', address1: 'Seoul', address2: 'Apt 101' },
|
|
85
87
|
orderProducts: [...],
|
|
86
88
|
totalAmount: 10000,
|
|
89
|
+
paymentId: 'pay_123', // optional (omit for free orders)
|
|
87
90
|
discountCode: 'WELCOME10', // optional
|
|
88
91
|
})
|
|
89
92
|
|
package/dist/auth.d.cts
CHANGED
package/dist/auth.d.ts
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { C as Config } from './payload-types-CZiaL4Wr.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Collection type derived from Payload Config.
|
|
5
|
+
* This ensures type safety and automatic synchronization with payload-types.ts
|
|
6
|
+
*/
|
|
7
|
+
type Collection = keyof Config['collections'];
|
|
8
|
+
/**
|
|
9
|
+
* Array of all public collection names for runtime use (e.g., Zod enum validation).
|
|
10
|
+
* This is the single source of truth for which collections are publicly accessible via SDK.
|
|
11
|
+
*/
|
|
12
|
+
declare const COLLECTIONS: readonly ["tenants", "tenant-metadata", "tenant-logos", "products", "product-variants", "product-options", "product-categories", "product-tags", "product-collections", "brands", "brand-logos", "orders", "order-products", "returns", "return-products", "exchanges", "exchange-products", "fulfillments", "fulfillment-items", "transactions", "customers", "customer-addresses", "customer-groups", "carts", "cart-items", "discounts", "shipping-policies", "documents", "document-categories", "document-types", "posts", "post-authors", "post-categories", "post-tags", "playlists", "playlist-categories", "playlist-tags", "musics", "galleries", "gallery-categories", "gallery-tags", "gallery-items", "flows", "flow-node-types", "flow-edge-types", "flow-categories", "flow-tags", "videos", "video-categories", "video-tags", "live-streams", "images", "forms", "form-submissions"];
|
|
13
|
+
/**
|
|
14
|
+
* Public collections available for SDK access.
|
|
15
|
+
* Derived from the COLLECTIONS array (single source of truth).
|
|
16
|
+
*/
|
|
17
|
+
type PublicCollection = (typeof COLLECTIONS)[number];
|
|
18
|
+
|
|
19
|
+
export { type Collection as C, type PublicCollection as P, COLLECTIONS as a };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { C as Config } from './payload-types-CZiaL4Wr.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Collection type derived from Payload Config.
|
|
5
|
+
* This ensures type safety and automatic synchronization with payload-types.ts
|
|
6
|
+
*/
|
|
7
|
+
type Collection = keyof Config['collections'];
|
|
8
|
+
/**
|
|
9
|
+
* Array of all public collection names for runtime use (e.g., Zod enum validation).
|
|
10
|
+
* This is the single source of truth for which collections are publicly accessible via SDK.
|
|
11
|
+
*/
|
|
12
|
+
declare const COLLECTIONS: readonly ["tenants", "tenant-metadata", "tenant-logos", "products", "product-variants", "product-options", "product-categories", "product-tags", "product-collections", "brands", "brand-logos", "orders", "order-products", "returns", "return-products", "exchanges", "exchange-products", "fulfillments", "fulfillment-items", "transactions", "customers", "customer-addresses", "customer-groups", "carts", "cart-items", "discounts", "shipping-policies", "documents", "document-categories", "document-types", "posts", "post-authors", "post-categories", "post-tags", "playlists", "playlist-categories", "playlist-tags", "musics", "galleries", "gallery-categories", "gallery-tags", "gallery-items", "flows", "flow-node-types", "flow-edge-types", "flow-categories", "flow-tags", "videos", "video-categories", "video-tags", "live-streams", "images", "forms", "form-submissions"];
|
|
13
|
+
/**
|
|
14
|
+
* Public collections available for SDK access.
|
|
15
|
+
* Derived from the COLLECTIONS array (single source of truth).
|
|
16
|
+
*/
|
|
17
|
+
type PublicCollection = (typeof COLLECTIONS)[number];
|
|
18
|
+
|
|
19
|
+
export { type Collection as C, type PublicCollection as P, COLLECTIONS as a };
|
package/dist/index.cjs
CHANGED
|
@@ -87,6 +87,7 @@ __export(src_exports, {
|
|
|
87
87
|
OrderApi: () => OrderApi,
|
|
88
88
|
ProductApi: () => ProductApi,
|
|
89
89
|
QueryHooks: () => QueryHooks,
|
|
90
|
+
RealtimeConnection: () => RealtimeConnection,
|
|
90
91
|
SDKError: () => SDKError,
|
|
91
92
|
ServerClient: () => ServerClient,
|
|
92
93
|
ServiceUnavailableError: () => ServiceUnavailableError,
|
|
@@ -215,7 +216,7 @@ function parseApiKey(apiKey) {
|
|
|
215
216
|
}
|
|
216
217
|
|
|
217
218
|
// src/core/internal/errors/index.ts
|
|
218
|
-
var SDKError = class
|
|
219
|
+
var SDKError = class extends Error {
|
|
219
220
|
constructor(code, message, status, details, userMessage, suggestion) {
|
|
220
221
|
super(message);
|
|
221
222
|
this.name = "SDKError";
|
|
@@ -225,7 +226,7 @@ var SDKError = class _SDKError extends Error {
|
|
|
225
226
|
this.userMessage = userMessage;
|
|
226
227
|
this.suggestion = suggestion;
|
|
227
228
|
if (Error.captureStackTrace) {
|
|
228
|
-
Error.captureStackTrace(this,
|
|
229
|
+
Error.captureStackTrace(this, new.target);
|
|
229
230
|
}
|
|
230
231
|
}
|
|
231
232
|
getUserMessage() {
|
|
@@ -423,7 +424,7 @@ function httpFetch(url, options) {
|
|
|
423
424
|
const redactedHeaders = Object.fromEntries(headers.entries());
|
|
424
425
|
if (redactedHeaders["authorization"]) {
|
|
425
426
|
const token = redactedHeaders["authorization"];
|
|
426
|
-
redactedHeaders["authorization"] = token.length >
|
|
427
|
+
redactedHeaders["authorization"] = token.length > 20 ? `Bearer ...****${token.slice(-8)}` : "****";
|
|
427
428
|
}
|
|
428
429
|
debugLog(debug, "request", url, {
|
|
429
430
|
method: requestInit.method || "GET",
|
|
@@ -562,6 +563,35 @@ function httpFetch(url, options) {
|
|
|
562
563
|
});
|
|
563
564
|
}
|
|
564
565
|
|
|
566
|
+
// src/core/api/parse-response.ts
|
|
567
|
+
function parseApiResponse(response, endpoint) {
|
|
568
|
+
return __async(this, null, function* () {
|
|
569
|
+
let data;
|
|
570
|
+
try {
|
|
571
|
+
data = yield response.json();
|
|
572
|
+
} catch (e) {
|
|
573
|
+
throw createApiError(
|
|
574
|
+
`Invalid JSON response from ${endpoint}`,
|
|
575
|
+
response.status,
|
|
576
|
+
void 0,
|
|
577
|
+
"Server returned an invalid response.",
|
|
578
|
+
"Check if the API endpoint is available."
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
if (data.error) {
|
|
582
|
+
const errorMessage = typeof data.error === "string" ? data.error : "Unknown API error";
|
|
583
|
+
throw createApiError(
|
|
584
|
+
errorMessage,
|
|
585
|
+
response.status,
|
|
586
|
+
data,
|
|
587
|
+
errorMessage,
|
|
588
|
+
"An error occurred while processing the request."
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
return data;
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
|
|
565
595
|
// src/core/api/base-api.ts
|
|
566
596
|
var BaseApi = class {
|
|
567
597
|
constructor(apiName, options) {
|
|
@@ -575,38 +605,17 @@ var BaseApi = class {
|
|
|
575
605
|
this.secretKey = options.secretKey;
|
|
576
606
|
this.baseUrl = options.baseUrl;
|
|
577
607
|
}
|
|
578
|
-
request(endpoint, body) {
|
|
608
|
+
request(endpoint, body, options) {
|
|
579
609
|
return __async(this, null, function* () {
|
|
580
|
-
|
|
581
|
-
|
|
610
|
+
var _a;
|
|
611
|
+
const method = (_a = options == null ? void 0 : options.method) != null ? _a : "POST";
|
|
612
|
+
const response = yield httpFetch(endpoint, __spreadValues(__spreadValues({
|
|
613
|
+
method,
|
|
582
614
|
clientKey: this.clientKey,
|
|
583
615
|
secretKey: this.secretKey,
|
|
584
|
-
baseUrl: this.baseUrl
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
let data;
|
|
588
|
-
try {
|
|
589
|
-
data = yield response.json();
|
|
590
|
-
} catch (e) {
|
|
591
|
-
throw createApiError(
|
|
592
|
-
`Invalid JSON response from ${endpoint}`,
|
|
593
|
-
response.status,
|
|
594
|
-
void 0,
|
|
595
|
-
"Server returned an invalid response.",
|
|
596
|
-
"Check if the API endpoint is available."
|
|
597
|
-
);
|
|
598
|
-
}
|
|
599
|
-
if (data.error) {
|
|
600
|
-
const errorMessage = typeof data.error === "string" ? data.error : "Unknown API error";
|
|
601
|
-
throw createApiError(
|
|
602
|
-
errorMessage,
|
|
603
|
-
response.status,
|
|
604
|
-
data,
|
|
605
|
-
errorMessage,
|
|
606
|
-
"An error occurred while processing the request."
|
|
607
|
-
);
|
|
608
|
-
}
|
|
609
|
-
return data;
|
|
616
|
+
baseUrl: this.baseUrl
|
|
617
|
+
}, body !== void 0 && { body: JSON.stringify(body) }), (options == null ? void 0 : options.headers) && { headers: options.headers }));
|
|
618
|
+
return parseApiResponse(response, endpoint);
|
|
610
619
|
});
|
|
611
620
|
}
|
|
612
621
|
};
|
|
@@ -696,29 +705,7 @@ var CartApi = class {
|
|
|
696
705
|
customerToken: token != null ? token : void 0,
|
|
697
706
|
baseUrl: this.baseUrl
|
|
698
707
|
}, token && this.onUnauthorized && { onUnauthorized: this.onUnauthorized }), body !== void 0 && { body: JSON.stringify(body) }));
|
|
699
|
-
|
|
700
|
-
try {
|
|
701
|
-
data = yield response.json();
|
|
702
|
-
} catch (e) {
|
|
703
|
-
throw createApiError(
|
|
704
|
-
`Invalid JSON response from ${endpoint}`,
|
|
705
|
-
response.status,
|
|
706
|
-
void 0,
|
|
707
|
-
"Server returned an invalid response.",
|
|
708
|
-
"Check if the API endpoint is available."
|
|
709
|
-
);
|
|
710
|
-
}
|
|
711
|
-
if (data.error) {
|
|
712
|
-
const errorMessage = typeof data.error === "string" ? data.error : "Unknown API error";
|
|
713
|
-
throw createApiError(
|
|
714
|
-
errorMessage,
|
|
715
|
-
response.status,
|
|
716
|
-
data,
|
|
717
|
-
errorMessage,
|
|
718
|
-
"An error occurred while processing the request."
|
|
719
|
-
);
|
|
720
|
-
}
|
|
721
|
-
return data;
|
|
708
|
+
return parseApiResponse(response, endpoint);
|
|
722
709
|
});
|
|
723
710
|
}
|
|
724
711
|
getCart(cartId) {
|
|
@@ -1018,7 +1005,7 @@ var HttpClient = class {
|
|
|
1018
1005
|
*/
|
|
1019
1006
|
parseFindResponse(response) {
|
|
1020
1007
|
return __async(this, null, function* () {
|
|
1021
|
-
var _a, _b;
|
|
1008
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1022
1009
|
const contentType = response.headers.get("content-type");
|
|
1023
1010
|
try {
|
|
1024
1011
|
this.assertJsonResponse(response);
|
|
@@ -1030,15 +1017,15 @@ var HttpClient = class {
|
|
|
1030
1017
|
}
|
|
1031
1018
|
return {
|
|
1032
1019
|
docs: jsonData.docs,
|
|
1033
|
-
totalDocs: jsonData.totalDocs
|
|
1020
|
+
totalDocs: (_a = jsonData.totalDocs) != null ? _a : 0,
|
|
1034
1021
|
limit: jsonData.limit || 20,
|
|
1035
|
-
totalPages: jsonData.totalPages
|
|
1022
|
+
totalPages: (_b = jsonData.totalPages) != null ? _b : 0,
|
|
1036
1023
|
page: jsonData.page || 1,
|
|
1037
1024
|
pagingCounter: jsonData.pagingCounter || 1,
|
|
1038
|
-
hasPrevPage: jsonData.hasPrevPage
|
|
1039
|
-
hasNextPage: jsonData.hasNextPage
|
|
1040
|
-
prevPage: (
|
|
1041
|
-
nextPage: (
|
|
1025
|
+
hasPrevPage: (_c = jsonData.hasPrevPage) != null ? _c : false,
|
|
1026
|
+
hasNextPage: (_d = jsonData.hasNextPage) != null ? _d : false,
|
|
1027
|
+
prevPage: (_e = jsonData.prevPage) != null ? _e : null,
|
|
1028
|
+
nextPage: (_f = jsonData.nextPage) != null ? _f : null
|
|
1042
1029
|
};
|
|
1043
1030
|
} catch (error) {
|
|
1044
1031
|
if (error instanceof SDKError) throw error;
|
|
@@ -1283,6 +1270,7 @@ var COLLECTIONS = [
|
|
|
1283
1270
|
"document-categories",
|
|
1284
1271
|
"document-types",
|
|
1285
1272
|
"posts",
|
|
1273
|
+
"post-authors",
|
|
1286
1274
|
"post-categories",
|
|
1287
1275
|
"post-tags",
|
|
1288
1276
|
"playlists",
|
|
@@ -1309,23 +1297,33 @@ var COLLECTIONS = [
|
|
|
1309
1297
|
|
|
1310
1298
|
// src/core/customer/customer-auth.ts
|
|
1311
1299
|
var DEFAULT_TIMEOUT2 = 15e3;
|
|
1300
|
+
function safeGetItem(key) {
|
|
1301
|
+
try {
|
|
1302
|
+
return localStorage.getItem(key);
|
|
1303
|
+
} catch (e) {
|
|
1304
|
+
return null;
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1312
1307
|
var CustomerAuth = class {
|
|
1313
1308
|
constructor(clientKey, baseUrl, options) {
|
|
1314
1309
|
this.refreshPromise = null;
|
|
1315
|
-
var _a, _b
|
|
1310
|
+
var _a, _b;
|
|
1316
1311
|
this.clientKey = clientKey;
|
|
1317
1312
|
this.baseUrl = baseUrl;
|
|
1318
1313
|
const persist = (_a = options == null ? void 0 : options.persist) != null ? _a : true;
|
|
1319
1314
|
if (persist) {
|
|
1320
1315
|
const key = typeof persist === "string" ? persist : "customer-token";
|
|
1321
1316
|
const isBrowser = typeof window !== "undefined";
|
|
1322
|
-
this.token = isBrowser ? (
|
|
1317
|
+
this.token = isBrowser ? safeGetItem(key) : null;
|
|
1323
1318
|
this.onTokenChange = isBrowser ? (token) => {
|
|
1324
|
-
|
|
1325
|
-
|
|
1319
|
+
try {
|
|
1320
|
+
if (token) localStorage.setItem(key, token);
|
|
1321
|
+
else localStorage.removeItem(key);
|
|
1322
|
+
} catch (e) {
|
|
1323
|
+
}
|
|
1326
1324
|
} : void 0;
|
|
1327
1325
|
} else {
|
|
1328
|
-
this.token = (
|
|
1326
|
+
this.token = (_b = options == null ? void 0 : options.token) != null ? _b : null;
|
|
1329
1327
|
this.onTokenChange = options == null ? void 0 : options.onTokenChange;
|
|
1330
1328
|
}
|
|
1331
1329
|
}
|
|
@@ -2121,6 +2119,157 @@ function createServerClient(options) {
|
|
|
2121
2119
|
return new ServerClient(options);
|
|
2122
2120
|
}
|
|
2123
2121
|
|
|
2122
|
+
// src/core/query/realtime.ts
|
|
2123
|
+
var INITIAL_RECONNECT_DELAY = 1e3;
|
|
2124
|
+
var MAX_RECONNECT_DELAY = 3e4;
|
|
2125
|
+
var RECONNECT_BACKOFF_FACTOR = 2;
|
|
2126
|
+
var MAX_NO_TOKEN_RETRIES = 5;
|
|
2127
|
+
var RealtimeConnection = class {
|
|
2128
|
+
constructor(baseUrl, clientKey, getToken, collections) {
|
|
2129
|
+
this.baseUrl = baseUrl;
|
|
2130
|
+
this.clientKey = clientKey;
|
|
2131
|
+
this.getToken = getToken;
|
|
2132
|
+
this.collections = collections;
|
|
2133
|
+
this.abortController = null;
|
|
2134
|
+
this.reconnectAttempt = 0;
|
|
2135
|
+
this.noTokenAttempts = 0;
|
|
2136
|
+
this.reconnectTimer = null;
|
|
2137
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
2138
|
+
this._connected = false;
|
|
2139
|
+
}
|
|
2140
|
+
get connected() {
|
|
2141
|
+
return this._connected;
|
|
2142
|
+
}
|
|
2143
|
+
addListener(fn) {
|
|
2144
|
+
this.listeners.add(fn);
|
|
2145
|
+
return () => this.listeners.delete(fn);
|
|
2146
|
+
}
|
|
2147
|
+
connect() {
|
|
2148
|
+
if (this.abortController) return;
|
|
2149
|
+
this.abortController = new AbortController();
|
|
2150
|
+
this.startStream(this.abortController.signal);
|
|
2151
|
+
}
|
|
2152
|
+
disconnect() {
|
|
2153
|
+
this._connected = false;
|
|
2154
|
+
if (this.reconnectTimer) {
|
|
2155
|
+
clearTimeout(this.reconnectTimer);
|
|
2156
|
+
this.reconnectTimer = null;
|
|
2157
|
+
}
|
|
2158
|
+
if (this.abortController) {
|
|
2159
|
+
this.abortController.abort();
|
|
2160
|
+
this.abortController = null;
|
|
2161
|
+
}
|
|
2162
|
+
this.reconnectAttempt = 0;
|
|
2163
|
+
this.noTokenAttempts = 0;
|
|
2164
|
+
}
|
|
2165
|
+
startStream(signal) {
|
|
2166
|
+
return __async(this, null, function* () {
|
|
2167
|
+
var _a;
|
|
2168
|
+
const token = this.getToken();
|
|
2169
|
+
if (!token) {
|
|
2170
|
+
this.noTokenAttempts++;
|
|
2171
|
+
if (this.noTokenAttempts >= MAX_NO_TOKEN_RETRIES) {
|
|
2172
|
+
this._connected = false;
|
|
2173
|
+
this.abortController = null;
|
|
2174
|
+
return;
|
|
2175
|
+
}
|
|
2176
|
+
this.scheduleReconnect();
|
|
2177
|
+
return;
|
|
2178
|
+
}
|
|
2179
|
+
this.noTokenAttempts = 0;
|
|
2180
|
+
const params = ((_a = this.collections) == null ? void 0 : _a.length) ? `?collections=${this.collections.join(",")}` : "";
|
|
2181
|
+
const url = `${this.baseUrl}/api/events/stream${params}`;
|
|
2182
|
+
try {
|
|
2183
|
+
const response = yield fetch(url, {
|
|
2184
|
+
headers: {
|
|
2185
|
+
"X-Client-Key": this.clientKey,
|
|
2186
|
+
Authorization: `Bearer ${token}`
|
|
2187
|
+
},
|
|
2188
|
+
signal
|
|
2189
|
+
});
|
|
2190
|
+
if (!response.ok) {
|
|
2191
|
+
if (response.status === 401) {
|
|
2192
|
+
this.scheduleReconnect();
|
|
2193
|
+
return;
|
|
2194
|
+
}
|
|
2195
|
+
throw new Error(`SSE connection failed: ${response.status}`);
|
|
2196
|
+
}
|
|
2197
|
+
if (!response.body) {
|
|
2198
|
+
throw new Error("SSE response has no body");
|
|
2199
|
+
}
|
|
2200
|
+
this._connected = true;
|
|
2201
|
+
this.reconnectAttempt = 0;
|
|
2202
|
+
yield this.readStream(response.body, signal);
|
|
2203
|
+
} catch (e) {
|
|
2204
|
+
if (signal.aborted) return;
|
|
2205
|
+
this._connected = false;
|
|
2206
|
+
this.scheduleReconnect();
|
|
2207
|
+
}
|
|
2208
|
+
});
|
|
2209
|
+
}
|
|
2210
|
+
readStream(body, signal) {
|
|
2211
|
+
return __async(this, null, function* () {
|
|
2212
|
+
var _a;
|
|
2213
|
+
const reader = body.getReader();
|
|
2214
|
+
const decoder = new TextDecoder();
|
|
2215
|
+
let buffer = "";
|
|
2216
|
+
let currentEvent = "";
|
|
2217
|
+
let currentData = "";
|
|
2218
|
+
try {
|
|
2219
|
+
while (true) {
|
|
2220
|
+
const { done, value } = yield reader.read();
|
|
2221
|
+
if (done || signal.aborted) break;
|
|
2222
|
+
buffer += decoder.decode(value, { stream: true });
|
|
2223
|
+
const lines = buffer.split("\n");
|
|
2224
|
+
buffer = (_a = lines.pop()) != null ? _a : "";
|
|
2225
|
+
for (const line of lines) {
|
|
2226
|
+
if (line.startsWith("event: ")) {
|
|
2227
|
+
currentEvent = line.slice(7);
|
|
2228
|
+
} else if (line.startsWith("data: ")) {
|
|
2229
|
+
currentData += (currentData ? "\n" : "") + line.slice(6);
|
|
2230
|
+
} else if (line === "") {
|
|
2231
|
+
if (currentEvent === "collection:change" && currentData) {
|
|
2232
|
+
try {
|
|
2233
|
+
const event = JSON.parse(currentData);
|
|
2234
|
+
for (const listener of this.listeners) {
|
|
2235
|
+
try {
|
|
2236
|
+
listener(event);
|
|
2237
|
+
} catch (e) {
|
|
2238
|
+
}
|
|
2239
|
+
}
|
|
2240
|
+
} catch (e) {
|
|
2241
|
+
}
|
|
2242
|
+
}
|
|
2243
|
+
currentEvent = "";
|
|
2244
|
+
currentData = "";
|
|
2245
|
+
}
|
|
2246
|
+
}
|
|
2247
|
+
}
|
|
2248
|
+
} catch (e) {
|
|
2249
|
+
} finally {
|
|
2250
|
+
reader.releaseLock();
|
|
2251
|
+
this._connected = false;
|
|
2252
|
+
if (!signal.aborted) {
|
|
2253
|
+
this.scheduleReconnect();
|
|
2254
|
+
}
|
|
2255
|
+
}
|
|
2256
|
+
});
|
|
2257
|
+
}
|
|
2258
|
+
scheduleReconnect() {
|
|
2259
|
+
if (this.reconnectTimer) return;
|
|
2260
|
+
const delay2 = Math.min(
|
|
2261
|
+
INITIAL_RECONNECT_DELAY * Math.pow(RECONNECT_BACKOFF_FACTOR, this.reconnectAttempt),
|
|
2262
|
+
MAX_RECONNECT_DELAY
|
|
2263
|
+
);
|
|
2264
|
+
this.reconnectAttempt++;
|
|
2265
|
+
this.reconnectTimer = setTimeout(() => {
|
|
2266
|
+
this.reconnectTimer = null;
|
|
2267
|
+
this.abortController = new AbortController();
|
|
2268
|
+
this.startStream(this.abortController.signal);
|
|
2269
|
+
}, delay2);
|
|
2270
|
+
}
|
|
2271
|
+
};
|
|
2272
|
+
|
|
2124
2273
|
// src/core/webhook/index.ts
|
|
2125
2274
|
function isValidWebhookEvent(data) {
|
|
2126
2275
|
if (typeof data !== "object" || data === null) return false;
|
|
@@ -2129,22 +2278,22 @@ function isValidWebhookEvent(data) {
|
|
|
2129
2278
|
}
|
|
2130
2279
|
function verifySignature(payload, secret, signature) {
|
|
2131
2280
|
return __async(this, null, function* () {
|
|
2281
|
+
var _a;
|
|
2132
2282
|
const encoder = new TextEncoder();
|
|
2133
2283
|
const key = yield crypto.subtle.importKey(
|
|
2134
2284
|
"raw",
|
|
2135
2285
|
encoder.encode(secret),
|
|
2136
2286
|
{ name: "HMAC", hash: "SHA-256" },
|
|
2137
2287
|
false,
|
|
2138
|
-
["
|
|
2288
|
+
["verify"]
|
|
2139
2289
|
);
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
let result = expected.length !== signature.length ? 1 : 0;
|
|
2143
|
-
const len = Math.max(expected.length, signature.length);
|
|
2144
|
-
for (let i = 0; i < len; i++) {
|
|
2145
|
-
result |= (expected.charCodeAt(i) || 0) ^ (signature.charCodeAt(i) || 0);
|
|
2290
|
+
if (signature.length % 2 !== 0 || !/^[0-9a-fA-F]*$/.test(signature)) {
|
|
2291
|
+
return false;
|
|
2146
2292
|
}
|
|
2147
|
-
|
|
2293
|
+
const sigBytes = new Uint8Array(
|
|
2294
|
+
((_a = signature.match(/.{2}/g)) != null ? _a : []).map((byte) => parseInt(byte, 16))
|
|
2295
|
+
);
|
|
2296
|
+
return crypto.subtle.verify("HMAC", key, sigBytes, encoder.encode(payload));
|
|
2148
2297
|
});
|
|
2149
2298
|
}
|
|
2150
2299
|
function handleWebhook(request, handler, options) {
|
|
@@ -2266,9 +2415,10 @@ function getImagePlaceholderStyle(image, options) {
|
|
|
2266
2415
|
// src/utils/order/generateOrderNumber.ts
|
|
2267
2416
|
var generateOrderNumber = () => {
|
|
2268
2417
|
var _a;
|
|
2269
|
-
const
|
|
2270
|
-
const
|
|
2271
|
-
const
|
|
2418
|
+
const now = /* @__PURE__ */ new Date();
|
|
2419
|
+
const year = now.getFullYear().toString().slice(-2);
|
|
2420
|
+
const month = (now.getMonth() + 1).toString().padStart(2, "0");
|
|
2421
|
+
const day = now.getDate().toString().padStart(2, "0");
|
|
2272
2422
|
const array = new Uint32Array(1);
|
|
2273
2423
|
globalThis.crypto.getRandomValues(array);
|
|
2274
2424
|
const random = (((_a = array[0]) != null ? _a : 0) % 1e6).toString().padStart(6, "0");
|