@betterstore/sdk 0.6.2 → 0.6.4

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
@@ -1,6 +1,7 @@
1
1
  import { OTPSignupParams, OTPSignupResponse, OTPLoginParams, OTPLoginResponse, OTPVerifyParams, OTPVerifyResponse, CustomerSession, CheckoutCreateParams, CheckoutSession, CheckoutUpdateParams, GetShippingRatesResponse, CustomerCreateParams, CustomerResponse, CustomerUpdateParams, AutosuggestAddressResult, LookupAddressResult, ListCollectionsParams, ListCollectionsResponse, RetrieveCollectionParams, RetrieveCollectionResponse, CustomerSubscriptionUpdateParams, CustomerSubscription, ListDiscountsParams, ListDiscountsResponse, RetrieveDiscountParams, RetrieveDiscountResponse, ListProductsParams, ListProductsResponse, RetrieveProductParams, RetrieveProductResponse } from '@betterstore/bridge';
2
2
  export { Address, AutosuggestAddressResult, CheckoutCreateParams, CheckoutSession, CheckoutUpdateParams, CustomShippingVendorRate, CustomerCreateParams, CustomerResponse, CustomerSession, CustomerSubscription, CustomerSubscriptionUpdateParams, CustomerUpdateParams, GetShippingRatesResponse, ListCollectionsParams, ListCollectionsResponse, ListDiscountsParams, ListDiscountsResponse, ListProductsParams, ListProductsResponse, LookupAddressResult, OTPLoginParams, OTPLoginResponse, OTPSignupParams, OTPSignupResponse, OTPVerifyParams, OTPVerifyResponse, ProductVariant, RetrieveCollectionParams, RetrieveCollectionResponse, RetrieveDiscountParams, RetrieveDiscountResponse, RetrieveProductParams, RetrieveProductResponse, formatCurrency, formatPrice } from '@betterstore/bridge';
3
3
  import axios from 'axios';
4
+ import * as _betterstore_bridge_generated_prisma_client from '@betterstore/bridge/generated/prisma/client';
4
5
 
5
6
  declare const createApiClient: (apiKey: string, proxy?: string) => axios.AxiosInstance;
6
7
 
@@ -132,12 +133,13 @@ type FormatResponseForSDK<T extends object> = keyof T extends [
132
133
  string,
133
134
  ...string[]
134
135
  ] ? T : T[keyof T];
136
+ type FormatParamsForSDK<GenericType, ConstraintType> = GenericType & Record<Exclude<keyof GenericType, keyof ConstraintType>, never>;
135
137
 
136
138
  declare class Collections {
137
139
  private apiClient;
138
140
  constructor(apiKey: string, proxy?: string);
139
- list<T extends ListCollectionsParams>(params?: T): Promise<FormatResponseForSDK<ListCollectionsResponse<T>>>;
140
- retrieve<T extends RetrieveCollectionParams>(params: T): Promise<FormatResponseForSDK<RetrieveCollectionResponse<T>> | null>;
141
+ list<T extends ListCollectionsParams>(params?: FormatParamsForSDK<T, ListCollectionsParams>): Promise<FormatResponseForSDK<ListCollectionsResponse<T>>>;
142
+ retrieve<T extends RetrieveCollectionParams>(params: FormatParamsForSDK<T, RetrieveCollectionParams>): Promise<FormatResponseForSDK<RetrieveCollectionResponse<T>> | null>;
141
143
  }
142
144
 
143
145
  declare class Customer {
@@ -168,17 +170,61 @@ declare class Customer {
168
170
  declare class Discounts {
169
171
  private apiClient;
170
172
  constructor(apiKey: string, proxy?: string);
171
- list<T extends ListDiscountsParams>(params?: T): Promise<FormatResponseForSDK<ListDiscountsResponse>>;
172
- retrieve<T extends RetrieveDiscountParams>(params: T): Promise<FormatResponseForSDK<RetrieveDiscountResponse> | null>;
173
+ list<T extends ListDiscountsParams>(params?: FormatParamsForSDK<T, ListDiscountsParams>): Promise<FormatResponseForSDK<ListDiscountsResponse>>;
174
+ retrieve<T extends RetrieveDiscountParams>(params: FormatParamsForSDK<T, RetrieveDiscountParams>): Promise<FormatResponseForSDK<RetrieveDiscountResponse> | null>;
173
175
  }
174
176
 
175
177
  declare class Products {
176
178
  private apiClient;
177
179
  constructor(apiKey: string, proxy?: string);
178
- list<T extends ListProductsParams>(params?: T): Promise<FormatResponseForSDK<ListProductsResponse<T>>>;
179
- retrieve<T extends RetrieveProductParams>(params: T): Promise<FormatResponseForSDK<RetrieveProductResponse<T>> | null>;
180
+ list<T extends ListProductsParams>(params?: FormatParamsForSDK<T, ListProductsParams>): Promise<FormatResponseForSDK<ListProductsResponse<T>>>;
181
+ retrieve<T extends RetrieveProductParams>(params: FormatParamsForSDK<T, RetrieveProductParams>): Promise<FormatResponseForSDK<RetrieveProductResponse<T>> | null>;
180
182
  }
181
183
 
184
+ type Discount = NonNullable<RetrieveDiscountResponse["discount"]>;
185
+ declare function findAutomaticProductDiscount({ productId, priceInCents, discounts, }: {
186
+ productId: string;
187
+ priceInCents: number;
188
+ discounts: Discount[];
189
+ }): {
190
+ discount: {
191
+ method: _betterstore_bridge_generated_prisma_client.$Enums.DiscountMethod;
192
+ testmode: boolean;
193
+ id: string;
194
+ createdAt: Date;
195
+ updatedAt: Date;
196
+ expiresAt: Date | null;
197
+ status: _betterstore_bridge_generated_prisma_client.$Enums.DiscountStatus;
198
+ code: string | null;
199
+ title: string | null;
200
+ type: _betterstore_bridge_generated_prisma_client.$Enums.DiscountType;
201
+ value: number;
202
+ valueType: _betterstore_bridge_generated_prisma_client.$Enums.DiscountValueType;
203
+ discountScope: _betterstore_bridge_generated_prisma_client.$Enums.ProductDiscountScope;
204
+ allowedProductIDs: string[];
205
+ allowedCollectionIDs: string[];
206
+ allowedCombinations: _betterstore_bridge_generated_prisma_client.$Enums.DiscountScope[];
207
+ minimumRequirementsType: _betterstore_bridge_generated_prisma_client.$Enums.MinimumRequirementsType;
208
+ minimumRequirementsValue: number | null;
209
+ requiredProductIDs: string[];
210
+ requiredCollectionIDs: string[];
211
+ minimumRequirementsScope: _betterstore_bridge_generated_prisma_client.$Enums.ProductDiscountScope;
212
+ maxUses: number | null;
213
+ maxUsesPerCustomer: number | null;
214
+ maxAllowedProductQuantity: number | null;
215
+ uses: number;
216
+ subscriptionDiscountDurationType: _betterstore_bridge_generated_prisma_client.$Enums.SubscriptionDiscountDurationType;
217
+ subscriptionDiscountDurationValue: number;
218
+ stripeDiscountId: string | null;
219
+ startsAt: Date;
220
+ } | undefined;
221
+ finalPrice: number;
222
+ discountAmount: number;
223
+ originalPrice: number;
224
+ isFree: boolean;
225
+ isDiscounted: boolean;
226
+ };
227
+
182
228
  declare function createBetterStore(config: {
183
229
  apiKey: string;
184
230
  proxy?: string;
@@ -194,4 +240,4 @@ declare function createStoreClient(config?: {
194
240
  proxy?: string;
195
241
  }): Client;
196
242
 
197
- export { createStoreClient, createBetterStore as default };
243
+ export { type Discount, createStoreClient, createBetterStore as default, findAutomaticProductDiscount };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { OTPSignupParams, OTPSignupResponse, OTPLoginParams, OTPLoginResponse, OTPVerifyParams, OTPVerifyResponse, CustomerSession, CheckoutCreateParams, CheckoutSession, CheckoutUpdateParams, GetShippingRatesResponse, CustomerCreateParams, CustomerResponse, CustomerUpdateParams, AutosuggestAddressResult, LookupAddressResult, ListCollectionsParams, ListCollectionsResponse, RetrieveCollectionParams, RetrieveCollectionResponse, CustomerSubscriptionUpdateParams, CustomerSubscription, ListDiscountsParams, ListDiscountsResponse, RetrieveDiscountParams, RetrieveDiscountResponse, ListProductsParams, ListProductsResponse, RetrieveProductParams, RetrieveProductResponse } from '@betterstore/bridge';
2
2
  export { Address, AutosuggestAddressResult, CheckoutCreateParams, CheckoutSession, CheckoutUpdateParams, CustomShippingVendorRate, CustomerCreateParams, CustomerResponse, CustomerSession, CustomerSubscription, CustomerSubscriptionUpdateParams, CustomerUpdateParams, GetShippingRatesResponse, ListCollectionsParams, ListCollectionsResponse, ListDiscountsParams, ListDiscountsResponse, ListProductsParams, ListProductsResponse, LookupAddressResult, OTPLoginParams, OTPLoginResponse, OTPSignupParams, OTPSignupResponse, OTPVerifyParams, OTPVerifyResponse, ProductVariant, RetrieveCollectionParams, RetrieveCollectionResponse, RetrieveDiscountParams, RetrieveDiscountResponse, RetrieveProductParams, RetrieveProductResponse, formatCurrency, formatPrice } from '@betterstore/bridge';
3
3
  import axios from 'axios';
4
+ import * as _betterstore_bridge_generated_prisma_client from '@betterstore/bridge/generated/prisma/client';
4
5
 
5
6
  declare const createApiClient: (apiKey: string, proxy?: string) => axios.AxiosInstance;
6
7
 
@@ -132,12 +133,13 @@ type FormatResponseForSDK<T extends object> = keyof T extends [
132
133
  string,
133
134
  ...string[]
134
135
  ] ? T : T[keyof T];
136
+ type FormatParamsForSDK<GenericType, ConstraintType> = GenericType & Record<Exclude<keyof GenericType, keyof ConstraintType>, never>;
135
137
 
136
138
  declare class Collections {
137
139
  private apiClient;
138
140
  constructor(apiKey: string, proxy?: string);
139
- list<T extends ListCollectionsParams>(params?: T): Promise<FormatResponseForSDK<ListCollectionsResponse<T>>>;
140
- retrieve<T extends RetrieveCollectionParams>(params: T): Promise<FormatResponseForSDK<RetrieveCollectionResponse<T>> | null>;
141
+ list<T extends ListCollectionsParams>(params?: FormatParamsForSDK<T, ListCollectionsParams>): Promise<FormatResponseForSDK<ListCollectionsResponse<T>>>;
142
+ retrieve<T extends RetrieveCollectionParams>(params: FormatParamsForSDK<T, RetrieveCollectionParams>): Promise<FormatResponseForSDK<RetrieveCollectionResponse<T>> | null>;
141
143
  }
142
144
 
143
145
  declare class Customer {
@@ -168,17 +170,61 @@ declare class Customer {
168
170
  declare class Discounts {
169
171
  private apiClient;
170
172
  constructor(apiKey: string, proxy?: string);
171
- list<T extends ListDiscountsParams>(params?: T): Promise<FormatResponseForSDK<ListDiscountsResponse>>;
172
- retrieve<T extends RetrieveDiscountParams>(params: T): Promise<FormatResponseForSDK<RetrieveDiscountResponse> | null>;
173
+ list<T extends ListDiscountsParams>(params?: FormatParamsForSDK<T, ListDiscountsParams>): Promise<FormatResponseForSDK<ListDiscountsResponse>>;
174
+ retrieve<T extends RetrieveDiscountParams>(params: FormatParamsForSDK<T, RetrieveDiscountParams>): Promise<FormatResponseForSDK<RetrieveDiscountResponse> | null>;
173
175
  }
174
176
 
175
177
  declare class Products {
176
178
  private apiClient;
177
179
  constructor(apiKey: string, proxy?: string);
178
- list<T extends ListProductsParams>(params?: T): Promise<FormatResponseForSDK<ListProductsResponse<T>>>;
179
- retrieve<T extends RetrieveProductParams>(params: T): Promise<FormatResponseForSDK<RetrieveProductResponse<T>> | null>;
180
+ list<T extends ListProductsParams>(params?: FormatParamsForSDK<T, ListProductsParams>): Promise<FormatResponseForSDK<ListProductsResponse<T>>>;
181
+ retrieve<T extends RetrieveProductParams>(params: FormatParamsForSDK<T, RetrieveProductParams>): Promise<FormatResponseForSDK<RetrieveProductResponse<T>> | null>;
180
182
  }
181
183
 
184
+ type Discount = NonNullable<RetrieveDiscountResponse["discount"]>;
185
+ declare function findAutomaticProductDiscount({ productId, priceInCents, discounts, }: {
186
+ productId: string;
187
+ priceInCents: number;
188
+ discounts: Discount[];
189
+ }): {
190
+ discount: {
191
+ method: _betterstore_bridge_generated_prisma_client.$Enums.DiscountMethod;
192
+ testmode: boolean;
193
+ id: string;
194
+ createdAt: Date;
195
+ updatedAt: Date;
196
+ expiresAt: Date | null;
197
+ status: _betterstore_bridge_generated_prisma_client.$Enums.DiscountStatus;
198
+ code: string | null;
199
+ title: string | null;
200
+ type: _betterstore_bridge_generated_prisma_client.$Enums.DiscountType;
201
+ value: number;
202
+ valueType: _betterstore_bridge_generated_prisma_client.$Enums.DiscountValueType;
203
+ discountScope: _betterstore_bridge_generated_prisma_client.$Enums.ProductDiscountScope;
204
+ allowedProductIDs: string[];
205
+ allowedCollectionIDs: string[];
206
+ allowedCombinations: _betterstore_bridge_generated_prisma_client.$Enums.DiscountScope[];
207
+ minimumRequirementsType: _betterstore_bridge_generated_prisma_client.$Enums.MinimumRequirementsType;
208
+ minimumRequirementsValue: number | null;
209
+ requiredProductIDs: string[];
210
+ requiredCollectionIDs: string[];
211
+ minimumRequirementsScope: _betterstore_bridge_generated_prisma_client.$Enums.ProductDiscountScope;
212
+ maxUses: number | null;
213
+ maxUsesPerCustomer: number | null;
214
+ maxAllowedProductQuantity: number | null;
215
+ uses: number;
216
+ subscriptionDiscountDurationType: _betterstore_bridge_generated_prisma_client.$Enums.SubscriptionDiscountDurationType;
217
+ subscriptionDiscountDurationValue: number;
218
+ stripeDiscountId: string | null;
219
+ startsAt: Date;
220
+ } | undefined;
221
+ finalPrice: number;
222
+ discountAmount: number;
223
+ originalPrice: number;
224
+ isFree: boolean;
225
+ isDiscounted: boolean;
226
+ };
227
+
182
228
  declare function createBetterStore(config: {
183
229
  apiKey: string;
184
230
  proxy?: string;
@@ -194,4 +240,4 @@ declare function createStoreClient(config?: {
194
240
  proxy?: string;
195
241
  }): Client;
196
242
 
197
- export { createStoreClient, createBetterStore as default };
243
+ 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 ?? 0,
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, discount };
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 ?? 0,
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, discount };
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.4",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -18,7 +18,7 @@
18
18
  "@betterstore/typescript-config": "0.0.0"
19
19
  },
20
20
  "dependencies": {
21
- "@betterstore/bridge": "^0.0.23",
21
+ "@betterstore/bridge": "^0.0.24",
22
22
  "axios": "^1.8.2",
23
23
  "zod": "^3.24.2"
24
24
  },