@shware/purchase 1.1.0 → 1.2.0

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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/stripe/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms, { type StringValue } from 'ms';\n\nexport type PriceId = `price_${string}`;\nexport type ProductId = Lowercase<string>;\nexport type CreditConfig = { credits: number; expiresIn: StringValue };\n\nconst addMethod = Symbol('addMethod');\n\nclass Product<P extends ProductId = ProductId, I extends PriceId = never> {\n readonly id: P;\n readonly config: StripeConfig;\n readonly plan: string | null;\n readonly prices: Map<PriceId, CreditConfig>;\n\n defaultPriceId: I | null;\n\n private constructor(config: StripeConfig, id: P, plan: string | null = null) {\n this.id = id;\n this.config = config;\n this.plan = plan;\n this.prices = new Map();\n this.defaultPriceId = null;\n }\n\n static create = <P extends ProductId>(\n config: StripeConfig,\n id: P,\n plan: string | null = null\n ) => {\n return new Product<P>(config, id, plan);\n };\n\n price = <K extends PriceId>(\n priceId: K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ): Product<P, I | K> => {\n this.prices.set(priceId, credit);\n return this as Product<P, I | K>;\n };\n\n default = (defaultPriceId: I) => {\n this.defaultPriceId = defaultPriceId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options = {\n returnUrl: string;\n cancelUrl: string;\n successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;\n allowPromotionCodes: boolean;\n};\n\nexport class StripeConfig {\n private products: Map<ProductId, Product> = new Map();\n\n public returnUrl: string;\n public cancelUrl: string;\n public successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;\n public allowPromotionCodes: boolean;\n\n get productIds(): ProductId[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (product: Product) => {\n this.products.set(product.id, product);\n return this;\n };\n\n private constructor(options: Options) {\n this.returnUrl = options.returnUrl;\n this.cancelUrl = options.cancelUrl;\n this.successUrl = options.successUrl;\n this.allowPromotionCodes = options.allowPromotionCodes;\n }\n\n static create = (options: Options) => {\n return new StripeConfig(options);\n };\n\n product = (id: ProductId, plan: string | null = null) => {\n return Product.create(this, id, plan);\n };\n\n getPlan = (productId: string): string => {\n const plan = this.products.get(productId as ProductId)?.plan;\n if (!plan) throw new Error(`Plan not found for product ${productId}`);\n return plan;\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n return product.plan === null ? 'payment' : 'subscription';\n };\n\n getPriceId = (productId: string): PriceId => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.defaultPriceId;\n };\n\n getCreditAmount = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return price.credits;\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.prices.get(product.defaultPriceId)?.credits ?? 0;\n };\n\n private getCreditExpiresIn = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return ms(price.expiresIn);\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return ms(product.prices.get(product.defaultPriceId)?.expiresIn ?? '0');\n };\n\n getCreditExpiresAt = (productId: string, priceId?: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId, priceId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;AAC1B,gBAAqC;AAMrC,IAAM,YAAY,uBAAO,WAAW;AAE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E;AACpF,eAAO,UAAAA,SAAG,QAAQ,OAAO,IAAI,QAAQ,cAAc,GAAG,aAAa,GAAG;AAAA,EACxE;AAAA,EAEA,qBAAqB,CAAC,WAAmB,YAA6B;AACpE,UAAM,YAAY,KAAK,mBAAmB,WAAW,OAAO;AAC5D,WAAO,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,EAAE,YAAY;AAAA,EACtD;AACF;","names":["ms"]}
1
+ {"version":3,"sources":["../../src/stripe/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms, { type StringValue } from 'ms';\n\nexport type PriceId = `price_${string}`;\nexport type ProductId = Lowercase<string>;\nexport type CreditConfig = { credits: number; expiresIn: StringValue };\n\nconst addMethod = Symbol('addMethod');\n\nclass Product<\n NS extends Lowercase<string> = Lowercase<string>,\n PL extends string = string,\n P extends ProductId = ProductId,\n I extends PriceId = never,\n> {\n readonly id: P;\n readonly config: StripeConfig<NS, PL>;\n readonly plan: PL | null;\n readonly prices: Map<PriceId, CreditConfig>;\n\n defaultPriceId: I | null;\n\n private constructor(config: StripeConfig<NS, PL>, id: P, plan: PL | null = null) {\n this.id = id;\n this.config = config;\n this.plan = plan;\n this.prices = new Map();\n this.defaultPriceId = null;\n }\n\n static create = <\n NS extends Lowercase<string>,\n PL extends string,\n P extends ProductId = ProductId,\n >(\n config: StripeConfig<NS, PL>,\n id: P,\n plan: PL | null = null\n ) => {\n return new Product<NS, PL, P>(config, id, plan);\n };\n\n price = <K extends PriceId>(\n priceId: K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ): Product<NS, PL, P, I | K> => {\n this.prices.set(priceId, credit);\n return this as Product<NS, PL, P, I | K>;\n };\n\n default = (defaultPriceId: I) => {\n this.defaultPriceId = defaultPriceId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options = {\n returnUrl: string;\n cancelUrl: string;\n successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;\n allowPromotionCodes: boolean;\n};\n\nexport class StripeConfig<\n NS extends Lowercase<string> = Lowercase<string>,\n PL extends string = string,\n> {\n private products: Map<ProductId, Product<NS, PL>> = new Map();\n\n public returnUrl: string;\n public cancelUrl: string;\n public successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;\n public allowPromotionCodes: boolean;\n\n get productIds(): ProductId[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (product: Product<NS, PL>) => {\n this.products.set(product.id, product);\n return this;\n };\n\n private constructor(options: Options) {\n this.returnUrl = options.returnUrl;\n this.cancelUrl = options.cancelUrl;\n this.successUrl = options.successUrl;\n this.allowPromotionCodes = options.allowPromotionCodes;\n }\n\n static create = <NS extends Lowercase<string>, P extends string>(options: Options) => {\n return new StripeConfig<NS, P>(options);\n };\n\n product = (id: `${NS}.${ProductId}`, plan: PL | null = null) => {\n return Product.create<NS, PL>(this, id as ProductId, plan);\n };\n\n getPlan = (productId: string): PL => {\n const plan = this.products.get(productId as ProductId)?.plan;\n if (!plan) throw new Error(`Plan not found for product ${productId}`);\n return plan;\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n return product.plan === null ? 'payment' : 'subscription';\n };\n\n getPriceId = (productId: string): PriceId => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.defaultPriceId;\n };\n\n getCreditAmount = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return price.credits;\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.prices.get(product.defaultPriceId)?.credits ?? 0;\n };\n\n private getCreditExpiresIn = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return ms(price.expiresIn);\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return ms(product.prices.get(product.defaultPriceId)?.expiresIn ?? '0');\n };\n\n getCreditExpiresAt = (productId: string, priceId?: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId, priceId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;AAC1B,gBAAqC;AAMrC,IAAM,YAAY,uBAAO,WAAW;AAEpC,IAAM,UAAN,MAAM,SAKJ;AAAA,EACS;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA,EAEQ,YAAY,QAA8B,IAAO,OAAkB,MAAM;AAC/E,SAAK,KAAK;AACV,SAAK,SAAS;AACd,SAAK,OAAO;AACZ,SAAK,SAAS,oBAAI,IAAI;AACtB,SAAK,iBAAiB;AAAA,EACxB;AAAA,EAEA,OAAO,SAAS,CAKd,QACA,IACA,OAAkB,SACf;AACH,WAAO,IAAI,SAAmB,QAAQ,IAAI,IAAI;AAAA,EAChD;AAAA,EAEA,QAAQ,CACN,SACA,SAAuB,EAAE,SAAS,GAAG,WAAW,IAAI,MACtB;AAC9B,SAAK,OAAO,IAAI,SAAS,MAAM;AAC/B,WAAO;AAAA,EACT;AAAA,EAEA,UAAU,CAAC,mBAAsB;AAC/B,SAAK,iBAAiB;AACtB,SAAK,OAAO,SAAS,EAAE,IAAa;AACpC,WAAO,KAAK;AAAA,EACd;AACF;AASO,IAAM,eAAN,MAAM,cAGX;AAAA,EACQ,WAA4C,oBAAI,IAAI;AAAA,EAErD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEP,IAAI,aAA0B;AAC5B,WAAO,MAAM,KAAK,KAAK,SAAS,KAAK,CAAC;AAAA,EACxC;AAAA,EAEA,CAAC,SAAS,IA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,SAAS,EAAE;AACpF,WAAO,QAAQ,OAAO,IAAI,QAAQ,cAAc,GAAG,WAAW;AAAA,EAChE;AAAA,EAEQ,qBAAqB,CAAC,WAAmB,YAA6B;AAC5E,UAAM,UAAU,KAAK,SAAS,IAAI,SAAsB;AACxD,gCAAU,SAAS,yBAAyB,SAAS,EAAE;AACvD,QAAI,SAAS;AACX,YAAM,QAAQ,QAAQ,OAAO,IAAI,OAAkB;AACnD,kCAAU,OAAO,uBAAuB,OAAO,EAAE;AACjD,iBAAO,UAAAA,SAAG,MAAM,SAAS;AAAA,IAC3B;AAEA,gCAAU,QAAQ,gBAAgB,uCAAuC,SAAS,EAAE;AACpF,eAAO,UAAAA,SAAG,QAAQ,OAAO,IAAI,QAAQ,cAAc,GAAG,aAAa,GAAG;AAAA,EACxE;AAAA,EAEA,qBAAqB,CAAC,WAAmB,YAA6B;AACpE,UAAM,YAAY,KAAK,mBAAmB,WAAW,OAAO;AAC5D,WAAO,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,EAAE,YAAY;AAAA,EACtD;AACF;","names":["ms"]}
@@ -7,16 +7,16 @@ type CreditConfig = {
7
7
  expiresIn: StringValue;
8
8
  };
9
9
  declare const addMethod: unique symbol;
10
- declare class Product<P extends ProductId = ProductId, I extends PriceId = never> {
10
+ declare class Product<NS extends Lowercase<string> = Lowercase<string>, PL extends string = string, P extends ProductId = ProductId, I extends PriceId = never> {
11
11
  readonly id: P;
12
- readonly config: StripeConfig;
13
- readonly plan: string | null;
12
+ readonly config: StripeConfig<NS, PL>;
13
+ readonly plan: PL | null;
14
14
  readonly prices: Map<PriceId, CreditConfig>;
15
15
  defaultPriceId: I | null;
16
16
  private constructor();
17
- static create: <P_1 extends ProductId>(config: StripeConfig, id: P_1, plan?: string | null) => Product<P_1, never>;
18
- price: <K extends PriceId>(priceId: K, credit?: CreditConfig) => Product<P, I | K>;
19
- default: (defaultPriceId: I) => StripeConfig;
17
+ static create: <NS_1 extends Lowercase<string>, PL_1 extends string, P_1 extends ProductId = ProductId>(config: StripeConfig<NS_1, PL_1>, id: P_1, plan?: PL_1 | null) => Product<NS_1, PL_1, P_1, never>;
18
+ price: <K extends PriceId>(priceId: K, credit?: CreditConfig) => Product<NS, PL, P, I | K>;
19
+ default: (defaultPriceId: I) => StripeConfig<NS, PL>;
20
20
  }
21
21
  type Options = {
22
22
  returnUrl: string;
@@ -24,18 +24,18 @@ type Options = {
24
24
  successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;
25
25
  allowPromotionCodes: boolean;
26
26
  };
27
- declare class StripeConfig {
27
+ declare class StripeConfig<NS extends Lowercase<string> = Lowercase<string>, PL extends string = string> {
28
28
  private products;
29
29
  returnUrl: string;
30
30
  cancelUrl: string;
31
31
  successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;
32
32
  allowPromotionCodes: boolean;
33
33
  get productIds(): ProductId[];
34
- [addMethod]: (product: Product) => this;
34
+ [addMethod]: (product: Product<NS, PL>) => this;
35
35
  private constructor();
36
- static create: (options: Options) => StripeConfig;
37
- product: (id: ProductId, plan?: string | null) => Product<Lowercase<string>, never>;
38
- getPlan: (productId: string) => string;
36
+ static create: <NS_1 extends Lowercase<string>, P extends string>(options: Options) => StripeConfig<NS_1, P>;
37
+ product: (id: `${NS}.${ProductId}`, plan?: PL | null) => Product<NS, PL, Lowercase<string>, never>;
38
+ getPlan: (productId: string) => PL;
39
39
  getMode: (productId: string) => "payment" | "subscription";
40
40
  getPriceId: (productId: string) => PriceId;
41
41
  getCreditAmount: (productId: string, priceId?: string) => number;
@@ -7,16 +7,16 @@ type CreditConfig = {
7
7
  expiresIn: StringValue;
8
8
  };
9
9
  declare const addMethod: unique symbol;
10
- declare class Product<P extends ProductId = ProductId, I extends PriceId = never> {
10
+ declare class Product<NS extends Lowercase<string> = Lowercase<string>, PL extends string = string, P extends ProductId = ProductId, I extends PriceId = never> {
11
11
  readonly id: P;
12
- readonly config: StripeConfig;
13
- readonly plan: string | null;
12
+ readonly config: StripeConfig<NS, PL>;
13
+ readonly plan: PL | null;
14
14
  readonly prices: Map<PriceId, CreditConfig>;
15
15
  defaultPriceId: I | null;
16
16
  private constructor();
17
- static create: <P_1 extends ProductId>(config: StripeConfig, id: P_1, plan?: string | null) => Product<P_1, never>;
18
- price: <K extends PriceId>(priceId: K, credit?: CreditConfig) => Product<P, I | K>;
19
- default: (defaultPriceId: I) => StripeConfig;
17
+ static create: <NS_1 extends Lowercase<string>, PL_1 extends string, P_1 extends ProductId = ProductId>(config: StripeConfig<NS_1, PL_1>, id: P_1, plan?: PL_1 | null) => Product<NS_1, PL_1, P_1, never>;
18
+ price: <K extends PriceId>(priceId: K, credit?: CreditConfig) => Product<NS, PL, P, I | K>;
19
+ default: (defaultPriceId: I) => StripeConfig<NS, PL>;
20
20
  }
21
21
  type Options = {
22
22
  returnUrl: string;
@@ -24,18 +24,18 @@ type Options = {
24
24
  successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;
25
25
  allowPromotionCodes: boolean;
26
26
  };
27
- declare class StripeConfig {
27
+ declare class StripeConfig<NS extends Lowercase<string> = Lowercase<string>, PL extends string = string> {
28
28
  private products;
29
29
  returnUrl: string;
30
30
  cancelUrl: string;
31
31
  successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;
32
32
  allowPromotionCodes: boolean;
33
33
  get productIds(): ProductId[];
34
- [addMethod]: (product: Product) => this;
34
+ [addMethod]: (product: Product<NS, PL>) => this;
35
35
  private constructor();
36
- static create: (options: Options) => StripeConfig;
37
- product: (id: ProductId, plan?: string | null) => Product<Lowercase<string>, never>;
38
- getPlan: (productId: string) => string;
36
+ static create: <NS_1 extends Lowercase<string>, P extends string>(options: Options) => StripeConfig<NS_1, P>;
37
+ product: (id: `${NS}.${ProductId}`, plan?: PL | null) => Product<NS, PL, Lowercase<string>, never>;
38
+ getPlan: (productId: string) => PL;
39
39
  getMode: (productId: string) => "payment" | "subscription";
40
40
  getPriceId: (productId: string) => PriceId;
41
41
  getCreditAmount: (productId: string, priceId?: string) => number;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/stripe/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms, { type StringValue } from 'ms';\n\nexport type PriceId = `price_${string}`;\nexport type ProductId = Lowercase<string>;\nexport type CreditConfig = { credits: number; expiresIn: StringValue };\n\nconst addMethod = Symbol('addMethod');\n\nclass Product<P extends ProductId = ProductId, I extends PriceId = never> {\n readonly id: P;\n readonly config: StripeConfig;\n readonly plan: string | null;\n readonly prices: Map<PriceId, CreditConfig>;\n\n defaultPriceId: I | null;\n\n private constructor(config: StripeConfig, id: P, plan: string | null = null) {\n this.id = id;\n this.config = config;\n this.plan = plan;\n this.prices = new Map();\n this.defaultPriceId = null;\n }\n\n static create = <P extends ProductId>(\n config: StripeConfig,\n id: P,\n plan: string | null = null\n ) => {\n return new Product<P>(config, id, plan);\n };\n\n price = <K extends PriceId>(\n priceId: K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ): Product<P, I | K> => {\n this.prices.set(priceId, credit);\n return this as Product<P, I | K>;\n };\n\n default = (defaultPriceId: I) => {\n this.defaultPriceId = defaultPriceId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options = {\n returnUrl: string;\n cancelUrl: string;\n successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;\n allowPromotionCodes: boolean;\n};\n\nexport class StripeConfig {\n private products: Map<ProductId, Product> = new Map();\n\n public returnUrl: string;\n public cancelUrl: string;\n public successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;\n public allowPromotionCodes: boolean;\n\n get productIds(): ProductId[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (product: Product) => {\n this.products.set(product.id, product);\n return this;\n };\n\n private constructor(options: Options) {\n this.returnUrl = options.returnUrl;\n this.cancelUrl = options.cancelUrl;\n this.successUrl = options.successUrl;\n this.allowPromotionCodes = options.allowPromotionCodes;\n }\n\n static create = (options: Options) => {\n return new StripeConfig(options);\n };\n\n product = (id: ProductId, plan: string | null = null) => {\n return Product.create(this, id, plan);\n };\n\n getPlan = (productId: string): string => {\n const plan = this.products.get(productId as ProductId)?.plan;\n if (!plan) throw new Error(`Plan not found for product ${productId}`);\n return plan;\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n return product.plan === null ? 'payment' : 'subscription';\n };\n\n getPriceId = (productId: string): PriceId => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.defaultPriceId;\n };\n\n getCreditAmount = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return price.credits;\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.prices.get(product.defaultPriceId)?.credits ?? 0;\n };\n\n private getCreditExpiresIn = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return ms(price.expiresIn);\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return ms(product.prices.get(product.defaultPriceId)?.expiresIn ?? '0');\n };\n\n getCreditExpiresAt = (productId: string, priceId?: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId, priceId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,OAAO,QAA8B;AAMrC,IAAM,YAAY,uBAAO,WAAW;AAEpC,IAAM,UAAN,MAAM,SAAoE;AAAA,EAC/D;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA,EAEQ,YAAY,QAAsB,IAAO,OAAsB,MAAM;AAC3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mB,WAAW,OAAO;AAC5D,WAAO,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,EAAE,YAAY;AAAA,EACtD;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/stripe/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms, { type StringValue } from 'ms';\n\nexport type PriceId = `price_${string}`;\nexport type ProductId = Lowercase<string>;\nexport type CreditConfig = { credits: number; expiresIn: StringValue };\n\nconst addMethod = Symbol('addMethod');\n\nclass Product<\n NS extends Lowercase<string> = Lowercase<string>,\n PL extends string = string,\n P extends ProductId = ProductId,\n I extends PriceId = never,\n> {\n readonly id: P;\n readonly config: StripeConfig<NS, PL>;\n readonly plan: PL | null;\n readonly prices: Map<PriceId, CreditConfig>;\n\n defaultPriceId: I | null;\n\n private constructor(config: StripeConfig<NS, PL>, id: P, plan: PL | null = null) {\n this.id = id;\n this.config = config;\n this.plan = plan;\n this.prices = new Map();\n this.defaultPriceId = null;\n }\n\n static create = <\n NS extends Lowercase<string>,\n PL extends string,\n P extends ProductId = ProductId,\n >(\n config: StripeConfig<NS, PL>,\n id: P,\n plan: PL | null = null\n ) => {\n return new Product<NS, PL, P>(config, id, plan);\n };\n\n price = <K extends PriceId>(\n priceId: K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ): Product<NS, PL, P, I | K> => {\n this.prices.set(priceId, credit);\n return this as Product<NS, PL, P, I | K>;\n };\n\n default = (defaultPriceId: I) => {\n this.defaultPriceId = defaultPriceId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options = {\n returnUrl: string;\n cancelUrl: string;\n successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;\n allowPromotionCodes: boolean;\n};\n\nexport class StripeConfig<\n NS extends Lowercase<string> = Lowercase<string>,\n PL extends string = string,\n> {\n private products: Map<ProductId, Product<NS, PL>> = new Map();\n\n public returnUrl: string;\n public cancelUrl: string;\n public successUrl: `${string}session_id={CHECKOUT_SESSION_ID}${string}`;\n public allowPromotionCodes: boolean;\n\n get productIds(): ProductId[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (product: Product<NS, PL>) => {\n this.products.set(product.id, product);\n return this;\n };\n\n private constructor(options: Options) {\n this.returnUrl = options.returnUrl;\n this.cancelUrl = options.cancelUrl;\n this.successUrl = options.successUrl;\n this.allowPromotionCodes = options.allowPromotionCodes;\n }\n\n static create = <NS extends Lowercase<string>, P extends string>(options: Options) => {\n return new StripeConfig<NS, P>(options);\n };\n\n product = (id: `${NS}.${ProductId}`, plan: PL | null = null) => {\n return Product.create<NS, PL>(this, id as ProductId, plan);\n };\n\n getPlan = (productId: string): PL => {\n const plan = this.products.get(productId as ProductId)?.plan;\n if (!plan) throw new Error(`Plan not found for product ${productId}`);\n return plan;\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n return product.plan === null ? 'payment' : 'subscription';\n };\n\n getPriceId = (productId: string): PriceId => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.defaultPriceId;\n };\n\n getCreditAmount = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return price.credits;\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return product.prices.get(product.defaultPriceId)?.credits ?? 0;\n };\n\n private getCreditExpiresIn = (productId: string, priceId?: string): number => {\n const product = this.products.get(productId as ProductId);\n invariant(product, `Product not found for ${productId}`);\n if (priceId) {\n const price = product.prices.get(priceId as PriceId);\n invariant(price, `Price not found for ${priceId}`);\n return ms(price.expiresIn);\n }\n\n invariant(product.defaultPriceId, `Default price not found for product ${productId}`);\n return ms(product.prices.get(product.defaultPriceId)?.expiresIn ?? '0');\n };\n\n getCreditExpiresAt = (productId: string, priceId?: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId, priceId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,OAAO,QAA8B;AAMrC,IAAM,YAAY,uBAAO,WAAW;AAEpC,IAAM,UAAN,MAAM,SAKJ;AAAA,EACS;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA,EAEQ,YAAY,QAA8B,IAAO,OAAkB,MAAM;AAC/E,SAAK,KAAK;AACV,SAAK,SAAS;AACd,SAAK,OAAO;AACZ,SAAK,SAAS,oBAAI,IAAI;AACtB,SAAK,iBAAiB;AAAA,EACxB;AAAA,EAEA,OAAO,SAAS,CAKd,QACA,IACA,OAAkB,SACf;AACH,WAAO,IAAI,SAAmB,QAAQ,IAAI,IAAI;AAAA,EAChD;AAAA,EAEA,QAAQ,CACN,SACA,SAAuB,EAAE,SAAS,GAAG,WAAW,IAAI,MACtB;AAC9B,SAAK,OAAO,IAAI,SAAS,MAAM;AAC/B,WAAO;AAAA,EACT;AAAA,EAEA,UAAU,CAAC,mBAAsB;AAC/B,SAAK,iBAAiB;AACtB,SAAK,OAAO,SAAS,EAAE,IAAa;AACpC,WAAO,KAAK;AAAA,EACd;AACF;AASO,IAAM,eAAN,MAAM,cAGX;AAAA,EACQ,WAA4C,oBAAI,IAAI;AAAA,EAErD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEP,IAAI,aAA0B;AAC5B,WAAO,MAAM,KAAK,KAAK,SAAS,KAAK,CAAC;AAAA,EACxC;AAAA,EAEA,CAAC,SAAS,IAAI,CAAC,YAA6B;AAC1C,SAAK,SAAS,IAAI,QAAQ,IAAI,OAAO;AACrC,WAAO;AAAA,EACT;AAAA,EAEQ,YAAY,SAAkB;AACpC,SAAK,YAAY,QAAQ;AACzB,SAAK,YAAY,QAAQ;AACzB,SAAK,aAAa,QAAQ;AAC1B,SAAK,sBAAsB,QAAQ;AA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;AACX,YAAM,QAAQ,QAAQ,OAAO,IAAI,OAAkB;AACnD,gBAAU,OAAO,uBAAuB,OAAO,EAAE;AACjD,aAAO,GAAG,MAAM,SAAS;AAAA,IAC3B;AAEA,cAAU,QAAQ,gBAAgB,uCAAuC,SAAS,EAAE;AACpF,WAAO,GAAG,QAAQ,OAAO,IAAI,QAAQ,cAAc,GAAG,aAAa,GAAG;AAAA,EACxE;AAAA,EAEA,qBAAqB,CAAC,WAAmB,YAA6B;AACpE,UAAM,YAAY,KAAK,mBAAmB,WAAW,OAAO;AAC5D,WAAO,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,EAAE,YAAY;AAAA,EACtD;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shware/purchase",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "repository": {