@01.software/sdk 0.21.0 → 0.22.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.js CHANGED
@@ -63,6 +63,46 @@ function resolveMetaImage(ref) {
63
63
  }
64
64
 
65
65
  // src/core/collection/query-builder.ts
66
+ var ReadOnlyCollectionQueryBuilder = class {
67
+ constructor(api, collection) {
68
+ this.api = api;
69
+ this.collection = collection;
70
+ }
71
+ async find(options) {
72
+ return this.api.requestFind(
73
+ `/api/${String(this.collection)}`,
74
+ options
75
+ );
76
+ }
77
+ async findById(id, options) {
78
+ return this.api.requestFindById(
79
+ `/api/${String(this.collection)}/${String(id)}`,
80
+ options
81
+ );
82
+ }
83
+ async count(options) {
84
+ return this.api.requestCount(
85
+ `/api/${String(this.collection)}/count`,
86
+ options
87
+ );
88
+ }
89
+ async findMetadata(options, metadataOptions) {
90
+ const { docs } = await this.find({ ...options, limit: 1, depth: 1 });
91
+ const doc = docs[0];
92
+ if (!doc) return null;
93
+ return generateMetadata(
94
+ extractSeo(doc),
95
+ metadataOptions
96
+ );
97
+ }
98
+ async findMetadataById(id, metadataOptions) {
99
+ const doc = await this.findById(id, { depth: 1 });
100
+ return generateMetadata(
101
+ extractSeo(doc),
102
+ metadataOptions
103
+ );
104
+ }
105
+ };
66
106
  var CollectionQueryBuilder = class {
67
107
  constructor(api, collection) {
68
108
  this.api = api;
@@ -236,8 +276,8 @@ var NetworkError = class extends SDKError {
236
276
  }
237
277
  };
238
278
  var ValidationError = class extends SDKError {
239
- constructor(message, details, userMessage, suggestion) {
240
- super("VALIDATION_ERROR", message, 400, details, userMessage, suggestion);
279
+ constructor(message, details, userMessage, suggestion, status = 400) {
280
+ super("VALIDATION_ERROR", message, status, details, userMessage, suggestion);
241
281
  this.name = "ValidationError";
242
282
  }
243
283
  };
@@ -366,7 +406,7 @@ function isRateLimitError(error) {
366
406
  return error instanceof RateLimitError;
367
407
  }
368
408
  var createNetworkError = (message, status, details, userMessage, suggestion) => new NetworkError(message, status, details, userMessage, suggestion);
369
- var createValidationError = (message, details, userMessage, suggestion) => new ValidationError(message, details, userMessage, suggestion);
409
+ var createValidationError = (message, details, userMessage, suggestion, status) => new ValidationError(message, details, userMessage, suggestion, status);
370
410
  var createApiError = (message, status, details, userMessage, suggestion) => new ApiError(message, status, details, userMessage, suggestion);
371
411
  var createConfigError = (message, details, userMessage, suggestion) => new ConfigError(message, details, userMessage, suggestion);
372
412
  var createTimeoutError = (message, details, userMessage, suggestion) => new TimeoutError(message, details, userMessage, suggestion);
@@ -377,6 +417,16 @@ var createNotFoundError = (message, details, userMessage, suggestion, requestId)
377
417
  var createConflictError = (message, details, userMessage, suggestion, requestId) => new ConflictError(message, details, userMessage, suggestion, requestId);
378
418
  var createRateLimitError = (message, retryAfter, details, userMessage, suggestion, requestId) => new RateLimitError(message, retryAfter, details, userMessage, suggestion, requestId);
379
419
 
420
+ // src/core/internal/utils/credentials.ts
421
+ function requirePublishableKeyForSecret(apiName, publishableKey, secretKey) {
422
+ if (secretKey && !publishableKey) {
423
+ throw createConfigError(
424
+ `publishableKey is required for ${apiName} when secretKey is used. It is sent as X-Publishable-Key for tenant routing, rate limiting, and quota enforcement.`
425
+ );
426
+ }
427
+ return publishableKey ?? "";
428
+ }
429
+
380
430
  // src/core/client/types.ts
381
431
  function resolveApiUrl() {
382
432
  if (typeof process !== "undefined" && process.env) {
@@ -391,7 +441,7 @@ function resolveApiUrl() {
391
441
  // src/core/internal/utils/http.ts
392
442
  var DEFAULT_TIMEOUT = 3e4;
393
443
  var DEFAULT_RETRYABLE_STATUSES = [408, 429, 500, 502, 503, 504];
394
- var NON_RETRYABLE_STATUSES = [400, 401, 403, 404, 422];
444
+ var NON_RETRYABLE_STATUSES = [400, 401, 403, 404, 409, 422];
395
445
  var SAFE_METHODS = ["GET", "HEAD", "OPTIONS"];
396
446
  function debugLog(debug, type, message, data) {
397
447
  if (!debug) return;
@@ -474,6 +524,80 @@ function attachRequestId(err, id) {
474
524
  if (id) err.requestId = id;
475
525
  return err;
476
526
  }
527
+ function createHttpStatusError(status, parsed, details, requestId) {
528
+ const errorDetails = {
529
+ ...details,
530
+ ...parsed.errors && { errors: parsed.errors },
531
+ ...parsed.body && { body: parsed.body }
532
+ };
533
+ const suggestion = getErrorSuggestion(status);
534
+ if (status === 400 || status === 422) {
535
+ return attachRequestId(
536
+ createValidationError(
537
+ parsed.errorMessage,
538
+ errorDetails,
539
+ parsed.userMessage,
540
+ suggestion,
541
+ status
542
+ ),
543
+ requestId
544
+ );
545
+ }
546
+ if (status === 401) {
547
+ return attachRequestId(
548
+ createAuthError(
549
+ parsed.errorMessage,
550
+ errorDetails,
551
+ parsed.userMessage,
552
+ suggestion
553
+ ),
554
+ requestId
555
+ );
556
+ }
557
+ if (status === 403) {
558
+ return attachRequestId(
559
+ createPermissionError(
560
+ parsed.errorMessage,
561
+ errorDetails,
562
+ parsed.userMessage,
563
+ suggestion
564
+ ),
565
+ requestId
566
+ );
567
+ }
568
+ if (status === 404) {
569
+ return attachRequestId(
570
+ createNotFoundError(
571
+ parsed.errorMessage,
572
+ errorDetails,
573
+ parsed.userMessage,
574
+ suggestion
575
+ ),
576
+ requestId
577
+ );
578
+ }
579
+ if (status === 409) {
580
+ return attachRequestId(
581
+ createConflictError(
582
+ parsed.errorMessage,
583
+ errorDetails,
584
+ parsed.userMessage,
585
+ suggestion
586
+ ),
587
+ requestId
588
+ );
589
+ }
590
+ return attachRequestId(
591
+ createNetworkError(
592
+ parsed.errorMessage,
593
+ status,
594
+ errorDetails,
595
+ parsed.userMessage,
596
+ suggestion
597
+ ),
598
+ requestId
599
+ );
600
+ }
477
601
  async function httpFetch(url, options) {
478
602
  const {
479
603
  publishableKey,
@@ -578,14 +702,10 @@ async function httpFetch(url, options) {
578
702
  attempt: attempt + 1
579
703
  };
580
704
  if (NON_RETRYABLE_STATUSES.includes(response.status)) {
581
- throw attachRequestId(
582
- createNetworkError(
583
- parsed.errorMessage,
584
- response.status,
585
- { ...details, ...parsed.errors && { errors: parsed.errors } },
586
- parsed.userMessage,
587
- getErrorSuggestion(response.status)
588
- ),
705
+ throw createHttpStatusError(
706
+ response.status,
707
+ parsed,
708
+ details,
589
709
  requestId
590
710
  );
591
711
  }
@@ -675,7 +795,11 @@ async function httpFetch(url, options) {
675
795
  // src/core/collection/http-client.ts
676
796
  var HttpClient = class {
677
797
  constructor(publishableKey, secretKey, getCustomerToken, onUnauthorized, onRequestId) {
678
- this.publishableKey = publishableKey;
798
+ this.publishableKey = requirePublishableKeyForSecret(
799
+ "CollectionClient",
800
+ publishableKey,
801
+ secretKey
802
+ );
679
803
  this.secretKey = secretKey;
680
804
  this.getCustomerToken = getCustomerToken;
681
805
  this.onUnauthorized = onUnauthorized;
@@ -948,6 +1072,35 @@ var CollectionClient = class extends HttpClient {
948
1072
  return this.parseMutationResponse(response);
949
1073
  }
950
1074
  };
1075
+ var ReadOnlyCollectionClient = class extends HttpClient {
1076
+ from(collection) {
1077
+ return new ReadOnlyCollectionQueryBuilder(this, collection);
1078
+ }
1079
+ async requestFind(endpoint, options) {
1080
+ const url = this.buildUrl(endpoint, options);
1081
+ const response = await this.fetchWithTracking(url, {
1082
+ ...this.defaultOptions,
1083
+ method: "GET"
1084
+ });
1085
+ return this.parseFindResponse(response);
1086
+ }
1087
+ async requestFindById(endpoint, options) {
1088
+ const url = this.buildUrl(endpoint, options);
1089
+ const response = await this.fetchWithTracking(url, {
1090
+ ...this.defaultOptions,
1091
+ method: "GET"
1092
+ });
1093
+ return this.parseDocumentResponse(response);
1094
+ }
1095
+ async requestCount(endpoint, options) {
1096
+ const url = this.buildUrl(endpoint, options);
1097
+ const response = await this.fetchWithTracking(url, {
1098
+ ...this.defaultOptions,
1099
+ method: "GET"
1100
+ });
1101
+ return this.parseDocumentResponse(response);
1102
+ }
1103
+ };
951
1104
 
952
1105
  // src/core/collection/const.ts
953
1106
  var INTERNAL_COLLECTIONS = [
@@ -970,6 +1123,7 @@ var INTERNAL_COLLECTIONS = [
970
1123
  "api-keys",
971
1124
  "personal-access-tokens",
972
1125
  "tenant-entitlements",
1126
+ "direct-upload-sessions",
973
1127
  "webhook-events",
974
1128
  "webhook-deliveries",
975
1129
  "audit-logs",
@@ -1106,7 +1260,11 @@ async function parseApiResponse(response, endpoint) {
1106
1260
  // src/core/community/community-client.ts
1107
1261
  var CommunityClient = class {
1108
1262
  constructor(options) {
1109
- this.publishableKey = options.publishableKey ?? "";
1263
+ this.publishableKey = requirePublishableKeyForSecret(
1264
+ "CommunityClient",
1265
+ options.publishableKey,
1266
+ options.secretKey
1267
+ );
1110
1268
  this.secretKey = options.secretKey;
1111
1269
  this.customerToken = options.customerToken;
1112
1270
  this.onUnauthorized = options.onUnauthorized;
@@ -1276,7 +1434,11 @@ var BaseApi = class {
1276
1434
  if (!options.secretKey) {
1277
1435
  throw createConfigError(`secretKey is required for ${apiName}.`);
1278
1436
  }
1279
- this.publishableKey = options.publishableKey ?? "";
1437
+ this.publishableKey = requirePublishableKeyForSecret(
1438
+ apiName,
1439
+ options.publishableKey,
1440
+ options.secretKey
1441
+ );
1280
1442
  this.secretKey = options.secretKey;
1281
1443
  this.onRequestId = options.onRequestId;
1282
1444
  }
@@ -1570,7 +1732,11 @@ var CartApi = class {
1570
1732
  "Either secretKey or customerToken is required for CartApi."
1571
1733
  );
1572
1734
  }
1573
- this.publishableKey = options.publishableKey ?? "";
1735
+ this.publishableKey = requirePublishableKeyForSecret(
1736
+ "CartApi",
1737
+ options.publishableKey,
1738
+ options.secretKey
1739
+ );
1574
1740
  this.secretKey = options.secretKey;
1575
1741
  this.customerToken = options.customerToken;
1576
1742
  this.onUnauthorized = options.onUnauthorized;
@@ -1766,8 +1932,13 @@ var OrderApi = class extends BaseApi {
1766
1932
  // src/core/commerce/server-commerce-client.ts
1767
1933
  var ServerCommerceClient = class {
1768
1934
  constructor(options) {
1935
+ const publishableKey = requirePublishableKeyForSecret(
1936
+ "ServerCommerceClient",
1937
+ options.publishableKey,
1938
+ options.secretKey
1939
+ );
1769
1940
  const serverOptions = {
1770
- publishableKey: options.publishableKey,
1941
+ publishableKey,
1771
1942
  secretKey: options.secretKey,
1772
1943
  onRequestId: options.onRequestId
1773
1944
  };
@@ -2386,7 +2557,14 @@ var Client = class {
2386
2557
  onUnauthorized,
2387
2558
  onRequestId
2388
2559
  });
2389
- this.collections = new CollectionClient(
2560
+ const collectionClient = new CollectionClient(
2561
+ this.config.publishableKey,
2562
+ void 0,
2563
+ () => this.customer.auth.getToken(),
2564
+ onUnauthorized,
2565
+ onRequestId
2566
+ );
2567
+ this.collections = new ReadOnlyCollectionClient(
2390
2568
  this.config.publishableKey,
2391
2569
  void 0,
2392
2570
  () => this.customer.auth.getToken(),
@@ -2395,7 +2573,7 @@ var Client = class {
2395
2573
  );
2396
2574
  this.query = new QueryHooks(
2397
2575
  this.queryClient,
2398
- this.collections,
2576
+ collectionClient,
2399
2577
  this.customer.auth
2400
2578
  );
2401
2579
  }
@@ -3284,6 +3462,7 @@ export {
3284
3462
  ProductApi,
3285
3463
  QueryHooks,
3286
3464
  RateLimitError,
3465
+ ReadOnlyCollectionClient,
3287
3466
  RealtimeConnection,
3288
3467
  SDKError,
3289
3468
  ServerClient,