@superwall/runtime 2.2.1 → 2.3.1

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.
@@ -100,4 +100,4 @@ export { normalizePlatform, SUPERWALL_PLATFORMS, type SuperwallPlatform } from "
100
100
  export { dismissesOnPurchase, type SuperwallPostPurchase } from "./postPurchase";
101
101
  export type { SuperwallPresentationConfig, SuperwallPresentationStyle } from "./presentation";
102
102
  export { teleportOf, type ResolvedTeleport, type SuperwallTeleportConfig, type SuperwallTeleportOptions, } from "./teleport";
103
- export { isPlatformProductIds, productIdFor, type SuperwallPlatformProductIds, type SuperwallProductSlot, type SuperwallProductSlotInput, } from "./products";
103
+ export { billingPlanFor, isPlatformProductIds, productIdFor, productIdsOf, SUPERWALL_BILLING_PLANS, type SuperwallBillingPlan, type SuperwallPlatformProductIds, type SuperwallProductSlot, type SuperwallProductSlotInput, } from "./products";
@@ -5,4 +5,4 @@ export { INSET_EDGES, insetsOf, } from "./insets";
5
5
  export { normalizePlatform, SUPERWALL_PLATFORMS } from "./platforms";
6
6
  export { dismissesOnPurchase } from "./postPurchase";
7
7
  export { teleportOf, } from "./teleport";
8
- export { isPlatformProductIds, productIdFor, } from "./products";
8
+ export { billingPlanFor, isPlatformProductIds, productIdFor, productIdsOf, SUPERWALL_BILLING_PLANS, } from "./products";
@@ -1,4 +1,11 @@
1
- import type { SuperwallPlatform } from "./platforms";
1
+ import { type SuperwallPlatform } from "./platforms";
2
+ /**
3
+ * How an App Store slot bills a product that offers Apple's monthly plan
4
+ * (a 12-month commitment paid monthly): `"monthly"` sells that plan,
5
+ * `"upFront"` sells the year paid at once.
6
+ */
7
+ export type SuperwallBillingPlan = "monthly" | "upFront";
8
+ export declare const SUPERWALL_BILLING_PLANS: readonly SuperwallBillingPlan[];
2
9
  /**
3
10
  * A product slot the paywall renders, keyed by reference in
4
11
  * `SuperwallConfig["products"]`. Product data (price, period, trial…) is
@@ -7,13 +14,27 @@ import type { SuperwallPlatform } from "./platforms";
7
14
  export type SuperwallProductSlot = {
8
15
  /** Store product identifier this slot maps to (e.g. "pro_3999_year"). */
9
16
  productId?: string;
17
+ /**
18
+ * Which Apple billing plan this slot sells, when its App Store product
19
+ * offers the monthly plan. iOS only: Android and web have no such plan and
20
+ * ignore it. Defaults to `"monthly"` for a product that offers the monthly
21
+ * plan; has no effect on one that does not.
22
+ */
23
+ billingPlan?: SuperwallBillingPlan;
10
24
  };
11
25
  /**
12
26
  * One store identifier per platform the paywall ships to, for a slot whose
13
27
  * underlying product differs by store — the same `purchase("annual")` buys
14
28
  * the App Store product on iOS and the Play product on Android.
15
29
  */
16
- export type SuperwallPlatformProductIds = Partial<Record<SuperwallPlatform, string>>;
30
+ export type SuperwallPlatformProductIds = Partial<Record<SuperwallPlatform, string>> & {
31
+ /**
32
+ * Which Apple billing plan the iOS product sells, when it offers the
33
+ * monthly plan. Android and web ignore it. Defaults to `"monthly"` for a
34
+ * product that offers the monthly plan; has no effect on one that does not.
35
+ */
36
+ billingPlan?: SuperwallBillingPlan;
37
+ };
17
38
  /**
18
39
  * A slot object, the store product identifier as shorthand, or one
19
40
  * identifier per platform.
@@ -21,3 +42,5 @@ export type SuperwallPlatformProductIds = Partial<Record<SuperwallPlatform, stri
21
42
  export type SuperwallProductSlotInput = string | SuperwallProductSlot | SuperwallPlatformProductIds;
22
43
  export declare const isPlatformProductIds: (input: SuperwallProductSlotInput) => input is SuperwallPlatformProductIds;
23
44
  export declare const productIdFor: (input: SuperwallProductSlotInput, platform?: SuperwallPlatform) => string | undefined;
45
+ export declare const productIdsOf: (input: SuperwallProductSlotInput) => string[];
46
+ export declare const billingPlanFor: (input: SuperwallProductSlotInput, label?: string) => SuperwallBillingPlan | undefined;
@@ -1,4 +1,11 @@
1
+ import { SUPERWALL_PLATFORMS } from "./platforms";
2
+ export const SUPERWALL_BILLING_PLANS = ["monthly", "upFront"];
1
3
  export const isPlatformProductIds = (input) => typeof input === "object" && input !== null && !("productId" in input);
4
+ const isPlatformKey = (key) => SUPERWALL_PLATFORMS.some((platform) => platform === key);
5
+ const platformIdsOf = (input) => Object.keys(input)
6
+ .filter(isPlatformKey)
7
+ .map((platform) => input[platform])
8
+ .filter((id) => typeof id === "string");
2
9
  export const productIdFor = (input, platform) => {
3
10
  if (typeof input === "string")
4
11
  return input;
@@ -6,5 +13,24 @@ export const productIdFor = (input, platform) => {
6
13
  return input.productId;
7
14
  if (platform !== undefined)
8
15
  return input[platform];
9
- return Object.values(input).find((id) => typeof id === "string");
16
+ return platformIdsOf(input)[0];
17
+ };
18
+ export const productIdsOf = (input) => {
19
+ if (typeof input === "string")
20
+ return [input];
21
+ if (isPlatformProductIds(input))
22
+ return [...new Set(platformIdsOf(input))];
23
+ return [input.productId].filter((id) => typeof id === "string");
24
+ };
25
+ export const billingPlanFor = (input, label = "billingPlan") => {
26
+ if (typeof input === "string")
27
+ return undefined;
28
+ const plan = input.billingPlan;
29
+ if (plan === undefined)
30
+ return undefined;
31
+ const known = SUPERWALL_BILLING_PLANS.find((candidate) => candidate === plan);
32
+ if (known === undefined) {
33
+ throw new Error(`${label} must be one of ${SUPERWALL_BILLING_PLANS.map((value) => `"${value}"`).join(", ")}, got ${JSON.stringify(plan)}.`);
34
+ }
35
+ return known;
10
36
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superwall/runtime",
3
- "version": "2.2.1",
3
+ "version": "2.3.1",
4
4
  "description": "Framework-agnostic runtime for headless Superwall paywalls: bridge, session, products, localization, mock host.",
5
5
  "keywords": [
6
6
  "paywall",
@@ -39,7 +39,7 @@
39
39
  "clean": "rm -rf dist coverage"
40
40
  },
41
41
  "dependencies": {
42
- "@superwall/protocol": "2.2.1"
42
+ "@superwall/protocol": "2.3.1"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@superwall/host": "workspace:*",
@@ -158,8 +158,12 @@ export {
158
158
  type SuperwallTeleportOptions,
159
159
  } from "./teleport";
160
160
  export {
161
+ billingPlanFor,
161
162
  isPlatformProductIds,
162
163
  productIdFor,
164
+ productIdsOf,
165
+ SUPERWALL_BILLING_PLANS,
166
+ type SuperwallBillingPlan,
163
167
  type SuperwallPlatformProductIds,
164
168
  type SuperwallProductSlot,
165
169
  type SuperwallProductSlotInput,
@@ -1,4 +1,13 @@
1
- import type { SuperwallPlatform } from "./platforms";
1
+ import { SUPERWALL_PLATFORMS, type SuperwallPlatform } from "./platforms";
2
+
3
+ /**
4
+ * How an App Store slot bills a product that offers Apple's monthly plan
5
+ * (a 12-month commitment paid monthly): `"monthly"` sells that plan,
6
+ * `"upFront"` sells the year paid at once.
7
+ */
8
+ export type SuperwallBillingPlan = "monthly" | "upFront";
9
+
10
+ export const SUPERWALL_BILLING_PLANS: readonly SuperwallBillingPlan[] = ["monthly", "upFront"];
2
11
 
3
12
  /**
4
13
  * A product slot the paywall renders, keyed by reference in
@@ -8,6 +17,13 @@ import type { SuperwallPlatform } from "./platforms";
8
17
  export type SuperwallProductSlot = {
9
18
  /** Store product identifier this slot maps to (e.g. "pro_3999_year"). */
10
19
  productId?: string;
20
+ /**
21
+ * Which Apple billing plan this slot sells, when its App Store product
22
+ * offers the monthly plan. iOS only: Android and web have no such plan and
23
+ * ignore it. Defaults to `"monthly"` for a product that offers the monthly
24
+ * plan; has no effect on one that does not.
25
+ */
26
+ billingPlan?: SuperwallBillingPlan;
11
27
  };
12
28
 
13
29
  /**
@@ -15,7 +31,14 @@ export type SuperwallProductSlot = {
15
31
  * underlying product differs by store — the same `purchase("annual")` buys
16
32
  * the App Store product on iOS and the Play product on Android.
17
33
  */
18
- export type SuperwallPlatformProductIds = Partial<Record<SuperwallPlatform, string>>;
34
+ export type SuperwallPlatformProductIds = Partial<Record<SuperwallPlatform, string>> & {
35
+ /**
36
+ * Which Apple billing plan the iOS product sells, when it offers the
37
+ * monthly plan. Android and web ignore it. Defaults to `"monthly"` for a
38
+ * product that offers the monthly plan; has no effect on one that does not.
39
+ */
40
+ billingPlan?: SuperwallBillingPlan;
41
+ };
19
42
 
20
43
  /**
21
44
  * A slot object, the store product identifier as shorthand, or one
@@ -28,6 +51,15 @@ export const isPlatformProductIds = (
28
51
  ): input is SuperwallPlatformProductIds =>
29
52
  typeof input === "object" && input !== null && !("productId" in input);
30
53
 
54
+ const isPlatformKey = (key: string): key is SuperwallPlatform =>
55
+ SUPERWALL_PLATFORMS.some((platform) => platform === key);
56
+
57
+ const platformIdsOf = (input: SuperwallPlatformProductIds): string[] =>
58
+ Object.keys(input)
59
+ .filter(isPlatformKey)
60
+ .map((platform) => input[platform])
61
+ .filter((id): id is string => typeof id === "string");
62
+
31
63
  export const productIdFor = (
32
64
  input: SuperwallProductSlotInput,
33
65
  platform?: SuperwallPlatform,
@@ -35,5 +67,27 @@ export const productIdFor = (
35
67
  if (typeof input === "string") return input;
36
68
  if (!isPlatformProductIds(input)) return input.productId;
37
69
  if (platform !== undefined) return input[platform];
38
- return Object.values(input).find((id) => typeof id === "string");
70
+ return platformIdsOf(input)[0];
71
+ };
72
+
73
+ export const productIdsOf = (input: SuperwallProductSlotInput): string[] => {
74
+ if (typeof input === "string") return [input];
75
+ if (isPlatformProductIds(input)) return [...new Set(platformIdsOf(input))];
76
+ return [input.productId].filter((id): id is string => typeof id === "string");
77
+ };
78
+
79
+ export const billingPlanFor = (
80
+ input: SuperwallProductSlotInput,
81
+ label = "billingPlan",
82
+ ): SuperwallBillingPlan | undefined => {
83
+ if (typeof input === "string") return undefined;
84
+ const plan: unknown = input.billingPlan;
85
+ if (plan === undefined) return undefined;
86
+ const known = SUPERWALL_BILLING_PLANS.find((candidate) => candidate === plan);
87
+ if (known === undefined) {
88
+ throw new Error(
89
+ `${label} must be one of ${SUPERWALL_BILLING_PLANS.map((value) => `"${value}"`).join(", ")}, got ${JSON.stringify(plan)}.`,
90
+ );
91
+ }
92
+ return known;
39
93
  };