@superwall/runtime 2.6.0-beta.1 → 2.6.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 CHANGED
@@ -73,7 +73,7 @@ yourself (tests, tools), or `transportKind` to force one.
73
73
 
74
74
  - `{ status: "completed", product, message }` from `transaction_complete`
75
75
  - `{ status: "abandoned", product, message }` from `transaction_abandon`
76
- - `{ status: "failed", product, reason: "sdk" }` from `transaction_fail` or
76
+ - `{ status: "failed", product, reason: "unknown" }` from `transaction_fail` or
77
77
  `transaction_timeout` (iOS reports these; Android does not, so there a
78
78
  failed purchase stays pending until the user abandons or completes)
79
79
  - `{ status: "failed", product, reason: "superseded" }` when a newer purchase
@@ -36,8 +36,9 @@ export type SuperwallTeleportOptions<TStripeReference extends string = string, T
36
36
  /**
37
37
  * What the web page does once the purchase completes: `"confirmation"`
38
38
  * shows "You're all set" with a Return to App button, `"direct"` opens the
39
- * app at once. Either way the app is opened through its universal link,
40
- * falling back to its URL scheme when the link doesn't open it.
39
+ * app at once, and shows that screen only when the app didn't open. Either
40
+ * way the app is opened through its universal link, falling back to its
41
+ * URL scheme when the link doesn't open it.
41
42
  * Default: `"confirmation"`.
42
43
  */
43
44
  completion?: "confirmation" | "direct";
@@ -4,6 +4,7 @@ import { dismissesOnPurchase, normalizePlatform, productIdFor } from "../config"
4
4
  import { createTranslator, languageCandidates, resolveLocale } from "../localization";
5
5
  import { createProductDeriver, createSuperwallProduct, findProductByStoreId, mergeIncomingProducts, mergeProductVariables, productReferenceFromInput, productVariablesFromTemplateVariables, productsByReference, } from "../products";
6
6
  import { snapshotPatchFor } from "./incoming";
7
+ import { recordFromVariables } from "./variables";
7
8
  import * as outgoing from "./outgoing";
8
9
  import { createPurchaseTracker } from "./purchases";
9
10
  import { createRestoreTracker } from "./restores";
@@ -75,6 +76,33 @@ const createSession = (options) => {
75
76
  for (const listener of listeners)
76
77
  listener();
77
78
  };
79
+ const mergeDeliveredProducts = (delivered) => {
80
+ const variables = productVariablesFromTemplateVariables(delivered);
81
+ for (const [reference, values] of variables) {
82
+ const baked = inline[reference];
83
+ if (baked)
84
+ variables.set(reference, { ...baked, ...values });
85
+ }
86
+ if (variables.size > 0)
87
+ raw = mergeProductVariables(raw, variables);
88
+ };
89
+ if (options.variables !== undefined)
90
+ mergeDeliveredProducts(options.variables);
91
+ if (options.variables !== undefined || options.introductoryOffer !== undefined) {
92
+ commit({
93
+ ...(options.variables === undefined
94
+ ? {}
95
+ : {
96
+ variables: options.variables,
97
+ device: recordFromVariables(options.variables, "device"),
98
+ user: recordFromVariables(options.variables, "user"),
99
+ params: recordFromVariables(options.variables, "params"),
100
+ }),
101
+ ...(options.introductoryOffer === undefined
102
+ ? {}
103
+ : { introductoryOffer: options.introductoryOffer }),
104
+ });
105
+ }
78
106
  const post = (message) => {
79
107
  transport.postMessage(message);
80
108
  };
@@ -137,17 +165,9 @@ const createSession = (options) => {
137
165
  case "products":
138
166
  raw = mergeIncomingProducts(raw, message.products);
139
167
  break;
140
- case "template_variables": {
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
- }
147
- if (variables.size > 0)
148
- raw = mergeProductVariables(raw, variables);
168
+ case "template_variables":
169
+ mergeDeliveredProducts(message.variables);
149
170
  break;
150
- }
151
171
  case "freeTrial_start": {
152
172
  const reminder = trialReminderFor(snapshot.config, message, {
153
173
  products: raw,
@@ -166,7 +186,7 @@ const createSession = (options) => {
166
186
  case "transaction_fail":
167
187
  case "transaction_timeout": {
168
188
  const pending = purchases.find(message);
169
- const failure = pending && purchases.fail(pending.product.reference, "sdk");
189
+ const failure = pending && purchases.fail(pending.product.reference, "unknown");
170
190
  if (failure)
171
191
  recordFailure(failure);
172
192
  break;
@@ -35,7 +35,7 @@ export type SuperwallTransaction<TProduct extends SuperwallProduct = SuperwallPr
35
35
  export type SuperwallPurchaseFailure<out TProduct extends SuperwallProduct = SuperwallProduct> = {
36
36
  status: "failed";
37
37
  product: TProduct;
38
- reason: "sdk" | "superseded";
38
+ reason: "unknown" | "superseded";
39
39
  };
40
40
  export type SuperwallRestoreState = {
41
41
  status: "started" | "completed" | "failed";
@@ -128,6 +128,8 @@ export type SuperwallSessionStartOptions = {
128
128
  export type SuperwallSessionOptions = {
129
129
  config?: SuperwallConfig;
130
130
  inlineProducts?: Readonly<Record<string, SuperwallProductVariables>>;
131
+ variables?: Readonly<Record<string, unknown>>;
132
+ introductoryOffer?: SuperwallIntroductoryOfferState;
131
133
  transport?: ITransport;
132
134
  transportKind?: TransportKind;
133
135
  onDecodeError?: (error: unknown) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superwall/runtime",
3
- "version": "2.6.0-beta.1",
3
+ "version": "2.6.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": "2.6.0-beta.1"
42
+ "@superwall/protocol": "2.6.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@superwall/host": "workspace:*",
@@ -41,8 +41,9 @@ export type SuperwallTeleportOptions<
41
41
  /**
42
42
  * What the web page does once the purchase completes: `"confirmation"`
43
43
  * shows "You're all set" with a Return to App button, `"direct"` opens the
44
- * app at once. Either way the app is opened through its universal link,
45
- * falling back to its URL scheme when the link doesn't open it.
44
+ * app at once, and shows that screen only when the app didn't open. Either
45
+ * way the app is opened through its universal link, falling back to its
46
+ * URL scheme when the link doesn't open it.
46
47
  * Default: `"confirmation"`.
47
48
  */
48
49
  completion?: "confirmation" | "direct";
@@ -1,5 +1,6 @@
1
1
  import type {
2
2
  TIncomingMessage,
3
+ TIncomingMessageTemplateVariables,
3
4
  TIncomingMessageTransactionAbandon,
4
5
  TIncomingMessageTransactionComplete,
5
6
  TOutgoingMessage,
@@ -21,6 +22,7 @@ import {
21
22
  type SuperwallProductInput,
22
23
  } from "../products";
23
24
  import { snapshotPatchFor } from "./incoming";
25
+ import { recordFromVariables } from "./variables";
24
26
  import * as outgoing from "./outgoing";
25
27
  import { createPurchaseTracker } from "./purchases";
26
28
  import { createRestoreTracker } from "./restores";
@@ -120,6 +122,33 @@ const createSession = (options: SuperwallSessionOptions): SuperwallSession => {
120
122
  for (const listener of listeners) listener();
121
123
  };
122
124
 
125
+ const mergeDeliveredProducts = (delivered: TIncomingMessageTemplateVariables["variables"]) => {
126
+ const variables = productVariablesFromTemplateVariables(delivered);
127
+ for (const [reference, values] of variables) {
128
+ const baked = inline[reference];
129
+ if (baked) variables.set(reference, { ...baked, ...values });
130
+ }
131
+ if (variables.size > 0) raw = mergeProductVariables(raw, variables);
132
+ };
133
+
134
+ if (options.variables !== undefined) mergeDeliveredProducts(options.variables);
135
+
136
+ if (options.variables !== undefined || options.introductoryOffer !== undefined) {
137
+ commit({
138
+ ...(options.variables === undefined
139
+ ? {}
140
+ : {
141
+ variables: options.variables,
142
+ device: recordFromVariables(options.variables, "device"),
143
+ user: recordFromVariables(options.variables, "user"),
144
+ params: recordFromVariables(options.variables, "params"),
145
+ }),
146
+ ...(options.introductoryOffer === undefined
147
+ ? {}
148
+ : { introductoryOffer: options.introductoryOffer }),
149
+ });
150
+ }
151
+
123
152
  const post = (message: TOutgoingMessage) => {
124
153
  transport.postMessage(message);
125
154
  };
@@ -196,15 +225,9 @@ const createSession = (options: SuperwallSessionOptions): SuperwallSession => {
196
225
  case "products":
197
226
  raw = mergeIncomingProducts(raw, message.products);
198
227
  break;
199
- case "template_variables": {
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
- }
205
- if (variables.size > 0) raw = mergeProductVariables(raw, variables);
228
+ case "template_variables":
229
+ mergeDeliveredProducts(message.variables);
206
230
  break;
207
- }
208
231
  case "freeTrial_start": {
209
232
  const reminder = trialReminderFor(snapshot.config, message, {
210
233
  products: raw,
@@ -222,7 +245,7 @@ const createSession = (options: SuperwallSessionOptions): SuperwallSession => {
222
245
  case "transaction_fail":
223
246
  case "transaction_timeout": {
224
247
  const pending = purchases.find(message);
225
- const failure = pending && purchases.fail(pending.product.reference, "sdk");
248
+ const failure = pending && purchases.fail(pending.product.reference, "unknown");
226
249
  if (failure) recordFailure(failure);
227
250
  break;
228
251
  }
@@ -72,7 +72,7 @@ export type SuperwallTransaction<TProduct extends SuperwallProduct = SuperwallPr
72
72
  export type SuperwallPurchaseFailure<out TProduct extends SuperwallProduct = SuperwallProduct> = {
73
73
  status: "failed";
74
74
  product: TProduct;
75
- reason: "sdk" | "superseded";
75
+ reason: "unknown" | "superseded";
76
76
  };
77
77
 
78
78
  export type SuperwallRestoreState = {
@@ -177,6 +177,8 @@ export type SuperwallSessionStartOptions = {
177
177
  export type SuperwallSessionOptions = {
178
178
  config?: SuperwallConfig;
179
179
  inlineProducts?: Readonly<Record<string, SuperwallProductVariables>>;
180
+ variables?: Readonly<Record<string, unknown>>;
181
+ introductoryOffer?: SuperwallIntroductoryOfferState;
180
182
  transport?: ITransport;
181
183
  transportKind?: TransportKind;
182
184
  onDecodeError?: (error: unknown) => void;