@superwall/runtime 1.1.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/LICENSE +105 -0
- package/README.md +93 -0
- package/dist/bridge/base64.d.ts +9 -0
- package/dist/bridge/base64.js +34 -0
- package/dist/bridge/index.d.ts +30 -0
- package/dist/bridge/index.js +49 -0
- package/dist/bridge/transport.d.ts +14 -0
- package/dist/bridge/transport.js +43 -0
- package/dist/config.d.ts +237 -0
- package/dist/config.js +138 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +11 -0
- package/dist/localization/index.d.ts +24 -0
- package/dist/localization/index.js +71 -0
- package/dist/localization/periods.d.ts +1 -0
- package/dist/localization/periods.js +753 -0
- package/dist/mock.d.ts +74 -0
- package/dist/mock.js +206 -0
- package/dist/products.d.ts +62 -0
- package/dist/products.js +105 -0
- package/dist/session/index.d.ts +103 -0
- package/dist/session/index.js +732 -0
- package/dist/session/state.d.ts +27 -0
- package/dist/session/state.js +60 -0
- package/dist/session/types.d.ts +93 -0
- package/dist/session/types.js +1 -0
- package/dist/session/variables.d.ts +121 -0
- package/dist/session/variables.js +5 -0
- package/package.json +51 -0
- package/src/bridge/base64.ts +56 -0
- package/src/bridge/index.ts +77 -0
- package/src/bridge/transport.ts +63 -0
- package/src/config.ts +408 -0
- package/src/index.ts +57 -0
- package/src/localization/index.ts +110 -0
- package/src/localization/periods.ts +790 -0
- package/src/mock.ts +333 -0
- package/src/products.ts +221 -0
- package/src/session/index.ts +1066 -0
- package/src/session/state.ts +112 -0
- package/src/session/types.ts +123 -0
- package/src/session/variables.ts +139 -0
package/src/mock.ts
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ITransport,
|
|
3
|
+
TIncomingHandler,
|
|
4
|
+
TIncomingMessage,
|
|
5
|
+
TIncomingMessageCallbackResult,
|
|
6
|
+
TOutgoingMessage,
|
|
7
|
+
TPermissionStatus,
|
|
8
|
+
TPermissionTypeMessage,
|
|
9
|
+
} from "@superwall/protocol";
|
|
10
|
+
import { normalizePlatform, normalizeProductSlot, type SuperwallConfig } from "./config";
|
|
11
|
+
import {
|
|
12
|
+
createSuperwallProduct,
|
|
13
|
+
type SuperwallProduct,
|
|
14
|
+
type SuperwallProductInput,
|
|
15
|
+
} from "./products";
|
|
16
|
+
import type {
|
|
17
|
+
SuperwallDeviceVariables,
|
|
18
|
+
SuperwallExperiment,
|
|
19
|
+
SuperwallPlacementParams,
|
|
20
|
+
SuperwallUserVariables,
|
|
21
|
+
} from "./session/variables";
|
|
22
|
+
|
|
23
|
+
export type SuperwallMockPurchaseOutcome = "completed" | "abandoned";
|
|
24
|
+
|
|
25
|
+
export type SuperwallMockPurchaseContext = {
|
|
26
|
+
reference: string;
|
|
27
|
+
productIdentifier?: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type SuperwallMockCallbackResult = {
|
|
31
|
+
status: TIncomingMessageCallbackResult["status"];
|
|
32
|
+
data?: Record<string, unknown>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type SuperwallMockOptions = {
|
|
36
|
+
/** Identity sent in `paywall_open`. */
|
|
37
|
+
paywall?: { id?: string; identifier?: string };
|
|
38
|
+
/**
|
|
39
|
+
* Products the mock SDK delivers. Defaults to the config's product slots,
|
|
40
|
+
* carrying only the product identifier — real product data comes from
|
|
41
|
+
* Superwall, never from the mock.
|
|
42
|
+
*/
|
|
43
|
+
products?: readonly SuperwallProductInput[];
|
|
44
|
+
/** Device variables merged over the mock defaults. */
|
|
45
|
+
device?: SuperwallDeviceVariables;
|
|
46
|
+
/** User attributes delivered with template variables. */
|
|
47
|
+
user?: SuperwallUserVariables;
|
|
48
|
+
/** Placement params delivered with template variables. */
|
|
49
|
+
params?: SuperwallPlacementParams;
|
|
50
|
+
/** Experiment assignment to report. Omitted by default. */
|
|
51
|
+
experiment?: SuperwallExperiment;
|
|
52
|
+
/** Extra top-level template variables. */
|
|
53
|
+
variables?: Record<string, unknown>;
|
|
54
|
+
/**
|
|
55
|
+
* Introductory-offer eligibility the mock reports. Defaults to true when
|
|
56
|
+
* any product has trial variables.
|
|
57
|
+
*/
|
|
58
|
+
trialEligible?: boolean;
|
|
59
|
+
purchaseOutcome?:
|
|
60
|
+
| SuperwallMockPurchaseOutcome
|
|
61
|
+
| ((
|
|
62
|
+
context: SuperwallMockPurchaseContext,
|
|
63
|
+
) => SuperwallMockPurchaseOutcome | Promise<SuperwallMockPurchaseOutcome>);
|
|
64
|
+
/** Whether restore emits a `transaction_complete`. Defaults to "completed". */
|
|
65
|
+
restoreOutcome?: "completed" | "none";
|
|
66
|
+
/** Permission request responses. Defaults to "granted". */
|
|
67
|
+
permissions?: Partial<Record<TPermissionTypeMessage, TPermissionStatus>>;
|
|
68
|
+
/** Answers a permission request per call, for hosts that decide at runtime. */
|
|
69
|
+
onPermission?: (
|
|
70
|
+
permissionType: TPermissionTypeMessage,
|
|
71
|
+
) => TPermissionStatus | Promise<TPermissionStatus>;
|
|
72
|
+
/** Answers a callback request per call, for hosts that decide at runtime. */
|
|
73
|
+
onCallback?: (
|
|
74
|
+
name: string,
|
|
75
|
+
variables?: Record<string, unknown>,
|
|
76
|
+
) => SuperwallMockCallbackResult | Promise<SuperwallMockCallbackResult>;
|
|
77
|
+
callbacks?: Record<
|
|
78
|
+
string,
|
|
79
|
+
| SuperwallMockCallbackResult
|
|
80
|
+
| ((
|
|
81
|
+
variables?: Record<string, unknown>,
|
|
82
|
+
) => SuperwallMockCallbackResult | Promise<SuperwallMockCallbackResult>)
|
|
83
|
+
>;
|
|
84
|
+
/** Simulated SDK response delay. Defaults to 0. */
|
|
85
|
+
latencyMs?: number;
|
|
86
|
+
/** Observe every message the paywall sends to the mock host. */
|
|
87
|
+
onOutgoing?: (message: TOutgoingMessage) => void;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export const MOCK_DEVICE_VARIABLES: SuperwallDeviceVariables = {
|
|
91
|
+
platform: "iOS",
|
|
92
|
+
deviceModel: "iPhone17,2",
|
|
93
|
+
osVersion: "18.0",
|
|
94
|
+
appVersion: "1.0.0",
|
|
95
|
+
deviceLocale: "en_US",
|
|
96
|
+
deviceLanguageCode: "en",
|
|
97
|
+
regionCode: "US",
|
|
98
|
+
preferredLocale: "en-US",
|
|
99
|
+
preferredLanguageCode: "en",
|
|
100
|
+
preferredRegionCode: "US",
|
|
101
|
+
deviceCurrencyCode: "USD",
|
|
102
|
+
deviceCurrencySymbol: "$",
|
|
103
|
+
interfaceStyle: "Light",
|
|
104
|
+
isSandbox: "true",
|
|
105
|
+
sdkVersion: "4.0.0",
|
|
106
|
+
daysSinceInstall: 0,
|
|
107
|
+
totalPaywallViews: 1,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export const mockProductsFromConfig = (
|
|
111
|
+
config: SuperwallConfig | undefined,
|
|
112
|
+
devicePlatform?: string,
|
|
113
|
+
): SuperwallProduct[] =>
|
|
114
|
+
Object.entries(config?.products ?? {}).map(([reference, input]) => {
|
|
115
|
+
const slot = normalizeProductSlot(input, normalizePlatform(devicePlatform));
|
|
116
|
+
const productId = slot.productId ?? reference;
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
reference,
|
|
120
|
+
product: reference,
|
|
121
|
+
id: productId,
|
|
122
|
+
productId,
|
|
123
|
+
productIdentifier: productId,
|
|
124
|
+
variables: { identifier: productId },
|
|
125
|
+
source: "placeholder",
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
export const mockOptionsFromConfig = (
|
|
130
|
+
config: SuperwallConfig | undefined,
|
|
131
|
+
options: SuperwallMockOptions = {},
|
|
132
|
+
): SuperwallMockOptions => ({
|
|
133
|
+
...options,
|
|
134
|
+
products:
|
|
135
|
+
options.products ??
|
|
136
|
+
mockProductsFromConfig(
|
|
137
|
+
config,
|
|
138
|
+
typeof options.device?.platform === "string" ? options.device.platform : undefined,
|
|
139
|
+
),
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
143
|
+
|
|
144
|
+
const trialPeriodDaysOf = (product: SuperwallProduct): number => {
|
|
145
|
+
// The SDK sends period counts as strings ("7"), so a `typeof === "number"`
|
|
146
|
+
// check silently reports every product as trial-less on a device.
|
|
147
|
+
const days = Number(product.variables.trialPeriodDays);
|
|
148
|
+
return Number.isFinite(days) ? days : 0;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export class MockSuperwallTransport implements ITransport {
|
|
152
|
+
private handlers: TIncomingHandler[] = [];
|
|
153
|
+
private listening = false;
|
|
154
|
+
private options: SuperwallMockOptions;
|
|
155
|
+
private products: SuperwallProduct[];
|
|
156
|
+
|
|
157
|
+
constructor(options: SuperwallMockOptions = {}) {
|
|
158
|
+
this.options = options;
|
|
159
|
+
this.products = (options.products ?? []).map((input) => {
|
|
160
|
+
const product = createSuperwallProduct(input);
|
|
161
|
+
return product.source === "unknown" ? { ...product, source: "placeholder" } : product;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public addListener(handler: TIncomingHandler) {
|
|
166
|
+
this.handlers.push(handler);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
public startListening() {
|
|
170
|
+
this.listening = true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
public accept(message: TIncomingMessage) {
|
|
174
|
+
if (!this.listening) return;
|
|
175
|
+
this.handlers.forEach((handler) => handler(message));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
public postMessage(message: TOutgoingMessage) {
|
|
179
|
+
this.options.onOutgoing?.(message);
|
|
180
|
+
this.handleOutgoing(message).catch((error) => {
|
|
181
|
+
console.error("[MockSuperwallTransport] Failed to handle outgoing message", {
|
|
182
|
+
event: message.event_name,
|
|
183
|
+
error,
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private get trialEligible(): boolean {
|
|
189
|
+
return (
|
|
190
|
+
this.options.trialEligible ?? this.products.some((product) => trialPeriodDaysOf(product) > 0)
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
private respond(messages: TIncomingMessage[]) {
|
|
195
|
+
const deliver = () => {
|
|
196
|
+
messages.forEach((message) => this.accept(message));
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const latency = this.options.latencyMs ?? 0;
|
|
200
|
+
if (latency > 0) {
|
|
201
|
+
setTimeout(deliver, latency);
|
|
202
|
+
} else {
|
|
203
|
+
queueMicrotask(deliver);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private async handleOutgoing(message: TOutgoingMessage) {
|
|
208
|
+
switch (message.event_name) {
|
|
209
|
+
case "ping": {
|
|
210
|
+
const replies: TIncomingMessage[] = [
|
|
211
|
+
{
|
|
212
|
+
event_name: "paywall_open",
|
|
213
|
+
paywall_id: this.options.paywall?.id ?? "mock-paywall",
|
|
214
|
+
paywall_identifier: this.options.paywall?.identifier ?? "mock-paywall-identifier",
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
event_name: "products",
|
|
218
|
+
products: this.products.map((product) => ({
|
|
219
|
+
product: product.reference,
|
|
220
|
+
productId: product.productIdentifier ?? product.productId ?? product.reference,
|
|
221
|
+
})),
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
event_name: "template_variables",
|
|
225
|
+
variables: {
|
|
226
|
+
...this.options.variables,
|
|
227
|
+
device: { ...MOCK_DEVICE_VARIABLES, ...this.options.device },
|
|
228
|
+
user: this.options.user ?? {},
|
|
229
|
+
params: this.options.params ?? {},
|
|
230
|
+
products: [
|
|
231
|
+
Object.fromEntries(
|
|
232
|
+
this.products.map((product) => [product.reference, product.variables]),
|
|
233
|
+
),
|
|
234
|
+
],
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
event_name: "template_substitutions_prefix",
|
|
239
|
+
prefix: this.trialEligible ? "freeTrial" : null,
|
|
240
|
+
},
|
|
241
|
+
];
|
|
242
|
+
|
|
243
|
+
if (this.options.experiment) {
|
|
244
|
+
replies.push({ event_name: "experiment", ...this.options.experiment });
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
this.respond(replies);
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
case "purchase": {
|
|
251
|
+
const outcome = await this.resolvePurchaseOutcome({
|
|
252
|
+
reference: message.product,
|
|
253
|
+
productIdentifier: message.product_identifier,
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const replies: TIncomingMessage[] = [
|
|
257
|
+
{
|
|
258
|
+
event_name: outcome === "completed" ? "transaction_complete" : "transaction_abandon",
|
|
259
|
+
product: message.product,
|
|
260
|
+
product_identifier: message.product_identifier,
|
|
261
|
+
},
|
|
262
|
+
];
|
|
263
|
+
|
|
264
|
+
const product = this.products.find((candidate) => candidate.reference === message.product);
|
|
265
|
+
const trialDays = product ? trialPeriodDaysOf(product) : 0;
|
|
266
|
+
if (outcome === "completed" && this.trialEligible && trialDays > 0) {
|
|
267
|
+
replies.push({
|
|
268
|
+
event_name: "freeTrial_start",
|
|
269
|
+
product_identifier: message.product_identifier,
|
|
270
|
+
trial_end_date: new Date(Date.now() + trialDays * DAY_MS).toISOString(),
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
this.respond(replies);
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
case "restore": {
|
|
278
|
+
if ((this.options.restoreOutcome ?? "completed") === "completed") {
|
|
279
|
+
this.respond([{ event_name: "transaction_complete" }]);
|
|
280
|
+
}
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
case "request_permission": {
|
|
284
|
+
const status =
|
|
285
|
+
this.options.permissions?.[message.permission_type] ??
|
|
286
|
+
(this.options.onPermission
|
|
287
|
+
? await this.options.onPermission(message.permission_type)
|
|
288
|
+
: "granted");
|
|
289
|
+
|
|
290
|
+
this.respond([
|
|
291
|
+
{
|
|
292
|
+
event_name: "permission_result",
|
|
293
|
+
permission_type: message.permission_type,
|
|
294
|
+
request_id: message.request_id,
|
|
295
|
+
status,
|
|
296
|
+
},
|
|
297
|
+
]);
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
case "request_callback": {
|
|
301
|
+
const callback = this.options.callbacks?.[message.name];
|
|
302
|
+
const result =
|
|
303
|
+
typeof callback === "function"
|
|
304
|
+
? await callback(message.variables)
|
|
305
|
+
: (callback ??
|
|
306
|
+
(this.options.onCallback
|
|
307
|
+
? await this.options.onCallback(message.name, message.variables)
|
|
308
|
+
: { status: "success" as const }));
|
|
309
|
+
|
|
310
|
+
this.respond([
|
|
311
|
+
{
|
|
312
|
+
event_name: "callback_result",
|
|
313
|
+
request_id: message.request_id,
|
|
314
|
+
name: message.name,
|
|
315
|
+
status: result.status,
|
|
316
|
+
data: result.data,
|
|
317
|
+
},
|
|
318
|
+
]);
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
default:
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
private resolvePurchaseOutcome(context: SuperwallMockPurchaseContext) {
|
|
327
|
+
const outcome = this.options.purchaseOutcome ?? "completed";
|
|
328
|
+
return typeof outcome === "function" ? outcome(context) : outcome;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export const createMockSuperwallTransport = (options: SuperwallMockOptions = {}) =>
|
|
333
|
+
new MockSuperwallTransport(options);
|
package/src/products.ts
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ProductIdentifierAndType,
|
|
3
|
+
TIncomingMessageProducts,
|
|
4
|
+
TIncomingMessageTemplateVariables,
|
|
5
|
+
} from "@superwall/protocol";
|
|
6
|
+
|
|
7
|
+
export type SuperwallKnownProductVariables = {
|
|
8
|
+
identifier?: string;
|
|
9
|
+
price?: string;
|
|
10
|
+
rawPrice?: string | number;
|
|
11
|
+
period?: string;
|
|
12
|
+
periodAlt?: string;
|
|
13
|
+
localizedPeriod?: string;
|
|
14
|
+
periodly?: string;
|
|
15
|
+
periodDays?: string | number;
|
|
16
|
+
periodWeeks?: string | number;
|
|
17
|
+
periodMonths?: string | number;
|
|
18
|
+
periodYears?: string | number;
|
|
19
|
+
dailyPrice?: string;
|
|
20
|
+
weeklyPrice?: string;
|
|
21
|
+
monthlyPrice?: string;
|
|
22
|
+
yearlyPrice?: string;
|
|
23
|
+
trialPeriodPrice?: string;
|
|
24
|
+
rawTrialPeriodPrice?: string | number;
|
|
25
|
+
trialPeriodDailyPrice?: string;
|
|
26
|
+
trialPeriodWeeklyPrice?: string;
|
|
27
|
+
trialPeriodMonthlyPrice?: string;
|
|
28
|
+
trialPeriodYearlyPrice?: string;
|
|
29
|
+
trialPeriodDays?: string | number;
|
|
30
|
+
trialPeriodWeeks?: string | number;
|
|
31
|
+
trialPeriodMonths?: string | number;
|
|
32
|
+
trialPeriodYears?: string | number;
|
|
33
|
+
trialPeriodText?: string;
|
|
34
|
+
trialPeriodEndDate?: string;
|
|
35
|
+
currencyCode?: string;
|
|
36
|
+
currencySymbol?: string;
|
|
37
|
+
locale?: string;
|
|
38
|
+
languageCode?: string;
|
|
39
|
+
isSubscribed?: boolean;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type SuperwallProductVariables = SuperwallKnownProductVariables;
|
|
43
|
+
|
|
44
|
+
export type SuperwallProductSource = "sdk" | "inline" | "placeholder" | "unknown";
|
|
45
|
+
|
|
46
|
+
export type SuperwallProduct<
|
|
47
|
+
TVariables extends SuperwallProductVariables = SuperwallProductVariables,
|
|
48
|
+
> = {
|
|
49
|
+
reference: string;
|
|
50
|
+
product: string;
|
|
51
|
+
id?: string;
|
|
52
|
+
productId?: string;
|
|
53
|
+
productIdentifier?: string;
|
|
54
|
+
variables: TVariables;
|
|
55
|
+
source: SuperwallProductSource;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export type SuperwallProductInput<TProduct extends SuperwallProduct = SuperwallProduct> =
|
|
59
|
+
| string
|
|
60
|
+
| TProduct
|
|
61
|
+
| {
|
|
62
|
+
reference: string;
|
|
63
|
+
product?: string;
|
|
64
|
+
id?: string;
|
|
65
|
+
productId?: string;
|
|
66
|
+
productIdentifier?: string;
|
|
67
|
+
variables?: SuperwallProductVariables;
|
|
68
|
+
source?: SuperwallProductSource;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const legacyProductReferences = ["primary", "secondary", "tertiary"] as const;
|
|
72
|
+
|
|
73
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
74
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
75
|
+
|
|
76
|
+
export const createSuperwallProduct = (
|
|
77
|
+
input: SuperwallProductInput,
|
|
78
|
+
variables: SuperwallProductVariables = {},
|
|
79
|
+
): SuperwallProduct => {
|
|
80
|
+
if (typeof input === "string") {
|
|
81
|
+
return {
|
|
82
|
+
reference: input,
|
|
83
|
+
product: input,
|
|
84
|
+
variables,
|
|
85
|
+
source: "unknown",
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const reference = input.reference ?? input.product;
|
|
90
|
+
const productId = input.productId ?? input.productIdentifier ?? input.id;
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
reference,
|
|
94
|
+
product: input.product ?? reference,
|
|
95
|
+
id: input.id ?? productId,
|
|
96
|
+
productId,
|
|
97
|
+
productIdentifier: input.productIdentifier ?? productId,
|
|
98
|
+
variables: input.variables ?? variables,
|
|
99
|
+
source: input.source ?? "unknown",
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const productReferenceFromInput = (input: SuperwallProductInput) =>
|
|
104
|
+
typeof input === "string" ? input : input.reference;
|
|
105
|
+
|
|
106
|
+
export const productsByReference = <TProduct extends SuperwallProduct>(
|
|
107
|
+
products: readonly TProduct[],
|
|
108
|
+
): Readonly<Record<string, TProduct>> => {
|
|
109
|
+
const indexed: Record<string, TProduct> = {};
|
|
110
|
+
|
|
111
|
+
for (const product of products) {
|
|
112
|
+
indexed[product.reference] = product;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return indexed;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* SDK-delivered variable records are trusted, not validated: the declared
|
|
120
|
+
* shapes are the SDK's side of the protocol contract. Numeric-looking fields
|
|
121
|
+
* arrive as strings ("59.99", "7"), which is why they are typed
|
|
122
|
+
* `string | number` — coerce with `Number()` before doing arithmetic.
|
|
123
|
+
*/
|
|
124
|
+
const asProductVariables = (record: Record<string, unknown>): SuperwallProductVariables =>
|
|
125
|
+
record as SuperwallProductVariables;
|
|
126
|
+
|
|
127
|
+
export const productVariablesFromTemplateVariables = (
|
|
128
|
+
variables: TIncomingMessageTemplateVariables["variables"],
|
|
129
|
+
): Map<string, SuperwallProductVariables> => {
|
|
130
|
+
const result = new Map<string, SuperwallProductVariables>();
|
|
131
|
+
|
|
132
|
+
if (Array.isArray(variables.products)) {
|
|
133
|
+
for (const productEntry of variables.products) {
|
|
134
|
+
if (!isRecord(productEntry)) continue;
|
|
135
|
+
|
|
136
|
+
for (const [reference, productVariables] of Object.entries(productEntry)) {
|
|
137
|
+
if (isRecord(productVariables)) {
|
|
138
|
+
result.set(reference, asProductVariables(productVariables));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
for (const reference of legacyProductReferences) {
|
|
145
|
+
const productVariables = variables[reference];
|
|
146
|
+
if (isRecord(productVariables) && !result.has(reference)) {
|
|
147
|
+
result.set(reference, asProductVariables(productVariables));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return result;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
export const productsFromIncomingMessage = (
|
|
155
|
+
message: TIncomingMessageProducts,
|
|
156
|
+
): SuperwallProduct[] =>
|
|
157
|
+
message.products.map((product) =>
|
|
158
|
+
createSuperwallProduct({
|
|
159
|
+
reference: product.product,
|
|
160
|
+
product: product.product,
|
|
161
|
+
productId: product.productId,
|
|
162
|
+
productIdentifier: product.productId,
|
|
163
|
+
source: "sdk",
|
|
164
|
+
}),
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
export const mergeIncomingProducts = (
|
|
168
|
+
current: readonly SuperwallProduct[],
|
|
169
|
+
incoming: readonly ProductIdentifierAndType[],
|
|
170
|
+
): SuperwallProduct[] => {
|
|
171
|
+
const next = new Map<string, SuperwallProduct>();
|
|
172
|
+
|
|
173
|
+
for (const product of current) {
|
|
174
|
+
next.set(product.reference, product);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
for (const product of incoming) {
|
|
178
|
+
const existing = next.get(product.product);
|
|
179
|
+
next.set(product.product, {
|
|
180
|
+
...createSuperwallProduct({
|
|
181
|
+
reference: product.product,
|
|
182
|
+
product: product.product,
|
|
183
|
+
productId: product.productId,
|
|
184
|
+
productIdentifier: product.productId,
|
|
185
|
+
source: "sdk",
|
|
186
|
+
}),
|
|
187
|
+
variables: existing?.variables ?? {},
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return Array.from(next.values());
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
export const mergeProductVariables = (
|
|
195
|
+
current: readonly SuperwallProduct[],
|
|
196
|
+
variables: Map<string, SuperwallProductVariables>,
|
|
197
|
+
): SuperwallProduct[] => {
|
|
198
|
+
const next = new Map<string, SuperwallProduct>();
|
|
199
|
+
|
|
200
|
+
for (const product of current) {
|
|
201
|
+
next.set(product.reference, product);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
for (const [reference, productVariables] of variables) {
|
|
205
|
+
const existing = next.get(reference);
|
|
206
|
+
next.set(reference, {
|
|
207
|
+
...createSuperwallProduct(
|
|
208
|
+
existing ?? {
|
|
209
|
+
reference,
|
|
210
|
+
product: reference,
|
|
211
|
+
source: "sdk",
|
|
212
|
+
},
|
|
213
|
+
productVariables,
|
|
214
|
+
),
|
|
215
|
+
variables: productVariables,
|
|
216
|
+
source: existing?.source ?? "sdk",
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return Array.from(next.values());
|
|
221
|
+
};
|