@01.software/sdk 0.18.0 → 0.20.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,17 +1155,18 @@ 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
1172
1167
  "event-calendars",
1173
1168
  "events",
1169
+ "event-categories",
1174
1170
  "event-occurrences",
1175
1171
  "event-tags"
1176
1172
  ];
@@ -1226,7 +1222,6 @@ var CommunityClient = class {
1226
1222
  constructor(options) {
1227
1223
  this.publishableKey = options.publishableKey ?? "";
1228
1224
  this.secretKey = options.secretKey;
1229
- this.tenantId = options.tenantId;
1230
1225
  this.customerToken = options.customerToken;
1231
1226
  this.onUnauthorized = options.onUnauthorized;
1232
1227
  this.onRequestId = options.onRequestId;
@@ -1238,13 +1233,11 @@ var CommunityClient = class {
1238
1233
  }
1239
1234
  async execute(endpoint, method, body) {
1240
1235
  const token = typeof this.customerToken === "function" ? this.customerToken() : this.customerToken;
1241
- const tenantId = this.secretKey?.startsWith("pat01_") && this.tenantId ? this.tenantId : void 0;
1242
1236
  try {
1243
1237
  const response = await httpFetch(endpoint, {
1244
1238
  method,
1245
1239
  publishableKey: this.publishableKey,
1246
1240
  secretKey: this.secretKey,
1247
- tenantId,
1248
1241
  customerToken: token ?? void 0,
1249
1242
  ...token && this.onUnauthorized && { onUnauthorized: this.onUnauthorized },
1250
1243
  ...body !== void 0 && { body: JSON.stringify(body) }
@@ -1257,49 +1250,48 @@ var CommunityClient = class {
1257
1250
  throw err;
1258
1251
  }
1259
1252
  }
1260
- // Threads
1261
- createThread(params) {
1262
- return this.execute("/api/threads", "POST", params);
1253
+ createPost(params) {
1254
+ return this.execute("/api/posts", "POST", params);
1263
1255
  }
1264
- getMyThreads(params) {
1256
+ getMyPosts(params) {
1265
1257
  return this.execute(
1266
- `/api/threads/my${this.buildQuery(params)}`,
1258
+ `/api/posts/my${this.buildQuery(params)}`,
1267
1259
  "GET"
1268
1260
  );
1269
1261
  }
1270
1262
  getTrending(params) {
1271
1263
  return this.execute(
1272
- `/api/threads/trending${this.buildQuery(params)}`,
1264
+ `/api/posts/trending${this.buildQuery(params)}`,
1273
1265
  "GET"
1274
1266
  );
1275
1267
  }
1276
1268
  incrementView(params) {
1277
1269
  return this.execute(
1278
- `/api/threads/${params.threadId}/view`,
1270
+ `/api/posts/${params.postId}/view`,
1279
1271
  "POST"
1280
1272
  );
1281
1273
  }
1282
- reportThread(params) {
1283
- const { threadId, ...body } = params;
1274
+ reportPost(params) {
1275
+ const { postId, ...body } = params;
1284
1276
  return this.execute(
1285
- `/api/threads/${threadId}/report`,
1277
+ `/api/posts/${postId}/report`,
1286
1278
  "POST",
1287
1279
  body
1288
1280
  );
1289
1281
  }
1290
1282
  // Comments
1291
1283
  createComment(params) {
1292
- const { threadId, parentId, body: commentBody } = params;
1293
- const body = { thread: threadId, body: commentBody };
1284
+ const { postId, parentId, body: commentBody } = params;
1285
+ const body = { post: postId, body: commentBody };
1294
1286
  if (parentId !== void 0) {
1295
1287
  body.parent = parentId;
1296
1288
  }
1297
1289
  return this.execute("/api/comments", "POST", body);
1298
1290
  }
1299
1291
  listComments(params) {
1300
- const { threadId, page, limit, rootComment } = params;
1292
+ const { postId, page, limit, rootComment } = params;
1301
1293
  const urlParams = new URLSearchParams();
1302
- urlParams.set("where[thread][equals]", threadId);
1294
+ urlParams.set("where[post][equals]", postId);
1303
1295
  urlParams.set("sort", "-createdAt");
1304
1296
  if (limit !== void 0) urlParams.set("limit", String(limit));
1305
1297
  if (page !== void 0) urlParams.set("page", String(page));
@@ -1333,16 +1325,16 @@ var CommunityClient = class {
1333
1325
  }
1334
1326
  // Reactions
1335
1327
  addReaction(params) {
1336
- const { threadId, type } = params;
1328
+ const { postId, type } = params;
1337
1329
  return this.execute("/api/reactions", "POST", {
1338
- thread: threadId,
1330
+ post: postId,
1339
1331
  type
1340
1332
  });
1341
1333
  }
1342
1334
  removeReaction(params) {
1343
- const { threadId, type } = params;
1335
+ const { postId, type } = params;
1344
1336
  return this.execute(
1345
- `/api/threads/${threadId}/react?type=${encodeURIComponent(type)}`,
1337
+ `/api/posts/${postId}/react?type=${encodeURIComponent(type)}`,
1346
1338
  "DELETE"
1347
1339
  );
1348
1340
  }
@@ -1362,7 +1354,7 @@ var CommunityClient = class {
1362
1354
  }
1363
1355
  getReactionSummary(params) {
1364
1356
  return this.execute(
1365
- `/api/threads/${params.threadId}/reactions`,
1357
+ `/api/posts/${params.postId}/reactions`,
1366
1358
  "GET"
1367
1359
  );
1368
1360
  }
@@ -1375,12 +1367,12 @@ var CommunityClient = class {
1375
1367
  // Bookmarks
1376
1368
  addBookmark(params) {
1377
1369
  return this.execute("/api/bookmarks", "POST", {
1378
- thread: params.threadId
1370
+ post: params.postId
1379
1371
  });
1380
1372
  }
1381
1373
  removeBookmark(params) {
1382
1374
  return this.execute(
1383
- `/api/threads/${params.threadId}/bookmark`,
1375
+ `/api/posts/${params.postId}/bookmark`,
1384
1376
  "DELETE"
1385
1377
  );
1386
1378
  }
@@ -1400,18 +1392,15 @@ var BaseApi = class {
1400
1392
  }
1401
1393
  this.publishableKey = options.publishableKey ?? "";
1402
1394
  this.secretKey = options.secretKey;
1403
- this.tenantId = options.tenantId;
1404
1395
  this.onRequestId = options.onRequestId;
1405
1396
  }
1406
1397
  async request(endpoint, body, options) {
1407
1398
  const method = options?.method ?? "POST";
1408
- const tenantId = this.secretKey.startsWith("pat01_") && this.tenantId ? this.tenantId : void 0;
1409
1399
  try {
1410
1400
  const response = await httpFetch(endpoint, {
1411
1401
  method,
1412
1402
  publishableKey: this.publishableKey,
1413
1403
  secretKey: this.secretKey,
1414
- tenantId,
1415
1404
  ...body !== void 0 && { body: JSON.stringify(body) },
1416
1405
  ...options?.headers && { headers: options.headers }
1417
1406
  });
@@ -1697,20 +1686,17 @@ var CartApi = class {
1697
1686
  }
1698
1687
  this.publishableKey = options.publishableKey ?? "";
1699
1688
  this.secretKey = options.secretKey;
1700
- this.tenantId = options.tenantId;
1701
1689
  this.customerToken = options.customerToken;
1702
1690
  this.onUnauthorized = options.onUnauthorized;
1703
1691
  this.onRequestId = options.onRequestId;
1704
1692
  }
1705
1693
  async execute(endpoint, method, body) {
1706
1694
  const token = typeof this.customerToken === "function" ? this.customerToken() : this.customerToken;
1707
- const tenantId = this.secretKey?.startsWith("pat01_") && this.tenantId ? this.tenantId : void 0;
1708
1695
  try {
1709
1696
  const response = await httpFetch(endpoint, {
1710
1697
  method,
1711
1698
  publishableKey: this.publishableKey,
1712
1699
  secretKey: this.secretKey,
1713
- tenantId,
1714
1700
  customerToken: token ?? void 0,
1715
1701
  ...token && this.onUnauthorized && { onUnauthorized: this.onUnauthorized },
1716
1702
  ...body !== void 0 && { body: JSON.stringify(body) }
@@ -1897,7 +1883,6 @@ var ServerCommerceClient = class {
1897
1883
  const serverOptions = {
1898
1884
  publishableKey: options.publishableKey,
1899
1885
  secretKey: options.secretKey,
1900
- tenantId: options.tenantId,
1901
1886
  onRequestId: options.onRequestId
1902
1887
  };
1903
1888
  const productApi = new ProductApi(serverOptions);
@@ -2550,7 +2535,6 @@ var ServerClient = class {
2550
2535
  const serverOptions = {
2551
2536
  publishableKey: this.config.publishableKey,
2552
2537
  secretKey: this.config.secretKey,
2553
- tenantId: this.config.tenantId,
2554
2538
  onRequestId
2555
2539
  };
2556
2540
  this.commerce = new ServerCommerceClient(serverOptions);
@@ -2567,8 +2551,7 @@ var ServerClient = class {
2567
2551
  this.config.secretKey,
2568
2552
  void 0,
2569
2553
  void 0,
2570
- onRequestId,
2571
- this.config.tenantId
2554
+ onRequestId
2572
2555
  );
2573
2556
  this.queryClient = getQueryClient();
2574
2557
  this.query = new QueryHooks(this.queryClient, this.collections);
@@ -2761,7 +2744,7 @@ function createCustomerAuthWebhookHandler(handlers) {
2761
2744
  await handlers.unhandled?.(event);
2762
2745
  };
2763
2746
  }
2764
- async function verifySignature(payload, secret, signature) {
2747
+ async function verifySignature(payload, secret, signature, timestamp, deliveryId) {
2765
2748
  const encoder = new TextEncoder();
2766
2749
  const key = await crypto.subtle.importKey(
2767
2750
  "raw",
@@ -2776,14 +2759,35 @@ async function verifySignature(payload, secret, signature) {
2776
2759
  const sigBytes = new Uint8Array(
2777
2760
  (signature.match(/.{2}/g) ?? []).map((byte) => parseInt(byte, 16))
2778
2761
  );
2779
- return crypto.subtle.verify("HMAC", key, sigBytes, encoder.encode(payload));
2762
+ return crypto.subtle.verify(
2763
+ "HMAC",
2764
+ key,
2765
+ sigBytes,
2766
+ encoder.encode(`${timestamp}.${deliveryId}.${payload}`)
2767
+ );
2768
+ }
2769
+ function timestampIsFresh(timestamp, toleranceSeconds) {
2770
+ if (!/^\d+$/.test(timestamp)) return false;
2771
+ const timestampMs = Number(timestamp);
2772
+ if (!Number.isFinite(timestampMs)) return false;
2773
+ const skewMs = Math.abs(Date.now() - timestampMs);
2774
+ return skewMs <= toleranceSeconds * 1e3;
2780
2775
  }
2781
2776
  async function handleWebhook(request, handler, options) {
2782
2777
  try {
2783
2778
  const rawBody = await request.text();
2784
2779
  if (options?.secret) {
2785
2780
  const signature = request.headers.get("x-webhook-signature") || "";
2786
- const valid = await verifySignature(rawBody, options.secret, signature);
2781
+ const timestamp = request.headers.get("x-webhook-timestamp") || "";
2782
+ const deliveryId = request.headers.get("x-webhook-delivery-id") || "";
2783
+ const toleranceSeconds = options.toleranceSeconds ?? 300;
2784
+ const valid = Boolean(timestamp && deliveryId) && timestampIsFresh(timestamp, toleranceSeconds) && await verifySignature(
2785
+ rawBody,
2786
+ options.secret,
2787
+ signature,
2788
+ timestamp,
2789
+ deliveryId
2790
+ );
2787
2791
  if (!valid) {
2788
2792
  return new Response(
2789
2793
  JSON.stringify({ error: "Invalid webhook signature" }),