From d4520c4489a1a60b302ed9f527272c703ee3f6cb Mon Sep 17 00:00:00 2001 From: Branko Conjic Date: Mon, 10 Jun 2024 18:28:31 -0400 Subject: [PATCH] fix: variantIds as optional --- src/discounts/index.ts | 11 +++++++---- src/discounts/types.ts | 40 ++++++++++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/discounts/index.ts b/src/discounts/index.ts index 80b1d50..b8512b6 100644 --- a/src/discounts/index.ts +++ b/src/discounts/index.ts @@ -55,20 +55,23 @@ export function createDiscount(discount: NewDiscount) { testMode, }); - const relationships = { + const relationships: Record = { store: { data: { type: "stores", id: storeId.toString(), }, }, - variants: { + }; + + if (variantIds && variantIds.length > 0) { + relationships.variants = { data: variantIds.map((id) => ({ type: "variants", id: id.toString(), })), - }, - }; + }; + } return $fetch({ path: "/v1/discounts", diff --git a/src/discounts/types.ts b/src/discounts/types.ts index 3d27208..cd6c99f 100644 --- a/src/discounts/types.ts +++ b/src/discounts/types.ts @@ -96,15 +96,12 @@ export type ListDiscountsParams = Params< GetDiscountParams["include"], { storeId?: string | number } >; -export type NewDiscount = { + +type NewDiscountBase = { /** * The store id this discount belongs to. */ storeId: number | string; - /** - * If `isLimitedToProducts` is `true`, the variant(s) the discount belongs to (this is not required otherwise). - */ - variantIds: Array; /** * The name of the discount. */ @@ -126,16 +123,12 @@ export type NewDiscount = { * The type of the amount. Either `percent` or `fixed`. */ amountType: AmountType; - /** - * Set this to true if the discount should only be applied to certain products/variants. See details in the Relationships section below. - */ - isLimitedToProducts?: boolean; /** * Set this to `true` if the discount should only be redeemed a limited number of times. See `maxRedemptions` below. */ isLimitedRedemptions?: boolean; /** - * If `isLimitedToProducts` is `true`, this is the maximum number of redemptions. + * If `isLimitedRedemptions` is `true`, this is the maximum number of redemptions. */ maxRedemptions?: number; /** @@ -170,10 +163,37 @@ export type NewDiscount = { */ testMode?: boolean; }; + +type NewDiscountWithVariants = NewDiscountBase & { + /** + * Set this to true if the discount should only be applied to certain products/variants. See details in the Relationships section below. + */ + isLimitedToProducts: true; + + /** + * If `isLimitedToProducts` is `true`, the variant(s) the discount belongs to (this is not required otherwise). + */ + variantIds: Array; +}; + +type NewDiscountWithoutVariants = NewDiscountBase & { + /** + * Set this to true if the discount should only be applied to certain products/variants. See details in the Relationships section below. + */ + isLimitedToProducts?: false; + /** + * Not required. + */ + variantIds?: Array; +}; + +export type NewDiscount = NewDiscountWithVariants | NewDiscountWithoutVariants; + export type Discount = Omit< LemonSqueezyResponse>, "meta" >; + export type ListDiscounts = LemonSqueezyResponse< DiscountData[], Pick,