@superwall/runtime 1.2.0 → 1.3.0-beta.4
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 +65 -49
- package/dist/bridge/base64.d.ts +0 -6
- package/dist/bridge/base64.js +5 -25
- package/dist/bridge/index.d.ts +2 -30
- package/dist/bridge/index.js +2 -49
- package/dist/bridge/transport.d.ts +2 -9
- package/dist/bridge/transport.js +9 -30
- package/dist/config/background.d.ts +16 -0
- package/dist/config/background.js +13 -0
- package/dist/config/checkout.d.ts +26 -0
- package/dist/config/checkout.js +2 -0
- package/dist/config/index.d.ts +66 -0
- package/dist/config/index.js +4 -0
- package/dist/config/localization.d.ts +13 -0
- package/dist/config/localization.js +1 -0
- package/dist/config/notifications.d.ts +40 -0
- package/dist/config/notifications.js +1 -0
- package/dist/config/platforms.d.ts +3 -0
- package/dist/config/platforms.js +5 -0
- package/dist/config/presentation.d.ts +22 -0
- package/dist/config/presentation.js +1 -0
- package/dist/config/products.d.ts +23 -0
- package/dist/config/products.js +10 -0
- package/dist/index.d.ts +2 -7
- package/dist/index.js +1 -6
- package/dist/localization/catalog.d.ts +8 -0
- package/dist/localization/catalog.js +21 -0
- package/dist/localization/index.d.ts +3 -24
- package/dist/localization/index.js +3 -71
- package/dist/localization/locale.d.ts +2 -0
- package/dist/localization/locale.js +22 -0
- package/dist/localization/periods.d.ts +1 -0
- package/dist/localization/periods.js +13 -5
- package/dist/products/derive.d.ts +4 -0
- package/dist/products/derive.js +45 -0
- package/dist/{products.d.ts → products/index.d.ts} +6 -14
- package/dist/products/index.js +15 -0
- package/dist/products/merge.d.ts +5 -0
- package/dist/products/merge.js +51 -0
- package/dist/session/incoming.d.ts +3 -0
- package/dist/session/incoming.js +47 -0
- package/dist/session/index.d.ts +7 -102
- package/dist/session/index.js +243 -694
- package/dist/session/outgoing.d.ts +42 -0
- package/dist/session/outgoing.js +70 -0
- package/dist/session/purchases.d.ts +18 -0
- package/dist/session/purchases.js +44 -0
- package/dist/session/reminders.d.ts +11 -0
- package/dist/session/reminders.js +41 -0
- package/dist/session/requests.d.ts +19 -0
- package/dist/session/requests.js +56 -0
- package/dist/session/restores.d.ts +6 -0
- package/dist/session/restores.js +20 -0
- package/dist/session/state.d.ts +3 -3
- package/dist/session/state.js +14 -39
- package/dist/session/types.d.ts +60 -29
- package/dist/session/variables.d.ts +3 -7
- package/package.json +3 -2
- package/src/bridge/base64.ts +10 -46
- package/src/bridge/index.ts +7 -77
- package/src/bridge/transport.ts +11 -46
- package/src/config/background.ts +24 -0
- package/src/config/checkout.ts +32 -0
- package/src/config/index.ts +102 -0
- package/src/config/localization.ts +12 -0
- package/src/config/notifications.ts +49 -0
- package/src/config/platforms.ts +8 -0
- package/src/config/presentation.ts +22 -0
- package/src/config/products.ts +39 -0
- package/src/index.ts +2 -43
- package/src/localization/catalog.ts +45 -0
- package/src/localization/index.ts +7 -110
- package/src/localization/locale.ts +25 -0
- package/src/localization/periods.ts +23 -8
- package/src/products/derive.ts +65 -0
- package/src/products/index.ts +87 -0
- package/src/products/merge.ts +78 -0
- package/src/session/incoming.ts +53 -0
- package/src/session/index.ts +286 -985
- package/src/session/outgoing.ts +132 -0
- package/src/session/purchases.ts +66 -0
- package/src/session/reminders.ts +56 -0
- package/src/session/requests.ts +87 -0
- package/src/session/restores.ts +27 -0
- package/src/session/state.ts +19 -59
- package/src/session/types.ts +78 -30
- package/src/session/variables.ts +3 -7
- package/dist/config.d.ts +0 -237
- package/dist/config.js +0 -138
- package/dist/mock.d.ts +0 -74
- package/dist/mock.js +0 -206
- package/dist/products.js +0 -105
- package/src/config.ts +0 -408
- package/src/mock.ts +0 -333
- package/src/products.ts +0 -221
package/dist/products.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
const legacyProductReferences = ["primary", "secondary", "tertiary"];
|
|
2
|
-
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3
|
-
export const createSuperwallProduct = (input, variables = {}) => {
|
|
4
|
-
if (typeof input === "string") {
|
|
5
|
-
return {
|
|
6
|
-
reference: input,
|
|
7
|
-
product: input,
|
|
8
|
-
variables,
|
|
9
|
-
source: "unknown",
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
const reference = input.reference ?? input.product;
|
|
13
|
-
const productId = input.productId ?? input.productIdentifier ?? input.id;
|
|
14
|
-
return {
|
|
15
|
-
reference,
|
|
16
|
-
product: input.product ?? reference,
|
|
17
|
-
id: input.id ?? productId,
|
|
18
|
-
productId,
|
|
19
|
-
productIdentifier: input.productIdentifier ?? productId,
|
|
20
|
-
variables: input.variables ?? variables,
|
|
21
|
-
source: input.source ?? "unknown",
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
export const productReferenceFromInput = (input) => typeof input === "string" ? input : input.reference;
|
|
25
|
-
export const productsByReference = (products) => {
|
|
26
|
-
const indexed = {};
|
|
27
|
-
for (const product of products) {
|
|
28
|
-
indexed[product.reference] = product;
|
|
29
|
-
}
|
|
30
|
-
return indexed;
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* SDK-delivered variable records are trusted, not validated: the declared
|
|
34
|
-
* shapes are the SDK's side of the protocol contract. Numeric-looking fields
|
|
35
|
-
* arrive as strings ("59.99", "7"), which is why they are typed
|
|
36
|
-
* `string | number` — coerce with `Number()` before doing arithmetic.
|
|
37
|
-
*/
|
|
38
|
-
const asProductVariables = (record) => record;
|
|
39
|
-
export const productVariablesFromTemplateVariables = (variables) => {
|
|
40
|
-
const result = new Map();
|
|
41
|
-
if (Array.isArray(variables.products)) {
|
|
42
|
-
for (const productEntry of variables.products) {
|
|
43
|
-
if (!isRecord(productEntry))
|
|
44
|
-
continue;
|
|
45
|
-
for (const [reference, productVariables] of Object.entries(productEntry)) {
|
|
46
|
-
if (isRecord(productVariables)) {
|
|
47
|
-
result.set(reference, asProductVariables(productVariables));
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
for (const reference of legacyProductReferences) {
|
|
53
|
-
const productVariables = variables[reference];
|
|
54
|
-
if (isRecord(productVariables) && !result.has(reference)) {
|
|
55
|
-
result.set(reference, asProductVariables(productVariables));
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return result;
|
|
59
|
-
};
|
|
60
|
-
export const productsFromIncomingMessage = (message) => message.products.map((product) => createSuperwallProduct({
|
|
61
|
-
reference: product.product,
|
|
62
|
-
product: product.product,
|
|
63
|
-
productId: product.productId,
|
|
64
|
-
productIdentifier: product.productId,
|
|
65
|
-
source: "sdk",
|
|
66
|
-
}));
|
|
67
|
-
export const mergeIncomingProducts = (current, incoming) => {
|
|
68
|
-
const next = new Map();
|
|
69
|
-
for (const product of current) {
|
|
70
|
-
next.set(product.reference, product);
|
|
71
|
-
}
|
|
72
|
-
for (const product of incoming) {
|
|
73
|
-
const existing = next.get(product.product);
|
|
74
|
-
next.set(product.product, {
|
|
75
|
-
...createSuperwallProduct({
|
|
76
|
-
reference: product.product,
|
|
77
|
-
product: product.product,
|
|
78
|
-
productId: product.productId,
|
|
79
|
-
productIdentifier: product.productId,
|
|
80
|
-
source: "sdk",
|
|
81
|
-
}),
|
|
82
|
-
variables: existing?.variables ?? {},
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
return Array.from(next.values());
|
|
86
|
-
};
|
|
87
|
-
export const mergeProductVariables = (current, variables) => {
|
|
88
|
-
const next = new Map();
|
|
89
|
-
for (const product of current) {
|
|
90
|
-
next.set(product.reference, product);
|
|
91
|
-
}
|
|
92
|
-
for (const [reference, productVariables] of variables) {
|
|
93
|
-
const existing = next.get(reference);
|
|
94
|
-
next.set(reference, {
|
|
95
|
-
...createSuperwallProduct(existing ?? {
|
|
96
|
-
reference,
|
|
97
|
-
product: reference,
|
|
98
|
-
source: "sdk",
|
|
99
|
-
}, productVariables),
|
|
100
|
-
variables: productVariables,
|
|
101
|
-
source: existing?.source ?? "sdk",
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
return Array.from(next.values());
|
|
105
|
-
};
|
package/src/config.ts
DELETED
|
@@ -1,408 +0,0 @@
|
|
|
1
|
-
import type { SuperwallProduct } from "./products";
|
|
2
|
-
|
|
3
|
-
export const SUPERWALL_PLATFORMS = ["ios", "android", "web"] as const;
|
|
4
|
-
|
|
5
|
-
export type SuperwallPlatform = (typeof SUPERWALL_PLATFORMS)[number];
|
|
6
|
-
|
|
7
|
-
export const normalizePlatform = (value: string | undefined): SuperwallPlatform | undefined => {
|
|
8
|
-
const normalized = (value ?? "").toLowerCase();
|
|
9
|
-
return SUPERWALL_PLATFORMS.find((platform) => platform === normalized);
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* A product slot the paywall renders, keyed by reference in
|
|
14
|
-
* `SuperwallConfig["products"]`. Product data (price, period, trial…) is
|
|
15
|
-
* SDK-owned and arrives at runtime; the mock host synthesizes it in dev.
|
|
16
|
-
*/
|
|
17
|
-
export type SuperwallProductSlot = {
|
|
18
|
-
/** Store product identifier this slot maps to (e.g. "pro_3999_year"). */
|
|
19
|
-
productId?: string;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* One store identifier per platform the paywall ships to, for a slot whose
|
|
24
|
-
* underlying product differs by store — the same `purchase("annual")` buys
|
|
25
|
-
* the App Store product on iOS and the Play product on Android.
|
|
26
|
-
*/
|
|
27
|
-
export type SuperwallPlatformProductIds = Partial<Record<SuperwallPlatform, string>>;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* A slot object, the store product identifier as shorthand, or one
|
|
31
|
-
* identifier per platform.
|
|
32
|
-
*/
|
|
33
|
-
export type SuperwallProductSlotInput = string | SuperwallProductSlot | SuperwallPlatformProductIds;
|
|
34
|
-
|
|
35
|
-
export const isPlatformProductIds = (
|
|
36
|
-
input: SuperwallProductSlotInput,
|
|
37
|
-
): input is SuperwallPlatformProductIds =>
|
|
38
|
-
typeof input === "object" && input !== null && !("productId" in input);
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* The store identifier a slot resolves to on a platform. A per-platform slot
|
|
42
|
-
* with no entry for the platform resolves to nothing; with no platform known
|
|
43
|
-
* at all it falls back to its first entry.
|
|
44
|
-
*/
|
|
45
|
-
export const productIdFor = (
|
|
46
|
-
input: SuperwallProductSlotInput,
|
|
47
|
-
platform?: SuperwallPlatform,
|
|
48
|
-
): string | undefined => {
|
|
49
|
-
if (typeof input === "string") return input;
|
|
50
|
-
if (!isPlatformProductIds(input)) return input.productId;
|
|
51
|
-
if (platform !== undefined) return input[platform];
|
|
52
|
-
return Object.values(input).find((id) => typeof id === "string");
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export type SuperwallPresentationStyle =
|
|
56
|
-
| "fullscreen"
|
|
57
|
-
| "modal"
|
|
58
|
-
| "push"
|
|
59
|
-
| "drawer"
|
|
60
|
-
| "popup"
|
|
61
|
-
| "noAnimation";
|
|
62
|
-
|
|
63
|
-
export type SuperwallPresentationConfig = {
|
|
64
|
-
/** How the native SDK presents the paywall. Defaults to "fullscreen". */
|
|
65
|
-
style?: SuperwallPresentationStyle;
|
|
66
|
-
/**
|
|
67
|
-
* Drawer geometry, as percentages of the screen and points. Only read when
|
|
68
|
-
* style is "drawer"; defaults to 70% height with a 15pt corner radius.
|
|
69
|
-
*/
|
|
70
|
-
drawer?: { height?: number; cornerRadius?: number };
|
|
71
|
-
/**
|
|
72
|
-
* Popup geometry, as percentages of the screen and points. Only read when
|
|
73
|
-
* style is "popup"; defaults to 80% × 60% with a 15pt corner radius.
|
|
74
|
-
*/
|
|
75
|
-
popup?: { width?: number; height?: number; cornerRadius?: number };
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
export type PaywallPresentationStyleWire =
|
|
79
|
-
| { type: "FULLSCREEN" | "MODAL" | "PUSH" | "NO_ANIMATION" }
|
|
80
|
-
| { type: "DRAWER"; height: number; corner_radius: number }
|
|
81
|
-
| { type: "POPUP"; width: number; height: number; corner_radius: number };
|
|
82
|
-
|
|
83
|
-
export type WebCheckoutDestinationWire = "EXTERNAL" | "PAYWALL_EMBEDDED" | "PAYWALL_APPLE_PAY";
|
|
84
|
-
|
|
85
|
-
export type PaywallSettings = {
|
|
86
|
-
presentation_style: PaywallPresentationStyleWire;
|
|
87
|
-
feature_gating: "gated" | "non_gated";
|
|
88
|
-
on_device_cache: boolean;
|
|
89
|
-
scroll_enabled: boolean;
|
|
90
|
-
game_controller_enabled: boolean;
|
|
91
|
-
introductory_offer_eligibility: "automatic" | "always_eligible" | "always_ineligible";
|
|
92
|
-
web_checkout_destination?: WebCheckoutDestinationWire;
|
|
93
|
-
background_color_hex?: string;
|
|
94
|
-
dark_background_color_hex?: string;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const DRAWER_DEFAULTS = { height: 70, cornerRadius: 15 };
|
|
98
|
-
const POPUP_DEFAULTS = { width: 80, height: 60, cornerRadius: 15 };
|
|
99
|
-
|
|
100
|
-
const dimension = (label: string, value: number | undefined, fallback: number): number => {
|
|
101
|
-
const resolved = value ?? fallback;
|
|
102
|
-
if (!Number.isFinite(resolved) || resolved <= 0) {
|
|
103
|
-
throw new Error(`presentation.${label} must be a positive number, got ${String(value)}`);
|
|
104
|
-
}
|
|
105
|
-
return resolved;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
export const presentationStyleWire = (
|
|
109
|
-
presentation: SuperwallPresentationConfig | undefined,
|
|
110
|
-
): PaywallPresentationStyleWire => {
|
|
111
|
-
switch (presentation?.style ?? "fullscreen") {
|
|
112
|
-
case "fullscreen":
|
|
113
|
-
return { type: "FULLSCREEN" };
|
|
114
|
-
case "modal":
|
|
115
|
-
return { type: "MODAL" };
|
|
116
|
-
case "push":
|
|
117
|
-
return { type: "PUSH" };
|
|
118
|
-
case "noAnimation":
|
|
119
|
-
return { type: "NO_ANIMATION" };
|
|
120
|
-
case "drawer":
|
|
121
|
-
return {
|
|
122
|
-
type: "DRAWER",
|
|
123
|
-
height: dimension("drawer.height", presentation?.drawer?.height, DRAWER_DEFAULTS.height),
|
|
124
|
-
corner_radius: dimension(
|
|
125
|
-
"drawer.cornerRadius",
|
|
126
|
-
presentation?.drawer?.cornerRadius,
|
|
127
|
-
DRAWER_DEFAULTS.cornerRadius,
|
|
128
|
-
),
|
|
129
|
-
};
|
|
130
|
-
case "popup":
|
|
131
|
-
return {
|
|
132
|
-
type: "POPUP",
|
|
133
|
-
width: dimension("popup.width", presentation?.popup?.width, POPUP_DEFAULTS.width),
|
|
134
|
-
height: dimension("popup.height", presentation?.popup?.height, POPUP_DEFAULTS.height),
|
|
135
|
-
corner_radius: dimension(
|
|
136
|
-
"popup.cornerRadius",
|
|
137
|
-
presentation?.popup?.cornerRadius,
|
|
138
|
-
POPUP_DEFAULTS.cornerRadius,
|
|
139
|
-
),
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
const ELIGIBILITY_WIRE = {
|
|
145
|
-
automatic: "automatic",
|
|
146
|
-
alwaysEligible: "always_eligible",
|
|
147
|
-
alwaysIneligible: "always_ineligible",
|
|
148
|
-
} as const;
|
|
149
|
-
|
|
150
|
-
const CHECKOUT_WIRE: Record<SuperwallCheckoutMode, WebCheckoutDestinationWire> = {
|
|
151
|
-
external: "EXTERNAL",
|
|
152
|
-
sheet: "PAYWALL_EMBEDDED",
|
|
153
|
-
applePay: "PAYWALL_APPLE_PAY",
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
export const webCheckoutDestinationOf = (
|
|
157
|
-
checkout: SuperwallCheckoutConfig | undefined,
|
|
158
|
-
): WebCheckoutDestinationWire | undefined => {
|
|
159
|
-
const mode = checkoutModeOf(checkout);
|
|
160
|
-
return mode === undefined ? undefined : CHECKOUT_WIRE[mode];
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
export const paywallSettingsOf = (config: SuperwallConfig | undefined): PaywallSettings => {
|
|
164
|
-
const background = backgroundOf(config?.background);
|
|
165
|
-
const destination = webCheckoutDestinationOf(config?.checkout);
|
|
166
|
-
return {
|
|
167
|
-
presentation_style: presentationStyleWire(config?.presentation),
|
|
168
|
-
feature_gating: config?.featureGating === "gated" ? "gated" : "non_gated",
|
|
169
|
-
on_device_cache: config?.onDeviceCacheEnabled ?? true,
|
|
170
|
-
scroll_enabled: config?.scrollEnabled ?? true,
|
|
171
|
-
game_controller_enabled: config?.gameControllerEnabled ?? false,
|
|
172
|
-
introductory_offer_eligibility:
|
|
173
|
-
ELIGIBILITY_WIRE[config?.introductoryOfferEligibility ?? "automatic"],
|
|
174
|
-
...(destination === undefined ? {} : { web_checkout_destination: destination }),
|
|
175
|
-
...(background === undefined ? {} : { background_color_hex: background.light }),
|
|
176
|
-
...(background?.dark === undefined ? {} : { dark_background_color_hex: background.dark }),
|
|
177
|
-
};
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
export type SuperwallTrialReminderStaticConfig = {
|
|
181
|
-
enabled?: boolean;
|
|
182
|
-
/** Notification title. Localizable: message keys resolve through `t()`. */
|
|
183
|
-
title: string;
|
|
184
|
-
subtitle?: string;
|
|
185
|
-
/** Notification body. Localizable: message keys resolve through `t()`. */
|
|
186
|
-
body: string;
|
|
187
|
-
/** How many days before the trial ends the reminder fires. Defaults to 1. */
|
|
188
|
-
beforeTrialEndDays?: number;
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
/** Context passed to a dynamic trial reminder function. */
|
|
192
|
-
export type SuperwallTrialReminderContext = {
|
|
193
|
-
/** When the trial ends, per the SDK. */
|
|
194
|
-
trialEndDate: Date;
|
|
195
|
-
/** Store identifier of the purchased product, when the SDK provides it. */
|
|
196
|
-
productIdentifier?: string;
|
|
197
|
-
/** The matching product, when it's one of this paywall's slots. */
|
|
198
|
-
product?: SuperwallProduct;
|
|
199
|
-
/** Translator for the active locale. */
|
|
200
|
-
t: (key: string, values?: Record<string, string | number>) => string;
|
|
201
|
-
locale: string;
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
/** What a dynamic trial reminder resolves to. */
|
|
205
|
-
export type SuperwallTrialReminderNotification = {
|
|
206
|
-
title: string;
|
|
207
|
-
subtitle?: string;
|
|
208
|
-
body: string;
|
|
209
|
-
/** Milliseconds from now until the notification fires. */
|
|
210
|
-
delayMs: number;
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Static config, or a function computing the notification from the trial
|
|
215
|
-
* context (schedule time, localized copy, per-product logic). Return null
|
|
216
|
-
* from the function to skip scheduling.
|
|
217
|
-
*/
|
|
218
|
-
export type SuperwallTrialReminderConfig =
|
|
219
|
-
| SuperwallTrialReminderStaticConfig
|
|
220
|
-
| ((
|
|
221
|
-
context: SuperwallTrialReminderContext,
|
|
222
|
-
) => SuperwallTrialReminderNotification | null | undefined);
|
|
223
|
-
|
|
224
|
-
export type SuperwallNotificationsConfig = {
|
|
225
|
-
/** Auto-scheduled when the SDK reports a trial started (`freeTrial_start`). */
|
|
226
|
-
trialReminder?: SuperwallTrialReminderConfig;
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Nested message catalog for one locale. Values interpolate `{name}` params:
|
|
231
|
-
* `{ paywall: { title: "Unlock {appName}" } }` → `t("paywall.title", { appName })`.
|
|
232
|
-
*/
|
|
233
|
-
export type SuperwallMessages = { [key: string]: string | SuperwallMessages };
|
|
234
|
-
|
|
235
|
-
export type SuperwallLocalizationConfig<TLocale extends string = string> = {
|
|
236
|
-
/** Locale used when the device locale has no catalog. */
|
|
237
|
-
defaultLocale: TLocale;
|
|
238
|
-
/** Message catalogs by locale (e.g. `{ en, de, "pt-BR": ptBR }`). */
|
|
239
|
-
messages?: Record<TLocale, SuperwallMessages> & Record<string, SuperwallMessages>;
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* How a purchase completes on the web. Native hosts ignore it — they always
|
|
244
|
-
* buy through the store.
|
|
245
|
-
*
|
|
246
|
-
* - `external` — leave for Superwall's hosted checkout page and come back.
|
|
247
|
-
* - `sheet` — take payment in the paywall, in a sheet Superwall renders.
|
|
248
|
-
* - `applePay` — go straight to Apple Pay, falling back to the sheet where
|
|
249
|
-
* Apple Pay is unavailable.
|
|
250
|
-
*
|
|
251
|
-
* The sheet's appearance is fixed and takes nothing from the paywall around
|
|
252
|
-
* it. Payment UI that borrows a paywall's fonts and colors stops looking like
|
|
253
|
-
* payment UI, which is the one place a shopper is entitled to see something
|
|
254
|
-
* they recognize.
|
|
255
|
-
*
|
|
256
|
-
* `sheet` and `applePay` carry Stripe's payment UI into the bundle; `external`
|
|
257
|
-
* costs nothing. Read this before shipping either:
|
|
258
|
-
* https://superwall.com/docs/web-checkout
|
|
259
|
-
*/
|
|
260
|
-
export type SuperwallCheckoutMode = "external" | "sheet" | "applePay";
|
|
261
|
-
|
|
262
|
-
export type SuperwallCheckoutConfig =
|
|
263
|
-
| SuperwallCheckoutMode
|
|
264
|
-
| { mode: SuperwallCheckoutMode; prefetch?: string | false };
|
|
265
|
-
|
|
266
|
-
export const checkoutModeOf = (
|
|
267
|
-
checkout: SuperwallCheckoutConfig | undefined,
|
|
268
|
-
): SuperwallCheckoutMode | undefined => (typeof checkout === "string" ? checkout : checkout?.mode);
|
|
269
|
-
|
|
270
|
-
export const checkoutPrefetchOf = (
|
|
271
|
-
checkout: SuperwallCheckoutConfig | undefined,
|
|
272
|
-
): string | false | undefined => (typeof checkout === "object" ? checkout.prefetch : undefined);
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* A paywall's background color. The string shorthand sets light mode; the
|
|
276
|
-
* object form adds a dark-mode color. Native SDKs paint it behind the webview
|
|
277
|
-
* (and derive the loading spinner from it), and the web document paints it as
|
|
278
|
-
* the page background — so the color a shopper sees while the paywall loads
|
|
279
|
-
* matches the color it settles on.
|
|
280
|
-
*/
|
|
281
|
-
export type SuperwallBackgroundConfig = string | { light: string; dark?: string };
|
|
282
|
-
|
|
283
|
-
export type ResolvedBackground = { light: string; dark?: string };
|
|
284
|
-
|
|
285
|
-
export const backgroundOf = (
|
|
286
|
-
background: SuperwallBackgroundConfig | undefined,
|
|
287
|
-
): ResolvedBackground | undefined => {
|
|
288
|
-
if (background === undefined) return undefined;
|
|
289
|
-
if (typeof background === "string") {
|
|
290
|
-
const light = background.trim();
|
|
291
|
-
return light === "" ? undefined : { light };
|
|
292
|
-
}
|
|
293
|
-
const light = background.light?.trim();
|
|
294
|
-
if (!light) return undefined;
|
|
295
|
-
const dark = background.dark?.trim();
|
|
296
|
-
return dark ? { light, dark } : { light };
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
export type SuperwallConfig<TLocale extends string = string> = {
|
|
300
|
-
/** Product slots this paywall renders, keyed by reference. */
|
|
301
|
-
products?: Record<string, SuperwallProductSlotInput>;
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* How a purchase completes on the web. Omit it for native-only paywalls —
|
|
305
|
-
* nothing about checkout is bundled unless this asks for it.
|
|
306
|
-
*/
|
|
307
|
-
checkout?: SuperwallCheckoutConfig;
|
|
308
|
-
|
|
309
|
-
/** How the native SDK presents the paywall: style and drawer/popup geometry. */
|
|
310
|
-
presentation?: SuperwallPresentationConfig;
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Background color painted behind the paywall while it loads and as the web
|
|
314
|
-
* document background. String shorthand = light mode; `{ light, dark }` adds
|
|
315
|
-
* a dark-mode color.
|
|
316
|
-
*/
|
|
317
|
-
background?: SuperwallBackgroundConfig;
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* Whether the feature behind the placement is gated on a purchase.
|
|
321
|
-
* Defaults to "nonGated".
|
|
322
|
-
*/
|
|
323
|
-
featureGating?: "gated" | "nonGated";
|
|
324
|
-
|
|
325
|
-
/** Trial eligibility handling. Defaults to "automatic" (SDK decides). */
|
|
326
|
-
introductoryOfferEligibility?: "automatic" | "alwaysEligible" | "alwaysIneligible";
|
|
327
|
-
|
|
328
|
-
/** Forward game controller input to the paywall. Defaults to false. */
|
|
329
|
-
gameControllerEnabled?: boolean;
|
|
330
|
-
|
|
331
|
-
/** Whether the webview scrolls. Defaults to true. */
|
|
332
|
-
scrollEnabled?: boolean;
|
|
333
|
-
|
|
334
|
-
/** Cache the paywall on device for instant presentation. Defaults to true. */
|
|
335
|
-
onDeviceCacheEnabled?: boolean;
|
|
336
|
-
|
|
337
|
-
/** Dismiss the paywall automatically after a completed purchase. */
|
|
338
|
-
dismissOnPurchase?: boolean;
|
|
339
|
-
|
|
340
|
-
/** Resolve pending purchases as failed after this many ms without a transaction. */
|
|
341
|
-
purchaseTimeoutMs?: number;
|
|
342
|
-
|
|
343
|
-
/** Local notifications the paywall schedules (e.g. trial reminders). */
|
|
344
|
-
notifications?: SuperwallNotificationsConfig;
|
|
345
|
-
|
|
346
|
-
/** Message catalogs and default locale for `t()`. */
|
|
347
|
-
localization?: SuperwallLocalizationConfig<TLocale>;
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* Declares the paywall's contract: product slots, presentation, gating,
|
|
352
|
-
* notifications, and localization. Consumed by the runtime session at render
|
|
353
|
-
* time and by the Superwall build pipeline at push time.
|
|
354
|
-
*/
|
|
355
|
-
export const defineSuperwallConfig = <TLocale extends string = string>(
|
|
356
|
-
config: SuperwallConfig<TLocale>,
|
|
357
|
-
) => config;
|
|
358
|
-
|
|
359
|
-
export const normalizeProductSlot = (
|
|
360
|
-
input: SuperwallProductSlotInput,
|
|
361
|
-
platform?: SuperwallPlatform,
|
|
362
|
-
): SuperwallProductSlot => ({ productId: productIdFor(input, platform) });
|
|
363
|
-
|
|
364
|
-
export const checkoutNeedsPaymentUi = (checkout: SuperwallCheckoutConfig | undefined): boolean => {
|
|
365
|
-
const mode = checkoutModeOf(checkout);
|
|
366
|
-
return mode === "sheet" || mode === "applePay";
|
|
367
|
-
};
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* Materializes the config's product slots as products so references exist
|
|
371
|
-
* before the SDK (or mock host) delivers real product data.
|
|
372
|
-
*/
|
|
373
|
-
export const productsFromConfig = (
|
|
374
|
-
config: SuperwallConfig | undefined,
|
|
375
|
-
platform?: SuperwallPlatform,
|
|
376
|
-
): SuperwallProduct[] =>
|
|
377
|
-
Object.entries(config?.products ?? {}).map(([reference, input]) => {
|
|
378
|
-
const slot = normalizeProductSlot(input, platform);
|
|
379
|
-
return {
|
|
380
|
-
reference,
|
|
381
|
-
product: reference,
|
|
382
|
-
id: slot.productId,
|
|
383
|
-
productId: slot.productId,
|
|
384
|
-
productIdentifier: slot.productId,
|
|
385
|
-
variables: {},
|
|
386
|
-
source: "inline",
|
|
387
|
-
};
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
/** @deprecated Use `SuperwallConfig`. */
|
|
391
|
-
export type SuperwallPaywallConfig<TLocale extends string = string> = SuperwallConfig<TLocale>;
|
|
392
|
-
|
|
393
|
-
/** The framework's paywall generation — v4 is the visual editor, v5 is code. */
|
|
394
|
-
export const SUPERWALL_GENERATION = 5;
|
|
395
|
-
|
|
396
|
-
/**
|
|
397
|
-
* Document semantics as `<html>` data attributes — the snapshot's half of the
|
|
398
|
-
* two-layer contract. The build stamps these statically (no flash before
|
|
399
|
-
* hydration) and `SuperwallProvider` reasserts them at runtime (dev has no
|
|
400
|
-
* build step). The platform's serve-time stylesheet (live.css) owns what they
|
|
401
|
-
* mean, so their interpretation can evolve fleet-wide without republishing.
|
|
402
|
-
*/
|
|
403
|
-
export const documentAttributes = (
|
|
404
|
-
config: SuperwallConfig | undefined,
|
|
405
|
-
): Record<string, string> => ({
|
|
406
|
-
"data-sw-generation": String(SUPERWALL_GENERATION),
|
|
407
|
-
"data-sw-scroll": String(config?.scrollEnabled ?? true),
|
|
408
|
-
});
|