@01.software/sdk 0.18.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -532,7 +532,7 @@ async function parseErrorBody(response) {
532
532
  };
533
533
  try {
534
534
  const body = await response.json();
535
- const reason = typeof body.reason === "string" ? body.reason : void 0;
535
+ const reason = typeof body.reason === "string" ? body.reason : typeof body.code === "string" ? body.code : void 0;
536
536
  if (body.errors && Array.isArray(body.errors)) {
537
537
  const fieldErrors = [];
538
538
  for (const e of body.errors) {
@@ -555,6 +555,7 @@ async function parseErrorBody(response) {
555
555
  errorMessage: `HTTP ${response.status}: ${details}`,
556
556
  userMessage: details,
557
557
  reason,
558
+ body,
558
559
  errors: fieldErrors.length > 0 ? fieldErrors : body.errors
559
560
  };
560
561
  }
@@ -563,17 +564,19 @@ async function parseErrorBody(response) {
563
564
  return {
564
565
  errorMessage: `HTTP ${response.status}: ${body.error}`,
565
566
  userMessage: body.error,
566
- reason
567
+ reason,
568
+ body
567
569
  };
568
570
  }
569
571
  if (body.message) {
570
572
  return {
571
573
  errorMessage: `HTTP ${response.status}: ${body.message}`,
572
574
  userMessage: body.message,
573
- reason
575
+ reason,
576
+ body
574
577
  };
575
578
  }
576
- return { ...fallback, reason };
579
+ return { ...fallback, reason, body };
577
580
  } catch {
578
581
  return fallback;
579
582
  }
@@ -590,7 +593,6 @@ async function httpFetch(url, options) {
590
593
  publishableKey,
591
594
  secretKey,
592
595
  customerToken,
593
- tenantId,
594
596
  timeout = DEFAULT_TIMEOUT,
595
597
  debug,
596
598
  retry,
@@ -620,9 +622,6 @@ async function httpFetch(url, options) {
620
622
  if (authToken) {
621
623
  headers.set("Authorization", `Bearer ${authToken}`);
622
624
  }
623
- if (tenantId) {
624
- headers.set("X-Tenant-Id", tenantId);
625
- }
626
625
  if (!headers.has("Content-Type") && requestInit.body && !(requestInit.body instanceof FormData)) {
627
626
  headers.set("Content-Type", "application/json");
628
627
  }
@@ -789,10 +788,9 @@ async function httpFetch(url, options) {
789
788
 
790
789
  // src/core/collection/http-client.ts
791
790
  var HttpClient = class {
792
- constructor(publishableKey, secretKey, getCustomerToken, onUnauthorized, onRequestId, tenantId) {
791
+ constructor(publishableKey, secretKey, getCustomerToken, onUnauthorized, onRequestId) {
793
792
  this.publishableKey = publishableKey;
794
793
  this.secretKey = secretKey;
795
- this.tenantId = tenantId;
796
794
  this.getCustomerToken = getCustomerToken;
797
795
  this.onUnauthorized = onUnauthorized;
798
796
  this.onRequestId = onRequestId;
@@ -802,9 +800,6 @@ var HttpClient = class {
802
800
  publishableKey: this.publishableKey,
803
801
  secretKey: this.secretKey
804
802
  };
805
- if (this.secretKey?.startsWith("pat01_") && this.tenantId) {
806
- opts.tenantId = this.tenantId;
807
- }
808
803
  const token = this.getCustomerToken?.();
809
804
  if (token) {
810
805
  opts.customerToken = token;
@@ -1128,10 +1123,10 @@ var COLLECTIONS = [
1128
1123
  "documents",
1129
1124
  "document-categories",
1130
1125
  "document-types",
1131
- "posts",
1132
- "post-authors",
1133
- "post-categories",
1134
- "post-tags",
1126
+ "articles",
1127
+ "article-authors",
1128
+ "article-categories",
1129
+ "article-tags",
1135
1130
  "playlists",
1136
1131
  "playlist-categories",
1137
1132
  "playlist-tags",
@@ -1160,12 +1155,12 @@ var COLLECTIONS = [
1160
1155
  "forms",
1161
1156
  "form-submissions",
1162
1157
  // Community
1163
- "threads",
1158
+ "posts",
1164
1159
  "comments",
1165
1160
  "reactions",
1166
1161
  "reaction-types",
1167
1162
  "bookmarks",
1168
- "thread-categories",
1163
+ "post-categories",
1169
1164
  "reports",
1170
1165
  "community-bans",
1171
1166
  // Events
@@ -1226,7 +1221,6 @@ var CommunityClient = class {
1226
1221
  constructor(options) {
1227
1222
  this.publishableKey = options.publishableKey ?? "";
1228
1223
  this.secretKey = options.secretKey;
1229
- this.tenantId = options.tenantId;
1230
1224
  this.customerToken = options.customerToken;
1231
1225
  this.onUnauthorized = options.onUnauthorized;
1232
1226
  this.onRequestId = options.onRequestId;
@@ -1238,13 +1232,11 @@ var CommunityClient = class {
1238
1232
  }
1239
1233
  async execute(endpoint, method, body) {
1240
1234
  const token = typeof this.customerToken === "function" ? this.customerToken() : this.customerToken;
1241
- const tenantId = this.secretKey?.startsWith("pat01_") && this.tenantId ? this.tenantId : void 0;
1242
1235
  try {
1243
1236
  const response = await httpFetch(endpoint, {
1244
1237
  method,
1245
1238
  publishableKey: this.publishableKey,
1246
1239
  secretKey: this.secretKey,
1247
- tenantId,
1248
1240
  customerToken: token ?? void 0,
1249
1241
  ...token && this.onUnauthorized && { onUnauthorized: this.onUnauthorized },
1250
1242
  ...body !== void 0 && { body: JSON.stringify(body) }
@@ -1257,49 +1249,48 @@ var CommunityClient = class {
1257
1249
  throw err;
1258
1250
  }
1259
1251
  }
1260
- // Threads
1261
- createThread(params) {
1262
- return this.execute("/api/threads", "POST", params);
1252
+ createPost(params) {
1253
+ return this.execute("/api/posts", "POST", params);
1263
1254
  }
1264
- getMyThreads(params) {
1255
+ getMyPosts(params) {
1265
1256
  return this.execute(
1266
- `/api/threads/my${this.buildQuery(params)}`,
1257
+ `/api/posts/my${this.buildQuery(params)}`,
1267
1258
  "GET"
1268
1259
  );
1269
1260
  }
1270
1261
  getTrending(params) {
1271
1262
  return this.execute(
1272
- `/api/threads/trending${this.buildQuery(params)}`,
1263
+ `/api/posts/trending${this.buildQuery(params)}`,
1273
1264
  "GET"
1274
1265
  );
1275
1266
  }
1276
1267
  incrementView(params) {
1277
1268
  return this.execute(
1278
- `/api/threads/${params.threadId}/view`,
1269
+ `/api/posts/${params.postId}/view`,
1279
1270
  "POST"
1280
1271
  );
1281
1272
  }
1282
- reportThread(params) {
1283
- const { threadId, ...body } = params;
1273
+ reportPost(params) {
1274
+ const { postId, ...body } = params;
1284
1275
  return this.execute(
1285
- `/api/threads/${threadId}/report`,
1276
+ `/api/posts/${postId}/report`,
1286
1277
  "POST",
1287
1278
  body
1288
1279
  );
1289
1280
  }
1290
1281
  // Comments
1291
1282
  createComment(params) {
1292
- const { threadId, parentId, body: commentBody } = params;
1293
- const body = { thread: threadId, body: commentBody };
1283
+ const { postId, parentId, body: commentBody } = params;
1284
+ const body = { post: postId, body: commentBody };
1294
1285
  if (parentId !== void 0) {
1295
1286
  body.parent = parentId;
1296
1287
  }
1297
1288
  return this.execute("/api/comments", "POST", body);
1298
1289
  }
1299
1290
  listComments(params) {
1300
- const { threadId, page, limit, rootComment } = params;
1291
+ const { postId, page, limit, rootComment } = params;
1301
1292
  const urlParams = new URLSearchParams();
1302
- urlParams.set("where[thread][equals]", threadId);
1293
+ urlParams.set("where[post][equals]", postId);
1303
1294
  urlParams.set("sort", "-createdAt");
1304
1295
  if (limit !== void 0) urlParams.set("limit", String(limit));
1305
1296
  if (page !== void 0) urlParams.set("page", String(page));
@@ -1333,16 +1324,16 @@ var CommunityClient = class {
1333
1324
  }
1334
1325
  // Reactions
1335
1326
  addReaction(params) {
1336
- const { threadId, type } = params;
1327
+ const { postId, type } = params;
1337
1328
  return this.execute("/api/reactions", "POST", {
1338
- thread: threadId,
1329
+ post: postId,
1339
1330
  type
1340
1331
  });
1341
1332
  }
1342
1333
  removeReaction(params) {
1343
- const { threadId, type } = params;
1334
+ const { postId, type } = params;
1344
1335
  return this.execute(
1345
- `/api/threads/${threadId}/react?type=${encodeURIComponent(type)}`,
1336
+ `/api/posts/${postId}/react?type=${encodeURIComponent(type)}`,
1346
1337
  "DELETE"
1347
1338
  );
1348
1339
  }
@@ -1362,7 +1353,7 @@ var CommunityClient = class {
1362
1353
  }
1363
1354
  getReactionSummary(params) {
1364
1355
  return this.execute(
1365
- `/api/threads/${params.threadId}/reactions`,
1356
+ `/api/posts/${params.postId}/reactions`,
1366
1357
  "GET"
1367
1358
  );
1368
1359
  }
@@ -1375,12 +1366,12 @@ var CommunityClient = class {
1375
1366
  // Bookmarks
1376
1367
  addBookmark(params) {
1377
1368
  return this.execute("/api/bookmarks", "POST", {
1378
- thread: params.threadId
1369
+ post: params.postId
1379
1370
  });
1380
1371
  }
1381
1372
  removeBookmark(params) {
1382
1373
  return this.execute(
1383
- `/api/threads/${params.threadId}/bookmark`,
1374
+ `/api/posts/${params.postId}/bookmark`,
1384
1375
  "DELETE"
1385
1376
  );
1386
1377
  }
@@ -1400,18 +1391,15 @@ var BaseApi = class {
1400
1391
  }
1401
1392
  this.publishableKey = options.publishableKey ?? "";
1402
1393
  this.secretKey = options.secretKey;
1403
- this.tenantId = options.tenantId;
1404
1394
  this.onRequestId = options.onRequestId;
1405
1395
  }
1406
1396
  async request(endpoint, body, options) {
1407
1397
  const method = options?.method ?? "POST";
1408
- const tenantId = this.secretKey.startsWith("pat01_") && this.tenantId ? this.tenantId : void 0;
1409
1398
  try {
1410
1399
  const response = await httpFetch(endpoint, {
1411
1400
  method,
1412
1401
  publishableKey: this.publishableKey,
1413
1402
  secretKey: this.secretKey,
1414
- tenantId,
1415
1403
  ...body !== void 0 && { body: JSON.stringify(body) },
1416
1404
  ...options?.headers && { headers: options.headers }
1417
1405
  });
@@ -1697,20 +1685,17 @@ var CartApi = class {
1697
1685
  }
1698
1686
  this.publishableKey = options.publishableKey ?? "";
1699
1687
  this.secretKey = options.secretKey;
1700
- this.tenantId = options.tenantId;
1701
1688
  this.customerToken = options.customerToken;
1702
1689
  this.onUnauthorized = options.onUnauthorized;
1703
1690
  this.onRequestId = options.onRequestId;
1704
1691
  }
1705
1692
  async execute(endpoint, method, body) {
1706
1693
  const token = typeof this.customerToken === "function" ? this.customerToken() : this.customerToken;
1707
- const tenantId = this.secretKey?.startsWith("pat01_") && this.tenantId ? this.tenantId : void 0;
1708
1694
  try {
1709
1695
  const response = await httpFetch(endpoint, {
1710
1696
  method,
1711
1697
  publishableKey: this.publishableKey,
1712
1698
  secretKey: this.secretKey,
1713
- tenantId,
1714
1699
  customerToken: token ?? void 0,
1715
1700
  ...token && this.onUnauthorized && { onUnauthorized: this.onUnauthorized },
1716
1701
  ...body !== void 0 && { body: JSON.stringify(body) }
@@ -1897,7 +1882,6 @@ var ServerCommerceClient = class {
1897
1882
  const serverOptions = {
1898
1883
  publishableKey: options.publishableKey,
1899
1884
  secretKey: options.secretKey,
1900
- tenantId: options.tenantId,
1901
1885
  onRequestId: options.onRequestId
1902
1886
  };
1903
1887
  const productApi = new ProductApi(serverOptions);
@@ -2550,7 +2534,6 @@ var ServerClient = class {
2550
2534
  const serverOptions = {
2551
2535
  publishableKey: this.config.publishableKey,
2552
2536
  secretKey: this.config.secretKey,
2553
- tenantId: this.config.tenantId,
2554
2537
  onRequestId
2555
2538
  };
2556
2539
  this.commerce = new ServerCommerceClient(serverOptions);
@@ -2567,8 +2550,7 @@ var ServerClient = class {
2567
2550
  this.config.secretKey,
2568
2551
  void 0,
2569
2552
  void 0,
2570
- onRequestId,
2571
- this.config.tenantId
2553
+ onRequestId
2572
2554
  );
2573
2555
  this.queryClient = getQueryClient();
2574
2556
  this.query = new QueryHooks(this.queryClient, this.collections);
@@ -2761,7 +2743,7 @@ function createCustomerAuthWebhookHandler(handlers) {
2761
2743
  await handlers.unhandled?.(event);
2762
2744
  };
2763
2745
  }
2764
- async function verifySignature(payload, secret, signature) {
2746
+ async function verifySignature(payload, secret, signature, timestamp, deliveryId) {
2765
2747
  const encoder = new TextEncoder();
2766
2748
  const key = await crypto.subtle.importKey(
2767
2749
  "raw",
@@ -2776,14 +2758,35 @@ async function verifySignature(payload, secret, signature) {
2776
2758
  const sigBytes = new Uint8Array(
2777
2759
  (signature.match(/.{2}/g) ?? []).map((byte) => parseInt(byte, 16))
2778
2760
  );
2779
- return crypto.subtle.verify("HMAC", key, sigBytes, encoder.encode(payload));
2761
+ return crypto.subtle.verify(
2762
+ "HMAC",
2763
+ key,
2764
+ sigBytes,
2765
+ encoder.encode(`${timestamp}.${deliveryId}.${payload}`)
2766
+ );
2767
+ }
2768
+ function timestampIsFresh(timestamp, toleranceSeconds) {
2769
+ if (!/^\d+$/.test(timestamp)) return false;
2770
+ const timestampMs = Number(timestamp);
2771
+ if (!Number.isFinite(timestampMs)) return false;
2772
+ const skewMs = Math.abs(Date.now() - timestampMs);
2773
+ return skewMs <= toleranceSeconds * 1e3;
2780
2774
  }
2781
2775
  async function handleWebhook(request, handler, options) {
2782
2776
  try {
2783
2777
  const rawBody = await request.text();
2784
2778
  if (options?.secret) {
2785
2779
  const signature = request.headers.get("x-webhook-signature") || "";
2786
- const valid = await verifySignature(rawBody, options.secret, signature);
2780
+ const timestamp = request.headers.get("x-webhook-timestamp") || "";
2781
+ const deliveryId = request.headers.get("x-webhook-delivery-id") || "";
2782
+ const toleranceSeconds = options.toleranceSeconds ?? 300;
2783
+ const valid = Boolean(timestamp && deliveryId) && timestampIsFresh(timestamp, toleranceSeconds) && await verifySignature(
2784
+ rawBody,
2785
+ options.secret,
2786
+ signature,
2787
+ timestamp,
2788
+ deliveryId
2789
+ );
2787
2790
  if (!valid) {
2788
2791
  return new Response(
2789
2792
  JSON.stringify({ error: "Invalid webhook signature" }),