@superwall/runtime 1.7.0 → 2.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/dist/session/index.d.ts +1 -1
- package/dist/session/index.js +35 -8
- package/dist/session/types.d.ts +12 -2
- package/dist/session/variables.d.ts +8 -0
- package/package.json +2 -2
- package/src/session/index.ts +33 -8
- package/src/session/types.ts +16 -2
- package/src/session/variables.ts +9 -0
package/dist/session/index.d.ts
CHANGED
|
@@ -5,4 +5,4 @@ export { PING_VERSION, type PageViewInput } from "./outgoing";
|
|
|
5
5
|
export type { CallbackBehavior } from "./requests";
|
|
6
6
|
export { buildSuperwallState, type BuildSuperwallStateOptions, type SuperwallCallbackState, type SuperwallCallbackStates, type SuperwallPermissionStatuses, type SuperwallSdkState, } from "./state";
|
|
7
7
|
export * from "./types";
|
|
8
|
-
export { recordFromVariables, type SuperwallCapability, type SuperwallCustomerInfo, type SuperwallDeviceEntitlement, type SuperwallDeviceTransaction, type SuperwallDeviceVariables, type SuperwallExperiment, type SuperwallPlacementParams, type SuperwallUserVariables, } from "./variables";
|
|
8
|
+
export { recordFromVariables, type SuperwallCapability, type SuperwallCustomerInfo, type SuperwallDeviceEntitlement, type SuperwallDeviceTransaction, type SuperwallDeviceVariables, type SuperwallExperiment, type SuperwallPlacementParams, type SuperwallSafeAreaInsets, type SuperwallUserVariables, } from "./variables";
|
package/dist/session/index.js
CHANGED
|
@@ -18,14 +18,24 @@ const createSession = (options) => {
|
|
|
18
18
|
const restores = createRestoreTracker();
|
|
19
19
|
const requests = createRequestTracker();
|
|
20
20
|
const derive = createProductDeriver();
|
|
21
|
-
|
|
21
|
+
const inline = options.inlineProducts ?? {};
|
|
22
|
+
let raw = Object.entries(inline).map(([reference, variables]) => {
|
|
23
|
+
const slot = options.config?.products?.[reference];
|
|
24
|
+
return createSuperwallProduct({
|
|
25
|
+
reference,
|
|
26
|
+
productId: slot === undefined ? undefined : productIdFor(slot),
|
|
27
|
+
variables,
|
|
28
|
+
source: "inline",
|
|
29
|
+
});
|
|
30
|
+
});
|
|
22
31
|
let localeOverride;
|
|
23
32
|
let translator = createTranslator(options.config?.localization, resolveLocale(undefined, options.config?.localization));
|
|
33
|
+
const seeded = derive(raw, languageCandidates(undefined));
|
|
24
34
|
let snapshot = {
|
|
25
35
|
status: "idle",
|
|
26
36
|
config: options.config,
|
|
27
|
-
products:
|
|
28
|
-
productsByReference:
|
|
37
|
+
products: seeded,
|
|
38
|
+
productsByReference: productsByReference(seeded),
|
|
29
39
|
variables: {},
|
|
30
40
|
device: {},
|
|
31
41
|
user: {},
|
|
@@ -115,7 +125,11 @@ const createSession = (options) => {
|
|
|
115
125
|
lastPurchaseFailure: undefined,
|
|
116
126
|
});
|
|
117
127
|
};
|
|
128
|
+
let hostAnswered = false;
|
|
129
|
+
let unconfirmed;
|
|
118
130
|
const handleIncoming = (message) => {
|
|
131
|
+
if (message.event_name === "template_variables")
|
|
132
|
+
hostAnswered = true;
|
|
119
133
|
switch (message.event_name) {
|
|
120
134
|
case "paywall_open":
|
|
121
135
|
purchases.failAll("superseded");
|
|
@@ -125,6 +139,11 @@ const createSession = (options) => {
|
|
|
125
139
|
break;
|
|
126
140
|
case "template_variables": {
|
|
127
141
|
const variables = productVariablesFromTemplateVariables(message.variables);
|
|
142
|
+
for (const [reference, delivered] of variables) {
|
|
143
|
+
const baked = inline[reference];
|
|
144
|
+
if (baked)
|
|
145
|
+
variables.set(reference, { ...baked, ...delivered });
|
|
146
|
+
}
|
|
128
147
|
if (variables.size > 0)
|
|
129
148
|
raw = mergeProductVariables(raw, variables);
|
|
130
149
|
break;
|
|
@@ -174,7 +193,15 @@ const createSession = (options) => {
|
|
|
174
193
|
default:
|
|
175
194
|
break;
|
|
176
195
|
}
|
|
177
|
-
|
|
196
|
+
const patch = snapshotPatchFor(snapshot, message) ?? {};
|
|
197
|
+
if (message.event_name === "template_variables" && unconfirmed) {
|
|
198
|
+
const user = { ...patch.user, ...unconfirmed };
|
|
199
|
+
unconfirmed = undefined;
|
|
200
|
+
commit({ ...patch, user, variables: { ...patch.variables, user } });
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
commit(patch);
|
|
204
|
+
}
|
|
178
205
|
emit(message.event_name, message);
|
|
179
206
|
emit("message", message);
|
|
180
207
|
};
|
|
@@ -256,10 +283,10 @@ const createSession = (options) => {
|
|
|
256
283
|
if (entries.length === 0)
|
|
257
284
|
return;
|
|
258
285
|
post(outgoing.userAttributeUpdated(entries));
|
|
259
|
-
const
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
};
|
|
286
|
+
const written = Object.fromEntries(entries.map(({ key, value }) => [key, value]));
|
|
287
|
+
if (!hostAnswered)
|
|
288
|
+
unconfirmed = { ...unconfirmed, ...written };
|
|
289
|
+
const user = { ...snapshot.user, ...written };
|
|
263
290
|
commit({ user, variables: { ...snapshot.variables, user } });
|
|
264
291
|
},
|
|
265
292
|
requestStoreReview: (reviewType) => post(outgoing.requestStoreReview(reviewType)),
|
package/dist/session/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ITransport, TEventTrackingBehavior, TIncomingMessage, TIncomingMessageCallbackResult, TIncomingMessageTransactionAbandon, TIncomingMessageTransactionComplete, TOutgoingMessage, TPermissionStatus, TPermissionTypeMessage, TransportKind } from "@superwall/protocol";
|
|
2
|
-
import type { SuperwallConfig, SuperwallPostPurchase } from "../config";
|
|
2
|
+
import type { SuperwallCheckoutMode, SuperwallConfig, SuperwallPostPurchase } from "../config";
|
|
3
3
|
import type { SuperwallTranslateValues } from "../localization";
|
|
4
|
-
import type { SuperwallProduct, SuperwallProductInput } from "../products";
|
|
4
|
+
import type { SuperwallProduct, SuperwallProductInput, SuperwallProductVariables } from "../products";
|
|
5
5
|
import type { Outgoing, PageViewInput } from "./outgoing";
|
|
6
6
|
import type { CallbackBehavior } from "./requests";
|
|
7
7
|
import type { SuperwallCallbackStates, SuperwallPermissionStatuses, SuperwallSdkState } from "./state";
|
|
@@ -87,6 +87,15 @@ export type SuperwallPurchaseOptions = {
|
|
|
87
87
|
* store purchases, which have no Stripe object to write to.
|
|
88
88
|
*/
|
|
89
89
|
stripeMetadata?: Readonly<Record<string, string>>;
|
|
90
|
+
/**
|
|
91
|
+
* How this purchase checks out on the web when the product is a Stripe
|
|
92
|
+
* product, overriding the config's `checkout` mode for this call — a
|
|
93
|
+
* button that always opens the sheet on a paywall whose default is
|
|
94
|
+
* `"applePay"`. Inside an iOS app a Stripe purchase always opens the
|
|
95
|
+
* hosted checkout whatever this says. Ignored by store purchases.
|
|
96
|
+
* Default: the config's `checkout` mode.
|
|
97
|
+
*/
|
|
98
|
+
checkout?: SuperwallCheckoutMode;
|
|
90
99
|
};
|
|
91
100
|
export type SuperwallCallbackOptions = {
|
|
92
101
|
behavior?: CallbackBehavior;
|
|
@@ -103,6 +112,7 @@ export type SuperwallSessionStartOptions = {
|
|
|
103
112
|
};
|
|
104
113
|
export type SuperwallSessionOptions = {
|
|
105
114
|
config?: SuperwallConfig;
|
|
115
|
+
inlineProducts?: Readonly<Record<string, SuperwallProductVariables>>;
|
|
106
116
|
transport?: ITransport;
|
|
107
117
|
transportKind?: TransportKind;
|
|
108
118
|
onDecodeError?: (error: unknown) => void;
|
|
@@ -50,6 +50,7 @@ export type SuperwallDeviceVariables = {
|
|
|
50
50
|
bundleId?: string;
|
|
51
51
|
osVersion?: string;
|
|
52
52
|
deviceModel?: string;
|
|
53
|
+
simulatorModel?: string;
|
|
53
54
|
deviceLocale?: string;
|
|
54
55
|
preferredLocale?: string;
|
|
55
56
|
deviceLanguageCode?: string;
|
|
@@ -98,6 +99,13 @@ export type SuperwallDeviceVariables = {
|
|
|
98
99
|
swiftVersion?: string;
|
|
99
100
|
compilerVersion?: string;
|
|
100
101
|
localResourceIds?: string;
|
|
102
|
+
safeAreaInsets?: SuperwallSafeAreaInsets;
|
|
103
|
+
};
|
|
104
|
+
export type SuperwallSafeAreaInsets = {
|
|
105
|
+
top: number;
|
|
106
|
+
right: number;
|
|
107
|
+
bottom: number;
|
|
108
|
+
left: number;
|
|
101
109
|
};
|
|
102
110
|
export type SuperwallUserVariables = {
|
|
103
111
|
seed?: number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superwall/runtime",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.0",
|
|
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": "1.
|
|
42
|
+
"@superwall/protocol": "2.1.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@superwall/host": "workspace:*",
|
package/src/session/index.ts
CHANGED
|
@@ -54,18 +54,28 @@ const createSession = (options: SuperwallSessionOptions): SuperwallSession => {
|
|
|
54
54
|
const requests = createRequestTracker();
|
|
55
55
|
const derive = createProductDeriver();
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
const inline = options.inlineProducts ?? {};
|
|
58
|
+
let raw: readonly SuperwallProduct[] = Object.entries(inline).map(([reference, variables]) => {
|
|
59
|
+
const slot = options.config?.products?.[reference];
|
|
60
|
+
return createSuperwallProduct({
|
|
61
|
+
reference,
|
|
62
|
+
productId: slot === undefined ? undefined : productIdFor(slot),
|
|
63
|
+
variables,
|
|
64
|
+
source: "inline",
|
|
65
|
+
});
|
|
66
|
+
});
|
|
58
67
|
let localeOverride: string | undefined;
|
|
59
68
|
let translator = createTranslator(
|
|
60
69
|
options.config?.localization,
|
|
61
70
|
resolveLocale(undefined, options.config?.localization),
|
|
62
71
|
);
|
|
72
|
+
const seeded = derive(raw, languageCandidates(undefined));
|
|
63
73
|
|
|
64
74
|
let snapshot: SuperwallRuntimeSnapshot = {
|
|
65
75
|
status: "idle",
|
|
66
76
|
config: options.config,
|
|
67
|
-
products:
|
|
68
|
-
productsByReference:
|
|
77
|
+
products: seeded,
|
|
78
|
+
productsByReference: productsByReference(seeded),
|
|
69
79
|
variables: {},
|
|
70
80
|
device: {},
|
|
71
81
|
user: {},
|
|
@@ -174,7 +184,11 @@ const createSession = (options: SuperwallSessionOptions): SuperwallSession => {
|
|
|
174
184
|
});
|
|
175
185
|
};
|
|
176
186
|
|
|
187
|
+
let hostAnswered = false;
|
|
188
|
+
let unconfirmed: Record<string, string> | undefined;
|
|
189
|
+
|
|
177
190
|
const handleIncoming = (message: TIncomingMessage) => {
|
|
191
|
+
if (message.event_name === "template_variables") hostAnswered = true;
|
|
178
192
|
switch (message.event_name) {
|
|
179
193
|
case "paywall_open":
|
|
180
194
|
purchases.failAll("superseded");
|
|
@@ -184,6 +198,10 @@ const createSession = (options: SuperwallSessionOptions): SuperwallSession => {
|
|
|
184
198
|
break;
|
|
185
199
|
case "template_variables": {
|
|
186
200
|
const variables = productVariablesFromTemplateVariables(message.variables);
|
|
201
|
+
for (const [reference, delivered] of variables) {
|
|
202
|
+
const baked = inline[reference];
|
|
203
|
+
if (baked) variables.set(reference, { ...baked, ...delivered });
|
|
204
|
+
}
|
|
187
205
|
if (variables.size > 0) raw = mergeProductVariables(raw, variables);
|
|
188
206
|
break;
|
|
189
207
|
}
|
|
@@ -231,7 +249,14 @@ const createSession = (options: SuperwallSessionOptions): SuperwallSession => {
|
|
|
231
249
|
break;
|
|
232
250
|
}
|
|
233
251
|
|
|
234
|
-
|
|
252
|
+
const patch = snapshotPatchFor(snapshot, message) ?? {};
|
|
253
|
+
if (message.event_name === "template_variables" && unconfirmed) {
|
|
254
|
+
const user = { ...patch.user, ...unconfirmed };
|
|
255
|
+
unconfirmed = undefined;
|
|
256
|
+
commit({ ...patch, user, variables: { ...patch.variables, user } });
|
|
257
|
+
} else {
|
|
258
|
+
commit(patch);
|
|
259
|
+
}
|
|
235
260
|
emit(message.event_name, message);
|
|
236
261
|
emit("message", message);
|
|
237
262
|
};
|
|
@@ -314,10 +339,9 @@ const createSession = (options: SuperwallSessionOptions): SuperwallSession => {
|
|
|
314
339
|
}));
|
|
315
340
|
if (entries.length === 0) return;
|
|
316
341
|
post(outgoing.userAttributeUpdated(entries));
|
|
317
|
-
const
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
};
|
|
342
|
+
const written = Object.fromEntries(entries.map(({ key, value }) => [key, value]));
|
|
343
|
+
if (!hostAnswered) unconfirmed = { ...unconfirmed, ...written };
|
|
344
|
+
const user = { ...snapshot.user, ...written };
|
|
321
345
|
commit({ user, variables: { ...snapshot.variables, user } });
|
|
322
346
|
},
|
|
323
347
|
requestStoreReview: (reviewType) => post(outgoing.requestStoreReview(reviewType)),
|
|
@@ -376,5 +400,6 @@ export {
|
|
|
376
400
|
type SuperwallDeviceVariables,
|
|
377
401
|
type SuperwallExperiment,
|
|
378
402
|
type SuperwallPlacementParams,
|
|
403
|
+
type SuperwallSafeAreaInsets,
|
|
379
404
|
type SuperwallUserVariables,
|
|
380
405
|
} from "./variables";
|
package/src/session/types.ts
CHANGED
|
@@ -10,9 +10,13 @@ import type {
|
|
|
10
10
|
TPermissionTypeMessage,
|
|
11
11
|
TransportKind,
|
|
12
12
|
} from "@superwall/protocol";
|
|
13
|
-
import type { SuperwallConfig, SuperwallPostPurchase } from "../config";
|
|
13
|
+
import type { SuperwallCheckoutMode, SuperwallConfig, SuperwallPostPurchase } from "../config";
|
|
14
14
|
import type { SuperwallTranslateValues } from "../localization";
|
|
15
|
-
import type {
|
|
15
|
+
import type {
|
|
16
|
+
SuperwallProduct,
|
|
17
|
+
SuperwallProductInput,
|
|
18
|
+
SuperwallProductVariables,
|
|
19
|
+
} from "../products";
|
|
16
20
|
import type { Outgoing, PageViewInput } from "./outgoing";
|
|
17
21
|
import type { CallbackBehavior } from "./requests";
|
|
18
22
|
import type {
|
|
@@ -121,6 +125,15 @@ export type SuperwallPurchaseOptions = {
|
|
|
121
125
|
* store purchases, which have no Stripe object to write to.
|
|
122
126
|
*/
|
|
123
127
|
stripeMetadata?: Readonly<Record<string, string>>;
|
|
128
|
+
/**
|
|
129
|
+
* How this purchase checks out on the web when the product is a Stripe
|
|
130
|
+
* product, overriding the config's `checkout` mode for this call — a
|
|
131
|
+
* button that always opens the sheet on a paywall whose default is
|
|
132
|
+
* `"applePay"`. Inside an iOS app a Stripe purchase always opens the
|
|
133
|
+
* hosted checkout whatever this says. Ignored by store purchases.
|
|
134
|
+
* Default: the config's `checkout` mode.
|
|
135
|
+
*/
|
|
136
|
+
checkout?: SuperwallCheckoutMode;
|
|
124
137
|
};
|
|
125
138
|
|
|
126
139
|
export type SuperwallCallbackOptions = {
|
|
@@ -142,6 +155,7 @@ export type SuperwallSessionStartOptions = {
|
|
|
142
155
|
|
|
143
156
|
export type SuperwallSessionOptions = {
|
|
144
157
|
config?: SuperwallConfig;
|
|
158
|
+
inlineProducts?: Readonly<Record<string, SuperwallProductVariables>>;
|
|
145
159
|
transport?: ITransport;
|
|
146
160
|
transportKind?: TransportKind;
|
|
147
161
|
onDecodeError?: (error: unknown) => void;
|
package/src/session/variables.ts
CHANGED
|
@@ -55,6 +55,7 @@ export type SuperwallDeviceVariables = {
|
|
|
55
55
|
bundleId?: string;
|
|
56
56
|
osVersion?: string;
|
|
57
57
|
deviceModel?: string;
|
|
58
|
+
simulatorModel?: string;
|
|
58
59
|
deviceLocale?: string;
|
|
59
60
|
preferredLocale?: string;
|
|
60
61
|
deviceLanguageCode?: string;
|
|
@@ -103,6 +104,14 @@ export type SuperwallDeviceVariables = {
|
|
|
103
104
|
swiftVersion?: string;
|
|
104
105
|
compilerVersion?: string;
|
|
105
106
|
localResourceIds?: string;
|
|
107
|
+
safeAreaInsets?: SuperwallSafeAreaInsets;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export type SuperwallSafeAreaInsets = {
|
|
111
|
+
top: number;
|
|
112
|
+
right: number;
|
|
113
|
+
bottom: number;
|
|
114
|
+
left: number;
|
|
106
115
|
};
|
|
107
116
|
|
|
108
117
|
export type SuperwallUserVariables = {
|