@betterstore/react 0.2.15 → 0.2.16

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @betterstore/sdk
2
2
 
3
+ ## 0.2.16
4
+
5
+ ### Patch Changes
6
+
7
+ - better currency formatting
8
+
3
9
  ## 0.2.15
4
10
 
5
11
  ### Patch Changes
package/dist/index.cjs.js CHANGED
@@ -20772,11 +20772,44 @@ var Client = class {
20772
20772
  }
20773
20773
  };
20774
20774
  var client_default = Client;
20775
+ var Helpers = class {
20776
+ constructor(proxy) {
20777
+ this.proxy = proxy;
20778
+ }
20779
+ formatPrice(priceInCents, currency, exchangeRate) {
20780
+ const amount = priceInCents / 100 * (exchangeRate != null ? exchangeRate : 1);
20781
+ const isWhole = amount % 1 === 0;
20782
+ const formattedPrice = new Intl.NumberFormat(void 0, {
20783
+ style: "currency",
20784
+ currency,
20785
+ minimumFractionDigits: isWhole ? 0 : 2,
20786
+ maximumFractionDigits: isWhole ? 0 : 2
20787
+ }).format(amount);
20788
+ return formattedPrice;
20789
+ }
20790
+ getExchangeRate(baseCurrency, targetCurrency) {
20791
+ return __async(this, null, function* () {
20792
+ const { data } = yield axios.get(
20793
+ `https://api.exchangerate-api.com/v4/latest/${baseCurrency}`
20794
+ );
20795
+ const rate = data.rates[targetCurrency];
20796
+ if (!rate) {
20797
+ throw new Error("Could not get exchange rate for target currency");
20798
+ }
20799
+ return rate;
20800
+ });
20801
+ }
20802
+ };
20803
+ var helpers_default = Helpers;
20775
20804
  function createStoreClient(config) {
20776
- return new client_default(config.proxy);
20805
+ return new client_default(config == null ? void 0 : config.proxy);
20806
+ }
20807
+ function createStoreHelpers(config) {
20808
+ return new helpers_default(void 0 );
20777
20809
  }
20778
20810
 
20779
20811
  const storeClient = createStoreClient({ proxy: "/api/betterstore" });
20812
+ const storeHelpers = createStoreHelpers();
20780
20813
 
20781
20814
  function Appearance({ appearance, }) {
20782
20815
  React.useEffect(() => {
@@ -31953,9 +31986,6 @@ function CheckoutSummary({ lineItems, shipping, tax, currency, cancelUrl, exchan
31953
31986
  return acc + ((_b = (_a = item.product) === null || _a === void 0 ? void 0 : _a.priceInCents) !== null && _b !== void 0 ? _b : 0) * item.quantity;
31954
31987
  }, 0);
31955
31988
  const total = subtotal + (tax !== null && tax !== void 0 ? tax : 0) + (shipping !== null && shipping !== void 0 ? shipping : 0);
31956
- const formatPrice = (cents) => {
31957
- return `${((cents / 100) * exchangeRate).toFixed(2)} ${currency}`;
31958
- };
31959
31989
  return (React.createElement("div", { className: "grid gap-5" },
31960
31990
  React.createElement("div", { className: "flex justify-between items-center" },
31961
31991
  React.createElement("h2", null, t("CheckoutEmbed.Summary.title")),
@@ -31965,18 +31995,18 @@ function CheckoutSummary({ lineItems, shipping, tax, currency, cancelUrl, exchan
31965
31995
  React.createElement("div", { className: "grid gap-3" },
31966
31996
  React.createElement("div", { className: "flex justify-between" },
31967
31997
  React.createElement("p", null, t("CheckoutEmbed.Summary.subtotal")),
31968
- React.createElement("p", null, formatPrice(subtotal))),
31998
+ React.createElement("p", null, storeHelpers.formatPrice(subtotal, currency, exchangeRate))),
31969
31999
  React.createElement("div", { className: "flex justify-between" },
31970
32000
  React.createElement("p", null, t("CheckoutEmbed.Summary.shipping")),
31971
32001
  React.createElement("p", null, !!shipping
31972
- ? formatPrice(shipping)
32002
+ ? storeHelpers.formatPrice(shipping, currency, exchangeRate)
31973
32003
  : t("CheckoutEmbed.Summary.calculatedAtNextStep"))),
31974
32004
  !!tax && (React.createElement("div", { className: "flex justify-between" },
31975
32005
  React.createElement("p", null, t("CheckoutEmbed.Summary.tax")),
31976
- React.createElement("p", null, formatPrice(tax)))),
32006
+ React.createElement("p", null, storeHelpers.formatPrice(tax, currency, exchangeRate)))),
31977
32007
  React.createElement("div", { className: "flex font-bold justify-between items-center" },
31978
32008
  React.createElement("p", null, t("CheckoutEmbed.Summary.total")),
31979
- React.createElement("p", null, formatPrice(total)))),
32009
+ React.createElement("p", null, storeHelpers.formatPrice(total, currency, exchangeRate)))),
31980
32010
  React.createElement("hr", null),
31981
32011
  lineItems.map((item, index) => {
31982
32012
  var _a, _b, _c, _d, _e;
@@ -31988,7 +32018,7 @@ function CheckoutSummary({ lineItems, shipping, tax, currency, cancelUrl, exchan
31988
32018
  React.createElement("h3", { className: "text-lg font-medium" }, (_c = item.product) === null || _c === void 0 ? void 0 : _c.title),
31989
32019
  React.createElement("p", { className: "text-muted-foreground text-sm" }, item.variantOptions.map((option) => option.name).join(" / "))),
31990
32020
  React.createElement("div", { className: "text-right" },
31991
- React.createElement("p", { className: "text-lg font-medium" }, formatPrice((_e = (_d = item.product) === null || _d === void 0 ? void 0 : _d.priceInCents) !== null && _e !== void 0 ? _e : 0)))));
32021
+ React.createElement("p", { className: "text-lg font-medium" }, storeHelpers.formatPrice((_e = (_d = item.product) === null || _d === void 0 ? void 0 : _d.priceInCents) !== null && _e !== void 0 ? _e : 0, currency, exchangeRate)))));
31992
32022
  })));
31993
32023
  }
31994
32024
 
package/dist/index.mjs CHANGED
@@ -20749,11 +20749,44 @@ var Client = class {
20749
20749
  }
20750
20750
  };
20751
20751
  var client_default = Client;
20752
+ var Helpers = class {
20753
+ constructor(proxy) {
20754
+ this.proxy = proxy;
20755
+ }
20756
+ formatPrice(priceInCents, currency, exchangeRate) {
20757
+ const amount = priceInCents / 100 * (exchangeRate != null ? exchangeRate : 1);
20758
+ const isWhole = amount % 1 === 0;
20759
+ const formattedPrice = new Intl.NumberFormat(void 0, {
20760
+ style: "currency",
20761
+ currency,
20762
+ minimumFractionDigits: isWhole ? 0 : 2,
20763
+ maximumFractionDigits: isWhole ? 0 : 2
20764
+ }).format(amount);
20765
+ return formattedPrice;
20766
+ }
20767
+ getExchangeRate(baseCurrency, targetCurrency) {
20768
+ return __async(this, null, function* () {
20769
+ const { data } = yield axios.get(
20770
+ `https://api.exchangerate-api.com/v4/latest/${baseCurrency}`
20771
+ );
20772
+ const rate = data.rates[targetCurrency];
20773
+ if (!rate) {
20774
+ throw new Error("Could not get exchange rate for target currency");
20775
+ }
20776
+ return rate;
20777
+ });
20778
+ }
20779
+ };
20780
+ var helpers_default = Helpers;
20752
20781
  function createStoreClient(config) {
20753
- return new client_default(config.proxy);
20782
+ return new client_default(config == null ? void 0 : config.proxy);
20783
+ }
20784
+ function createStoreHelpers(config) {
20785
+ return new helpers_default(void 0 );
20754
20786
  }
20755
20787
 
20756
20788
  const storeClient = createStoreClient({ proxy: "/api/betterstore" });
20789
+ const storeHelpers = createStoreHelpers();
20757
20790
 
20758
20791
  function Appearance({ appearance, }) {
20759
20792
  useEffect(() => {
@@ -31930,9 +31963,6 @@ function CheckoutSummary({ lineItems, shipping, tax, currency, cancelUrl, exchan
31930
31963
  return acc + ((_b = (_a = item.product) === null || _a === void 0 ? void 0 : _a.priceInCents) !== null && _b !== void 0 ? _b : 0) * item.quantity;
31931
31964
  }, 0);
31932
31965
  const total = subtotal + (tax !== null && tax !== void 0 ? tax : 0) + (shipping !== null && shipping !== void 0 ? shipping : 0);
31933
- const formatPrice = (cents) => {
31934
- return `${((cents / 100) * exchangeRate).toFixed(2)} ${currency}`;
31935
- };
31936
31966
  return (React__default.createElement("div", { className: "grid gap-5" },
31937
31967
  React__default.createElement("div", { className: "flex justify-between items-center" },
31938
31968
  React__default.createElement("h2", null, t("CheckoutEmbed.Summary.title")),
@@ -31942,18 +31972,18 @@ function CheckoutSummary({ lineItems, shipping, tax, currency, cancelUrl, exchan
31942
31972
  React__default.createElement("div", { className: "grid gap-3" },
31943
31973
  React__default.createElement("div", { className: "flex justify-between" },
31944
31974
  React__default.createElement("p", null, t("CheckoutEmbed.Summary.subtotal")),
31945
- React__default.createElement("p", null, formatPrice(subtotal))),
31975
+ React__default.createElement("p", null, storeHelpers.formatPrice(subtotal, currency, exchangeRate))),
31946
31976
  React__default.createElement("div", { className: "flex justify-between" },
31947
31977
  React__default.createElement("p", null, t("CheckoutEmbed.Summary.shipping")),
31948
31978
  React__default.createElement("p", null, !!shipping
31949
- ? formatPrice(shipping)
31979
+ ? storeHelpers.formatPrice(shipping, currency, exchangeRate)
31950
31980
  : t("CheckoutEmbed.Summary.calculatedAtNextStep"))),
31951
31981
  !!tax && (React__default.createElement("div", { className: "flex justify-between" },
31952
31982
  React__default.createElement("p", null, t("CheckoutEmbed.Summary.tax")),
31953
- React__default.createElement("p", null, formatPrice(tax)))),
31983
+ React__default.createElement("p", null, storeHelpers.formatPrice(tax, currency, exchangeRate)))),
31954
31984
  React__default.createElement("div", { className: "flex font-bold justify-between items-center" },
31955
31985
  React__default.createElement("p", null, t("CheckoutEmbed.Summary.total")),
31956
- React__default.createElement("p", null, formatPrice(total)))),
31986
+ React__default.createElement("p", null, storeHelpers.formatPrice(total, currency, exchangeRate)))),
31957
31987
  React__default.createElement("hr", null),
31958
31988
  lineItems.map((item, index) => {
31959
31989
  var _a, _b, _c, _d, _e;
@@ -31965,7 +31995,7 @@ function CheckoutSummary({ lineItems, shipping, tax, currency, cancelUrl, exchan
31965
31995
  React__default.createElement("h3", { className: "text-lg font-medium" }, (_c = item.product) === null || _c === void 0 ? void 0 : _c.title),
31966
31996
  React__default.createElement("p", { className: "text-muted-foreground text-sm" }, item.variantOptions.map((option) => option.name).join(" / "))),
31967
31997
  React__default.createElement("div", { className: "text-right" },
31968
- React__default.createElement("p", { className: "text-lg font-medium" }, formatPrice((_e = (_d = item.product) === null || _d === void 0 ? void 0 : _d.priceInCents) !== null && _e !== void 0 ? _e : 0)))));
31998
+ React__default.createElement("p", { className: "text-lg font-medium" }, storeHelpers.formatPrice((_e = (_d = item.product) === null || _d === void 0 ? void 0 : _d.priceInCents) !== null && _e !== void 0 ? _e : 0, currency, exchangeRate)))));
31969
31999
  })));
31970
32000
  }
31971
32001
 
@@ -8,3 +8,8 @@ export declare const storeClient: {
8
8
  retrieveCustomer(clientSecret: string, idOrEmail: string): Promise<import("@betterstore/sdk").Customer>;
9
9
  updateCustomer(clientSecret: string, customerId: string, params: import("@betterstore/sdk").CustomerUpdateParams): Promise<import("@betterstore/sdk").Customer>;
10
10
  };
11
+ export declare const storeHelpers: {
12
+ proxy?: string;
13
+ formatPrice(priceInCents: number, currency: string, exchangeRate?: number): string;
14
+ getExchangeRate(baseCurrency: string, targetCurrency: string): Promise<number>;
15
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@betterstore/react",
3
- "version": "0.2.15",
3
+ "version": "0.2.16",
4
4
  "description": "E-commerce for Developers",
5
5
  "private": false,
6
6
  "publishConfig": {
@@ -19,7 +19,7 @@
19
19
  "author": "Better Store",
20
20
  "license": "MIT",
21
21
  "devDependencies": {
22
- "@betterstore/sdk": "^0.3.16",
22
+ "@betterstore/sdk": "^0.3.21",
23
23
  "@changesets/cli": "^2.28.1",
24
24
  "@rollup/plugin-commonjs": "^28.0.3",
25
25
  "@rollup/plugin-json": "^6.1.0",