@shware/purchase 7.6.0 → 8.0.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.
- package/README.md +25 -14
- package/dist/app-store/config.cjs +24 -3
- package/dist/app-store/config.cjs.map +1 -1
- package/dist/app-store/config.d.cts +14 -2
- package/dist/app-store/config.d.cts.map +1 -1
- package/dist/app-store/config.d.mts +14 -2
- package/dist/app-store/config.d.mts.map +1 -1
- package/dist/app-store/config.mjs +24 -4
- package/dist/app-store/config.mjs.map +1 -1
- package/dist/app-store/index.cjs +1 -0
- package/dist/app-store/index.d.cts +2 -2
- package/dist/app-store/index.d.mts +2 -2
- package/dist/app-store/index.mjs +2 -2
- package/dist/catalog.cjs +22 -0
- package/dist/catalog.cjs.map +1 -0
- package/dist/catalog.d.cts +21 -0
- package/dist/catalog.d.cts.map +1 -0
- package/dist/catalog.d.mts +21 -0
- package/dist/catalog.d.mts.map +1 -0
- package/dist/catalog.mjs +19 -0
- package/dist/catalog.mjs.map +1 -0
- package/dist/google-play/config.cjs +23 -3
- package/dist/google-play/config.cjs.map +1 -1
- package/dist/google-play/config.d.cts +14 -2
- package/dist/google-play/config.d.cts.map +1 -1
- package/dist/google-play/config.d.mts +14 -2
- package/dist/google-play/config.d.mts.map +1 -1
- package/dist/google-play/config.mjs +23 -4
- package/dist/google-play/config.mjs.map +1 -1
- package/dist/google-play/index.cjs +1 -0
- package/dist/google-play/index.d.cts +2 -2
- package/dist/google-play/index.d.mts +2 -2
- package/dist/google-play/index.mjs +2 -2
- package/dist/index.cjs +3 -0
- package/dist/index.d.cts +4 -4
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +4 -4
- package/dist/stripe/config.cjs +41 -4
- package/dist/stripe/config.cjs.map +1 -1
- package/dist/stripe/config.d.cts +30 -2
- package/dist/stripe/config.d.cts.map +1 -1
- package/dist/stripe/config.d.mts +30 -2
- package/dist/stripe/config.d.mts.map +1 -1
- package/dist/stripe/config.mjs +41 -5
- package/dist/stripe/config.mjs.map +1 -1
- package/dist/stripe/index.cjs +5 -0
- package/dist/stripe/index.d.cts +3 -2
- package/dist/stripe/index.d.mts +3 -2
- package/dist/stripe/index.mjs +3 -2
- package/dist/stripe/plan-change.cjs +400 -0
- package/dist/stripe/plan-change.cjs.map +1 -0
- package/dist/stripe/plan-change.d.cts +142 -0
- package/dist/stripe/plan-change.d.cts.map +1 -0
- package/dist/stripe/plan-change.d.mts +142 -0
- package/dist/stripe/plan-change.d.mts.map +1 -0
- package/dist/stripe/plan-change.mjs +397 -0
- package/dist/stripe/plan-change.mjs.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -63,19 +63,24 @@ const entitled = (AVAILABLE_STATUSES as readonly SubscriptionStatus[]).includes(
|
|
|
63
63
|
|
|
64
64
|
## Product catalogs
|
|
65
65
|
|
|
66
|
-
Each config is built with a fluent, type-checked chain.
|
|
67
|
-
namespace
|
|
68
|
-
|
|
66
|
+
Each config is built with a fluent, type-checked chain. Every config takes the product id
|
|
67
|
+
`namespace`, your `plans` (lowest tier first) and your `billingPeriods` (shortest first), and infers
|
|
68
|
+
its types from them, so no type arguments are needed; a config with no subscriptions passes empty
|
|
69
|
+
arrays. Product ids must start with the namespace, plans and periods must be declared ones, and
|
|
70
|
+
duplicate products or plan/period pairs fail at compile time and at runtime. Credit metadata
|
|
69
71
|
is `{ credits: number; expiresIn: string }` where `expiresIn` is an
|
|
70
72
|
[`ms`](https://github.com/vercel/ms) duration.
|
|
71
73
|
|
|
72
74
|
```ts
|
|
73
|
-
import {
|
|
75
|
+
import { defineAppStoreConfig, defineGooglePlayConfig, defineStripeConfig } from '@shware/purchase';
|
|
74
76
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
const PLANS = ['STARTER', 'PRO'] as const;
|
|
78
|
+
const BILLING_PERIODS = ['MONTHLY', 'YEARLY'] as const;
|
|
77
79
|
|
|
78
|
-
export const stripe =
|
|
80
|
+
export const stripe = defineStripeConfig({
|
|
81
|
+
namespace: 'com.example.stripe',
|
|
82
|
+
plans: PLANS,
|
|
83
|
+
billingPeriods: BILLING_PERIODS,
|
|
79
84
|
mode: env.STRIPE_SECRET_KEY.startsWith('sk_test_') ? 'test' : 'live',
|
|
80
85
|
allowPromotionCodes: false,
|
|
81
86
|
cancellationCouponId: 'retention_30pct_3mo',
|
|
@@ -93,7 +98,10 @@ export const stripe = StripeConfig.create<'com.example.stripe', Plan, BillingPer
|
|
|
93
98
|
.price('price_live_starter_v1', { credits: 100, expiresIn: '30 days' }) // grandfathered
|
|
94
99
|
.default('price_live_starter_v2', { test: 'price_test_starter' });
|
|
95
100
|
|
|
96
|
-
export const appStore =
|
|
101
|
+
export const appStore = defineAppStoreConfig({
|
|
102
|
+
namespace: 'com.example.appstore',
|
|
103
|
+
plans: PLANS,
|
|
104
|
+
billingPeriods: BILLING_PERIODS,
|
|
97
105
|
appId: '1234567890',
|
|
98
106
|
bundleId: 'com.example.app',
|
|
99
107
|
})
|
|
@@ -102,7 +110,10 @@ export const appStore = AppStoreConfig.create<'com.example.appstore', Plan, Bill
|
|
|
102
110
|
.product('com.example.appstore.sub.monthly.starter.v1', { credits: 100, expiresIn: '30d' })
|
|
103
111
|
.default('com.example.appstore.sub.monthly.starter.v1');
|
|
104
112
|
|
|
105
|
-
export const googlePlay =
|
|
113
|
+
export const googlePlay = defineGooglePlayConfig({
|
|
114
|
+
namespace: 'com.example.googleplay',
|
|
115
|
+
plans: PLANS,
|
|
116
|
+
billingPeriods: BILLING_PERIODS,
|
|
106
117
|
package: 'com.example.app',
|
|
107
118
|
audience: 'api.example.com', // expected audience of the RTDN push OIDC token
|
|
108
119
|
})
|
|
@@ -115,11 +126,11 @@ export const googlePlay = GooglePlayConfig.create<'com.example.googleplay', Plan
|
|
|
115
126
|
|
|
116
127
|
Lookups:
|
|
117
128
|
|
|
118
|
-
| Config | Methods
|
|
119
|
-
| ------------------ |
|
|
120
|
-
| `StripeConfig` | `productIds`, `getPriceId(productId)`, `getPlan`, `getBillingPeriod`, `getMode`, `getCreditAmount(productId, priceId?)`, `getCreditExpiresAt(productId, priceId?)`
|
|
121
|
-
| `AppStoreConfig` | `productIds`, `getDefaultProductId(plan, billingPeriod)`, `getPlan`, `getBillingPeriod`, `getMode`, `getCreditAmount`, `getCreditExpiresAt`
|
|
122
|
-
| `GooglePlayConfig` | `productIds`, `getDefaultPlanIds(productId)`, `getPlan(productId, planId)`, `getBillingPeriod(productId, planId)`, `getMode`, `getCreditAmount(productId, planId?)`, `getCreditExpiresAt(productId, planId?)`
|
|
129
|
+
| Config | Methods |
|
|
130
|
+
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
131
|
+
| `StripeConfig` | `productIds`, `getPriceId(productId)`, `getProductId(priceId)`, `getPlan`, `getBillingPeriod`, `getMode`, `isSubscription`, `comparePlans(from, to)`, `getCreditAmount(productId, priceId?)`, `getCreditExpiresAt(productId, priceId?)` |
|
|
132
|
+
| `AppStoreConfig` | `productIds`, `getDefaultProductId(plan, billingPeriod)`, `getPlan`, `getBillingPeriod`, `getMode`, `getCreditAmount`, `getCreditExpiresAt` |
|
|
133
|
+
| `GooglePlayConfig` | `productIds`, `getDefaultPlanIds(productId)`, `getPlan(productId, planId)`, `getBillingPeriod(productId, planId)`, `getMode`, `getCreditAmount(productId, planId?)`, `getCreditExpiresAt(productId, planId?)` |
|
|
123
134
|
|
|
124
135
|
In Stripe test mode the `test` twin of the default price is what `getPriceId` returns, and it
|
|
125
136
|
resolves to the default price's credits. Non-default prices, products and base plans stay declared
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
|
|
3
|
+
const require_catalog = require("../catalog.cjs");
|
|
3
4
|
let _shware_utils = require("@shware/utils");
|
|
4
5
|
let ms = require("ms");
|
|
5
6
|
ms = require_runtime.__toESM(ms, 1);
|
|
6
7
|
//#region src/app-store/config.ts
|
|
7
8
|
const addMethod = Symbol("addMethod");
|
|
9
|
+
/** The private constructor's one door, for this module's `define` function only. */
|
|
10
|
+
const construct = Symbol("construct");
|
|
8
11
|
var Subscription = class {
|
|
9
12
|
plan;
|
|
10
13
|
billingPeriod;
|
|
@@ -27,6 +30,7 @@ var Subscription = class {
|
|
|
27
30
|
credits: 0,
|
|
28
31
|
expiresIn: "0"
|
|
29
32
|
}) => {
|
|
33
|
+
require_catalog.assertInNamespace(this.config.namespace, productId);
|
|
30
34
|
(0, _shware_utils.invariant)(!this.products.has(productId), `Duplicate product ${productId}`);
|
|
31
35
|
this.products.set(productId, metadata);
|
|
32
36
|
return this;
|
|
@@ -41,6 +45,9 @@ var Subscription = class {
|
|
|
41
45
|
var AppStoreConfig = class AppStoreConfig {
|
|
42
46
|
appId;
|
|
43
47
|
bundleId;
|
|
48
|
+
namespace;
|
|
49
|
+
plans;
|
|
50
|
+
billingPeriods;
|
|
44
51
|
products;
|
|
45
52
|
consumables;
|
|
46
53
|
subscriptions;
|
|
@@ -59,6 +66,10 @@ var AppStoreConfig = class AppStoreConfig {
|
|
|
59
66
|
return this;
|
|
60
67
|
};
|
|
61
68
|
constructor(options) {
|
|
69
|
+
require_catalog.assertCatalog(options);
|
|
70
|
+
this.namespace = options.namespace;
|
|
71
|
+
this.plans = options.plans;
|
|
72
|
+
this.billingPeriods = options.billingPeriods;
|
|
62
73
|
this.appId = options.appId;
|
|
63
74
|
this.bundleId = options.bundleId;
|
|
64
75
|
this.products = /* @__PURE__ */ new Map();
|
|
@@ -66,23 +77,24 @@ var AppStoreConfig = class AppStoreConfig {
|
|
|
66
77
|
this.subscriptions = /* @__PURE__ */ new Map();
|
|
67
78
|
this.nonConsumables = /* @__PURE__ */ new Set();
|
|
68
79
|
}
|
|
69
|
-
static
|
|
70
|
-
return new AppStoreConfig(options);
|
|
71
|
-
};
|
|
80
|
+
static [construct] = (options) => new AppStoreConfig(options);
|
|
72
81
|
/** The same plan may be declared once per billing period; duplicate pairs are rejected. */
|
|
73
82
|
subscription = (plan, billingPeriod) => {
|
|
83
|
+
require_catalog.assertDeclared(this, plan, billingPeriod);
|
|
74
84
|
return new Subscription(this, plan, billingPeriod);
|
|
75
85
|
};
|
|
76
86
|
consumable = (productId, metadata = {
|
|
77
87
|
credits: 0,
|
|
78
88
|
expiresIn: "0"
|
|
79
89
|
}) => {
|
|
90
|
+
require_catalog.assertInNamespace(this.namespace, productId);
|
|
80
91
|
(0, _shware_utils.invariant)(!this.products.has(productId), `Duplicate product ${productId}`);
|
|
81
92
|
this.consumables.add(productId);
|
|
82
93
|
this.products.set(productId, metadata);
|
|
83
94
|
return this;
|
|
84
95
|
};
|
|
85
96
|
nonConsumable = (productId) => {
|
|
97
|
+
require_catalog.assertInNamespace(this.namespace, productId);
|
|
86
98
|
(0, _shware_utils.invariant)(!this.products.has(productId), `Duplicate product ${productId}`);
|
|
87
99
|
this.nonConsumables.add(productId);
|
|
88
100
|
this.products.set(productId, null);
|
|
@@ -124,7 +136,16 @@ var AppStoreConfig = class AppStoreConfig {
|
|
|
124
136
|
return new Date(Date.now() + expiresIn).toISOString();
|
|
125
137
|
};
|
|
126
138
|
};
|
|
139
|
+
/**
|
|
140
|
+
* Declare the AppStore product catalog. Every type parameter is inferred from the options:
|
|
141
|
+
* the namespace from `namespace`, the plans and billing periods from `plans` and `billingPeriods`
|
|
142
|
+
* (`const`, so a literal array needs no `as const`). Products are then added with the fluent chain.
|
|
143
|
+
*/
|
|
144
|
+
function defineAppStoreConfig(options) {
|
|
145
|
+
return AppStoreConfig[construct](options);
|
|
146
|
+
}
|
|
127
147
|
//#endregion
|
|
128
148
|
exports.AppStoreConfig = AppStoreConfig;
|
|
149
|
+
exports.defineAppStoreConfig = defineAppStoreConfig;
|
|
129
150
|
|
|
130
151
|
//# sourceMappingURL=config.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.cjs","names":[],"sources":["../../src/app-store/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms from 'ms';\nimport type { Metadata } from '../types';\n\nconst addMethod = Symbol('addMethod');\n\nclass Subscription<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n BP extends string = string,\n PL extends string = never,\n PI extends string = never,\n I extends string = never,\n> {\n readonly plan: PE;\n readonly billingPeriod: BP;\n readonly config: AppStoreConfig<NS, PE, BP, PL, PI>;\n readonly products: Map<string, Metadata>;\n\n defaultProductId: I | null;\n\n constructor(config: AppStoreConfig<NS, PE, BP, PL, PI>, plan: PE, billingPeriod: BP) {\n this.plan = plan;\n this.billingPeriod = billingPeriod;\n this.config = config;\n this.products = new Map();\n this.defaultProductId = null;\n }\n\n /**\n * Non-default products stay declared on purpose: they keep resolving for\n * grandfathered subscribers and serve as price-experiment arms. Never\n * sell a legacy product to new users unless that is intentional.\n */\n product = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ): Subscription<NS, PE, BP, PL, PI | K, I | K> => {\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.products.set(productId, metadata);\n return this as Subscription<NS, PE, BP, PL, PI | K, I | K>;\n };\n\n default = (defaultProductId: I) => {\n invariant(\n this.products.has(defaultProductId),\n `Default product ${defaultProductId} is not declared`\n );\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 BP extends string = string,\n PL extends string = never,\n PI extends string = never,\n> {\n readonly appId: string;\n readonly bundleId: string;\n\n private readonly products: Map<string, Metadata | null>;\n private readonly consumables: Set<string>;\n private readonly subscriptions: Map<string, Subscription<NS, PE, BP, PL, PI>>;\n private readonly nonConsumables: Set<string>;\n\n get productIds(): string[] {\n return Array.from(this.products.keys());\n }\n\n // runtime mirror of the type-level guards: generic accumulation only\n // protects a single fluent chain, not statement-style re-entry or casts\n [addMethod] = (subscription: Subscription<NS, PE, BP, PL, PI>) => {\n const key = `${subscription.plan}:${subscription.billingPeriod}`;\n invariant(!this.subscriptions.has(key), `Duplicate plan ${key}`);\n subscription.products.forEach((metadata, productId) => {\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.products.set(productId, metadata);\n });\n this.subscriptions.set(key, 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, BP extends string = string>(\n options: Options\n ) => {\n return new AppStoreConfig<NS, PE, BP>(options);\n };\n\n /** The same plan may be declared once per billing period; duplicate pairs are rejected. */\n subscription = <K extends PE, P extends BP>(\n plan: K,\n billingPeriod: `${K}:${P}` extends PL ? never : P\n ) => {\n // oxlint-disable-next-line typescript/no-unnecessary-type-assertion -- tsc requires the cast (tsgolint false positive)\n return new Subscription<NS, PE, BP, `${K}:${P}` | PL, PI>(this as never, plan, billingPeriod);\n };\n\n consumable = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ) => {\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.consumables.add(productId);\n this.products.set(productId, metadata);\n return this as AppStoreConfig<NS, PE, BP, PL, PI | K>;\n };\n\n nonConsumable = <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K) => {\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.nonConsumables.add(productId);\n this.products.set(productId, null);\n return this as AppStoreConfig<NS, PE, BP, PL, PI | K>;\n };\n\n /** Product id that new subscribers should purchase for the plan and billing period. */\n getDefaultProductId = (plan: PE, billingPeriod: BP): string => {\n const subscription = this.subscriptions.get(`${plan}:${billingPeriod}`);\n invariant(subscription, `Subscription not found for ${plan}:${billingPeriod}`);\n invariant(\n subscription.defaultProductId,\n `Default product not found for ${plan}:${billingPeriod}`\n );\n return subscription.defaultProductId;\n };\n\n getPlan = (productId: string): PE => {\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return subscription.plan;\n }\n throw new Error(`Plan not found for product ${productId}`);\n };\n\n getBillingPeriod = (productId: string): BP => {\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return subscription.billingPeriod;\n }\n throw new Error(`Billing period 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 !== undefined, `Product not found for ${productId}`);\n invariant(config !== null, `Credits not available for non-consumable product ${productId}`);\n return config.credits;\n };\n\n private readonly 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":";;;;;;AAIA,MAAM,YAAY,OAAO,WAAW;AAEpC,IAAM,eAAN,MAOE;CACA;CACA;CACA;CACA;CAEA;CAEA,YAAY,QAA4C,MAAU,eAAmB;EACnF,KAAK,OAAO;EACZ,KAAK,gBAAgB;EACrB,KAAK,SAAS;EACd,KAAK,2BAAW,IAAI,IAAI;EACxB,KAAK,mBAAmB;CAC1B;;;;;;CAOA,WACE,WACA,WAAqB;EAAE,SAAS;EAAG,WAAW;CAAI,MACF;EAChD,CAAA,GAAA,cAAA,UAAA,CAAU,CAAC,KAAK,SAAS,IAAI,SAAS,GAAG,qBAAqB,WAAW;EACzE,KAAK,SAAS,IAAI,W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yB,WAAW;EACpE,CAAA,GAAA,cAAA,UAAA,CAAU,WAAW,MAAM,oDAAoD,WAAW;EAC1F,OAAO,OAAO;CAChB;CAEA,sBAAuC,cAA8B;EACnE,MAAM,SAAS,KAAK,SAAS,IAAI,SAAS;EAC1C,CAAA,GAAA,cAAA,UAAA,CAAU,QAAQ,yBAAyB,WAAW;EACtD,QAAA,GAAO,GAAA,QAAA,CAAG,OAAO,SAAS;CAC5B;CAEA,sBAAsB,cAA8B;EAClD,MAAM,YAAY,KAAK,mBAAmB,SAAS;EACnD,OAAO,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,CAAC,CAAC,YAAY;CACtD;AACF"}
|
|
1
|
+
{"version":3,"file":"config.cjs","names":[],"sources":["../../src/app-store/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms from 'ms';\nimport { type Catalog, assertCatalog, assertDeclared, assertInNamespace } from '../catalog';\nimport type { Metadata } from '../types';\n\nconst addMethod = Symbol('addMethod');\n/** The private constructor's one door, for this module's `define` function only. */\nconst construct = Symbol('construct');\n\nclass Subscription<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n BP extends string = string,\n PL extends string = never,\n PI extends string = never,\n I extends string = never,\n> {\n readonly plan: PE;\n readonly billingPeriod: BP;\n readonly config: AppStoreConfig<NS, PE, BP, PL, PI>;\n readonly products: Map<string, Metadata>;\n\n defaultProductId: I | null;\n\n constructor(config: AppStoreConfig<NS, PE, BP, PL, PI>, plan: PE, billingPeriod: BP) {\n this.plan = plan;\n this.billingPeriod = billingPeriod;\n this.config = config;\n this.products = new Map();\n this.defaultProductId = null;\n }\n\n /**\n * Non-default products stay declared on purpose: they keep resolving for\n * grandfathered subscribers and serve as price-experiment arms. Never\n * sell a legacy product to new users unless that is intentional.\n */\n product = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ): Subscription<NS, PE, BP, PL, PI | K, I | K> => {\n assertInNamespace(this.config.namespace, productId);\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.products.set(productId, metadata);\n return this as Subscription<NS, PE, BP, PL, PI | K, I | K>;\n };\n\n default = (defaultProductId: I) => {\n invariant(\n this.products.has(defaultProductId),\n `Default product ${defaultProductId} is not declared`\n );\n this.defaultProductId = defaultProductId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options<NS extends Lowercase<string>, PE extends string, BP extends string> = Catalog<\n NS,\n PE,\n BP\n> & { appId: `${number}`; bundleId: string };\n\nexport class AppStoreConfig<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n BP extends string = string,\n PL extends string = never,\n PI extends string = never,\n> {\n readonly appId: string;\n readonly bundleId: string;\n readonly namespace: NS;\n readonly plans: readonly PE[];\n readonly billingPeriods: readonly BP[];\n\n private readonly products: Map<string, Metadata | null>;\n private readonly consumables: Set<string>;\n private readonly subscriptions: Map<string, Subscription<NS, PE, BP, PL, PI>>;\n private readonly nonConsumables: Set<string>;\n\n get productIds(): string[] {\n return Array.from(this.products.keys());\n }\n\n // runtime mirror of the type-level guards: generic accumulation only\n // protects a single fluent chain, not statement-style re-entry or casts\n [addMethod] = (subscription: Subscription<NS, PE, BP, PL, PI>) => {\n const key = `${subscription.plan}:${subscription.billingPeriod}`;\n invariant(!this.subscriptions.has(key), `Duplicate plan ${key}`);\n subscription.products.forEach((metadata, productId) => {\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.products.set(productId, metadata);\n });\n this.subscriptions.set(key, subscription);\n return this;\n };\n\n private constructor(options: Options<NS, PE, BP>) {\n assertCatalog(options);\n this.namespace = options.namespace;\n this.plans = options.plans;\n this.billingPeriods = options.billingPeriods;\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 [construct] = <NS extends Lowercase<string>, PE extends string, BP extends string>(\n options: Options<NS, PE, BP>\n ) => new AppStoreConfig<NS, PE, BP>(options);\n\n /** The same plan may be declared once per billing period; duplicate pairs are rejected. */\n subscription = <K extends PE, P extends BP>(\n plan: K,\n billingPeriod: `${K}:${P}` extends PL ? never : P\n ) => {\n assertDeclared(this, plan, billingPeriod);\n // oxlint-disable-next-line typescript/no-unnecessary-type-assertion -- tsc requires the cast (tsgolint false positive)\n return new Subscription<NS, PE, BP, `${K}:${P}` | PL, PI>(this as never, plan, billingPeriod);\n };\n\n consumable = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ) => {\n assertInNamespace(this.namespace, productId);\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.consumables.add(productId);\n this.products.set(productId, metadata);\n return this as AppStoreConfig<NS, PE, BP, PL, PI | K>;\n };\n\n nonConsumable = <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K) => {\n assertInNamespace(this.namespace, productId);\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.nonConsumables.add(productId);\n this.products.set(productId, null);\n return this as AppStoreConfig<NS, PE, BP, PL, PI | K>;\n };\n\n /** Product id that new subscribers should purchase for the plan and billing period. */\n getDefaultProductId = (plan: PE, billingPeriod: BP): string => {\n const subscription = this.subscriptions.get(`${plan}:${billingPeriod}`);\n invariant(subscription, `Subscription not found for ${plan}:${billingPeriod}`);\n invariant(\n subscription.defaultProductId,\n `Default product not found for ${plan}:${billingPeriod}`\n );\n return subscription.defaultProductId;\n };\n\n getPlan = (productId: string): PE => {\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return subscription.plan;\n }\n throw new Error(`Plan not found for product ${productId}`);\n };\n\n getBillingPeriod = (productId: string): BP => {\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return subscription.billingPeriod;\n }\n throw new Error(`Billing period 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 !== undefined, `Product not found for ${productId}`);\n invariant(config !== null, `Credits not available for non-consumable product ${productId}`);\n return config.credits;\n };\n\n private readonly 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\n/**\n * Declare the AppStore product catalog. Every type parameter is inferred from the options:\n * the namespace from `namespace`, the plans and billing periods from `plans` and `billingPeriods`\n * (`const`, so a literal array needs no `as const`). Products are then added with the fluent chain.\n */\nexport function defineAppStoreConfig<\n const NS extends Lowercase<string>,\n const PE extends string,\n const BP extends string,\n>(options: Options<NS, PE, BP>): AppStoreConfig<NS, PE, BP> {\n return AppStoreConfig[construct](options);\n}\n"],"mappings":";;;;;;;AAKA,MAAM,YAAY,OAAO,WAAW;;AAEpC,MAAM,YAAY,OAAO,WAAW;AAEpC,IAAM,eAAN,MAOE;CACA;CACA;CACA;CACA;CAEA;CAEA,YAAY,QAA4C,MAAU,eAAmB;EACnF,KAAK,OAAO;EACZ,KAAK,gBAAgB;EACrB,KAAK,SAAS;EACd,KAAK,2BAAW,IAAI,IAAI;EACxB,KAAK,mBAAmB;CAC1B;;;;;;CAOA,WACE,WACA,WAAqB;EAAE,SAAS;EAAG,WAAW;CAAI,MACF;EAChD,gBAAA,kBAAkB,KAAK,OAAO,WAAW,SAAS;EAClD,CAAA,GAAA,cAAA,UAAA,CAAU,CAAC,KAAK,SAAS,IAAI,SAAS,GAAG,qBAAqB,WAAW;EACzE,KAAK,SAAS,IAAI,WAAW,QAAQ;EACrC,OAAO;CACT;CAEA,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AS,GAAG,OAAO;EAEnD,MAAM,IAAI,MAAM,8BAA8B,WAAW;CAC3D;CAEA,mBAAmB,cAA8B;EAC/C,MAAM,SAAS,KAAK,SAAS,IAAI,SAAS;EAC1C,CAAA,GAAA,cAAA,UAAA,CAAU,WAAW,KAAA,GAAW,yBAAyB,WAAW;EACpE,CAAA,GAAA,cAAA,UAAA,CAAU,WAAW,MAAM,oDAAoD,WAAW;EAC1F,OAAO,OAAO;CAChB;CAEA,sBAAuC,cAA8B;EACnE,MAAM,SAAS,KAAK,SAAS,IAAI,SAAS;EAC1C,CAAA,GAAA,cAAA,UAAA,CAAU,QAAQ,yBAAyB,WAAW;EACtD,QAAA,GAAO,GAAA,QAAA,CAAG,OAAO,SAAS;CAC5B;CAEA,sBAAsB,cAA8B;EAClD,MAAM,YAAY,KAAK,mBAAmB,SAAS;EACnD,OAAO,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,CAAC,CAAC,YAAY;CACtD;AACF;;;;;;AAOA,SAAgB,qBAId,SAA0D;CAC1D,OAAO,eAAe,UAAU,CAAC,OAAO;AAC1C"}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { Catalog } from "../catalog.cjs";
|
|
1
2
|
import { Metadata } from "../types.cjs";
|
|
2
3
|
//#region src/app-store/config.d.ts
|
|
3
4
|
declare const addMethod: unique symbol;
|
|
5
|
+
/** The private constructor's one door, for this module's `define` function only. */
|
|
6
|
+
declare const construct: unique symbol;
|
|
4
7
|
declare class Subscription<NS extends Lowercase<string> = Lowercase<string>, PE extends string = string, BP extends string = string, PL extends string = never, PI extends string = never, I extends string = never> {
|
|
5
8
|
readonly plan: PE;
|
|
6
9
|
readonly billingPeriod: BP;
|
|
@@ -16,13 +19,16 @@ declare class Subscription<NS extends Lowercase<string> = Lowercase<string>, PE
|
|
|
16
19
|
product: <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K, metadata?: Metadata) => Subscription<NS, PE, BP, PL, PI | K, I | K>;
|
|
17
20
|
default: (defaultProductId: I) => AppStoreConfig<NS, PE, BP, PL, PI>;
|
|
18
21
|
}
|
|
19
|
-
type Options = {
|
|
22
|
+
type Options<NS extends Lowercase<string>, PE extends string, BP extends string> = Catalog<NS, PE, BP> & {
|
|
20
23
|
appId: `${number}`;
|
|
21
24
|
bundleId: string;
|
|
22
25
|
};
|
|
23
26
|
export declare class AppStoreConfig<NS extends Lowercase<string> = Lowercase<string>, PE extends string = string, BP extends string = string, PL extends string = never, PI extends string = never> {
|
|
24
27
|
readonly appId: string;
|
|
25
28
|
readonly bundleId: string;
|
|
29
|
+
readonly namespace: NS;
|
|
30
|
+
readonly plans: readonly PE[];
|
|
31
|
+
readonly billingPeriods: readonly BP[];
|
|
26
32
|
private readonly products;
|
|
27
33
|
private readonly consumables;
|
|
28
34
|
private readonly subscriptions;
|
|
@@ -30,7 +36,7 @@ export declare class AppStoreConfig<NS extends Lowercase<string> = Lowercase<str
|
|
|
30
36
|
get productIds(): string[];
|
|
31
37
|
[addMethod]: (subscription: Subscription<NS, PE, BP, PL, PI>) => this;
|
|
32
38
|
private constructor();
|
|
33
|
-
static
|
|
39
|
+
static [construct]: <NS_1 extends Lowercase<string>, PE_1 extends string, BP_1 extends string>(options: Options<NS_1, PE_1, BP_1>) => AppStoreConfig<NS_1, PE_1, BP_1, never, never>;
|
|
34
40
|
/** The same plan may be declared once per billing period; duplicate pairs are rejected. */
|
|
35
41
|
subscription: <K extends PE, P extends BP>(plan: K, billingPeriod: `${K}:${P}` extends PL ? never : P) => Subscription<NS, PE, BP, PL | `${K}:${P}`, PI, never>;
|
|
36
42
|
consumable: <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K, metadata?: Metadata) => AppStoreConfig<NS, PE, BP, PL, PI | K>;
|
|
@@ -44,6 +50,12 @@ export declare class AppStoreConfig<NS extends Lowercase<string> = Lowercase<str
|
|
|
44
50
|
private readonly getCreditExpiresIn;
|
|
45
51
|
getCreditExpiresAt: (productId: string) => string;
|
|
46
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Declare the AppStore product catalog. Every type parameter is inferred from the options:
|
|
55
|
+
* the namespace from `namespace`, the plans and billing periods from `plans` and `billingPeriods`
|
|
56
|
+
* (`const`, so a literal array needs no `as const`). Products are then added with the fluent chain.
|
|
57
|
+
*/
|
|
58
|
+
export declare function defineAppStoreConfig<const NS extends Lowercase<string>, const PE extends string, const BP extends string>(options: Options<NS, PE, BP>): AppStoreConfig<NS, PE, BP>;
|
|
47
59
|
//#endregion
|
|
48
60
|
export {};
|
|
49
61
|
//# sourceMappingURL=config.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.cts","names":[],"sources":["../../src/app-store/config.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"config.d.cts","names":[],"sources":["../../src/app-store/config.ts"],"mappings":";;;cAKM;;cAEA;cAEA,aACJ,WAAW,oBAAoB,mBAC/B,4BACA,4BACA,2BACA,2BACA;WAES,MAAM;WACN,eAAe;WACf,QAAQ,eAAe,IAAI,IAAI,IAAI,IAAI;WACvC,UAAU,YAAY;EAE/B,kBAAkB;EAElB,YAAY,QAAQ,eAAe,IAAI,IAAI,IAAI,IAAI,KAAK,MAAM,IAAI,eAAe;;;;;;EAajF,UAAW,aAAa,MAAM,qBAAmB,WACpC,UAAU,aAAa,GAAC,WACzB,aACT,aAAa,IAAI,IAAI,IAAI,IAAI,KAAK,GAAG,IAAI;EAO5C,UAAO,kBAAsB,MAAC,eAAA,IAAA,IAAA,IAAA,IAAA;;KAW3B,QAAQ,WAAW,mBAAmB,mBAAmB,qBAAqB,QACjF,IACA,IACA;EACI;EAAoB;;qBAEb,eACX,WAAW,oBAAoB,mBAC/B,4BACA,4BACA,2BACA;WAES;WACA;WACA,WAAW;WACX,gBAAgB;WAChB,yBAAyB;mBAEjB;mBACA;mBACA;mBACA;MAEb;GAMH,aAAU,cAAkB,aAAa,IAAI,IAAI,IAAI,IAAI;UAWnD;UAaC,aAAc,aAAW,mBAAmB,qBAAmB,qBAAiB,SAC7E,QAAQ,MAAI,MAAI,UAAG,eAAA,MAAA,MAAA;;EAI9B,eAAgB,UAAU,IAAI,UAAU,IAAE,MAClC,GAAC,kBACW,KAAK,YAAY,aAAa,MAAC,aAAA,IAAA,IAAA,IAAA,QAAA,KAAA,KAAA;EAOnD,aAAc,aAAa,MAAM,qBAAmB,WACvC,UAAU,aAAa,GAAC,WACzB,aAMK,eAAe,IAAI,IAAI,IAAI,IAAI,KAAK;EAGrD,gBAAiB,aAAa,MAAM,qBAAmB,WAAa,UAAU,aAAa,MAK1E,eAAe,IAAI,IAAI,IAAI,IAAI,KAAK;;EAIrD,sBAAmB,MAAU,IAAE,eAAiB;EAUhD,UAAO,sBAAwB;EAO/B,mBAAgB,sBAAwB;EAOxC,UAAO;EAQP,kBAAe;mBAOE;EAMjB,qBAAkB;;;;;;;wBAWJ,2BACR,WAAW,yBACX,yBACA,mBACN,SAAS,QAAQ,IAAI,IAAI,MAAM,eAAe,IAAI,IAAI"}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { Catalog } from "../catalog.mjs";
|
|
1
2
|
import { Metadata } from "../types.mjs";
|
|
2
3
|
//#region src/app-store/config.d.ts
|
|
3
4
|
declare const addMethod: unique symbol;
|
|
5
|
+
/** The private constructor's one door, for this module's `define` function only. */
|
|
6
|
+
declare const construct: unique symbol;
|
|
4
7
|
declare class Subscription<NS extends Lowercase<string> = Lowercase<string>, PE extends string = string, BP extends string = string, PL extends string = never, PI extends string = never, I extends string = never> {
|
|
5
8
|
readonly plan: PE;
|
|
6
9
|
readonly billingPeriod: BP;
|
|
@@ -16,13 +19,16 @@ declare class Subscription<NS extends Lowercase<string> = Lowercase<string>, PE
|
|
|
16
19
|
product: <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K, metadata?: Metadata) => Subscription<NS, PE, BP, PL, PI | K, I | K>;
|
|
17
20
|
default: (defaultProductId: I) => AppStoreConfig<NS, PE, BP, PL, PI>;
|
|
18
21
|
}
|
|
19
|
-
type Options = {
|
|
22
|
+
type Options<NS extends Lowercase<string>, PE extends string, BP extends string> = Catalog<NS, PE, BP> & {
|
|
20
23
|
appId: `${number}`;
|
|
21
24
|
bundleId: string;
|
|
22
25
|
};
|
|
23
26
|
export declare class AppStoreConfig<NS extends Lowercase<string> = Lowercase<string>, PE extends string = string, BP extends string = string, PL extends string = never, PI extends string = never> {
|
|
24
27
|
readonly appId: string;
|
|
25
28
|
readonly bundleId: string;
|
|
29
|
+
readonly namespace: NS;
|
|
30
|
+
readonly plans: readonly PE[];
|
|
31
|
+
readonly billingPeriods: readonly BP[];
|
|
26
32
|
private readonly products;
|
|
27
33
|
private readonly consumables;
|
|
28
34
|
private readonly subscriptions;
|
|
@@ -30,7 +36,7 @@ export declare class AppStoreConfig<NS extends Lowercase<string> = Lowercase<str
|
|
|
30
36
|
get productIds(): string[];
|
|
31
37
|
[addMethod]: (subscription: Subscription<NS, PE, BP, PL, PI>) => this;
|
|
32
38
|
private constructor();
|
|
33
|
-
static
|
|
39
|
+
static [construct]: <NS_1 extends Lowercase<string>, PE_1 extends string, BP_1 extends string>(options: Options<NS_1, PE_1, BP_1>) => AppStoreConfig<NS_1, PE_1, BP_1, never, never>;
|
|
34
40
|
/** The same plan may be declared once per billing period; duplicate pairs are rejected. */
|
|
35
41
|
subscription: <K extends PE, P extends BP>(plan: K, billingPeriod: `${K}:${P}` extends PL ? never : P) => Subscription<NS, PE, BP, PL | `${K}:${P}`, PI, never>;
|
|
36
42
|
consumable: <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K, metadata?: Metadata) => AppStoreConfig<NS, PE, BP, PL, PI | K>;
|
|
@@ -44,6 +50,12 @@ export declare class AppStoreConfig<NS extends Lowercase<string> = Lowercase<str
|
|
|
44
50
|
private readonly getCreditExpiresIn;
|
|
45
51
|
getCreditExpiresAt: (productId: string) => string;
|
|
46
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Declare the AppStore product catalog. Every type parameter is inferred from the options:
|
|
55
|
+
* the namespace from `namespace`, the plans and billing periods from `plans` and `billingPeriods`
|
|
56
|
+
* (`const`, so a literal array needs no `as const`). Products are then added with the fluent chain.
|
|
57
|
+
*/
|
|
58
|
+
export declare function defineAppStoreConfig<const NS extends Lowercase<string>, const PE extends string, const BP extends string>(options: Options<NS, PE, BP>): AppStoreConfig<NS, PE, BP>;
|
|
47
59
|
//#endregion
|
|
48
60
|
export {};
|
|
49
61
|
//# sourceMappingURL=config.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.mts","names":[],"sources":["../../src/app-store/config.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"config.d.mts","names":[],"sources":["../../src/app-store/config.ts"],"mappings":";;;cAKM;;cAEA;cAEA,aACJ,WAAW,oBAAoB,mBAC/B,4BACA,4BACA,2BACA,2BACA;WAES,MAAM;WACN,eAAe;WACf,QAAQ,eAAe,IAAI,IAAI,IAAI,IAAI;WACvC,UAAU,YAAY;EAE/B,kBAAkB;EAElB,YAAY,QAAQ,eAAe,IAAI,IAAI,IAAI,IAAI,KAAK,MAAM,IAAI,eAAe;;;;;;EAajF,UAAW,aAAa,MAAM,qBAAmB,WACpC,UAAU,aAAa,GAAC,WACzB,aACT,aAAa,IAAI,IAAI,IAAI,IAAI,KAAK,GAAG,IAAI;EAO5C,UAAO,kBAAsB,MAAC,eAAA,IAAA,IAAA,IAAA,IAAA;;KAW3B,QAAQ,WAAW,mBAAmB,mBAAmB,qBAAqB,QACjF,IACA,IACA;EACI;EAAoB;;qBAEb,eACX,WAAW,oBAAoB,mBAC/B,4BACA,4BACA,2BACA;WAES;WACA;WACA,WAAW;WACX,gBAAgB;WAChB,yBAAyB;mBAEjB;mBACA;mBACA;mBACA;MAEb;GAMH,aAAU,cAAkB,aAAa,IAAI,IAAI,IAAI,IAAI;UAWnD;UAaC,aAAc,aAAW,mBAAmB,qBAAmB,qBAAiB,SAC7E,QAAQ,MAAI,MAAI,UAAG,eAAA,MAAA,MAAA;;EAI9B,eAAgB,UAAU,IAAI,UAAU,IAAE,MAClC,GAAC,kBACW,KAAK,YAAY,aAAa,MAAC,aAAA,IAAA,IAAA,IAAA,QAAA,KAAA,KAAA;EAOnD,aAAc,aAAa,MAAM,qBAAmB,WACvC,UAAU,aAAa,GAAC,WACzB,aAMK,eAAe,IAAI,IAAI,IAAI,IAAI,KAAK;EAGrD,gBAAiB,aAAa,MAAM,qBAAmB,WAAa,UAAU,aAAa,MAK1E,eAAe,IAAI,IAAI,IAAI,IAAI,KAAK;;EAIrD,sBAAmB,MAAU,IAAE,eAAiB;EAUhD,UAAO,sBAAwB;EAO/B,mBAAgB,sBAAwB;EAOxC,UAAO;EAQP,kBAAe;mBAOE;EAMjB,qBAAkB;;;;;;;wBAWJ,2BACR,WAAW,yBACX,yBACA,mBACN,SAAS,QAAQ,IAAI,IAAI,MAAM,eAAe,IAAI,IAAI"}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import { assertCatalog, assertDeclared, assertInNamespace } from "../catalog.mjs";
|
|
1
2
|
import { invariant } from "@shware/utils";
|
|
2
3
|
import ms from "ms";
|
|
3
4
|
//#region src/app-store/config.ts
|
|
4
5
|
const addMethod = Symbol("addMethod");
|
|
6
|
+
/** The private constructor's one door, for this module's `define` function only. */
|
|
7
|
+
const construct = Symbol("construct");
|
|
5
8
|
var Subscription = class {
|
|
6
9
|
plan;
|
|
7
10
|
billingPeriod;
|
|
@@ -24,6 +27,7 @@ var Subscription = class {
|
|
|
24
27
|
credits: 0,
|
|
25
28
|
expiresIn: "0"
|
|
26
29
|
}) => {
|
|
30
|
+
assertInNamespace(this.config.namespace, productId);
|
|
27
31
|
invariant(!this.products.has(productId), `Duplicate product ${productId}`);
|
|
28
32
|
this.products.set(productId, metadata);
|
|
29
33
|
return this;
|
|
@@ -38,6 +42,9 @@ var Subscription = class {
|
|
|
38
42
|
var AppStoreConfig = class AppStoreConfig {
|
|
39
43
|
appId;
|
|
40
44
|
bundleId;
|
|
45
|
+
namespace;
|
|
46
|
+
plans;
|
|
47
|
+
billingPeriods;
|
|
41
48
|
products;
|
|
42
49
|
consumables;
|
|
43
50
|
subscriptions;
|
|
@@ -56,6 +63,10 @@ var AppStoreConfig = class AppStoreConfig {
|
|
|
56
63
|
return this;
|
|
57
64
|
};
|
|
58
65
|
constructor(options) {
|
|
66
|
+
assertCatalog(options);
|
|
67
|
+
this.namespace = options.namespace;
|
|
68
|
+
this.plans = options.plans;
|
|
69
|
+
this.billingPeriods = options.billingPeriods;
|
|
59
70
|
this.appId = options.appId;
|
|
60
71
|
this.bundleId = options.bundleId;
|
|
61
72
|
this.products = /* @__PURE__ */ new Map();
|
|
@@ -63,23 +74,24 @@ var AppStoreConfig = class AppStoreConfig {
|
|
|
63
74
|
this.subscriptions = /* @__PURE__ */ new Map();
|
|
64
75
|
this.nonConsumables = /* @__PURE__ */ new Set();
|
|
65
76
|
}
|
|
66
|
-
static
|
|
67
|
-
return new AppStoreConfig(options);
|
|
68
|
-
};
|
|
77
|
+
static [construct] = (options) => new AppStoreConfig(options);
|
|
69
78
|
/** The same plan may be declared once per billing period; duplicate pairs are rejected. */
|
|
70
79
|
subscription = (plan, billingPeriod) => {
|
|
80
|
+
assertDeclared(this, plan, billingPeriod);
|
|
71
81
|
return new Subscription(this, plan, billingPeriod);
|
|
72
82
|
};
|
|
73
83
|
consumable = (productId, metadata = {
|
|
74
84
|
credits: 0,
|
|
75
85
|
expiresIn: "0"
|
|
76
86
|
}) => {
|
|
87
|
+
assertInNamespace(this.namespace, productId);
|
|
77
88
|
invariant(!this.products.has(productId), `Duplicate product ${productId}`);
|
|
78
89
|
this.consumables.add(productId);
|
|
79
90
|
this.products.set(productId, metadata);
|
|
80
91
|
return this;
|
|
81
92
|
};
|
|
82
93
|
nonConsumable = (productId) => {
|
|
94
|
+
assertInNamespace(this.namespace, productId);
|
|
83
95
|
invariant(!this.products.has(productId), `Duplicate product ${productId}`);
|
|
84
96
|
this.nonConsumables.add(productId);
|
|
85
97
|
this.products.set(productId, null);
|
|
@@ -121,7 +133,15 @@ var AppStoreConfig = class AppStoreConfig {
|
|
|
121
133
|
return new Date(Date.now() + expiresIn).toISOString();
|
|
122
134
|
};
|
|
123
135
|
};
|
|
136
|
+
/**
|
|
137
|
+
* Declare the AppStore product catalog. Every type parameter is inferred from the options:
|
|
138
|
+
* the namespace from `namespace`, the plans and billing periods from `plans` and `billingPeriods`
|
|
139
|
+
* (`const`, so a literal array needs no `as const`). Products are then added with the fluent chain.
|
|
140
|
+
*/
|
|
141
|
+
function defineAppStoreConfig(options) {
|
|
142
|
+
return AppStoreConfig[construct](options);
|
|
143
|
+
}
|
|
124
144
|
//#endregion
|
|
125
|
-
export { AppStoreConfig };
|
|
145
|
+
export { AppStoreConfig, defineAppStoreConfig };
|
|
126
146
|
|
|
127
147
|
//# sourceMappingURL=config.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.mjs","names":[],"sources":["../../src/app-store/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms from 'ms';\nimport type { Metadata } from '../types';\n\nconst addMethod = Symbol('addMethod');\n\nclass Subscription<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n BP extends string = string,\n PL extends string = never,\n PI extends string = never,\n I extends string = never,\n> {\n readonly plan: PE;\n readonly billingPeriod: BP;\n readonly config: AppStoreConfig<NS, PE, BP, PL, PI>;\n readonly products: Map<string, Metadata>;\n\n defaultProductId: I | null;\n\n constructor(config: AppStoreConfig<NS, PE, BP, PL, PI>, plan: PE, billingPeriod: BP) {\n this.plan = plan;\n this.billingPeriod = billingPeriod;\n this.config = config;\n this.products = new Map();\n this.defaultProductId = null;\n }\n\n /**\n * Non-default products stay declared on purpose: they keep resolving for\n * grandfathered subscribers and serve as price-experiment arms. Never\n * sell a legacy product to new users unless that is intentional.\n */\n product = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ): Subscription<NS, PE, BP, PL, PI | K, I | K> => {\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.products.set(productId, metadata);\n return this as Subscription<NS, PE, BP, PL, PI | K, I | K>;\n };\n\n default = (defaultProductId: I) => {\n invariant(\n this.products.has(defaultProductId),\n `Default product ${defaultProductId} is not declared`\n );\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 BP extends string = string,\n PL extends string = never,\n PI extends string = never,\n> {\n readonly appId: string;\n readonly bundleId: string;\n\n private readonly products: Map<string, Metadata | null>;\n private readonly consumables: Set<string>;\n private readonly subscriptions: Map<string, Subscription<NS, PE, BP, PL, PI>>;\n private readonly nonConsumables: Set<string>;\n\n get productIds(): string[] {\n return Array.from(this.products.keys());\n }\n\n // runtime mirror of the type-level guards: generic accumulation only\n // protects a single fluent chain, not statement-style re-entry or casts\n [addMethod] = (subscription: Subscription<NS, PE, BP, PL, PI>) => {\n const key = `${subscription.plan}:${subscription.billingPeriod}`;\n invariant(!this.subscriptions.has(key), `Duplicate plan ${key}`);\n subscription.products.forEach((metadata, productId) => {\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.products.set(productId, metadata);\n });\n this.subscriptions.set(key, 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, BP extends string = string>(\n options: Options\n ) => {\n return new AppStoreConfig<NS, PE, BP>(options);\n };\n\n /** The same plan may be declared once per billing period; duplicate pairs are rejected. */\n subscription = <K extends PE, P extends BP>(\n plan: K,\n billingPeriod: `${K}:${P}` extends PL ? never : P\n ) => {\n // oxlint-disable-next-line typescript/no-unnecessary-type-assertion -- tsc requires the cast (tsgolint false positive)\n return new Subscription<NS, PE, BP, `${K}:${P}` | PL, PI>(this as never, plan, billingPeriod);\n };\n\n consumable = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ) => {\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.consumables.add(productId);\n this.products.set(productId, metadata);\n return this as AppStoreConfig<NS, PE, BP, PL, PI | K>;\n };\n\n nonConsumable = <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K) => {\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.nonConsumables.add(productId);\n this.products.set(productId, null);\n return this as AppStoreConfig<NS, PE, BP, PL, PI | K>;\n };\n\n /** Product id that new subscribers should purchase for the plan and billing period. */\n getDefaultProductId = (plan: PE, billingPeriod: BP): string => {\n const subscription = this.subscriptions.get(`${plan}:${billingPeriod}`);\n invariant(subscription, `Subscription not found for ${plan}:${billingPeriod}`);\n invariant(\n subscription.defaultProductId,\n `Default product not found for ${plan}:${billingPeriod}`\n );\n return subscription.defaultProductId;\n };\n\n getPlan = (productId: string): PE => {\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return subscription.plan;\n }\n throw new Error(`Plan not found for product ${productId}`);\n };\n\n getBillingPeriod = (productId: string): BP => {\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return subscription.billingPeriod;\n }\n throw new Error(`Billing period 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 !== undefined, `Product not found for ${productId}`);\n invariant(config !== null, `Credits not available for non-consumable product ${productId}`);\n return config.credits;\n };\n\n private readonly 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":";;;AAIA,MAAM,YAAY,OAAO,WAAW;AAEpC,IAAM,eAAN,MAOE;CACA;CACA;CACA;CACA;CAEA;CAEA,YAAY,QAA4C,MAAU,eAAmB;EACnF,KAAK,OAAO;EACZ,KAAK,gBAAgB;EACrB,KAAK,SAAS;EACd,KAAK,2BAAW,IAAI,IAAI;EACxB,KAAK,mBAAmB;CAC1B;;;;;;CAOA,WACE,WACA,WAAqB;EAAE,SAAS;EAAG,WAAW;CAAI,MACF;EAChD,UAAU,CAAC,KAAK,SAAS,IAAI,SAAS,GAAG,qBAAqB,WAAW;EACzE,KAAK,SAAS,IAAI,WAAW,QAAQ;EACrC,OAAO;CACT;CAEA,WAAW,qBAAwB;EACjC,UACE,KAAK,SAAS,IAAI,gBAAgB,GAClC,mBAAmB,iBAAiB,iBACtC;EACA,KAAK,mBAAmB;EACxB,KAAK,OAAO,UAAU,CAAC,IAAa;EACpC,OAAO,KAAK;CACd;AACF;AAIA,IAAa,iBAAb,MAAa,eAMX;CACA;CACA;CAEA;CACA;CACA;CACA;CAEA,IAAI,aAAuB;EACzB,OAAO,MAAM,KAAK,KAAK,SAAS,KAAK,CAAC;CACxC;CAIA,CAAC,cAAc,iBAAmD;EAChE,MAAM,MAAM,GAAG,aAAa,KAAK,GAAG,aAAa;EACjD,UAAU,CAAC,KAAK,cAAc,IAAI,GAAG,GAAG,kBAAkB,KAAK;EAC/D,aAAa,SAAS,SAAS,UAAU,cAAc;GACrD,UAAU,CAAC,KAAK,SAAS,IAAI,SAAS,GAAG,qBAAqB,WAAW;GACzE,KAAK,SAAS,IAAI,WAAW,QAAQ;EACvC,C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a,SAAS,IAAI,SAAS,GAAG,OAAO,aAAa;EAEhE,MAAM,IAAI,MAAM,wCAAwC,WAAW;CACrE;CAEA,WAAW,cAAkD;EAC3D,IAAI,KAAK,YAAY,IAAI,SAAS,KAAK,KAAK,eAAe,IAAI,SAAS,GAAG,OAAO;EAClF,KAAK,MAAM,gBAAgB,KAAK,cAAc,OAAO,GACnD,IAAI,aAAa,SAAS,IAAI,SAAS,GAAG,OAAO;EAEnD,MAAM,IAAI,MAAM,8BAA8B,WAAW;CAC3D;CAEA,mBAAmB,cAA8B;EAC/C,MAAM,SAAS,KAAK,SAAS,IAAI,SAAS;EAC1C,UAAU,WAAW,KAAA,GAAW,yBAAyB,WAAW;EACpE,UAAU,WAAW,MAAM,oDAAoD,WAAW;EAC1F,OAAO,OAAO;CAChB;CAEA,sBAAuC,cAA8B;EACnE,MAAM,SAAS,KAAK,SAAS,IAAI,SAAS;EAC1C,UAAU,QAAQ,yBAAyB,WAAW;EACtD,OAAO,GAAG,OAAO,SAAS;CAC5B;CAEA,sBAAsB,cAA8B;EAClD,MAAM,YAAY,KAAK,mBAAmB,SAAS;EACnD,OAAO,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,CAAC,CAAC,YAAY;CACtD;AACF"}
|
|
1
|
+
{"version":3,"file":"config.mjs","names":[],"sources":["../../src/app-store/config.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport ms from 'ms';\nimport { type Catalog, assertCatalog, assertDeclared, assertInNamespace } from '../catalog';\nimport type { Metadata } from '../types';\n\nconst addMethod = Symbol('addMethod');\n/** The private constructor's one door, for this module's `define` function only. */\nconst construct = Symbol('construct');\n\nclass Subscription<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n BP extends string = string,\n PL extends string = never,\n PI extends string = never,\n I extends string = never,\n> {\n readonly plan: PE;\n readonly billingPeriod: BP;\n readonly config: AppStoreConfig<NS, PE, BP, PL, PI>;\n readonly products: Map<string, Metadata>;\n\n defaultProductId: I | null;\n\n constructor(config: AppStoreConfig<NS, PE, BP, PL, PI>, plan: PE, billingPeriod: BP) {\n this.plan = plan;\n this.billingPeriod = billingPeriod;\n this.config = config;\n this.products = new Map();\n this.defaultProductId = null;\n }\n\n /**\n * Non-default products stay declared on purpose: they keep resolving for\n * grandfathered subscribers and serve as price-experiment arms. Never\n * sell a legacy product to new users unless that is intentional.\n */\n product = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ): Subscription<NS, PE, BP, PL, PI | K, I | K> => {\n assertInNamespace(this.config.namespace, productId);\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.products.set(productId, metadata);\n return this as Subscription<NS, PE, BP, PL, PI | K, I | K>;\n };\n\n default = (defaultProductId: I) => {\n invariant(\n this.products.has(defaultProductId),\n `Default product ${defaultProductId} is not declared`\n );\n this.defaultProductId = defaultProductId;\n this.config[addMethod](this as never);\n return this.config;\n };\n}\n\ntype Options<NS extends Lowercase<string>, PE extends string, BP extends string> = Catalog<\n NS,\n PE,\n BP\n> & { appId: `${number}`; bundleId: string };\n\nexport class AppStoreConfig<\n NS extends Lowercase<string> = Lowercase<string>,\n PE extends string = string,\n BP extends string = string,\n PL extends string = never,\n PI extends string = never,\n> {\n readonly appId: string;\n readonly bundleId: string;\n readonly namespace: NS;\n readonly plans: readonly PE[];\n readonly billingPeriods: readonly BP[];\n\n private readonly products: Map<string, Metadata | null>;\n private readonly consumables: Set<string>;\n private readonly subscriptions: Map<string, Subscription<NS, PE, BP, PL, PI>>;\n private readonly nonConsumables: Set<string>;\n\n get productIds(): string[] {\n return Array.from(this.products.keys());\n }\n\n // runtime mirror of the type-level guards: generic accumulation only\n // protects a single fluent chain, not statement-style re-entry or casts\n [addMethod] = (subscription: Subscription<NS, PE, BP, PL, PI>) => {\n const key = `${subscription.plan}:${subscription.billingPeriod}`;\n invariant(!this.subscriptions.has(key), `Duplicate plan ${key}`);\n subscription.products.forEach((metadata, productId) => {\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.products.set(productId, metadata);\n });\n this.subscriptions.set(key, subscription);\n return this;\n };\n\n private constructor(options: Options<NS, PE, BP>) {\n assertCatalog(options);\n this.namespace = options.namespace;\n this.plans = options.plans;\n this.billingPeriods = options.billingPeriods;\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 [construct] = <NS extends Lowercase<string>, PE extends string, BP extends string>(\n options: Options<NS, PE, BP>\n ) => new AppStoreConfig<NS, PE, BP>(options);\n\n /** The same plan may be declared once per billing period; duplicate pairs are rejected. */\n subscription = <K extends PE, P extends BP>(\n plan: K,\n billingPeriod: `${K}:${P}` extends PL ? never : P\n ) => {\n assertDeclared(this, plan, billingPeriod);\n // oxlint-disable-next-line typescript/no-unnecessary-type-assertion -- tsc requires the cast (tsgolint false positive)\n return new Subscription<NS, PE, BP, `${K}:${P}` | PL, PI>(this as never, plan, billingPeriod);\n };\n\n consumable = <K extends `${NS}.${Lowercase<string>}`>(\n productId: K extends PI ? never : K,\n metadata: Metadata = { credits: 0, expiresIn: '0' }\n ) => {\n assertInNamespace(this.namespace, productId);\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.consumables.add(productId);\n this.products.set(productId, metadata);\n return this as AppStoreConfig<NS, PE, BP, PL, PI | K>;\n };\n\n nonConsumable = <K extends `${NS}.${Lowercase<string>}`>(productId: K extends PI ? never : K) => {\n assertInNamespace(this.namespace, productId);\n invariant(!this.products.has(productId), `Duplicate product ${productId}`);\n this.nonConsumables.add(productId);\n this.products.set(productId, null);\n return this as AppStoreConfig<NS, PE, BP, PL, PI | K>;\n };\n\n /** Product id that new subscribers should purchase for the plan and billing period. */\n getDefaultProductId = (plan: PE, billingPeriod: BP): string => {\n const subscription = this.subscriptions.get(`${plan}:${billingPeriod}`);\n invariant(subscription, `Subscription not found for ${plan}:${billingPeriod}`);\n invariant(\n subscription.defaultProductId,\n `Default product not found for ${plan}:${billingPeriod}`\n );\n return subscription.defaultProductId;\n };\n\n getPlan = (productId: string): PE => {\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return subscription.plan;\n }\n throw new Error(`Plan not found for product ${productId}`);\n };\n\n getBillingPeriod = (productId: string): BP => {\n for (const subscription of this.subscriptions.values()) {\n if (subscription.products.has(productId)) return subscription.billingPeriod;\n }\n throw new Error(`Billing period 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 !== undefined, `Product not found for ${productId}`);\n invariant(config !== null, `Credits not available for non-consumable product ${productId}`);\n return config.credits;\n };\n\n private readonly 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\n/**\n * Declare the AppStore product catalog. Every type parameter is inferred from the options:\n * the namespace from `namespace`, the plans and billing periods from `plans` and `billingPeriods`\n * (`const`, so a literal array needs no `as const`). Products are then added with the fluent chain.\n */\nexport function defineAppStoreConfig<\n const NS extends Lowercase<string>,\n const PE extends string,\n const BP extends string,\n>(options: Options<NS, PE, BP>): AppStoreConfig<NS, PE, BP> {\n return AppStoreConfig[construct](options);\n}\n"],"mappings":";;;;AAKA,MAAM,YAAY,OAAO,WAAW;;AAEpC,MAAM,YAAY,OAAO,WAAW;AAEpC,IAAM,eAAN,MAOE;CACA;CACA;CACA;CACA;CAEA;CAEA,YAAY,QAA4C,MAAU,eAAmB;EACnF,KAAK,OAAO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SAAS,KAAK,KAAK,eAAe,IAAI,SAAS,GAAG,OAAO;EAClF,KAAK,MAAM,gBAAgB,KAAK,cAAc,OAAO,GACnD,IAAI,aAAa,SAAS,IAAI,SAAS,GAAG,OAAO;EAEnD,MAAM,IAAI,MAAM,8BAA8B,WAAW;CAC3D;CAEA,mBAAmB,cAA8B;EAC/C,MAAM,SAAS,KAAK,SAAS,IAAI,SAAS;EAC1C,UAAU,WAAW,KAAA,GAAW,yBAAyB,WAAW;EACpE,UAAU,WAAW,MAAM,oDAAoD,WAAW;EAC1F,OAAO,OAAO;CAChB;CAEA,sBAAuC,cAA8B;EACnE,MAAM,SAAS,KAAK,SAAS,IAAI,SAAS;EAC1C,UAAU,QAAQ,yBAAyB,WAAW;EACtD,OAAO,GAAG,OAAO,SAAS;CAC5B;CAEA,sBAAsB,cAA8B;EAClD,MAAM,YAAY,KAAK,mBAAmB,SAAS;EACnD,OAAO,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS,CAAC,CAAC,YAAY;CACtD;AACF;;;;;;AAOA,SAAgB,qBAId,SAA0D;CAC1D,OAAO,eAAe,UAAU,CAAC,OAAO;AAC1C"}
|
package/dist/app-store/index.cjs
CHANGED
|
@@ -4,5 +4,6 @@ const require_app_store_handler = require("./handler.cjs");
|
|
|
4
4
|
const require_app_store_mapper = require("./mapper.cjs");
|
|
5
5
|
exports.AppStoreConfig = require_app_store_config.AppStoreConfig;
|
|
6
6
|
exports.AppStoreNotificationHandler = require_app_store_handler.AppStoreNotificationHandler;
|
|
7
|
+
exports.defineAppStoreConfig = require_app_store_config.defineAppStoreConfig;
|
|
7
8
|
exports.mapCancellationDetails = require_app_store_mapper.mapCancellationDetails;
|
|
8
9
|
exports.mapPaymentType = require_app_store_mapper.mapPaymentType;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AppStoreConfig } from "./config.cjs";
|
|
1
|
+
import { AppStoreConfig, defineAppStoreConfig } from "./config.cjs";
|
|
2
2
|
import { AppStoreNotificationEvent, AppStoreNotificationHandler } from "./handler.cjs";
|
|
3
3
|
import { AppStoreCancellationSource, AppStorePaymentSource, mapCancellationDetails, mapPaymentType } from "./mapper.cjs";
|
|
4
|
-
export { type AppStoreCancellationSource, AppStoreConfig, type AppStoreNotificationEvent, AppStoreNotificationHandler, type AppStorePaymentSource, mapCancellationDetails, mapPaymentType };
|
|
4
|
+
export { type AppStoreCancellationSource, AppStoreConfig, type AppStoreNotificationEvent, AppStoreNotificationHandler, type AppStorePaymentSource, defineAppStoreConfig, mapCancellationDetails, mapPaymentType };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AppStoreConfig } from "./config.mjs";
|
|
1
|
+
import { AppStoreConfig, defineAppStoreConfig } from "./config.mjs";
|
|
2
2
|
import { AppStoreNotificationEvent, AppStoreNotificationHandler } from "./handler.mjs";
|
|
3
3
|
import { AppStoreCancellationSource, AppStorePaymentSource, mapCancellationDetails, mapPaymentType } from "./mapper.mjs";
|
|
4
|
-
export { type AppStoreCancellationSource, AppStoreConfig, type AppStoreNotificationEvent, AppStoreNotificationHandler, type AppStorePaymentSource, mapCancellationDetails, mapPaymentType };
|
|
4
|
+
export { type AppStoreCancellationSource, AppStoreConfig, type AppStoreNotificationEvent, AppStoreNotificationHandler, type AppStorePaymentSource, defineAppStoreConfig, mapCancellationDetails, mapPaymentType };
|
package/dist/app-store/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AppStoreConfig } from "./config.mjs";
|
|
1
|
+
import { AppStoreConfig, defineAppStoreConfig } from "./config.mjs";
|
|
2
2
|
import { AppStoreNotificationHandler } from "./handler.mjs";
|
|
3
3
|
import { mapCancellationDetails, mapPaymentType } from "./mapper.mjs";
|
|
4
|
-
export { AppStoreConfig, AppStoreNotificationHandler, mapCancellationDetails, mapPaymentType };
|
|
4
|
+
export { AppStoreConfig, AppStoreNotificationHandler, defineAppStoreConfig, mapCancellationDetails, mapPaymentType };
|
package/dist/catalog.cjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _shware_utils = require("@shware/utils");
|
|
3
|
+
//#region src/catalog.ts
|
|
4
|
+
function assertCatalog({ plans, billingPeriods }) {
|
|
5
|
+
(0, _shware_utils.invariant)(new Set(plans).size === plans.length, "Duplicate plan in plans");
|
|
6
|
+
(0, _shware_utils.invariant)(new Set(billingPeriods).size === billingPeriods.length, "Duplicate billing period in billingPeriods");
|
|
7
|
+
}
|
|
8
|
+
/** Runtime mirror of the template-literal type, for ids built at runtime or cast. */
|
|
9
|
+
function assertInNamespace(namespace, productId) {
|
|
10
|
+
(0, _shware_utils.invariant)(productId.startsWith(`${namespace}.`), `Product ${productId} is not in namespace ${namespace}`);
|
|
11
|
+
}
|
|
12
|
+
/** Runtime mirror of the plan and period unions. */
|
|
13
|
+
function assertDeclared({ plans, billingPeriods }, plan, billingPeriod) {
|
|
14
|
+
(0, _shware_utils.invariant)(plan === null || plans.includes(plan), `Plan ${plan} is not in plans`);
|
|
15
|
+
(0, _shware_utils.invariant)(billingPeriod === null || billingPeriods.includes(billingPeriod), `Billing period ${billingPeriod} is not in billingPeriods`);
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
exports.assertCatalog = assertCatalog;
|
|
19
|
+
exports.assertDeclared = assertDeclared;
|
|
20
|
+
exports.assertInNamespace = assertInNamespace;
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=catalog.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalog.cjs","names":[],"sources":["../src/catalog.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\n\n/**\n * What every store config is declared over, and what its type parameters are inferred from: the\n * prefix of every product id, the plans lowest tier first, the billing periods shortest first. A\n * config with no subscriptions passes empty arrays.\n */\nexport interface Catalog<NS extends Lowercase<string>, PE extends string, BP extends string> {\n /** The prefix every product id starts with (`io.example` for `io.example.pro.monthly`). */\n namespace: NS;\n /** Every plan, lowest tier first: its level, as Apple orders a subscription group. */\n plans: readonly PE[];\n /** Every billing period, shortest first. */\n billingPeriods: readonly BP[];\n}\n\nexport function assertCatalog({\n plans,\n billingPeriods,\n}: Catalog<Lowercase<string>, string, string>) {\n invariant(new Set(plans).size === plans.length, 'Duplicate plan in plans');\n invariant(\n new Set(billingPeriods).size === billingPeriods.length,\n 'Duplicate billing period in billingPeriods'\n );\n}\n\n/** Runtime mirror of the template-literal type, for ids built at runtime or cast. */\nexport function assertInNamespace(namespace: string, productId: string) {\n invariant(\n productId.startsWith(`${namespace}.`),\n `Product ${productId} is not in namespace ${namespace}`\n );\n}\n\n/** Runtime mirror of the plan and period unions. */\nexport function assertDeclared(\n { plans, billingPeriods }: Catalog<Lowercase<string>, string, string>,\n plan: string | null,\n billingPeriod: string | null\n) {\n invariant(plan === null || plans.includes(plan), `Plan ${plan} is not in plans`);\n invariant(\n billingPeriod === null || billingPeriods.includes(billingPeriod),\n `Billing period ${billingPeriod} is not in billingPeriods`\n );\n}\n"],"mappings":";;;AAgBA,SAAgB,cAAc,EAC5B,OACA,kBAC6C;CAC7C,CAAA,GAAA,cAAA,UAAA,CAAU,IAAI,IAAI,KAAK,CAAC,CAAC,SAAS,MAAM,QAAQ,yBAAyB;CACzE,CAAA,GAAA,cAAA,UAAA,CACE,IAAI,IAAI,cAAc,CAAC,CAAC,SAAS,eAAe,QAChD,4CACF;AACF;;AAGA,SAAgB,kBAAkB,WAAmB,WAAmB;CACtE,CAAA,GAAA,cAAA,UAAA,CACE,UAAU,WAAW,GAAG,UAAU,EAAE,GACpC,WAAW,UAAU,uBAAuB,WAC9C;AACF;;AAGA,SAAgB,eACd,EAAE,OAAO,kBACT,MACA,eACA;CACA,CAAA,GAAA,cAAA,UAAA,CAAU,SAAS,QAAQ,MAAM,SAAS,IAAI,GAAG,QAAQ,KAAK,iBAAiB;CAC/E,CAAA,GAAA,cAAA,UAAA,CACE,kBAAkB,QAAQ,eAAe,SAAS,aAAa,GAC/D,kBAAkB,cAAc,0BAClC;AACF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/catalog.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* What every store config is declared over, and what its type parameters are inferred from: the
|
|
4
|
+
* prefix of every product id, the plans lowest tier first, the billing periods shortest first. A
|
|
5
|
+
* config with no subscriptions passes empty arrays.
|
|
6
|
+
*/
|
|
7
|
+
export interface Catalog<NS extends Lowercase<string>, PE extends string, BP extends string> {
|
|
8
|
+
/** The prefix every product id starts with (`io.example` for `io.example.pro.monthly`). */
|
|
9
|
+
namespace: NS;
|
|
10
|
+
/** Every plan, lowest tier first: its level, as Apple orders a subscription group. */
|
|
11
|
+
plans: readonly PE[];
|
|
12
|
+
/** Every billing period, shortest first. */
|
|
13
|
+
billingPeriods: readonly BP[];
|
|
14
|
+
}
|
|
15
|
+
export declare function assertCatalog({ plans, billingPeriods }: Catalog<Lowercase<string>, string, string>): void;
|
|
16
|
+
/** Runtime mirror of the template-literal type, for ids built at runtime or cast. */
|
|
17
|
+
export declare function assertInNamespace(namespace: string, productId: string): void;
|
|
18
|
+
/** Runtime mirror of the plan and period unions. */
|
|
19
|
+
export declare function assertDeclared({ plans, billingPeriods }: Catalog<Lowercase<string>, string, string>, plan: string | null, billingPeriod: string | null): void;
|
|
20
|
+
//#endregion
|
|
21
|
+
//# sourceMappingURL=catalog.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalog.d.cts","names":[],"sources":["../src/catalog.ts"],"mappings":";;;;;;iBAOiB,QAAQ,WAAW,mBAAmB,mBAAmB;;EAExE,WAAW;;EAEX,gBAAgB;;EAEhB,yBAAyB;;wBAGX,gBACd,OACA,kBACC,QAAQ;;wBASK,kBAAkB,mBAAmB;;wBAQrC,iBACZ,OAAO,kBAAkB,QAAQ,oCACnC,qBACA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/catalog.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* What every store config is declared over, and what its type parameters are inferred from: the
|
|
4
|
+
* prefix of every product id, the plans lowest tier first, the billing periods shortest first. A
|
|
5
|
+
* config with no subscriptions passes empty arrays.
|
|
6
|
+
*/
|
|
7
|
+
export interface Catalog<NS extends Lowercase<string>, PE extends string, BP extends string> {
|
|
8
|
+
/** The prefix every product id starts with (`io.example` for `io.example.pro.monthly`). */
|
|
9
|
+
namespace: NS;
|
|
10
|
+
/** Every plan, lowest tier first: its level, as Apple orders a subscription group. */
|
|
11
|
+
plans: readonly PE[];
|
|
12
|
+
/** Every billing period, shortest first. */
|
|
13
|
+
billingPeriods: readonly BP[];
|
|
14
|
+
}
|
|
15
|
+
export declare function assertCatalog({ plans, billingPeriods }: Catalog<Lowercase<string>, string, string>): void;
|
|
16
|
+
/** Runtime mirror of the template-literal type, for ids built at runtime or cast. */
|
|
17
|
+
export declare function assertInNamespace(namespace: string, productId: string): void;
|
|
18
|
+
/** Runtime mirror of the plan and period unions. */
|
|
19
|
+
export declare function assertDeclared({ plans, billingPeriods }: Catalog<Lowercase<string>, string, string>, plan: string | null, billingPeriod: string | null): void;
|
|
20
|
+
//#endregion
|
|
21
|
+
//# sourceMappingURL=catalog.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalog.d.mts","names":[],"sources":["../src/catalog.ts"],"mappings":";;;;;;iBAOiB,QAAQ,WAAW,mBAAmB,mBAAmB;;EAExE,WAAW;;EAEX,gBAAgB;;EAEhB,yBAAyB;;wBAGX,gBACd,OACA,kBACC,QAAQ;;wBASK,kBAAkB,mBAAmB;;wBAQrC,iBACZ,OAAO,kBAAkB,QAAQ,oCACnC,qBACA"}
|
package/dist/catalog.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { invariant } from "@shware/utils";
|
|
2
|
+
//#region src/catalog.ts
|
|
3
|
+
function assertCatalog({ plans, billingPeriods }) {
|
|
4
|
+
invariant(new Set(plans).size === plans.length, "Duplicate plan in plans");
|
|
5
|
+
invariant(new Set(billingPeriods).size === billingPeriods.length, "Duplicate billing period in billingPeriods");
|
|
6
|
+
}
|
|
7
|
+
/** Runtime mirror of the template-literal type, for ids built at runtime or cast. */
|
|
8
|
+
function assertInNamespace(namespace, productId) {
|
|
9
|
+
invariant(productId.startsWith(`${namespace}.`), `Product ${productId} is not in namespace ${namespace}`);
|
|
10
|
+
}
|
|
11
|
+
/** Runtime mirror of the plan and period unions. */
|
|
12
|
+
function assertDeclared({ plans, billingPeriods }, plan, billingPeriod) {
|
|
13
|
+
invariant(plan === null || plans.includes(plan), `Plan ${plan} is not in plans`);
|
|
14
|
+
invariant(billingPeriod === null || billingPeriods.includes(billingPeriod), `Billing period ${billingPeriod} is not in billingPeriods`);
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { assertCatalog, assertDeclared, assertInNamespace };
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=catalog.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalog.mjs","names":[],"sources":["../src/catalog.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\n\n/**\n * What every store config is declared over, and what its type parameters are inferred from: the\n * prefix of every product id, the plans lowest tier first, the billing periods shortest first. A\n * config with no subscriptions passes empty arrays.\n */\nexport interface Catalog<NS extends Lowercase<string>, PE extends string, BP extends string> {\n /** The prefix every product id starts with (`io.example` for `io.example.pro.monthly`). */\n namespace: NS;\n /** Every plan, lowest tier first: its level, as Apple orders a subscription group. */\n plans: readonly PE[];\n /** Every billing period, shortest first. */\n billingPeriods: readonly BP[];\n}\n\nexport function assertCatalog({\n plans,\n billingPeriods,\n}: Catalog<Lowercase<string>, string, string>) {\n invariant(new Set(plans).size === plans.length, 'Duplicate plan in plans');\n invariant(\n new Set(billingPeriods).size === billingPeriods.length,\n 'Duplicate billing period in billingPeriods'\n );\n}\n\n/** Runtime mirror of the template-literal type, for ids built at runtime or cast. */\nexport function assertInNamespace(namespace: string, productId: string) {\n invariant(\n productId.startsWith(`${namespace}.`),\n `Product ${productId} is not in namespace ${namespace}`\n );\n}\n\n/** Runtime mirror of the plan and period unions. */\nexport function assertDeclared(\n { plans, billingPeriods }: Catalog<Lowercase<string>, string, string>,\n plan: string | null,\n billingPeriod: string | null\n) {\n invariant(plan === null || plans.includes(plan), `Plan ${plan} is not in plans`);\n invariant(\n billingPeriod === null || billingPeriods.includes(billingPeriod),\n `Billing period ${billingPeriod} is not in billingPeriods`\n );\n}\n"],"mappings":";;AAgBA,SAAgB,cAAc,EAC5B,OACA,kBAC6C;CAC7C,UAAU,IAAI,IAAI,KAAK,CAAC,CAAC,SAAS,MAAM,QAAQ,yBAAyB;CACzE,UACE,IAAI,IAAI,cAAc,CAAC,CAAC,SAAS,eAAe,QAChD,4CACF;AACF;;AAGA,SAAgB,kBAAkB,WAAmB,WAAmB;CACtE,UACE,UAAU,WAAW,GAAG,UAAU,EAAE,GACpC,WAAW,UAAU,uBAAuB,WAC9C;AACF;;AAGA,SAAgB,eACd,EAAE,OAAO,kBACT,MACA,eACA;CACA,UAAU,SAAS,QAAQ,MAAM,SAAS,IAAI,GAAG,QAAQ,KAAK,iBAAiB;CAC/E,UACE,kBAAkB,QAAQ,eAAe,SAAS,aAAa,GAC/D,kBAAkB,cAAc,0BAClC;AACF"}
|