@shware/purchase 1.3.0 → 1.4.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.
|
@@ -58,8 +58,12 @@ var Subscription = class {
|
|
|
58
58
|
};
|
|
59
59
|
};
|
|
60
60
|
var AppStoreConfig = class _AppStoreConfig {
|
|
61
|
+
appId;
|
|
62
|
+
bundleId;
|
|
61
63
|
products;
|
|
64
|
+
consumables;
|
|
62
65
|
subscriptions;
|
|
66
|
+
nonConsumables;
|
|
63
67
|
get productIds() {
|
|
64
68
|
return Array.from(this.products.keys());
|
|
65
69
|
}
|
|
@@ -68,16 +72,43 @@ var AppStoreConfig = class _AppStoreConfig {
|
|
|
68
72
|
this.subscriptions.set(subscription.plan, subscription);
|
|
69
73
|
return this;
|
|
70
74
|
};
|
|
71
|
-
constructor() {
|
|
75
|
+
constructor(options) {
|
|
76
|
+
this.appId = options.appId;
|
|
77
|
+
this.bundleId = options.bundleId;
|
|
72
78
|
this.products = /* @__PURE__ */ new Map();
|
|
79
|
+
this.consumables = /* @__PURE__ */ new Set();
|
|
73
80
|
this.subscriptions = /* @__PURE__ */ new Map();
|
|
81
|
+
this.nonConsumables = /* @__PURE__ */ new Set();
|
|
74
82
|
}
|
|
75
|
-
static create = () => {
|
|
76
|
-
return new _AppStoreConfig();
|
|
83
|
+
static create = (options) => {
|
|
84
|
+
return new _AppStoreConfig(options);
|
|
77
85
|
};
|
|
78
86
|
subscription = (plan) => {
|
|
79
87
|
return new Subscription(this, plan);
|
|
80
88
|
};
|
|
89
|
+
consumable = (productId, credit = { credits: 0, expiresIn: "0" }) => {
|
|
90
|
+
this.consumables.add(productId);
|
|
91
|
+
this.products.set(productId, credit);
|
|
92
|
+
return this;
|
|
93
|
+
};
|
|
94
|
+
nonConsumable = (productId) => {
|
|
95
|
+
this.nonConsumables.add(productId);
|
|
96
|
+
this.products.set(productId, null);
|
|
97
|
+
return this;
|
|
98
|
+
};
|
|
99
|
+
getPlan = (productId) => {
|
|
100
|
+
for (const [plan, subscription] of this.subscriptions.entries()) {
|
|
101
|
+
if (subscription.products.has(productId)) return plan;
|
|
102
|
+
}
|
|
103
|
+
throw new Error(`Plan not found for product ${productId}`);
|
|
104
|
+
};
|
|
105
|
+
getMode = (productId) => {
|
|
106
|
+
if (this.consumables.has(productId) || this.nonConsumables.has(productId)) return "payment";
|
|
107
|
+
for (const subscription of this.subscriptions.values()) {
|
|
108
|
+
if (subscription.products.has(productId)) return "subscription";
|
|
109
|
+
}
|
|
110
|
+
throw new Error(`Mode not found for product ${productId}`);
|
|
111
|
+
};
|
|
81
112
|
getCreditAmount = (productId) => {
|
|
82
113
|
const config = this.products.get(productId);
|
|
83
114
|
(0, import_utils.invariant)(config, `Product not found for ${productId}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/app-store/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms, { type StringValue } from 'ms';\n\nexport type CreditConfig = { credits: number; expiresIn: StringValue };\n\nconst addMethod = Symbol('addMethod');\n\nclass Subscription<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n I extends string = never,\n> {\n readonly plan: PL;\n readonly config: AppStoreConfig<NS, PE, PL, PI>;\n readonly products: Map<string, CreditConfig>;\n\n defaultProductId: I | null;\n\n constructor(config: AppStoreConfig<NS, PE, PL, PI>, plan: PL) {\n this.plan = plan;\n this.config = config;\n this.products = new Map();\n this.defaultProductId = null;\n }\n\n product = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ): Subscription<NS, PE, PL, PI | K, I | K> => {\n this.products.set(productId, credit);\n return this as Subscription<NS, PE, PL, PI | K, I | K>;\n };\n\n default = (defaultProductId: I) => {\n this.defaultProductId = defaultProductId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\nexport class AppStoreConfig<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n> {\n private products: Map<string, CreditConfig>;\n private subscriptions: Map<PE, Subscription<NS, PE, PL, PI>>;\n\n get productIds(): string[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (subscription: Subscription<NS, PE, PL, PI>) => {\n subscription.products.forEach((credit, productId) => this.products.set(productId, credit));\n this.subscriptions.set(subscription.plan, subscription);\n return this;\n };\n\n private constructor() {\n this.products = new Map();\n this.subscriptions = new Map();\n }\n\n static create = <NS extends Lowercase<string>, PE extends string>() => {\n return new AppStoreConfig<NS, PE>();\n };\n\n subscription = <K extends PE>(plan: K extends PL ? never : K) => {\n return new Subscription<NS, PE, K | PL, PI>(this, plan);\n };\n\n getCreditAmount = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return config.credits;\n };\n\n private getCreditExpiresIn = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return ms(config.expiresIn);\n };\n\n getCreditExpiresAt = (productId: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;AAC1B,gBAAqC;AAIrC,IAAM,YAAY,uBAAO,WAAW;AAEpC,IAAM,eAAN,MAME;AAAA,EACS;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA,EAEA,YAAY,QAAwC,MAAU;AAC5D,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,WAAW,oBAAI,IAAI;AACxB,SAAK,mBAAmB;AAAA,EAC1B;AAAA,EAEA,UAAU,CACR,WACA,SAAuB,EAAE,SAAS,GAAG,WAAW,IAAI,MACR;AAC5C,SAAK,SAAS,IAAI,WAAW,MAAM;AACnC,WAAO;AAAA,EACT;AAAA,EAEA,UAAU,CAAC,qBAAwB;AACjC,SAAK,mBAAmB;AACxB,SAAK,OAAO,SAAS,EAAE,IAAa;AACpC,WAAO,KAAK;AAAA,EACd;AACF;
|
|
1
|
+
{"version":3,"sources":["../../src/app-store/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms, { type StringValue } from 'ms';\n\nexport type CreditConfig = { credits: number; expiresIn: StringValue };\n\nconst addMethod = Symbol('addMethod');\n\nclass Subscription<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n I extends string = never,\n> {\n readonly plan: PL;\n readonly config: AppStoreConfig<NS, PE, PL, PI>;\n readonly products: Map<string, CreditConfig>;\n\n defaultProductId: I | null;\n\n constructor(config: AppStoreConfig<NS, PE, PL, PI>, plan: PL) {\n this.plan = plan;\n this.config = config;\n this.products = new Map();\n this.defaultProductId = null;\n }\n\n product = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ): Subscription<NS, PE, PL, PI | K, I | K> => {\n this.products.set(productId, credit);\n return this as Subscription<NS, PE, PL, PI | K, I | K>;\n };\n\n default = (defaultProductId: I) => {\n this.defaultProductId = defaultProductId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options = { appId: `${number}`; bundleId: string };\n\nexport class AppStoreConfig<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n> {\n readonly appId: string;\n readonly bundleId: string;\n\n private products: Map<string, CreditConfig | null>;\n private consumables: Set<string>;\n private subscriptions: Map<PE, Subscription<NS, PE, PL, PI>>;\n private nonConsumables: Set<string>;\n\n get productIds(): string[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (subscription: Subscription<NS, PE, PL, PI>) => {\n subscription.products.forEach((credit, productId) => this.products.set(productId, credit));\n this.subscriptions.set(subscription.plan, subscription);\n return this;\n };\n\n private constructor(options: Options) {\n this.appId = options.appId;\n this.bundleId = options.bundleId;\n this.products = new Map();\n this.consumables = new Set();\n this.subscriptions = new Map();\n this.nonConsumables = new Set();\n }\n\n static create = <NS extends Lowercase<string>, PE extends string>(options: Options) => {\n return new AppStoreConfig<NS, PE>(options);\n };\n\n subscription = <K extends PE>(plan: K extends PL ? never : K) => {\n return new Subscription<NS, PE, K | PL, PI>(this, plan);\n };\n\n consumable = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ) => {\n this.consumables.add(productId);\n this.products.set(productId, credit);\n return this as AppStoreConfig<NS, PE, PL, PI | K>;\n };\n\n nonConsumable = <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K) => {\n this.nonConsumables.add(productId);\n this.products.set(productId, null);\n return this as AppStoreConfig<NS, PE, PL, PI | K>;\n };\n\n getPlan = (productId: string): PE => {\n for (const [plan, subscription] of this.subscriptions.entries()) {\n if (subscription.products.has(productId)) return plan;\n }\n throw new Error(`Plan not found for product ${productId}`);\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n if (this.consumables.has(productId) || this.nonConsumables.has(productId)) return 'payment';\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return 'subscription';\n }\n throw new Error(`Mode not found for product ${productId}`);\n };\n\n getCreditAmount = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return config.credits;\n };\n\n private getCreditExpiresIn = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return ms(config.expiresIn);\n };\n\n getCreditExpiresAt = (productId: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;AAC1B,gBAAqC;AAIrC,IAAM,YAAY,uBAAO,WAAW;AAEpC,IA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14,14 +14,26 @@ declare class Subscription<NS extends Lowercase<string> = Lowercase<string>, PE
|
|
|
14
14
|
product: <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K, credit?: CreditConfig) => Subscription<NS, PE, PL, PI | K, I | K>;
|
|
15
15
|
default: (defaultProductId: I) => AppStoreConfig<NS, PE, PL, PI>;
|
|
16
16
|
}
|
|
17
|
+
type Options = {
|
|
18
|
+
appId: `${number}`;
|
|
19
|
+
bundleId: string;
|
|
20
|
+
};
|
|
17
21
|
declare class AppStoreConfig<NS extends Lowercase<string> = Lowercase<string>, PE extends string = string, PL extends PE = never, PI extends string = never> {
|
|
22
|
+
readonly appId: string;
|
|
23
|
+
readonly bundleId: string;
|
|
18
24
|
private products;
|
|
25
|
+
private consumables;
|
|
19
26
|
private subscriptions;
|
|
27
|
+
private nonConsumables;
|
|
20
28
|
get productIds(): string[];
|
|
21
29
|
[addMethod]: (subscription: Subscription<NS, PE, PL, PI>) => this;
|
|
22
30
|
private constructor();
|
|
23
|
-
static create: <NS_1 extends Lowercase<string>, PE_1 extends string>() => AppStoreConfig<NS_1, PE_1, never, never>;
|
|
31
|
+
static create: <NS_1 extends Lowercase<string>, PE_1 extends string>(options: Options) => AppStoreConfig<NS_1, PE_1, never, never>;
|
|
24
32
|
subscription: <K extends PE>(plan: K extends PL ? never : K) => Subscription<NS, PE, PL | K, PI, never>;
|
|
33
|
+
consumable: <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K, credit?: CreditConfig) => AppStoreConfig<NS, PE, PL, PI | K>;
|
|
34
|
+
nonConsumable: <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K) => AppStoreConfig<NS, PE, PL, PI | K>;
|
|
35
|
+
getPlan: (productId: string) => PE;
|
|
36
|
+
getMode: (productId: string) => "payment" | "subscription";
|
|
25
37
|
getCreditAmount: (productId: string) => number;
|
|
26
38
|
private getCreditExpiresIn;
|
|
27
39
|
getCreditExpiresAt: (productId: string) => string;
|
|
@@ -14,14 +14,26 @@ declare class Subscription<NS extends Lowercase<string> = Lowercase<string>, PE
|
|
|
14
14
|
product: <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K, credit?: CreditConfig) => Subscription<NS, PE, PL, PI | K, I | K>;
|
|
15
15
|
default: (defaultProductId: I) => AppStoreConfig<NS, PE, PL, PI>;
|
|
16
16
|
}
|
|
17
|
+
type Options = {
|
|
18
|
+
appId: `${number}`;
|
|
19
|
+
bundleId: string;
|
|
20
|
+
};
|
|
17
21
|
declare class AppStoreConfig<NS extends Lowercase<string> = Lowercase<string>, PE extends string = string, PL extends PE = never, PI extends string = never> {
|
|
22
|
+
readonly appId: string;
|
|
23
|
+
readonly bundleId: string;
|
|
18
24
|
private products;
|
|
25
|
+
private consumables;
|
|
19
26
|
private subscriptions;
|
|
27
|
+
private nonConsumables;
|
|
20
28
|
get productIds(): string[];
|
|
21
29
|
[addMethod]: (subscription: Subscription<NS, PE, PL, PI>) => this;
|
|
22
30
|
private constructor();
|
|
23
|
-
static create: <NS_1 extends Lowercase<string>, PE_1 extends string>() => AppStoreConfig<NS_1, PE_1, never, never>;
|
|
31
|
+
static create: <NS_1 extends Lowercase<string>, PE_1 extends string>(options: Options) => AppStoreConfig<NS_1, PE_1, never, never>;
|
|
24
32
|
subscription: <K extends PE>(plan: K extends PL ? never : K) => Subscription<NS, PE, PL | K, PI, never>;
|
|
33
|
+
consumable: <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K, credit?: CreditConfig) => AppStoreConfig<NS, PE, PL, PI | K>;
|
|
34
|
+
nonConsumable: <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K) => AppStoreConfig<NS, PE, PL, PI | K>;
|
|
35
|
+
getPlan: (productId: string) => PE;
|
|
36
|
+
getMode: (productId: string) => "payment" | "subscription";
|
|
25
37
|
getCreditAmount: (productId: string) => number;
|
|
26
38
|
private getCreditExpiresIn;
|
|
27
39
|
getCreditExpiresAt: (productId: string) => string;
|
|
@@ -24,8 +24,12 @@ var Subscription = class {
|
|
|
24
24
|
};
|
|
25
25
|
};
|
|
26
26
|
var AppStoreConfig = class _AppStoreConfig {
|
|
27
|
+
appId;
|
|
28
|
+
bundleId;
|
|
27
29
|
products;
|
|
30
|
+
consumables;
|
|
28
31
|
subscriptions;
|
|
32
|
+
nonConsumables;
|
|
29
33
|
get productIds() {
|
|
30
34
|
return Array.from(this.products.keys());
|
|
31
35
|
}
|
|
@@ -34,16 +38,43 @@ var AppStoreConfig = class _AppStoreConfig {
|
|
|
34
38
|
this.subscriptions.set(subscription.plan, subscription);
|
|
35
39
|
return this;
|
|
36
40
|
};
|
|
37
|
-
constructor() {
|
|
41
|
+
constructor(options) {
|
|
42
|
+
this.appId = options.appId;
|
|
43
|
+
this.bundleId = options.bundleId;
|
|
38
44
|
this.products = /* @__PURE__ */ new Map();
|
|
45
|
+
this.consumables = /* @__PURE__ */ new Set();
|
|
39
46
|
this.subscriptions = /* @__PURE__ */ new Map();
|
|
47
|
+
this.nonConsumables = /* @__PURE__ */ new Set();
|
|
40
48
|
}
|
|
41
|
-
static create = () => {
|
|
42
|
-
return new _AppStoreConfig();
|
|
49
|
+
static create = (options) => {
|
|
50
|
+
return new _AppStoreConfig(options);
|
|
43
51
|
};
|
|
44
52
|
subscription = (plan) => {
|
|
45
53
|
return new Subscription(this, plan);
|
|
46
54
|
};
|
|
55
|
+
consumable = (productId, credit = { credits: 0, expiresIn: "0" }) => {
|
|
56
|
+
this.consumables.add(productId);
|
|
57
|
+
this.products.set(productId, credit);
|
|
58
|
+
return this;
|
|
59
|
+
};
|
|
60
|
+
nonConsumable = (productId) => {
|
|
61
|
+
this.nonConsumables.add(productId);
|
|
62
|
+
this.products.set(productId, null);
|
|
63
|
+
return this;
|
|
64
|
+
};
|
|
65
|
+
getPlan = (productId) => {
|
|
66
|
+
for (const [plan, subscription] of this.subscriptions.entries()) {
|
|
67
|
+
if (subscription.products.has(productId)) return plan;
|
|
68
|
+
}
|
|
69
|
+
throw new Error(`Plan not found for product ${productId}`);
|
|
70
|
+
};
|
|
71
|
+
getMode = (productId) => {
|
|
72
|
+
if (this.consumables.has(productId) || this.nonConsumables.has(productId)) return "payment";
|
|
73
|
+
for (const subscription of this.subscriptions.values()) {
|
|
74
|
+
if (subscription.products.has(productId)) return "subscription";
|
|
75
|
+
}
|
|
76
|
+
throw new Error(`Mode not found for product ${productId}`);
|
|
77
|
+
};
|
|
47
78
|
getCreditAmount = (productId) => {
|
|
48
79
|
const config = this.products.get(productId);
|
|
49
80
|
invariant(config, `Product not found for ${productId}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/app-store/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms, { type StringValue } from 'ms';\n\nexport type CreditConfig = { credits: number; expiresIn: StringValue };\n\nconst addMethod = Symbol('addMethod');\n\nclass Subscription<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n I extends string = never,\n> {\n readonly plan: PL;\n readonly config: AppStoreConfig<NS, PE, PL, PI>;\n readonly products: Map<string, CreditConfig>;\n\n defaultProductId: I | null;\n\n constructor(config: AppStoreConfig<NS, PE, PL, PI>, plan: PL) {\n this.plan = plan;\n this.config = config;\n this.products = new Map();\n this.defaultProductId = null;\n }\n\n product = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ): Subscription<NS, PE, PL, PI | K, I | K> => {\n this.products.set(productId, credit);\n return this as Subscription<NS, PE, PL, PI | K, I | K>;\n };\n\n default = (defaultProductId: I) => {\n this.defaultProductId = defaultProductId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\nexport class AppStoreConfig<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n> {\n private products: Map<string, CreditConfig>;\n private subscriptions: Map<PE, Subscription<NS, PE, PL, PI>>;\n\n get productIds(): string[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (subscription: Subscription<NS, PE, PL, PI>) => {\n subscription.products.forEach((credit, productId) => this.products.set(productId, credit));\n this.subscriptions.set(subscription.plan, subscription);\n return this;\n };\n\n private constructor() {\n this.products = new Map();\n this.subscriptions = new Map();\n }\n\n static create = <NS extends Lowercase<string>, PE extends string>() => {\n return new AppStoreConfig<NS, PE>();\n };\n\n subscription = <K extends PE>(plan: K extends PL ? never : K) => {\n return new Subscription<NS, PE, K | PL, PI>(this, plan);\n };\n\n getCreditAmount = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return config.credits;\n };\n\n private getCreditExpiresIn = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return ms(config.expiresIn);\n };\n\n getCreditExpiresAt = (productId: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,OAAO,QAA8B;AAIrC,IAAM,YAAY,uBAAO,WAAW;AAEpC,IAAM,eAAN,MAME;AAAA,EACS;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA,EAEA,YAAY,QAAwC,MAAU;AAC5D,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,WAAW,oBAAI,IAAI;AACxB,SAAK,mBAAmB;AAAA,EAC1B;AAAA,EAEA,UAAU,CACR,WACA,SAAuB,EAAE,SAAS,GAAG,WAAW,IAAI,MACR;AAC5C,SAAK,SAAS,IAAI,WAAW,MAAM;AACnC,WAAO;AAAA,EACT;AAAA,EAEA,UAAU,CAAC,qBAAwB;AACjC,SAAK,mBAAmB;AACxB,SAAK,OAAO,SAAS,EAAE,IAAa;AACpC,WAAO,KAAK;AAAA,EACd;AACF;
|
|
1
|
+
{"version":3,"sources":["../../src/app-store/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms, { type StringValue } from 'ms';\n\nexport type CreditConfig = { credits: number; expiresIn: StringValue };\n\nconst addMethod = Symbol('addMethod');\n\nclass Subscription<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n I extends string = never,\n> {\n readonly plan: PL;\n readonly config: AppStoreConfig<NS, PE, PL, PI>;\n readonly products: Map<string, CreditConfig>;\n\n defaultProductId: I | null;\n\n constructor(config: AppStoreConfig<NS, PE, PL, PI>, plan: PL) {\n this.plan = plan;\n this.config = config;\n this.products = new Map();\n this.defaultProductId = null;\n }\n\n product = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ): Subscription<NS, PE, PL, PI | K, I | K> => {\n this.products.set(productId, credit);\n return this as Subscription<NS, PE, PL, PI | K, I | K>;\n };\n\n default = (defaultProductId: I) => {\n this.defaultProductId = defaultProductId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options = { appId: `${number}`; bundleId: string };\n\nexport class AppStoreConfig<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n PL extends PE = never,\n PI extends string = never,\n> {\n readonly appId: string;\n readonly bundleId: string;\n\n private products: Map<string, CreditConfig | null>;\n private consumables: Set<string>;\n private subscriptions: Map<PE, Subscription<NS, PE, PL, PI>>;\n private nonConsumables: Set<string>;\n\n get productIds(): string[] {\n return Array.from(this.products.keys());\n }\n\n [addMethod] = (subscription: Subscription<NS, PE, PL, PI>) => {\n subscription.products.forEach((credit, productId) => this.products.set(productId, credit));\n this.subscriptions.set(subscription.plan, subscription);\n return this;\n };\n\n private constructor(options: Options) {\n this.appId = options.appId;\n this.bundleId = options.bundleId;\n this.products = new Map();\n this.consumables = new Set();\n this.subscriptions = new Map();\n this.nonConsumables = new Set();\n }\n\n static create = <NS extends Lowercase<string>, PE extends string>(options: Options) => {\n return new AppStoreConfig<NS, PE>(options);\n };\n\n subscription = <K extends PE>(plan: K extends PL ? never : K) => {\n return new Subscription<NS, PE, K | PL, PI>(this, plan);\n };\n\n consumable = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n credit: CreditConfig = { credits: 0, expiresIn: '0' }\n ) => {\n this.consumables.add(productId);\n this.products.set(productId, credit);\n return this as AppStoreConfig<NS, PE, PL, PI | K>;\n };\n\n nonConsumable = <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K) => {\n this.nonConsumables.add(productId);\n this.products.set(productId, null);\n return this as AppStoreConfig<NS, PE, PL, PI | K>;\n };\n\n getPlan = (productId: string): PE => {\n for (const [plan, subscription] of this.subscriptions.entries()) {\n if (subscription.products.has(productId)) return plan;\n }\n throw new Error(`Plan not found for product ${productId}`);\n };\n\n getMode = (productId: string): 'payment' | 'subscription' => {\n if (this.consumables.has(productId) || this.nonConsumables.has(productId)) return 'payment';\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return 'subscription';\n }\n throw new Error(`Mode not found for product ${productId}`);\n };\n\n getCreditAmount = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return config.credits;\n };\n\n private getCreditExpiresIn = (productId: string): number => {\n const config = this.products.get(productId);\n invariant(config, `Product not found for ${productId}`);\n return ms(config.expiresIn);\n };\n\n getCreditExpiresAt = (productId: string): string => {\n const expiresIn = this.getCreditExpiresIn(productId);\n return new Date(Date.now() + expiresIn).toISOString();\n };\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,OAAO,QAA8B;AAIrC,IAAM,YAAY,uBAAO,WAAW;AAEpC,IAAM,eAAN,MAME;AAAA,EACS;AAAA,EACA;AAAA,EACA;AAAA,EAET;AAAA,EAEA,YAAY,QAAwC,MAAU;AAC5D,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,WAAW,oBAAI,IAAI;AACxB,SAAK,mBAAmB;AAAA,EAC1B;AAAA,EAEA,UAAU,CACR,WACA,SAAuB,E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