@betterstore/sdk 0.6.2 → 0.6.3

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.d.mts CHANGED
@@ -179,6 +179,19 @@ declare class Products {
179
179
  retrieve<T extends RetrieveProductParams>(params: T): Promise<FormatResponseForSDK<RetrieveProductResponse<T>> | null>;
180
180
  }
181
181
 
182
+ type Discount = NonNullable<RetrieveDiscountResponse["discount"]>;
183
+ declare function findAutomaticProductDiscount({ productId, priceInCents, discounts, }: {
184
+ productId: string;
185
+ priceInCents: number;
186
+ discounts: Discount[];
187
+ }): {
188
+ finalPrice: number;
189
+ discountAmount: number | undefined;
190
+ originalPrice: number;
191
+ isFree: boolean;
192
+ isDiscounted: boolean;
193
+ };
194
+
182
195
  declare function createBetterStore(config: {
183
196
  apiKey: string;
184
197
  proxy?: string;
@@ -194,4 +207,4 @@ declare function createStoreClient(config?: {
194
207
  proxy?: string;
195
208
  }): Client;
196
209
 
197
- export { createStoreClient, createBetterStore as default };
210
+ export { type Discount, createStoreClient, createBetterStore as default, findAutomaticProductDiscount };
package/dist/index.d.ts CHANGED
@@ -179,6 +179,19 @@ declare class Products {
179
179
  retrieve<T extends RetrieveProductParams>(params: T): Promise<FormatResponseForSDK<RetrieveProductResponse<T>> | null>;
180
180
  }
181
181
 
182
+ type Discount = NonNullable<RetrieveDiscountResponse["discount"]>;
183
+ declare function findAutomaticProductDiscount({ productId, priceInCents, discounts, }: {
184
+ productId: string;
185
+ priceInCents: number;
186
+ discounts: Discount[];
187
+ }): {
188
+ finalPrice: number;
189
+ discountAmount: number | undefined;
190
+ originalPrice: number;
191
+ isFree: boolean;
192
+ isDiscounted: boolean;
193
+ };
194
+
182
195
  declare function createBetterStore(config: {
183
196
  apiKey: string;
184
197
  proxy?: string;
@@ -194,4 +207,4 @@ declare function createStoreClient(config?: {
194
207
  proxy?: string;
195
208
  }): Client;
196
209
 
197
- export { createStoreClient, createBetterStore as default };
210
+ export { type Discount, createStoreClient, createBetterStore as default, findAutomaticProductDiscount };
package/dist/index.js CHANGED
@@ -32,12 +32,13 @@ var index_exports = {};
32
32
  __export(index_exports, {
33
33
  createStoreClient: () => createStoreClient,
34
34
  default: () => createBetterStore,
35
+ findAutomaticProductDiscount: () => findAutomaticProductDiscount,
35
36
  formatCurrency: () => import_bridge.formatCurrency,
36
37
  formatPrice: () => import_bridge.formatPrice
37
38
  });
38
39
  module.exports = __toCommonJS(index_exports);
39
40
 
40
- // src/utils/axios.ts
41
+ // src/_utils/axios.ts
41
42
  var import_axios = __toESM(require("axios"));
42
43
  var API_BASE_URL = "https://v1.betterstore.io";
43
44
  var createApiClient = (apiKey, proxy) => {
@@ -580,9 +581,53 @@ var Products = class {
580
581
  };
581
582
  var products_default = Products;
582
583
 
583
- // src/types.ts
584
+ // src/types-and-methods.ts
584
585
  var import_bridge = require("@betterstore/bridge");
585
586
 
587
+ // src/methods/cart-discounts.ts
588
+ function getHighestDiscount(productId, priceInCents, discounts) {
589
+ const applicableDiscounts = discounts.filter(
590
+ (discount) => discount.allowedProductIDs.includes(productId)
591
+ );
592
+ const bestDiscount = applicableDiscounts.length > 1 ? applicableDiscounts.reduce((bestSoFar, currentDiscount) => {
593
+ let currentDiscountValueInCents = currentDiscount.value;
594
+ if (currentDiscount.valueType === "PERCENTAGE") {
595
+ currentDiscountValueInCents = currentDiscount.value / 100 * priceInCents;
596
+ } else if (currentDiscount.valueType === "FREE") {
597
+ currentDiscountValueInCents = priceInCents;
598
+ }
599
+ return (bestSoFar?.value ?? 0) > currentDiscountValueInCents ? bestSoFar : currentDiscount;
600
+ }, applicableDiscounts[0]) : applicableDiscounts[0];
601
+ return bestDiscount;
602
+ }
603
+ function calculateDiscountAmount(originalPrice, discount) {
604
+ let discountValueInCents = discount?.value;
605
+ const isFreeDiscount = discount?.valueType === "FREE";
606
+ if (discount?.valueType === "PERCENTAGE") {
607
+ discountValueInCents = discount.value / 100 * originalPrice;
608
+ } else if (discount?.valueType === "FREE") {
609
+ discountValueInCents = originalPrice;
610
+ }
611
+ const finalPrice = discountValueInCents ? Math.max(originalPrice - discountValueInCents, 0) : originalPrice;
612
+ const isDiscounted = discountValueInCents === 0;
613
+ return {
614
+ finalPrice,
615
+ discountAmount: discountValueInCents,
616
+ originalPrice,
617
+ isFree: isFreeDiscount,
618
+ isDiscounted
619
+ };
620
+ }
621
+ function findAutomaticProductDiscount({
622
+ productId,
623
+ priceInCents,
624
+ discounts
625
+ }) {
626
+ const discount = getHighestDiscount(productId, priceInCents, discounts);
627
+ const result = calculateDiscountAmount(priceInCents, discount);
628
+ return result;
629
+ }
630
+
586
631
  // src/index.ts
587
632
  function createBetterStore(config) {
588
633
  if (!config.apiKey) {
@@ -603,6 +648,7 @@ function createStoreClient(config) {
603
648
  // Annotate the CommonJS export names for ESM import in node:
604
649
  0 && (module.exports = {
605
650
  createStoreClient,
651
+ findAutomaticProductDiscount,
606
652
  formatCurrency,
607
653
  formatPrice
608
654
  });
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- // src/utils/axios.ts
1
+ // src/_utils/axios.ts
2
2
  import axios from "axios";
3
3
  var API_BASE_URL = "https://v1.betterstore.io";
4
4
  var createApiClient = (apiKey, proxy) => {
@@ -541,9 +541,53 @@ var Products = class {
541
541
  };
542
542
  var products_default = Products;
543
543
 
544
- // src/types.ts
544
+ // src/types-and-methods.ts
545
545
  import { formatCurrency, formatPrice } from "@betterstore/bridge";
546
546
 
547
+ // src/methods/cart-discounts.ts
548
+ function getHighestDiscount(productId, priceInCents, discounts) {
549
+ const applicableDiscounts = discounts.filter(
550
+ (discount) => discount.allowedProductIDs.includes(productId)
551
+ );
552
+ const bestDiscount = applicableDiscounts.length > 1 ? applicableDiscounts.reduce((bestSoFar, currentDiscount) => {
553
+ let currentDiscountValueInCents = currentDiscount.value;
554
+ if (currentDiscount.valueType === "PERCENTAGE") {
555
+ currentDiscountValueInCents = currentDiscount.value / 100 * priceInCents;
556
+ } else if (currentDiscount.valueType === "FREE") {
557
+ currentDiscountValueInCents = priceInCents;
558
+ }
559
+ return (bestSoFar?.value ?? 0) > currentDiscountValueInCents ? bestSoFar : currentDiscount;
560
+ }, applicableDiscounts[0]) : applicableDiscounts[0];
561
+ return bestDiscount;
562
+ }
563
+ function calculateDiscountAmount(originalPrice, discount) {
564
+ let discountValueInCents = discount?.value;
565
+ const isFreeDiscount = discount?.valueType === "FREE";
566
+ if (discount?.valueType === "PERCENTAGE") {
567
+ discountValueInCents = discount.value / 100 * originalPrice;
568
+ } else if (discount?.valueType === "FREE") {
569
+ discountValueInCents = originalPrice;
570
+ }
571
+ const finalPrice = discountValueInCents ? Math.max(originalPrice - discountValueInCents, 0) : originalPrice;
572
+ const isDiscounted = discountValueInCents === 0;
573
+ return {
574
+ finalPrice,
575
+ discountAmount: discountValueInCents,
576
+ originalPrice,
577
+ isFree: isFreeDiscount,
578
+ isDiscounted
579
+ };
580
+ }
581
+ function findAutomaticProductDiscount({
582
+ productId,
583
+ priceInCents,
584
+ discounts
585
+ }) {
586
+ const discount = getHighestDiscount(productId, priceInCents, discounts);
587
+ const result = calculateDiscountAmount(priceInCents, discount);
588
+ return result;
589
+ }
590
+
547
591
  // src/index.ts
548
592
  function createBetterStore(config) {
549
593
  if (!config.apiKey) {
@@ -564,6 +608,7 @@ function createStoreClient(config) {
564
608
  export {
565
609
  createStoreClient,
566
610
  createBetterStore as default,
611
+ findAutomaticProductDiscount,
567
612
  formatCurrency,
568
613
  formatPrice
569
614
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@betterstore/sdk",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",