@01.software/sdk 0.2.9-dev.260314.f493447 → 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.
Files changed (50) hide show
  1. package/README.md +5 -2
  2. package/dist/auth.d.cts +1 -1
  3. package/dist/auth.d.ts +1 -1
  4. package/dist/const-BpirbGBD.d.cts +19 -0
  5. package/dist/const-qZSQiSSC.d.ts +19 -0
  6. package/dist/index.cjs +229 -79
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.cts +114 -196
  9. package/dist/index.d.ts +114 -196
  10. package/dist/index.js +229 -79
  11. package/dist/index.js.map +1 -1
  12. package/dist/{payload-types-ggU6BNuH.d.cts → payload-types-CZiaL4Wr.d.cts} +6 -5
  13. package/dist/{payload-types-ggU6BNuH.d.ts → payload-types-CZiaL4Wr.d.ts} +6 -5
  14. package/dist/realtime-DupPIYx-.d.cts +33 -0
  15. package/dist/realtime-DupPIYx-.d.ts +33 -0
  16. package/dist/realtime.cjs +261 -0
  17. package/dist/realtime.cjs.map +1 -0
  18. package/dist/realtime.d.cts +38 -0
  19. package/dist/realtime.d.ts +38 -0
  20. package/dist/realtime.js +239 -0
  21. package/dist/realtime.js.map +1 -0
  22. package/dist/ui/code-block.cjs +3 -1
  23. package/dist/ui/code-block.cjs.map +1 -1
  24. package/dist/ui/code-block.js +4 -2
  25. package/dist/ui/code-block.js.map +1 -1
  26. package/dist/ui/flow.cjs +18 -3
  27. package/dist/ui/flow.cjs.map +1 -1
  28. package/dist/ui/flow.js +18 -3
  29. package/dist/ui/flow.js.map +1 -1
  30. package/dist/ui/form.d.cts +1 -1
  31. package/dist/ui/form.d.ts +1 -1
  32. package/dist/ui/video.cjs +219 -0
  33. package/dist/ui/video.cjs.map +1 -0
  34. package/dist/ui/video.d.cts +96 -0
  35. package/dist/ui/video.d.ts +96 -0
  36. package/dist/ui/video.js +191 -0
  37. package/dist/ui/video.js.map +1 -0
  38. package/dist/video-DbLL8yuc.d.cts +85 -0
  39. package/dist/video-DbLL8yuc.d.ts +85 -0
  40. package/dist/webhook-3iL9OEyq.d.cts +20 -0
  41. package/dist/webhook-DuTqrH9x.d.ts +20 -0
  42. package/dist/webhook.cjs +8 -8
  43. package/dist/webhook.cjs.map +1 -1
  44. package/dist/webhook.d.cts +3 -2
  45. package/dist/webhook.d.ts +3 -2
  46. package/dist/webhook.js +8 -8
  47. package/dist/webhook.js.map +1 -1
  48. package/package.json +29 -4
  49. package/dist/webhook-B54a-HGd.d.ts +0 -35
  50. 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 _SDKError extends Error {
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, _SDKError);
148
+ Error.captureStackTrace(this, new.target);
149
149
  }
150
150
  }
151
151
  getUserMessage() {
@@ -265,7 +265,7 @@ function resolveApiUrl() {
265
265
  if (envUrl) {
266
266
  return envUrl.replace(/\/$/, "");
267
267
  }
268
- return "https://api-dev.01.software";
268
+ return "https://api.01.software";
269
269
  }
270
270
 
271
271
  // src/core/internal/utils/http.ts
@@ -343,7 +343,7 @@ function httpFetch(url, options) {
343
343
  const redactedHeaders = Object.fromEntries(headers.entries());
344
344
  if (redactedHeaders["authorization"]) {
345
345
  const token = redactedHeaders["authorization"];
346
- redactedHeaders["authorization"] = token.length > 15 ? `${token.slice(0, 15)}...****` : "****";
346
+ redactedHeaders["authorization"] = token.length > 20 ? `Bearer ...****${token.slice(-8)}` : "****";
347
347
  }
348
348
  debugLog(debug, "request", url, {
349
349
  method: requestInit.method || "GET",
@@ -482,6 +482,35 @@ function httpFetch(url, options) {
482
482
  });
483
483
  }
484
484
 
485
+ // src/core/api/parse-response.ts
486
+ function parseApiResponse(response, endpoint) {
487
+ return __async(this, null, function* () {
488
+ let data;
489
+ try {
490
+ data = yield response.json();
491
+ } catch (e) {
492
+ throw createApiError(
493
+ `Invalid JSON response from ${endpoint}`,
494
+ response.status,
495
+ void 0,
496
+ "Server returned an invalid response.",
497
+ "Check if the API endpoint is available."
498
+ );
499
+ }
500
+ if (data.error) {
501
+ const errorMessage = typeof data.error === "string" ? data.error : "Unknown API error";
502
+ throw createApiError(
503
+ errorMessage,
504
+ response.status,
505
+ data,
506
+ errorMessage,
507
+ "An error occurred while processing the request."
508
+ );
509
+ }
510
+ return data;
511
+ });
512
+ }
513
+
485
514
  // src/core/api/base-api.ts
486
515
  var BaseApi = class {
487
516
  constructor(apiName, options) {
@@ -495,38 +524,17 @@ var BaseApi = class {
495
524
  this.secretKey = options.secretKey;
496
525
  this.baseUrl = options.baseUrl;
497
526
  }
498
- request(endpoint, body) {
527
+ request(endpoint, body, options) {
499
528
  return __async(this, null, function* () {
500
- const response = yield httpFetch(endpoint, {
501
- method: "POST",
529
+ var _a;
530
+ const method = (_a = options == null ? void 0 : options.method) != null ? _a : "POST";
531
+ const response = yield httpFetch(endpoint, __spreadValues(__spreadValues({
532
+ method,
502
533
  clientKey: this.clientKey,
503
534
  secretKey: this.secretKey,
504
- baseUrl: this.baseUrl,
505
- body: JSON.stringify(body)
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;
535
+ baseUrl: this.baseUrl
536
+ }, body !== void 0 && { body: JSON.stringify(body) }), (options == null ? void 0 : options.headers) && { headers: options.headers }));
537
+ return parseApiResponse(response, endpoint);
530
538
  });
531
539
  }
532
540
  };
@@ -616,29 +624,7 @@ var CartApi = class {
616
624
  customerToken: token != null ? token : void 0,
617
625
  baseUrl: this.baseUrl
618
626
  }, token && this.onUnauthorized && { onUnauthorized: this.onUnauthorized }), body !== void 0 && { body: JSON.stringify(body) }));
619
- let data;
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;
627
+ return parseApiResponse(response, endpoint);
642
628
  });
643
629
  }
644
630
  getCart(cartId) {
@@ -938,7 +924,7 @@ var HttpClient = class {
938
924
  */
939
925
  parseFindResponse(response) {
940
926
  return __async(this, null, function* () {
941
- var _a, _b;
927
+ var _a, _b, _c, _d, _e, _f;
942
928
  const contentType = response.headers.get("content-type");
943
929
  try {
944
930
  this.assertJsonResponse(response);
@@ -950,15 +936,15 @@ var HttpClient = class {
950
936
  }
951
937
  return {
952
938
  docs: jsonData.docs,
953
- totalDocs: jsonData.totalDocs || 0,
939
+ totalDocs: (_a = jsonData.totalDocs) != null ? _a : 0,
954
940
  limit: jsonData.limit || 20,
955
- totalPages: jsonData.totalPages || 0,
941
+ totalPages: (_b = jsonData.totalPages) != null ? _b : 0,
956
942
  page: jsonData.page || 1,
957
943
  pagingCounter: jsonData.pagingCounter || 1,
958
- hasPrevPage: jsonData.hasPrevPage || false,
959
- hasNextPage: jsonData.hasNextPage || false,
960
- prevPage: (_a = jsonData.prevPage) != null ? _a : null,
961
- nextPage: (_b = jsonData.nextPage) != null ? _b : null
944
+ hasPrevPage: (_c = jsonData.hasPrevPage) != null ? _c : false,
945
+ hasNextPage: (_d = jsonData.hasNextPage) != null ? _d : false,
946
+ prevPage: (_e = jsonData.prevPage) != null ? _e : null,
947
+ nextPage: (_f = jsonData.nextPage) != null ? _f : null
962
948
  };
963
949
  } catch (error) {
964
950
  if (error instanceof SDKError) throw error;
@@ -1203,6 +1189,7 @@ var COLLECTIONS = [
1203
1189
  "document-categories",
1204
1190
  "document-types",
1205
1191
  "posts",
1192
+ "post-authors",
1206
1193
  "post-categories",
1207
1194
  "post-tags",
1208
1195
  "playlists",
@@ -1229,23 +1216,33 @@ var COLLECTIONS = [
1229
1216
 
1230
1217
  // src/core/customer/customer-auth.ts
1231
1218
  var DEFAULT_TIMEOUT2 = 15e3;
1219
+ function safeGetItem(key) {
1220
+ try {
1221
+ return localStorage.getItem(key);
1222
+ } catch (e) {
1223
+ return null;
1224
+ }
1225
+ }
1232
1226
  var CustomerAuth = class {
1233
1227
  constructor(clientKey, baseUrl, options) {
1234
1228
  this.refreshPromise = null;
1235
- var _a, _b, _c;
1229
+ var _a, _b;
1236
1230
  this.clientKey = clientKey;
1237
1231
  this.baseUrl = baseUrl;
1238
1232
  const persist = (_a = options == null ? void 0 : options.persist) != null ? _a : true;
1239
1233
  if (persist) {
1240
1234
  const key = typeof persist === "string" ? persist : "customer-token";
1241
1235
  const isBrowser = typeof window !== "undefined";
1242
- this.token = isBrowser ? (_b = localStorage.getItem(key)) != null ? _b : null : null;
1236
+ this.token = isBrowser ? safeGetItem(key) : null;
1243
1237
  this.onTokenChange = isBrowser ? (token) => {
1244
- if (token) localStorage.setItem(key, token);
1245
- else localStorage.removeItem(key);
1238
+ try {
1239
+ if (token) localStorage.setItem(key, token);
1240
+ else localStorage.removeItem(key);
1241
+ } catch (e) {
1242
+ }
1246
1243
  } : void 0;
1247
1244
  } else {
1248
- this.token = (_c = options == null ? void 0 : options.token) != null ? _c : null;
1245
+ this.token = (_b = options == null ? void 0 : options.token) != null ? _b : null;
1249
1246
  this.onTokenChange = options == null ? void 0 : options.onTokenChange;
1250
1247
  }
1251
1248
  }
@@ -2054,6 +2051,157 @@ function createServerClient(options) {
2054
2051
  return new ServerClient(options);
2055
2052
  }
2056
2053
 
2054
+ // src/core/query/realtime.ts
2055
+ var INITIAL_RECONNECT_DELAY = 1e3;
2056
+ var MAX_RECONNECT_DELAY = 3e4;
2057
+ var RECONNECT_BACKOFF_FACTOR = 2;
2058
+ var MAX_NO_TOKEN_RETRIES = 5;
2059
+ var RealtimeConnection = class {
2060
+ constructor(baseUrl, clientKey, getToken, collections) {
2061
+ this.baseUrl = baseUrl;
2062
+ this.clientKey = clientKey;
2063
+ this.getToken = getToken;
2064
+ this.collections = collections;
2065
+ this.abortController = null;
2066
+ this.reconnectAttempt = 0;
2067
+ this.noTokenAttempts = 0;
2068
+ this.reconnectTimer = null;
2069
+ this.listeners = /* @__PURE__ */ new Set();
2070
+ this._connected = false;
2071
+ }
2072
+ get connected() {
2073
+ return this._connected;
2074
+ }
2075
+ addListener(fn) {
2076
+ this.listeners.add(fn);
2077
+ return () => this.listeners.delete(fn);
2078
+ }
2079
+ connect() {
2080
+ if (this.abortController) return;
2081
+ this.abortController = new AbortController();
2082
+ this.startStream(this.abortController.signal);
2083
+ }
2084
+ disconnect() {
2085
+ this._connected = false;
2086
+ if (this.reconnectTimer) {
2087
+ clearTimeout(this.reconnectTimer);
2088
+ this.reconnectTimer = null;
2089
+ }
2090
+ if (this.abortController) {
2091
+ this.abortController.abort();
2092
+ this.abortController = null;
2093
+ }
2094
+ this.reconnectAttempt = 0;
2095
+ this.noTokenAttempts = 0;
2096
+ }
2097
+ startStream(signal) {
2098
+ return __async(this, null, function* () {
2099
+ var _a;
2100
+ const token = this.getToken();
2101
+ if (!token) {
2102
+ this.noTokenAttempts++;
2103
+ if (this.noTokenAttempts >= MAX_NO_TOKEN_RETRIES) {
2104
+ this._connected = false;
2105
+ this.abortController = null;
2106
+ return;
2107
+ }
2108
+ this.scheduleReconnect();
2109
+ return;
2110
+ }
2111
+ this.noTokenAttempts = 0;
2112
+ const params = ((_a = this.collections) == null ? void 0 : _a.length) ? `?collections=${this.collections.join(",")}` : "";
2113
+ const url = `${this.baseUrl}/api/events/stream${params}`;
2114
+ try {
2115
+ const response = yield fetch(url, {
2116
+ headers: {
2117
+ "X-Client-Key": this.clientKey,
2118
+ Authorization: `Bearer ${token}`
2119
+ },
2120
+ signal
2121
+ });
2122
+ if (!response.ok) {
2123
+ if (response.status === 401) {
2124
+ this.scheduleReconnect();
2125
+ return;
2126
+ }
2127
+ throw new Error(`SSE connection failed: ${response.status}`);
2128
+ }
2129
+ if (!response.body) {
2130
+ throw new Error("SSE response has no body");
2131
+ }
2132
+ this._connected = true;
2133
+ this.reconnectAttempt = 0;
2134
+ yield this.readStream(response.body, signal);
2135
+ } catch (e) {
2136
+ if (signal.aborted) return;
2137
+ this._connected = false;
2138
+ this.scheduleReconnect();
2139
+ }
2140
+ });
2141
+ }
2142
+ readStream(body, signal) {
2143
+ return __async(this, null, function* () {
2144
+ var _a;
2145
+ const reader = body.getReader();
2146
+ const decoder = new TextDecoder();
2147
+ let buffer = "";
2148
+ let currentEvent = "";
2149
+ let currentData = "";
2150
+ try {
2151
+ while (true) {
2152
+ const { done, value } = yield reader.read();
2153
+ if (done || signal.aborted) break;
2154
+ buffer += decoder.decode(value, { stream: true });
2155
+ const lines = buffer.split("\n");
2156
+ buffer = (_a = lines.pop()) != null ? _a : "";
2157
+ for (const line of lines) {
2158
+ if (line.startsWith("event: ")) {
2159
+ currentEvent = line.slice(7);
2160
+ } else if (line.startsWith("data: ")) {
2161
+ currentData += (currentData ? "\n" : "") + line.slice(6);
2162
+ } else if (line === "") {
2163
+ if (currentEvent === "collection:change" && currentData) {
2164
+ try {
2165
+ const event = JSON.parse(currentData);
2166
+ for (const listener of this.listeners) {
2167
+ try {
2168
+ listener(event);
2169
+ } catch (e) {
2170
+ }
2171
+ }
2172
+ } catch (e) {
2173
+ }
2174
+ }
2175
+ currentEvent = "";
2176
+ currentData = "";
2177
+ }
2178
+ }
2179
+ }
2180
+ } catch (e) {
2181
+ } finally {
2182
+ reader.releaseLock();
2183
+ this._connected = false;
2184
+ if (!signal.aborted) {
2185
+ this.scheduleReconnect();
2186
+ }
2187
+ }
2188
+ });
2189
+ }
2190
+ scheduleReconnect() {
2191
+ if (this.reconnectTimer) return;
2192
+ const delay2 = Math.min(
2193
+ INITIAL_RECONNECT_DELAY * Math.pow(RECONNECT_BACKOFF_FACTOR, this.reconnectAttempt),
2194
+ MAX_RECONNECT_DELAY
2195
+ );
2196
+ this.reconnectAttempt++;
2197
+ this.reconnectTimer = setTimeout(() => {
2198
+ this.reconnectTimer = null;
2199
+ this.abortController = new AbortController();
2200
+ this.startStream(this.abortController.signal);
2201
+ }, delay2);
2202
+ }
2203
+ };
2204
+
2057
2205
  // src/core/webhook/index.ts
2058
2206
  function isValidWebhookEvent(data) {
2059
2207
  if (typeof data !== "object" || data === null) return false;
@@ -2062,22 +2210,22 @@ function isValidWebhookEvent(data) {
2062
2210
  }
2063
2211
  function verifySignature(payload, secret, signature) {
2064
2212
  return __async(this, null, function* () {
2213
+ var _a;
2065
2214
  const encoder = new TextEncoder();
2066
2215
  const key = yield crypto.subtle.importKey(
2067
2216
  "raw",
2068
2217
  encoder.encode(secret),
2069
2218
  { name: "HMAC", hash: "SHA-256" },
2070
2219
  false,
2071
- ["sign"]
2220
+ ["verify"]
2072
2221
  );
2073
- const sig = yield crypto.subtle.sign("HMAC", key, encoder.encode(payload));
2074
- const expected = Array.from(new Uint8Array(sig)).map((b) => b.toString(16).padStart(2, "0")).join("");
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);
2222
+ if (signature.length % 2 !== 0 || !/^[0-9a-fA-F]*$/.test(signature)) {
2223
+ return false;
2079
2224
  }
2080
- return result === 0;
2225
+ const sigBytes = new Uint8Array(
2226
+ ((_a = signature.match(/.{2}/g)) != null ? _a : []).map((byte) => parseInt(byte, 16))
2227
+ );
2228
+ return crypto.subtle.verify("HMAC", key, sigBytes, encoder.encode(payload));
2081
2229
  });
2082
2230
  }
2083
2231
  function handleWebhook(request, handler, options) {
@@ -2199,9 +2347,10 @@ function getImagePlaceholderStyle(image, options) {
2199
2347
  // src/utils/order/generateOrderNumber.ts
2200
2348
  var generateOrderNumber = () => {
2201
2349
  var _a;
2202
- const year = (/* @__PURE__ */ new Date()).getFullYear().toString().slice(-2);
2203
- const month = ((/* @__PURE__ */ new Date()).getMonth() + 1).toString().padStart(2, "0");
2204
- const day = (/* @__PURE__ */ new Date()).getDate().toString().padStart(2, "0");
2350
+ const now = /* @__PURE__ */ new Date();
2351
+ const year = now.getFullYear().toString().slice(-2);
2352
+ const month = (now.getMonth() + 1).toString().padStart(2, "0");
2353
+ const day = now.getDate().toString().padStart(2, "0");
2205
2354
  const array = new Uint32Array(1);
2206
2355
  globalThis.crypto.getRandomValues(array);
2207
2356
  const random = (((_a = array[0]) != null ? _a : 0) % 1e6).toString().padStart(6, "0");
@@ -2267,6 +2416,7 @@ export {
2267
2416
  OrderApi,
2268
2417
  ProductApi,
2269
2418
  QueryHooks,
2419
+ RealtimeConnection,
2270
2420
  SDKError,
2271
2421
  ServerClient,
2272
2422
  ServiceUnavailableError,