@cimplify/sdk 0.7.0 → 0.7.2

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/react.js CHANGED
@@ -885,6 +885,22 @@ function readFinalPrice(value) {
885
885
  }
886
886
  return void 0;
887
887
  }
888
+ function normalizeAddOnPayload(addOn) {
889
+ if (!isRecord(addOn)) return addOn;
890
+ const normalizedAddOn = { ...addOn };
891
+ const options = normalizedAddOn["options"];
892
+ if (!Array.isArray(options)) return normalizedAddOn;
893
+ normalizedAddOn["options"] = options.map((option) => {
894
+ if (!isRecord(option)) return option;
895
+ const normalizedOption = { ...option };
896
+ const optionPrice = normalizedOption["default_price"];
897
+ if (optionPrice === void 0 || optionPrice === null || optionPrice === "") {
898
+ normalizedOption["default_price"] = readFinalPrice(normalizedOption["default_price_info"]) || readFinalPrice(normalizedOption["price_info"]) || "0";
899
+ }
900
+ return normalizedOption;
901
+ });
902
+ return normalizedAddOn;
903
+ }
888
904
  function normalizeCatalogueProductPayload(product) {
889
905
  const normalized = { ...product };
890
906
  const defaultPrice = normalized["default_price"];
@@ -906,22 +922,7 @@ function normalizeCatalogueProductPayload(product) {
906
922
  }
907
923
  const addOns = normalized["add_ons"];
908
924
  if (Array.isArray(addOns)) {
909
- normalized["add_ons"] = addOns.map((addOn) => {
910
- if (!isRecord(addOn)) return addOn;
911
- const normalizedAddOn = { ...addOn };
912
- const options = normalizedAddOn["options"];
913
- if (!Array.isArray(options)) return normalizedAddOn;
914
- normalizedAddOn["options"] = options.map((option) => {
915
- if (!isRecord(option)) return option;
916
- const normalizedOption = { ...option };
917
- const optionPrice = normalizedOption["default_price"];
918
- if (optionPrice === void 0 || optionPrice === null || optionPrice === "") {
919
- normalizedOption["default_price"] = readFinalPrice(normalizedOption["default_price_info"]) || readFinalPrice(normalizedOption["price_info"]) || "0";
920
- }
921
- return normalizedOption;
922
- });
923
- return normalizedAddOn;
924
- });
925
+ normalized["add_ons"] = addOns.map((addOn) => normalizeAddOnPayload(addOn));
925
926
  }
926
927
  return normalized;
927
928
  }
@@ -994,10 +995,47 @@ function normalizeCatalogueResult(payload) {
994
995
  pagination: normalizePagination(payload.pagination)
995
996
  };
996
997
  }
998
+ function normalizeCatalogueSnapshot(payload) {
999
+ if (Array.isArray(payload)) {
1000
+ return {
1001
+ categories: [],
1002
+ products: payload.map((product) => normalizeCatalogueProductPayload(product)),
1003
+ add_ons: [],
1004
+ is_complete: true
1005
+ };
1006
+ }
1007
+ if (!isRecord(payload)) {
1008
+ return {
1009
+ categories: [],
1010
+ products: [],
1011
+ add_ons: [],
1012
+ is_complete: true
1013
+ };
1014
+ }
1015
+ const rawProducts = Array.isArray(payload.products) ? payload.products : Array.isArray(payload.items) ? payload.items : [];
1016
+ const rawCategories = Array.isArray(payload.categories) ? payload.categories : [];
1017
+ const rawAddOns = Array.isArray(payload.add_ons) ? payload.add_ons : [];
1018
+ return {
1019
+ categories: rawCategories,
1020
+ products: rawProducts.map((product) => normalizeCatalogueProductPayload(product)),
1021
+ add_ons: rawAddOns.map((addOn) => normalizeAddOnPayload(addOn)),
1022
+ is_complete: typeof payload.is_complete === "boolean" ? payload.is_complete : true,
1023
+ total_available: toFiniteNumber(payload.total_available),
1024
+ pagination: normalizePagination(payload.pagination)
1025
+ };
1026
+ }
997
1027
  var CatalogueQueries = class {
998
1028
  constructor(client) {
999
1029
  this.client = client;
1000
1030
  }
1031
+ async getCatalogue() {
1032
+ const result = await safeWithFallback(
1033
+ () => this.client.get("/api/v1/catalogue"),
1034
+ () => this.client.query("catalogue")
1035
+ );
1036
+ if (!result.ok) return result;
1037
+ return ok(normalizeCatalogueSnapshot(result.value));
1038
+ }
1001
1039
  async getProducts(options) {
1002
1040
  let query = "products";
1003
1041
  const filters = [];
@@ -2361,12 +2399,16 @@ var CheckoutService = class {
2361
2399
  constructor(client) {
2362
2400
  this.client = client;
2363
2401
  }
2402
+ orderTokenParam(orderId) {
2403
+ const token = this.client.getOrderToken(orderId);
2404
+ return token ? `?token=${encodeURIComponent(token)}` : "";
2405
+ }
2364
2406
  async process(data) {
2365
2407
  const checkoutData = {
2366
2408
  ...data,
2367
2409
  idempotency_key: data.idempotency_key || generateIdempotencyKey()
2368
2410
  };
2369
- return safeWithFallback3(
2411
+ const result = await safeWithFallback3(
2370
2412
  () => this.client.post("/api/v1/checkout", {
2371
2413
  checkout_data: checkoutData
2372
2414
  }),
@@ -2374,6 +2416,10 @@ var CheckoutService = class {
2374
2416
  checkout_data: checkoutData
2375
2417
  })
2376
2418
  );
2419
+ if (result.ok && result.value.bill_token) {
2420
+ this.client.setOrderToken(result.value.order_id, result.value.bill_token);
2421
+ }
2422
+ return result;
2377
2423
  }
2378
2424
  async initializePayment(orderId, method) {
2379
2425
  return safe3(
@@ -2391,15 +2437,17 @@ var CheckoutService = class {
2391
2437
  }
2392
2438
  async pollPaymentStatus(orderId) {
2393
2439
  const encodedId = encodeURIComponent(orderId);
2440
+ const tokenParam = this.orderTokenParam(orderId);
2394
2441
  return safeWithFallback3(
2395
- () => this.client.get(`/api/v1/orders/${encodedId}/payment-status`),
2442
+ () => this.client.get(`/api/v1/orders/${encodedId}/payment-status${tokenParam}`),
2396
2443
  () => this.client.call(PAYMENT_MUTATION.CHECK_STATUS, orderId)
2397
2444
  );
2398
2445
  }
2399
2446
  async updateOrderCustomer(orderId, customer) {
2400
2447
  const encodedId = encodeURIComponent(orderId);
2448
+ const tokenParam = this.orderTokenParam(orderId);
2401
2449
  return safeWithFallback3(
2402
- () => this.client.post(`/api/v1/orders/${encodedId}/customer`, customer),
2450
+ () => this.client.post(`/api/v1/orders/${encodedId}/customer${tokenParam}`, customer),
2403
2451
  () => this.client.call(ORDER_MUTATION.UPDATE_CUSTOMER, {
2404
2452
  order_id: orderId,
2405
2453
  ...customer
@@ -2542,6 +2590,10 @@ var OrderQueries = class {
2542
2590
  constructor(client) {
2543
2591
  this.client = client;
2544
2592
  }
2593
+ orderTokenParam(orderId) {
2594
+ const token = this.client.getOrderToken(orderId);
2595
+ return token ? `?token=${encodeURIComponent(token)}` : "";
2596
+ }
2545
2597
  async list(options) {
2546
2598
  let query = "orders";
2547
2599
  if (options?.status) {
@@ -2566,8 +2618,9 @@ var OrderQueries = class {
2566
2618
  }
2567
2619
  async get(orderId) {
2568
2620
  const encodedId = encodeURIComponent(orderId);
2621
+ const tokenParam = this.orderTokenParam(orderId);
2569
2622
  return safeWithFallback4(
2570
- () => this.client.get(`/api/v1/orders/${encodedId}`),
2623
+ () => this.client.get(`/api/v1/orders/${encodedId}${tokenParam}`),
2571
2624
  () => this.client.query(`orders.${orderId}`)
2572
2625
  );
2573
2626
  }
@@ -2579,8 +2632,9 @@ var OrderQueries = class {
2579
2632
  }
2580
2633
  async cancel(orderId, reason) {
2581
2634
  const encodedId = encodeURIComponent(orderId);
2635
+ const tokenParam = this.orderTokenParam(orderId);
2582
2636
  return safeWithFallback4(
2583
- () => this.client.post(`/api/v1/orders/${encodedId}/cancel`, {
2637
+ () => this.client.post(`/api/v1/orders/${encodedId}/cancel${tokenParam}`, {
2584
2638
  reason
2585
2639
  }),
2586
2640
  () => this.client.call("order.cancelOrder", {
@@ -3796,6 +3850,7 @@ function createElements(client, businessId, options) {
3796
3850
 
3797
3851
  // src/client.ts
3798
3852
  var ACCESS_TOKEN_STORAGE_KEY = "cimplify_access_token";
3853
+ var ORDER_TOKEN_PREFIX = "cimplify_ot_";
3799
3854
  var DEFAULT_TIMEOUT_MS = 3e4;
3800
3855
  var DEFAULT_MAX_RETRIES = 3;
3801
3856
  var DEFAULT_RETRY_DELAY_MS = 1e3;
@@ -3937,7 +3992,29 @@ var CimplifyClient = class {
3937
3992
  source: "clear"
3938
3993
  });
3939
3994
  }
3940
- /** Set the active location/branch for all subsequent requests */
3995
+ setOrderToken(orderId, token) {
3996
+ if (typeof window !== "undefined" && window.localStorage) {
3997
+ localStorage.setItem(`${ORDER_TOKEN_PREFIX}${orderId}`, token);
3998
+ }
3999
+ }
4000
+ getOrderToken(orderId) {
4001
+ if (typeof window !== "undefined" && window.localStorage) {
4002
+ return localStorage.getItem(`${ORDER_TOKEN_PREFIX}${orderId}`);
4003
+ }
4004
+ return null;
4005
+ }
4006
+ clearOrderTokens() {
4007
+ if (typeof window !== "undefined" && window.localStorage) {
4008
+ const keysToRemove = [];
4009
+ for (let i = 0; i < localStorage.length; i++) {
4010
+ const key = localStorage.key(i);
4011
+ if (key?.startsWith(ORDER_TOKEN_PREFIX)) {
4012
+ keysToRemove.push(key);
4013
+ }
4014
+ }
4015
+ keysToRemove.forEach((k) => localStorage.removeItem(k));
4016
+ }
4017
+ }
3941
4018
  setLocationId(locationId) {
3942
4019
  if (locationId) {
3943
4020
  this.context.location_id = locationId;
package/dist/react.mjs CHANGED
@@ -883,6 +883,22 @@ function readFinalPrice(value) {
883
883
  }
884
884
  return void 0;
885
885
  }
886
+ function normalizeAddOnPayload(addOn) {
887
+ if (!isRecord(addOn)) return addOn;
888
+ const normalizedAddOn = { ...addOn };
889
+ const options = normalizedAddOn["options"];
890
+ if (!Array.isArray(options)) return normalizedAddOn;
891
+ normalizedAddOn["options"] = options.map((option) => {
892
+ if (!isRecord(option)) return option;
893
+ const normalizedOption = { ...option };
894
+ const optionPrice = normalizedOption["default_price"];
895
+ if (optionPrice === void 0 || optionPrice === null || optionPrice === "") {
896
+ normalizedOption["default_price"] = readFinalPrice(normalizedOption["default_price_info"]) || readFinalPrice(normalizedOption["price_info"]) || "0";
897
+ }
898
+ return normalizedOption;
899
+ });
900
+ return normalizedAddOn;
901
+ }
886
902
  function normalizeCatalogueProductPayload(product) {
887
903
  const normalized = { ...product };
888
904
  const defaultPrice = normalized["default_price"];
@@ -904,22 +920,7 @@ function normalizeCatalogueProductPayload(product) {
904
920
  }
905
921
  const addOns = normalized["add_ons"];
906
922
  if (Array.isArray(addOns)) {
907
- normalized["add_ons"] = addOns.map((addOn) => {
908
- if (!isRecord(addOn)) return addOn;
909
- const normalizedAddOn = { ...addOn };
910
- const options = normalizedAddOn["options"];
911
- if (!Array.isArray(options)) return normalizedAddOn;
912
- normalizedAddOn["options"] = options.map((option) => {
913
- if (!isRecord(option)) return option;
914
- const normalizedOption = { ...option };
915
- const optionPrice = normalizedOption["default_price"];
916
- if (optionPrice === void 0 || optionPrice === null || optionPrice === "") {
917
- normalizedOption["default_price"] = readFinalPrice(normalizedOption["default_price_info"]) || readFinalPrice(normalizedOption["price_info"]) || "0";
918
- }
919
- return normalizedOption;
920
- });
921
- return normalizedAddOn;
922
- });
923
+ normalized["add_ons"] = addOns.map((addOn) => normalizeAddOnPayload(addOn));
923
924
  }
924
925
  return normalized;
925
926
  }
@@ -992,10 +993,47 @@ function normalizeCatalogueResult(payload) {
992
993
  pagination: normalizePagination(payload.pagination)
993
994
  };
994
995
  }
996
+ function normalizeCatalogueSnapshot(payload) {
997
+ if (Array.isArray(payload)) {
998
+ return {
999
+ categories: [],
1000
+ products: payload.map((product) => normalizeCatalogueProductPayload(product)),
1001
+ add_ons: [],
1002
+ is_complete: true
1003
+ };
1004
+ }
1005
+ if (!isRecord(payload)) {
1006
+ return {
1007
+ categories: [],
1008
+ products: [],
1009
+ add_ons: [],
1010
+ is_complete: true
1011
+ };
1012
+ }
1013
+ const rawProducts = Array.isArray(payload.products) ? payload.products : Array.isArray(payload.items) ? payload.items : [];
1014
+ const rawCategories = Array.isArray(payload.categories) ? payload.categories : [];
1015
+ const rawAddOns = Array.isArray(payload.add_ons) ? payload.add_ons : [];
1016
+ return {
1017
+ categories: rawCategories,
1018
+ products: rawProducts.map((product) => normalizeCatalogueProductPayload(product)),
1019
+ add_ons: rawAddOns.map((addOn) => normalizeAddOnPayload(addOn)),
1020
+ is_complete: typeof payload.is_complete === "boolean" ? payload.is_complete : true,
1021
+ total_available: toFiniteNumber(payload.total_available),
1022
+ pagination: normalizePagination(payload.pagination)
1023
+ };
1024
+ }
995
1025
  var CatalogueQueries = class {
996
1026
  constructor(client) {
997
1027
  this.client = client;
998
1028
  }
1029
+ async getCatalogue() {
1030
+ const result = await safeWithFallback(
1031
+ () => this.client.get("/api/v1/catalogue"),
1032
+ () => this.client.query("catalogue")
1033
+ );
1034
+ if (!result.ok) return result;
1035
+ return ok(normalizeCatalogueSnapshot(result.value));
1036
+ }
999
1037
  async getProducts(options) {
1000
1038
  let query = "products";
1001
1039
  const filters = [];
@@ -2359,12 +2397,16 @@ var CheckoutService = class {
2359
2397
  constructor(client) {
2360
2398
  this.client = client;
2361
2399
  }
2400
+ orderTokenParam(orderId) {
2401
+ const token = this.client.getOrderToken(orderId);
2402
+ return token ? `?token=${encodeURIComponent(token)}` : "";
2403
+ }
2362
2404
  async process(data) {
2363
2405
  const checkoutData = {
2364
2406
  ...data,
2365
2407
  idempotency_key: data.idempotency_key || generateIdempotencyKey()
2366
2408
  };
2367
- return safeWithFallback3(
2409
+ const result = await safeWithFallback3(
2368
2410
  () => this.client.post("/api/v1/checkout", {
2369
2411
  checkout_data: checkoutData
2370
2412
  }),
@@ -2372,6 +2414,10 @@ var CheckoutService = class {
2372
2414
  checkout_data: checkoutData
2373
2415
  })
2374
2416
  );
2417
+ if (result.ok && result.value.bill_token) {
2418
+ this.client.setOrderToken(result.value.order_id, result.value.bill_token);
2419
+ }
2420
+ return result;
2375
2421
  }
2376
2422
  async initializePayment(orderId, method) {
2377
2423
  return safe3(
@@ -2389,15 +2435,17 @@ var CheckoutService = class {
2389
2435
  }
2390
2436
  async pollPaymentStatus(orderId) {
2391
2437
  const encodedId = encodeURIComponent(orderId);
2438
+ const tokenParam = this.orderTokenParam(orderId);
2392
2439
  return safeWithFallback3(
2393
- () => this.client.get(`/api/v1/orders/${encodedId}/payment-status`),
2440
+ () => this.client.get(`/api/v1/orders/${encodedId}/payment-status${tokenParam}`),
2394
2441
  () => this.client.call(PAYMENT_MUTATION.CHECK_STATUS, orderId)
2395
2442
  );
2396
2443
  }
2397
2444
  async updateOrderCustomer(orderId, customer) {
2398
2445
  const encodedId = encodeURIComponent(orderId);
2446
+ const tokenParam = this.orderTokenParam(orderId);
2399
2447
  return safeWithFallback3(
2400
- () => this.client.post(`/api/v1/orders/${encodedId}/customer`, customer),
2448
+ () => this.client.post(`/api/v1/orders/${encodedId}/customer${tokenParam}`, customer),
2401
2449
  () => this.client.call(ORDER_MUTATION.UPDATE_CUSTOMER, {
2402
2450
  order_id: orderId,
2403
2451
  ...customer
@@ -2540,6 +2588,10 @@ var OrderQueries = class {
2540
2588
  constructor(client) {
2541
2589
  this.client = client;
2542
2590
  }
2591
+ orderTokenParam(orderId) {
2592
+ const token = this.client.getOrderToken(orderId);
2593
+ return token ? `?token=${encodeURIComponent(token)}` : "";
2594
+ }
2543
2595
  async list(options) {
2544
2596
  let query = "orders";
2545
2597
  if (options?.status) {
@@ -2564,8 +2616,9 @@ var OrderQueries = class {
2564
2616
  }
2565
2617
  async get(orderId) {
2566
2618
  const encodedId = encodeURIComponent(orderId);
2619
+ const tokenParam = this.orderTokenParam(orderId);
2567
2620
  return safeWithFallback4(
2568
- () => this.client.get(`/api/v1/orders/${encodedId}`),
2621
+ () => this.client.get(`/api/v1/orders/${encodedId}${tokenParam}`),
2569
2622
  () => this.client.query(`orders.${orderId}`)
2570
2623
  );
2571
2624
  }
@@ -2577,8 +2630,9 @@ var OrderQueries = class {
2577
2630
  }
2578
2631
  async cancel(orderId, reason) {
2579
2632
  const encodedId = encodeURIComponent(orderId);
2633
+ const tokenParam = this.orderTokenParam(orderId);
2580
2634
  return safeWithFallback4(
2581
- () => this.client.post(`/api/v1/orders/${encodedId}/cancel`, {
2635
+ () => this.client.post(`/api/v1/orders/${encodedId}/cancel${tokenParam}`, {
2582
2636
  reason
2583
2637
  }),
2584
2638
  () => this.client.call("order.cancelOrder", {
@@ -3794,6 +3848,7 @@ function createElements(client, businessId, options) {
3794
3848
 
3795
3849
  // src/client.ts
3796
3850
  var ACCESS_TOKEN_STORAGE_KEY = "cimplify_access_token";
3851
+ var ORDER_TOKEN_PREFIX = "cimplify_ot_";
3797
3852
  var DEFAULT_TIMEOUT_MS = 3e4;
3798
3853
  var DEFAULT_MAX_RETRIES = 3;
3799
3854
  var DEFAULT_RETRY_DELAY_MS = 1e3;
@@ -3935,7 +3990,29 @@ var CimplifyClient = class {
3935
3990
  source: "clear"
3936
3991
  });
3937
3992
  }
3938
- /** Set the active location/branch for all subsequent requests */
3993
+ setOrderToken(orderId, token) {
3994
+ if (typeof window !== "undefined" && window.localStorage) {
3995
+ localStorage.setItem(`${ORDER_TOKEN_PREFIX}${orderId}`, token);
3996
+ }
3997
+ }
3998
+ getOrderToken(orderId) {
3999
+ if (typeof window !== "undefined" && window.localStorage) {
4000
+ return localStorage.getItem(`${ORDER_TOKEN_PREFIX}${orderId}`);
4001
+ }
4002
+ return null;
4003
+ }
4004
+ clearOrderTokens() {
4005
+ if (typeof window !== "undefined" && window.localStorage) {
4006
+ const keysToRemove = [];
4007
+ for (let i = 0; i < localStorage.length; i++) {
4008
+ const key = localStorage.key(i);
4009
+ if (key?.startsWith(ORDER_TOKEN_PREFIX)) {
4010
+ keysToRemove.push(key);
4011
+ }
4012
+ }
4013
+ keysToRemove.forEach((k) => localStorage.removeItem(k));
4014
+ }
4015
+ }
3939
4016
  setLocationId(locationId) {
3940
4017
  if (locationId) {
3941
4018
  this.context.location_id = locationId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cimplify/sdk",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "description": "Cimplify Commerce SDK for storefronts",
5
5
  "keywords": [
6
6
  "cimplify",