arky-sdk 0.3.29 → 0.3.32

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
@@ -517,6 +517,193 @@ var createAnalyticsApi = (apiConfig) => {
517
517
  };
518
518
  };
519
519
 
520
+ // src/utils/slug.ts
521
+ var isUuid = (str) => {
522
+ const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
523
+ return uuidRegex.test(str);
524
+ };
525
+ var formatIdOrSlug = (id, apiConfig) => {
526
+ if (isUuid(id)) {
527
+ return id;
528
+ }
529
+ if (id.includes(":")) {
530
+ return id;
531
+ }
532
+ return `${apiConfig.businessId}:${apiConfig.locale}:${id}`;
533
+ };
534
+
535
+ // src/utils/blocks.ts
536
+ function getBlockLabel(block, locale = "en") {
537
+ if (!block) return "";
538
+ if (block.properties?.label) {
539
+ if (typeof block.properties.label === "object") {
540
+ return block.properties.label[locale] || block.properties.label.en || Object.values(block.properties.label)[0] || "";
541
+ }
542
+ if (typeof block.properties.label === "string") {
543
+ return block.properties.label;
544
+ }
545
+ }
546
+ return block.key?.replace(/_/g, " ").replace(/\b\w/g, (l) => l.toUpperCase()) || "";
547
+ }
548
+ function formatBlockValue(block) {
549
+ if (!block || block.value === null || block.value === void 0) {
550
+ return "";
551
+ }
552
+ switch (block.type) {
553
+ case "BOOLEAN":
554
+ return block.value ? "Yes" : "No";
555
+ case "NUMBER":
556
+ if (block.properties?.variant === "DATE" || block.properties?.variant === "DATE_TIME") {
557
+ try {
558
+ return new Date(block.value).toLocaleDateString();
559
+ } catch (e) {
560
+ return String(block.value);
561
+ }
562
+ }
563
+ return String(block.value);
564
+ case "RELATIONSHIP":
565
+ if (Array.isArray(block.value) && block.value.length > 0) {
566
+ const firstValue = block.value[0];
567
+ if (firstValue && firstValue.mimeType) {
568
+ return firstValue.name || firstValue.id || "Media";
569
+ }
570
+ return firstValue.title || firstValue.name || firstValue.id || "Entry";
571
+ }
572
+ return String(block.value);
573
+ default:
574
+ return String(block.value);
575
+ }
576
+ }
577
+ function prepareBlocksForSubmission(formData) {
578
+ const preparedBlocks = [];
579
+ Object.keys(formData).forEach((key) => {
580
+ if (formData[key] !== null && formData[key] !== void 0) {
581
+ preparedBlocks.push({
582
+ key,
583
+ value: [formData[key]]
584
+ });
585
+ }
586
+ });
587
+ return preparedBlocks;
588
+ }
589
+ function extractBlockValues(blocks) {
590
+ const values = {};
591
+ blocks.forEach((block) => {
592
+ if (block.value && block.value.length > 0) {
593
+ values[block.key] = block.value[0];
594
+ } else {
595
+ values[block.key] = null;
596
+ }
597
+ });
598
+ return values;
599
+ }
600
+ function getBlockTextValue(block, locale = "en") {
601
+ if (!block || !block.value || block.value.length === 0) return "";
602
+ const firstValue = block.value[0];
603
+ if (typeof firstValue === "object" && firstValue !== null) {
604
+ if (firstValue[locale]) return firstValue[locale];
605
+ if (firstValue.en) return firstValue.en;
606
+ const values = Object.values(firstValue);
607
+ return String(values[0] || "");
608
+ }
609
+ return String(firstValue);
610
+ }
611
+ var getBlockValue = (entry, blockKey) => {
612
+ if (!entry || !entry.blocks) return null;
613
+ const block = entry.blocks.find((f) => f.key === blockKey);
614
+ if (!block || !block.value || block.value.length === 0) return null;
615
+ return block.value[0];
616
+ };
617
+ var getBlockValues = (entry, blockKey) => {
618
+ if (!entry || !entry.blocks) return null;
619
+ const block = entry.blocks.find((f) => f.key === blockKey);
620
+ if (!block || !block.value || block.value.length === 0) return null;
621
+ return block.value;
622
+ };
623
+ function unwrapBlock(block, locale) {
624
+ if (!block?.type || block.value === void 0) return block;
625
+ if (block.type === "BLOCK") {
626
+ return block.value.map((obj) => {
627
+ const parsed = {};
628
+ for (const [k, v] of Object.entries(obj)) {
629
+ parsed[k] = unwrapBlock(v, locale);
630
+ }
631
+ return parsed;
632
+ });
633
+ }
634
+ const isLocalized = block.type === "TEXT";
635
+ const isList = block.properties?.ui === "list" || (block.properties?.maxValues ?? 1) > 1 || block.value.length > 1;
636
+ if (isList) {
637
+ return isLocalized ? block.value.map((v) => v[locale] || v["en"]) : [...block.value];
638
+ }
639
+ return isLocalized ? block.value[0][locale] || block.value[0]["en"] : block.value[0];
640
+ }
641
+ var getBlockObjectValues = (entry, blockKey, locale = "en") => {
642
+ if (!entry) {
643
+ return [];
644
+ }
645
+ const values = getBlockValues(entry, blockKey);
646
+ const parsed = values.map((obj) => {
647
+ const res = obj.value.reduce((acc, current) => {
648
+ acc[current.key] = unwrapBlock(current, locale);
649
+ return acc;
650
+ }, {});
651
+ return res;
652
+ });
653
+ return parsed;
654
+ };
655
+ var getBlockFromArray = (entry, blockKey, locale = "en") => {
656
+ if (!entry) {
657
+ return [];
658
+ }
659
+ const values = getBlockValues(entry, blockKey);
660
+ return values.reduce((acc, current) => {
661
+ acc[current.key] = unwrapBlock(current, locale);
662
+ return acc;
663
+ });
664
+ };
665
+ var getImageUrl = (imageBlock, isBlock = true, storageUrl = "https://storage.arky.io/dev") => {
666
+ if (!imageBlock) return null;
667
+ const isExternalUrl = (url) => {
668
+ return url.startsWith("http://") || url.startsWith("https://");
669
+ };
670
+ if (imageBlock.type === "RELATIONSHIP" && Array.isArray(imageBlock.value)) {
671
+ const mediaValue = imageBlock.value[0];
672
+ if (mediaValue && mediaValue.mimeType) {
673
+ if (mediaValue.resolutions && mediaValue.resolutions.original && mediaValue.resolutions.original.url) {
674
+ const url = mediaValue.resolutions.original.url;
675
+ return isExternalUrl(url) ? url : `${storageUrl}/${url}`;
676
+ }
677
+ if (mediaValue.url) {
678
+ return isExternalUrl(mediaValue.url) ? mediaValue.url : `${storageUrl}/${mediaValue.url}`;
679
+ }
680
+ }
681
+ return null;
682
+ }
683
+ if (isBlock) {
684
+ if (typeof imageBlock === "string") {
685
+ if (isExternalUrl(imageBlock)) {
686
+ return imageBlock;
687
+ }
688
+ return `${storageUrl}/${imageBlock}`;
689
+ }
690
+ if (imageBlock.url) {
691
+ if (isExternalUrl(imageBlock.url)) {
692
+ return imageBlock.url;
693
+ }
694
+ return `${storageUrl}/${imageBlock.url}`;
695
+ }
696
+ }
697
+ if (imageBlock.resolutions && imageBlock.resolutions.original && imageBlock.resolutions.original.url) {
698
+ const url = imageBlock.resolutions.original.url;
699
+ if (isExternalUrl(url)) {
700
+ return url;
701
+ }
702
+ return `${storageUrl}/${url}`;
703
+ }
704
+ return null;
705
+ };
706
+
520
707
  // src/api/cms.ts
521
708
  var createCmsApi = (apiConfig) => {
522
709
  return {
@@ -542,10 +729,24 @@ var createCmsApi = (apiConfig) => {
542
729
  );
543
730
  },
544
731
  async getCollection(params, options) {
545
- return apiConfig.httpClient.get(
546
- `/v1/businesses/${apiConfig.businessId}/collections/${params.id}`,
732
+ const formattedId = formatIdOrSlug(params.id, apiConfig);
733
+ const response = await apiConfig.httpClient.get(
734
+ `/v1/businesses/${apiConfig.businessId}/collections/${formattedId}`,
547
735
  options
548
736
  );
737
+ return {
738
+ ...response,
739
+ getBlock(key) {
740
+ return getBlockFromArray(response, key, apiConfig.locale);
741
+ },
742
+ getBlockValues(key) {
743
+ return getBlockObjectValues(response, key, apiConfig.locale);
744
+ },
745
+ getImage(key) {
746
+ const block = getBlockFromArray(response, key, apiConfig.locale);
747
+ return getImageUrl(block, true, apiConfig.storageUrl);
748
+ }
749
+ };
549
750
  },
550
751
  async getCollections(params, options) {
551
752
  return apiConfig.httpClient.get(
@@ -607,8 +808,9 @@ var createCmsApi = (apiConfig) => {
607
808
  );
608
809
  },
609
810
  async getCollectionEntry(params, options) {
811
+ const formattedId = formatIdOrSlug(params.id, apiConfig);
610
812
  return apiConfig.httpClient.get(
611
- `/v1/businesses/${apiConfig.businessId}/entries/${params.id}`,
813
+ `/v1/businesses/${apiConfig.businessId}/entries/${formattedId}`,
612
814
  options
613
815
  );
614
816
  },
@@ -633,8 +835,9 @@ var createCmsApi = (apiConfig) => {
633
835
  },
634
836
  // ===== COLLECTION SUBSCRIPTIONS =====
635
837
  async getCollectionSubscribers(params, options) {
838
+ const formattedId = formatIdOrSlug(params.id, apiConfig);
636
839
  return apiConfig.httpClient.get(
637
- `/v1/businesses/${apiConfig.businessId}/collections/${params.id}/subscribers`,
840
+ `/v1/businesses/${apiConfig.businessId}/collections/${formattedId}/subscribers`,
638
841
  options
639
842
  );
640
843
  },
@@ -650,10 +853,13 @@ var createCmsApi = (apiConfig) => {
650
853
  );
651
854
  },
652
855
  async unsubscribeFromCollection(params, options) {
653
- return apiConfig.httpClient.get(`/v1/businesses/${apiConfig.businessId}/collections/unsubscribe`, {
654
- ...options,
655
- params
656
- });
856
+ return apiConfig.httpClient.get(
857
+ `/v1/businesses/${apiConfig.businessId}/collections/unsubscribe`,
858
+ {
859
+ ...options,
860
+ params
861
+ }
862
+ );
657
863
  }
658
864
  };
659
865
  };
@@ -683,10 +889,22 @@ var createEshopApi = (apiConfig) => {
683
889
  );
684
890
  },
685
891
  async getProduct(params, options) {
686
- return apiConfig.httpClient.get(
687
- `/v1/businesses/${apiConfig.businessId}/products/${params.id}`,
892
+ const formattedId = formatIdOrSlug(params.id, apiConfig);
893
+ const response = await apiConfig.httpClient.get(
894
+ `/v1/businesses/${apiConfig.businessId}/products/${formattedId}`,
688
895
  options
689
896
  );
897
+ return {
898
+ ...response,
899
+ getName() {
900
+ const locale = apiConfig.locale;
901
+ return response.name?.[locale] || response.name?.en || response.name || "";
902
+ },
903
+ getDescription() {
904
+ const locale = apiConfig.locale;
905
+ return response.description?.[locale] || response.description?.en || response.description || "";
906
+ }
907
+ };
690
908
  },
691
909
  async getProducts(params, options) {
692
910
  return apiConfig.httpClient.get(
@@ -755,7 +973,9 @@ var createEshopApi = (apiConfig) => {
755
973
  currency: params.currency,
756
974
  paymentMethod: params.paymentMethod,
757
975
  lines,
758
- ...params.shippingMethodId && { shippingMethodId: params.shippingMethodId },
976
+ ...params.shippingMethodId && {
977
+ shippingMethodId: params.shippingMethodId
978
+ },
759
979
  ...params.promoCode && { promoCode: params.promoCode }
760
980
  };
761
981
  return apiConfig.httpClient.post(`/v1/payments/quote`, payload, options);
@@ -793,7 +1013,11 @@ var createReservationApi = (apiConfig) => {
793
1013
  },
794
1014
  async updateReservation(params, options) {
795
1015
  const { id, ...payload } = params;
796
- return apiConfig.httpClient.put(`/v1/reservations/${id}`, payload, options);
1016
+ return apiConfig.httpClient.put(
1017
+ `/v1/reservations/${id}`,
1018
+ payload,
1019
+ options
1020
+ );
797
1021
  },
798
1022
  async checkout(params, options) {
799
1023
  const payload = {
@@ -804,7 +1028,11 @@ var createReservationApi = (apiConfig) => {
804
1028
  ...params.paymentMethod && { paymentMethod: params.paymentMethod },
805
1029
  ...params.promoCode && { promoCode: params.promoCode }
806
1030
  };
807
- return apiConfig.httpClient.post(`/v1/reservations/checkout`, payload, options);
1031
+ return apiConfig.httpClient.post(
1032
+ `/v1/reservations/checkout`,
1033
+ payload,
1034
+ options
1035
+ );
808
1036
  },
809
1037
  async getReservation(params, options) {
810
1038
  return apiConfig.httpClient.get(`/v1/reservations/${params.id}`, {
@@ -872,10 +1100,22 @@ var createReservationApi = (apiConfig) => {
872
1100
  );
873
1101
  },
874
1102
  async getService(params, options) {
875
- return apiConfig.httpClient.get(
876
- `/v1/businesses/${apiConfig.businessId}/services/${params.id}`,
1103
+ const formattedId = formatIdOrSlug(params.id, apiConfig);
1104
+ const response = await apiConfig.httpClient.get(
1105
+ `/v1/businesses/${apiConfig.businessId}/services/${formattedId}`,
877
1106
  options
878
1107
  );
1108
+ return {
1109
+ ...response,
1110
+ getName() {
1111
+ const locale = apiConfig.locale;
1112
+ return response.name?.[locale] || response.name?.en || response.name || "";
1113
+ },
1114
+ getDescription() {
1115
+ const locale = apiConfig.locale;
1116
+ return response.description?.[locale] || response.description?.en || response.description || "";
1117
+ }
1118
+ };
879
1119
  },
880
1120
  async getServices(params, options) {
881
1121
  return apiConfig.httpClient.get(
@@ -969,178 +1209,6 @@ var createPaymentApi = (apiConfig) => {
969
1209
  };
970
1210
  };
971
1211
 
972
- // src/utils/blocks.ts
973
- function getBlockLabel(block, locale = "en") {
974
- if (!block) return "";
975
- if (block.properties?.label) {
976
- if (typeof block.properties.label === "object") {
977
- return block.properties.label[locale] || block.properties.label.en || Object.values(block.properties.label)[0] || "";
978
- }
979
- if (typeof block.properties.label === "string") {
980
- return block.properties.label;
981
- }
982
- }
983
- return block.key?.replace(/_/g, " ").replace(/\b\w/g, (l) => l.toUpperCase()) || "";
984
- }
985
- function formatBlockValue(block) {
986
- if (!block || block.value === null || block.value === void 0) {
987
- return "";
988
- }
989
- switch (block.type) {
990
- case "BOOLEAN":
991
- return block.value ? "Yes" : "No";
992
- case "NUMBER":
993
- if (block.properties?.variant === "DATE" || block.properties?.variant === "DATE_TIME") {
994
- try {
995
- return new Date(block.value).toLocaleDateString();
996
- } catch (e) {
997
- return String(block.value);
998
- }
999
- }
1000
- return String(block.value);
1001
- case "RELATIONSHIP":
1002
- if (Array.isArray(block.value) && block.value.length > 0) {
1003
- const firstValue = block.value[0];
1004
- if (firstValue && firstValue.mimeType) {
1005
- return firstValue.name || firstValue.id || "Media";
1006
- }
1007
- return firstValue.title || firstValue.name || firstValue.id || "Entry";
1008
- }
1009
- return String(block.value);
1010
- default:
1011
- return String(block.value);
1012
- }
1013
- }
1014
- function prepareBlocksForSubmission(formData) {
1015
- const preparedBlocks = [];
1016
- Object.keys(formData).forEach((key) => {
1017
- if (formData[key] !== null && formData[key] !== void 0) {
1018
- preparedBlocks.push({
1019
- key,
1020
- value: [formData[key]]
1021
- });
1022
- }
1023
- });
1024
- return preparedBlocks;
1025
- }
1026
- function extractBlockValues(blocks) {
1027
- const values = {};
1028
- blocks.forEach((block) => {
1029
- if (block.value && block.value.length > 0) {
1030
- values[block.key] = block.value[0];
1031
- } else {
1032
- values[block.key] = null;
1033
- }
1034
- });
1035
- return values;
1036
- }
1037
- function getBlockTextValue(block, locale = "en") {
1038
- if (!block || !block.value || block.value.length === 0) return "";
1039
- const firstValue = block.value[0];
1040
- if (typeof firstValue === "object" && firstValue !== null) {
1041
- if (firstValue[locale]) return firstValue[locale];
1042
- if (firstValue.en) return firstValue.en;
1043
- const values = Object.values(firstValue);
1044
- return String(values[0] || "");
1045
- }
1046
- return String(firstValue);
1047
- }
1048
- var getBlockValue = (entry, blockKey) => {
1049
- if (!entry || !entry.blocks) return null;
1050
- const block = entry.blocks.find((f) => f.key === blockKey);
1051
- if (!block || !block.value || block.value.length === 0) return null;
1052
- return block.value[0];
1053
- };
1054
- var getBlockValues = (entry, blockKey) => {
1055
- if (!entry || !entry.blocks) return null;
1056
- const block = entry.blocks.find((f) => f.key === blockKey);
1057
- if (!block || !block.value || block.value.length === 0) return null;
1058
- return block.value;
1059
- };
1060
- function unwrapBlock(block, locale) {
1061
- if (!block?.type || block.value === void 0) return block;
1062
- if (block.type === "BLOCK") {
1063
- return block.value.map((obj) => {
1064
- const parsed = {};
1065
- for (const [k, v] of Object.entries(obj)) {
1066
- parsed[k] = unwrapBlock(v, locale);
1067
- }
1068
- return parsed;
1069
- });
1070
- }
1071
- const isLocalized = block.type === "TEXT";
1072
- const isList = block.properties?.ui === "list" || (block.properties?.maxValues ?? 1) > 1 || block.value.length > 1;
1073
- if (isList) {
1074
- return isLocalized ? block.value.map((v) => v[locale] || v["en"]) : [...block.value];
1075
- }
1076
- return isLocalized ? block.value[0][locale] || block.value[0]["en"] : block.value[0];
1077
- }
1078
- var getBlockObjectValues = (entry, blockKey, locale = "en") => {
1079
- if (!entry) {
1080
- return [];
1081
- }
1082
- const values = getBlockValues(entry, blockKey);
1083
- const parsed = values.map((obj) => {
1084
- const res = obj.value.reduce((acc, current) => {
1085
- acc[current.key] = unwrapBlock(current, locale);
1086
- return acc;
1087
- }, {});
1088
- return res;
1089
- });
1090
- return parsed;
1091
- };
1092
- var getBlockFromArray = (entry, blockKey, locale = "en") => {
1093
- if (!entry) {
1094
- return [];
1095
- }
1096
- const values = getBlockValues(entry, blockKey);
1097
- return values.reduce((acc, current) => {
1098
- acc[current.key] = unwrapBlock(current, locale);
1099
- return acc;
1100
- });
1101
- };
1102
- var getImageUrl = (imageBlock, isBlock = true, storageUrl = "https://storage.arky.io/dev") => {
1103
- if (!imageBlock) return null;
1104
- const isExternalUrl = (url) => {
1105
- return url.startsWith("http://") || url.startsWith("https://");
1106
- };
1107
- if (imageBlock.type === "RELATIONSHIP" && Array.isArray(imageBlock.value)) {
1108
- const mediaValue = imageBlock.value[0];
1109
- if (mediaValue && mediaValue.mimeType) {
1110
- if (mediaValue.resolutions && mediaValue.resolutions.original && mediaValue.resolutions.original.url) {
1111
- const url = mediaValue.resolutions.original.url;
1112
- return isExternalUrl(url) ? url : `${storageUrl}/${url}`;
1113
- }
1114
- if (mediaValue.url) {
1115
- return isExternalUrl(mediaValue.url) ? mediaValue.url : `${storageUrl}/${mediaValue.url}`;
1116
- }
1117
- }
1118
- return null;
1119
- }
1120
- if (isBlock) {
1121
- if (typeof imageBlock === "string") {
1122
- if (isExternalUrl(imageBlock)) {
1123
- return imageBlock;
1124
- }
1125
- return `${storageUrl}/${imageBlock}`;
1126
- }
1127
- if (imageBlock.url) {
1128
- if (isExternalUrl(imageBlock.url)) {
1129
- return imageBlock.url;
1130
- }
1131
- return `${storageUrl}/${imageBlock.url}`;
1132
- }
1133
- }
1134
- if (imageBlock.resolutions && imageBlock.resolutions.original && imageBlock.resolutions.original.url) {
1135
- const url = imageBlock.resolutions.original.url;
1136
- if (isExternalUrl(url)) {
1137
- return url;
1138
- }
1139
- return `${storageUrl}/${url}`;
1140
- }
1141
- return null;
1142
- };
1143
-
1144
1212
  // src/utils/currency.ts
1145
1213
  function getCurrencySymbol(currency) {
1146
1214
  const currencySymbols = {
@@ -1489,7 +1557,7 @@ async function injectSvgIntoElement(mediaObject, targetElement, className) {
1489
1557
  }
1490
1558
 
1491
1559
  // src/index.ts
1492
- var SDK_VERSION = "0.3.28";
1560
+ var SDK_VERSION = "0.3.30";
1493
1561
  var SUPPORTED_FRAMEWORKS = [
1494
1562
  "astro",
1495
1563
  "react",
@@ -1498,8 +1566,9 @@ var SUPPORTED_FRAMEWORKS = [
1498
1566
  "vanilla"
1499
1567
  ];
1500
1568
  function createArkySDK(config) {
1569
+ const locale = config.locale || "en";
1501
1570
  console.log(
1502
- `[Arky SDK v${SDK_VERSION}] Initializing with market: ${config.market}, businessId: ${config.businessId}`
1571
+ `[Arky SDK v${SDK_VERSION}] Initializing with market: ${config.market}, businessId: ${config.businessId}, locale: ${locale}`
1503
1572
  );
1504
1573
  const httpClient = createHttpClient(config);
1505
1574
  const storageUrl = config.storageUrl || "https://storage.arky.io/dev";
@@ -1509,6 +1578,7 @@ function createArkySDK(config) {
1509
1578
  storageUrl,
1510
1579
  baseUrl: config.baseUrl,
1511
1580
  market: config.market,
1581
+ locale,
1512
1582
  setToken: config.setToken,
1513
1583
  getToken: config.getToken
1514
1584
  };
@@ -1534,6 +1604,10 @@ function createArkySDK(config) {
1534
1604
  apiConfig.market = market;
1535
1605
  },
1536
1606
  getMarket: () => apiConfig.market,
1607
+ setLocale: (locale2) => {
1608
+ apiConfig.locale = locale2;
1609
+ },
1610
+ getLocale: () => apiConfig.locale,
1537
1611
  isAuthenticated: config.isAuthenticated || (() => false),
1538
1612
  logout: config.logout,
1539
1613
  setToken: config.setToken,