Skip to content

Commit

Permalink
fix: variantIds as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
brankoconjic committed Jun 10, 2024
1 parent 5cdad7b commit d4520c4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
11 changes: 7 additions & 4 deletions src/discounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,23 @@ export function createDiscount(discount: NewDiscount) {
testMode,
});

const relationships = {
const relationships: Record<string, any> = {
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<Discount>({
path: "/v1/discounts",
Expand Down
40 changes: 30 additions & 10 deletions src/discounts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number | string>;
/**
* The name of the discount.
*/
Expand All @@ -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;
/**
Expand Down Expand Up @@ -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<number | string>;
};

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<number | string>;
};

export type NewDiscount = NewDiscountWithVariants | NewDiscountWithoutVariants;

export type Discount = Omit<
LemonSqueezyResponse<DiscountData, unknown, Pick<Links, "self">>,
"meta"
>;

export type ListDiscounts = LemonSqueezyResponse<
DiscountData[],
Pick<Meta, "page">,
Expand Down

0 comments on commit d4520c4

Please sign in to comment.