@superwall/runtime 1.4.0 → 1.5.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 +17 -1
- package/dist/config/allowedHosts.d.ts +61 -0
- package/dist/config/allowedHosts.js +71 -0
- package/dist/config/checkout.d.ts +10 -3
- package/dist/config/checkout.js +6 -0
- package/dist/config/index.d.ts +34 -6
- package/dist/config/index.js +4 -1
- package/dist/config/insets.d.ts +25 -0
- package/dist/config/insets.js +23 -0
- package/dist/config/postPurchase.d.ts +29 -0
- package/dist/config/postPurchase.js +6 -0
- package/dist/session/index.js +16 -2
- package/dist/session/outgoing.d.ts +1 -0
- package/dist/session/outgoing.js +1 -0
- package/dist/session/types.d.ts +15 -2
- package/package.json +2 -2
- package/src/config/allowedHosts.ts +115 -0
- package/src/config/checkout.ts +18 -4
- package/src/config/index.ts +52 -5
- package/src/config/insets.ts +51 -0
- package/src/config/postPurchase.ts +30 -0
- package/src/session/index.ts +15 -2
- package/src/session/outgoing.ts +4 -0
- package/src/session/types.ts +16 -2
package/README.md
CHANGED
|
@@ -36,7 +36,7 @@ a reply; everything in its snapshot arrived from a host.
|
|
|
36
36
|
| Directory | Owns |
|
|
37
37
|
| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
38
38
|
| `src/bridge/` | The wire: transport detection (`?platform=android`, `?transport=editor`, else WebKit), `detectSuperwallHost`, base64 batch decoding for `accept64` |
|
|
39
|
-
| `src/config/` | The `SuperwallConfig` contract a running paywall reads, one file per concern (products, checkout, presentation, background, notifications, localization)
|
|
39
|
+
| `src/config/` | The `SuperwallConfig` contract a running paywall reads, one file per concern (products, checkout, presentation, background, insets, notifications, localization) |
|
|
40
40
|
| `src/localization/` | Locale resolution, the `t()` catalog, and `periods.ts` — the editor's period translation table, copied verbatim, with its lookup code trimmed of unreachable branches |
|
|
41
41
|
| `src/products/` | The product model, merging of `products` + `template_variables`, and derived variables (localized `period`/`periodly`, `trialPeriodEndDate`) |
|
|
42
42
|
| `src/session/` | `createSuperwallSession`: outgoing message builders, purchase and request trackers, trial reminders, the incoming reducer, and the state `window.app.getAllState()` returns |
|
|
@@ -81,6 +81,12 @@ yourself (tests, tools), or `transportKind` to force one.
|
|
|
81
81
|
|
|
82
82
|
The paywall runs no timer of its own: the SDK owns purchase timing.
|
|
83
83
|
|
|
84
|
+
What happens after a completed purchase is `config.postPurchase` (`"dismiss"`,
|
|
85
|
+
`"stay"`, or the web-only `"redeem"` / `{ redirect }`), overridable per call
|
|
86
|
+
with `purchase(ref, { postPurchase })`; natively it becomes `should_dismiss`
|
|
87
|
+
on the purchase message. `purchase(ref, { stripeMetadata })` is carried to web
|
|
88
|
+
checkout by the framework and ignored by store purchases.
|
|
89
|
+
|
|
84
90
|
A transaction is attributed to a pending purchase by reference, then by store
|
|
85
91
|
identifier, then, for a bare transaction, to the only pending purchase.
|
|
86
92
|
|
|
@@ -91,6 +97,16 @@ identifier, then, for a bare transaction, to the only pending purchase.
|
|
|
91
97
|
`restore_complete`, `restore_fail` events. iOS reports all three; Android
|
|
92
98
|
reports only `restore_fail`.
|
|
93
99
|
|
|
100
|
+
### User attributes
|
|
101
|
+
|
|
102
|
+
`setUserAttributes({ email: "ada@example.com", plan: "pro" })` sends
|
|
103
|
+
`user_attribute_updated` — the wire the editor's "Set User Attribute" action
|
|
104
|
+
uses, so every host already merges and persists it — and applies the same
|
|
105
|
+
merge to `snapshot.user` (and `getAllState().user`) immediately. Hosts never
|
|
106
|
+
send variables back after an attribute update, so without the local merge the
|
|
107
|
+
paywall would keep reading the user as it was before its own action. Values
|
|
108
|
+
travel as strings; an empty object sends nothing.
|
|
109
|
+
|
|
94
110
|
### Introductory offers
|
|
95
111
|
|
|
96
112
|
The SDK signals eligibility via `template_substitutions_prefix`
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hosts this surface is allowed to reach over the network.
|
|
3
|
+
*
|
|
4
|
+
* A published surface runs under a Content-Security-Policy the platform
|
|
5
|
+
* enforces at serve time, and by default it may only talk to Superwall and
|
|
6
|
+
* Stripe. Anything else — a customer's own API, a lead-capture endpoint, a
|
|
7
|
+
* custom analytics collector — has to be named here, or the browser refuses
|
|
8
|
+
* the request before it reaches the network.
|
|
9
|
+
*
|
|
10
|
+
* The allowlist is deliberately narrow rather than absent. The policy permits
|
|
11
|
+
* `unsafe-eval` and `blob:` scripts (the bundler output needs both), so the
|
|
12
|
+
* set of reachable hosts is what keeps a published surface from fetching and
|
|
13
|
+
* evaluating code nobody reviewed. An open policy would make the bytes that
|
|
14
|
+
* run unknowable; a named host keeps them attributable.
|
|
15
|
+
*
|
|
16
|
+
* Write bare hostnames — `https` is implied and enforced:
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* export default definePaywall({
|
|
20
|
+
* allowedHosts: ["api.example.com"],
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* A full `https://` origin is accepted too and normalizes to the same thing,
|
|
25
|
+
* so pasting a URL from a fetch call does the expected thing.
|
|
26
|
+
*/
|
|
27
|
+
export type SuperwallAllowedHostsConfig = readonly string[];
|
|
28
|
+
export type ResolvedAllowedHosts = {
|
|
29
|
+
/** CSP source expressions, `https://host[:port]`, deduped and sorted. */
|
|
30
|
+
origins: string[];
|
|
31
|
+
/** Entries that could not be read as a host, verbatim, in input order. */
|
|
32
|
+
invalid: string[];
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* How many hosts one surface may name. Not a technical ceiling — a surface
|
|
36
|
+
* reaching more than a handful of origins is usually a sign the work belongs
|
|
37
|
+
* on a backend, and every entry widens the policy for every shopper.
|
|
38
|
+
*/
|
|
39
|
+
export declare const MAX_ALLOWED_HOSTS = 20;
|
|
40
|
+
/**
|
|
41
|
+
* The loopback interface, which is never a valid entry.
|
|
42
|
+
*
|
|
43
|
+
* Not a security judgement so much as an arithmetic one: a pushed paywall
|
|
44
|
+
* runs on somebody else's device, where `localhost` is *their* machine, not
|
|
45
|
+
* the developer's. An entry naming it can only ever widen the policy for
|
|
46
|
+
* every shopper in exchange for a request that cannot succeed.
|
|
47
|
+
*
|
|
48
|
+
* Local development does not need it either — `superwall dev` serves the
|
|
49
|
+
* surface with no policy at all, so a `fetch` to a local API works there
|
|
50
|
+
* whether or not it is named here. To exercise a local API from a *pushed*
|
|
51
|
+
* build, put it behind an https tunnel and name that hostname.
|
|
52
|
+
*
|
|
53
|
+
* Detected only to say so plainly; see `allowedHostsWire`.
|
|
54
|
+
*/
|
|
55
|
+
export declare const LOOPBACK_HOST: RegExp;
|
|
56
|
+
/**
|
|
57
|
+
* Total by design — it never throws, so a malformed entry cannot white-screen
|
|
58
|
+
* a paywall at runtime. Rejects are returned rather than dropped so the build
|
|
59
|
+
* can fail loudly instead, which is the only moment anyone is looking.
|
|
60
|
+
*/
|
|
61
|
+
export declare const allowedHostsOf: (allowedHosts: SuperwallAllowedHostsConfig | undefined) => ResolvedAllowedHosts;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How many hosts one surface may name. Not a technical ceiling — a surface
|
|
3
|
+
* reaching more than a handful of origins is usually a sign the work belongs
|
|
4
|
+
* on a backend, and every entry widens the policy for every shopper.
|
|
5
|
+
*/
|
|
6
|
+
export const MAX_ALLOWED_HOSTS = 20;
|
|
7
|
+
/**
|
|
8
|
+
* Host plus optional port. Deliberately strict: no scheme (stripped before
|
|
9
|
+
* this runs), no userinfo, no path, no query, no wildcard. A wildcard would
|
|
10
|
+
* re-open the hole the allowlist exists to close, and `*.example.com` is not
|
|
11
|
+
* reviewable — the point is naming the host you actually call.
|
|
12
|
+
*/
|
|
13
|
+
const HOST_PATTERN = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*(:\d{1,5})?$/;
|
|
14
|
+
/**
|
|
15
|
+
* The loopback interface, which is never a valid entry.
|
|
16
|
+
*
|
|
17
|
+
* Not a security judgement so much as an arithmetic one: a pushed paywall
|
|
18
|
+
* runs on somebody else's device, where `localhost` is *their* machine, not
|
|
19
|
+
* the developer's. An entry naming it can only ever widen the policy for
|
|
20
|
+
* every shopper in exchange for a request that cannot succeed.
|
|
21
|
+
*
|
|
22
|
+
* Local development does not need it either — `superwall dev` serves the
|
|
23
|
+
* surface with no policy at all, so a `fetch` to a local API works there
|
|
24
|
+
* whether or not it is named here. To exercise a local API from a *pushed*
|
|
25
|
+
* build, put it behind an https tunnel and name that hostname.
|
|
26
|
+
*
|
|
27
|
+
* Detected only to say so plainly; see `allowedHostsWire`.
|
|
28
|
+
*/
|
|
29
|
+
export const LOOPBACK_HOST = /^(https?:\/\/)?(localhost|127\.0\.0\.1|\[::1\])(:\d{1,5})?\/?$/;
|
|
30
|
+
const originOf = (entry) => {
|
|
31
|
+
const trimmed = entry.trim().toLowerCase();
|
|
32
|
+
if (trimmed === "")
|
|
33
|
+
return undefined;
|
|
34
|
+
const withoutSlash = trimmed.endsWith("/") ? trimmed.slice(0, -1) : trimmed;
|
|
35
|
+
// https only, everywhere, with no exception — a served policy should never
|
|
36
|
+
// be able to carry a plaintext origin. `http://` is refused rather than
|
|
37
|
+
// upgraded: silently rewriting it would produce an origin the author did
|
|
38
|
+
// not ask for and every request against it would fail unexplained.
|
|
39
|
+
if (LOOPBACK_HOST.test(withoutSlash))
|
|
40
|
+
return undefined;
|
|
41
|
+
// A pasted `https://api.example.com` and a bare `api.example.com` mean the
|
|
42
|
+
// same thing. Anything else with a scheme is a mistake worth surfacing.
|
|
43
|
+
const host = withoutSlash.startsWith("https://")
|
|
44
|
+
? withoutSlash.slice("https://".length)
|
|
45
|
+
: withoutSlash;
|
|
46
|
+
if (!HOST_PATTERN.test(host))
|
|
47
|
+
return undefined;
|
|
48
|
+
return `https://${host}`;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Total by design — it never throws, so a malformed entry cannot white-screen
|
|
52
|
+
* a paywall at runtime. Rejects are returned rather than dropped so the build
|
|
53
|
+
* can fail loudly instead, which is the only moment anyone is looking.
|
|
54
|
+
*/
|
|
55
|
+
export const allowedHostsOf = (allowedHosts) => {
|
|
56
|
+
if (allowedHosts === undefined)
|
|
57
|
+
return { origins: [], invalid: [] };
|
|
58
|
+
const origins = new Set();
|
|
59
|
+
const invalid = [];
|
|
60
|
+
for (const entry of allowedHosts) {
|
|
61
|
+
const origin = typeof entry === "string" ? originOf(entry) : undefined;
|
|
62
|
+
if (origin === undefined) {
|
|
63
|
+
invalid.push(String(entry));
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
origins.add(origin);
|
|
67
|
+
}
|
|
68
|
+
// Sorted so the same config always produces the same wire bytes, whatever
|
|
69
|
+
// order the author listed them in.
|
|
70
|
+
return { origins: [...origins].sort(), invalid };
|
|
71
|
+
};
|
|
@@ -14,13 +14,20 @@
|
|
|
14
14
|
*/
|
|
15
15
|
export type SuperwallCheckoutMode = "external" | "sheet" | "applePay";
|
|
16
16
|
/**
|
|
17
|
-
* The mode alone, or the object form to steer
|
|
18
|
-
*
|
|
19
|
-
*
|
|
17
|
+
* The mode alone, or the object form to steer it:
|
|
18
|
+
*
|
|
19
|
+
* - `prefetch` names the Stripe product warmed on paywall open (default: the
|
|
20
|
+
* first Stripe product); `false` disables automatic prefetch.
|
|
21
|
+
* - `managedPayments` overrides the account's Stripe managed-payments
|
|
22
|
+
* setting for this paywall: `true` forces it on, `false` forces it off.
|
|
23
|
+
* Omitted, the account setting applies.
|
|
20
24
|
*/
|
|
21
25
|
export type SuperwallCheckoutConfig<TReference extends string = string> = SuperwallCheckoutMode | {
|
|
22
26
|
mode: SuperwallCheckoutMode;
|
|
23
27
|
prefetch?: TReference | false;
|
|
28
|
+
managedPayments?: boolean;
|
|
24
29
|
};
|
|
30
|
+
export type SuperwallManagedPaymentsOverride = "USE_MANAGED_PAYMENTS" | "USE_DEFAULT_PAYMENTS";
|
|
25
31
|
export declare const checkoutModeOf: (checkout: SuperwallCheckoutConfig | undefined) => SuperwallCheckoutMode | undefined;
|
|
26
32
|
export declare const checkoutPrefetchOf: (checkout: SuperwallCheckoutConfig | undefined) => string | false | undefined;
|
|
33
|
+
export declare const checkoutManagedPaymentsOf: (checkout: SuperwallCheckoutConfig | undefined) => SuperwallManagedPaymentsOverride | undefined;
|
package/dist/config/checkout.js
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
export const checkoutModeOf = (checkout) => (typeof checkout === "string" ? checkout : checkout?.mode);
|
|
2
2
|
export const checkoutPrefetchOf = (checkout) => (typeof checkout === "object" ? checkout.prefetch : undefined);
|
|
3
|
+
export const checkoutManagedPaymentsOf = (checkout) => {
|
|
4
|
+
const override = typeof checkout === "object" ? checkout.managedPayments : undefined;
|
|
5
|
+
if (override === undefined)
|
|
6
|
+
return undefined;
|
|
7
|
+
return override ? "USE_MANAGED_PAYMENTS" : "USE_DEFAULT_PAYMENTS";
|
|
8
|
+
};
|
package/dist/config/index.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import type { SuperwallAllowedHostsConfig } from "./allowedHosts";
|
|
1
2
|
import type { SuperwallBackgroundConfig } from "./background";
|
|
2
3
|
import type { SuperwallCheckoutConfig } from "./checkout";
|
|
4
|
+
import type { SuperwallInsetsConfig } from "./insets";
|
|
3
5
|
import type { SuperwallLocalizationConfig } from "./localization";
|
|
4
6
|
import type { SuperwallNotificationsConfig } from "./notifications";
|
|
5
7
|
import type { SuperwallPresentationConfig } from "./presentation";
|
|
8
|
+
import type { SuperwallPostPurchase } from "./postPurchase";
|
|
6
9
|
import type { SuperwallProductSlotInput } from "./products";
|
|
10
|
+
export type SuperwallIntroductoryOfferEligibility = "automatic" | "eligible" | "ineligible";
|
|
7
11
|
/**
|
|
8
12
|
* A paywall's contract: product slots, presentation, gating, notifications,
|
|
9
13
|
* and localization. The session reads it at render time and the build reads
|
|
@@ -28,6 +32,13 @@ export type SuperwallConfig<TLocale extends string = string, TStripeReference ex
|
|
|
28
32
|
* a dark-mode color. Default: the platform default.
|
|
29
33
|
*/
|
|
30
34
|
background?: SuperwallBackgroundConfig;
|
|
35
|
+
/**
|
|
36
|
+
* What every page is padded with: `"safe-area"` (the device's safe area,
|
|
37
|
+
* floored per platform and presentation), `"none"`, pixels, a CSS length,
|
|
38
|
+
* or one value per edge. Chrome that sits over the bars on purpose reads
|
|
39
|
+
* `--sw-safe-area-inset-*` instead. Default: `"safe-area"`.
|
|
40
|
+
*/
|
|
41
|
+
insets?: SuperwallInsetsConfig;
|
|
31
42
|
/**
|
|
32
43
|
* Whether the feature behind the placement is gated on a purchase.
|
|
33
44
|
* Default: `"nonGated"`.
|
|
@@ -40,12 +51,19 @@ export type SuperwallConfig<TLocale extends string = string, TStripeReference ex
|
|
|
40
51
|
/** Cache the paywall on device for instant presentation. Default: `true`. */
|
|
41
52
|
onDeviceCacheEnabled?: boolean;
|
|
42
53
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
54
|
+
* How the SDK decides introductory-offer (trial) eligibility for this
|
|
55
|
+
* paywall's products: `"automatic"` asks the store, `"eligible"` and
|
|
56
|
+
* `"ineligible"` force the answer — useful to preview or ship the one
|
|
57
|
+
* variant regardless of the shopper's history. Default: `"automatic"`.
|
|
58
|
+
*/
|
|
59
|
+
introductoryOfferEligibility?: SuperwallIntroductoryOfferEligibility;
|
|
60
|
+
/**
|
|
61
|
+
* What happens once a purchase completes: `"dismiss"` (default), `"stay"`
|
|
62
|
+
* to keep the paywall in control, and on the web also `"redeem"` or
|
|
63
|
+
* `{ redirect: url }`. See `SuperwallPostPurchase`. A `purchase()` call can
|
|
64
|
+
* override it per button.
|
|
47
65
|
*/
|
|
48
|
-
|
|
66
|
+
postPurchase?: SuperwallPostPurchase;
|
|
49
67
|
/**
|
|
50
68
|
* Local notifications the paywall schedules (e.g. trial reminders).
|
|
51
69
|
* Default: none.
|
|
@@ -56,11 +74,21 @@ export type SuperwallConfig<TLocale extends string = string, TStripeReference ex
|
|
|
56
74
|
* `{ defaultLocale: "en" }`; file-based catalogs need no config.
|
|
57
75
|
*/
|
|
58
76
|
localization?: SuperwallLocalizationConfig<TLocale>;
|
|
77
|
+
/**
|
|
78
|
+
* Hosts this surface may reach over the network, as bare hostnames —
|
|
79
|
+
* `["api.example.com"]`. Superwall and Stripe are always reachable;
|
|
80
|
+
* everything else is refused by the browser until it is named here.
|
|
81
|
+
* Default: none. See `SuperwallAllowedHostsConfig`.
|
|
82
|
+
*/
|
|
83
|
+
allowedHosts?: SuperwallAllowedHostsConfig;
|
|
59
84
|
};
|
|
85
|
+
export { allowedHostsOf, LOOPBACK_HOST, MAX_ALLOWED_HOSTS, type ResolvedAllowedHosts, type SuperwallAllowedHostsConfig, } from "./allowedHosts";
|
|
60
86
|
export { backgroundOf, type ResolvedBackground, type SuperwallBackgroundConfig, } from "./background";
|
|
61
|
-
export { checkoutModeOf, checkoutPrefetchOf, type SuperwallCheckoutConfig, type SuperwallCheckoutMode, } from "./checkout";
|
|
87
|
+
export { checkoutManagedPaymentsOf, checkoutModeOf, checkoutPrefetchOf, type SuperwallCheckoutConfig, type SuperwallCheckoutMode, type SuperwallManagedPaymentsOverride, } from "./checkout";
|
|
88
|
+
export { INSET_EDGES, insetsOf, type InsetEdge, type ResolvedInsets, type SuperwallInsetSide, type SuperwallInsetsConfig, } from "./insets";
|
|
62
89
|
export type { SuperwallLocalizationConfig, SuperwallMessages } from "./localization";
|
|
63
90
|
export type { SuperwallNotificationsConfig, SuperwallTrialReminderConfig, SuperwallTrialReminderContext, SuperwallTrialReminderNotification, SuperwallTrialReminderStaticConfig, } from "./notifications";
|
|
64
91
|
export { normalizePlatform, SUPERWALL_PLATFORMS, type SuperwallPlatform } from "./platforms";
|
|
92
|
+
export { dismissesOnPurchase, type SuperwallPostPurchase } from "./postPurchase";
|
|
65
93
|
export type { SuperwallPresentationConfig, SuperwallPresentationStyle } from "./presentation";
|
|
66
94
|
export { isPlatformProductIds, productIdFor, type SuperwallPlatformProductIds, type SuperwallProductSlot, type SuperwallProductSlotInput, } from "./products";
|
package/dist/config/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
export { allowedHostsOf, LOOPBACK_HOST, MAX_ALLOWED_HOSTS, } from "./allowedHosts";
|
|
1
2
|
export { backgroundOf, } from "./background";
|
|
2
|
-
export { checkoutModeOf, checkoutPrefetchOf, } from "./checkout";
|
|
3
|
+
export { checkoutManagedPaymentsOf, checkoutModeOf, checkoutPrefetchOf, } from "./checkout";
|
|
4
|
+
export { INSET_EDGES, insetsOf, } from "./insets";
|
|
3
5
|
export { normalizePlatform, SUPERWALL_PLATFORMS } from "./platforms";
|
|
6
|
+
export { dismissesOnPurchase } from "./postPurchase";
|
|
4
7
|
export { isPlatformProductIds, productIdFor, } from "./products";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One edge of the padding the framework puts around every page:
|
|
3
|
+
* `"safe-area"` (the device's safe area, floored per platform and
|
|
4
|
+
* presentation), `"none"` (flush to the edge), a number of pixels, or any
|
|
5
|
+
* CSS length.
|
|
6
|
+
*/
|
|
7
|
+
export type SuperwallInsetSide = "safe-area" | "none" | number | string;
|
|
8
|
+
/**
|
|
9
|
+
* What every page is padded with. One value applies to all four edges; the
|
|
10
|
+
* object form sets edges individually and leaves the rest at `"safe-area"`.
|
|
11
|
+
*/
|
|
12
|
+
export type SuperwallInsetsConfig = SuperwallInsetSide | {
|
|
13
|
+
/** Default: `"safe-area"`. */
|
|
14
|
+
top?: SuperwallInsetSide;
|
|
15
|
+
/** Default: `"safe-area"`. */
|
|
16
|
+
right?: SuperwallInsetSide;
|
|
17
|
+
/** Default: `"safe-area"`. */
|
|
18
|
+
bottom?: SuperwallInsetSide;
|
|
19
|
+
/** Default: `"safe-area"`. */
|
|
20
|
+
left?: SuperwallInsetSide;
|
|
21
|
+
};
|
|
22
|
+
export declare const INSET_EDGES: readonly ["top", "right", "bottom", "left"];
|
|
23
|
+
export type InsetEdge = (typeof INSET_EDGES)[number];
|
|
24
|
+
export type ResolvedInsets = Record<InsetEdge, string | undefined>;
|
|
25
|
+
export declare const insetsOf: (insets: SuperwallInsetsConfig | undefined) => ResolvedInsets;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const INSET_EDGES = ["top", "right", "bottom", "left"];
|
|
2
|
+
const lengthOf = (side) => {
|
|
3
|
+
if (side === undefined || side === "safe-area")
|
|
4
|
+
return undefined;
|
|
5
|
+
if (side === "none")
|
|
6
|
+
return "0px";
|
|
7
|
+
if (typeof side === "number")
|
|
8
|
+
return `${side}px`;
|
|
9
|
+
const length = side.trim();
|
|
10
|
+
return length === "" ? undefined : length;
|
|
11
|
+
};
|
|
12
|
+
export const insetsOf = (insets) => {
|
|
13
|
+
if (insets === undefined || typeof insets !== "object") {
|
|
14
|
+
const all = lengthOf(insets);
|
|
15
|
+
return { top: all, right: all, bottom: all, left: all };
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
top: lengthOf(insets.top),
|
|
19
|
+
right: lengthOf(insets.right),
|
|
20
|
+
bottom: lengthOf(insets.bottom),
|
|
21
|
+
left: lengthOf(insets.left),
|
|
22
|
+
};
|
|
23
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What happens once a purchase completes. One vocabulary for both platforms;
|
|
3
|
+
* each host reads the part it can act on:
|
|
4
|
+
*
|
|
5
|
+
* - `"dismiss"` — the default. Natively the SDK dismisses the paywall
|
|
6
|
+
* (`should_dismiss` on the purchase message; the app can still veto it
|
|
7
|
+
* with `SuperwallOptions.paywalls.automaticallyDismiss = false`). On the
|
|
8
|
+
* web the host does what the placement's delivery mode says: grant access
|
|
9
|
+
* and close inside a Web SDK overlay, redeem on a hosted page.
|
|
10
|
+
* - `"stay"` — the paywall keeps control. Natively nothing is dismissed. On
|
|
11
|
+
* the web the host hands the completed purchase back to the paywall, which
|
|
12
|
+
* finishes it with `useCheckoutRedemption()` — a success page, a "Continue
|
|
13
|
+
* to app" button, whatever the flow needs.
|
|
14
|
+
* - `"redeem"` — web only: send the shopper to the redemption page, which
|
|
15
|
+
* opens the app with their code. Natively the same as `"dismiss"`.
|
|
16
|
+
* - `{ redirect: url }` — web only: send the shopper to that URL with their
|
|
17
|
+
* redemption code attached. Natively the same as `"dismiss"`.
|
|
18
|
+
*
|
|
19
|
+
* A `purchase()` call can override it per button.
|
|
20
|
+
*/
|
|
21
|
+
export type SuperwallPostPurchase = "dismiss" | "stay" | "redeem" | {
|
|
22
|
+
redirect: string;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Whether the native SDK should dismiss after the purchase — everything but
|
|
26
|
+
* `"stay"`. Undefined when nothing was chosen, so the purchase message omits
|
|
27
|
+
* the flag and the SDK keeps its own default, as before.
|
|
28
|
+
*/
|
|
29
|
+
export declare const dismissesOnPurchase: (postPurchase: SuperwallPostPurchase | undefined) => boolean | undefined;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether the native SDK should dismiss after the purchase — everything but
|
|
3
|
+
* `"stay"`. Undefined when nothing was chosen, so the purchase message omits
|
|
4
|
+
* the flag and the SDK keeps its own default, as before.
|
|
5
|
+
*/
|
|
6
|
+
export const dismissesOnPurchase = (postPurchase) => (postPurchase === undefined ? undefined : postPurchase !== "stay");
|
package/dist/session/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { decodeIncomingMessages } from "../bridge/base64";
|
|
2
2
|
import { createRuntimeTransport } from "../bridge/transport";
|
|
3
|
-
import { normalizePlatform, productIdFor } from "../config";
|
|
3
|
+
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";
|
|
@@ -248,6 +248,20 @@ const createSession = (options) => {
|
|
|
248
248
|
openExternalUrl: (url) => post(outgoing.openExternalUrl(url)),
|
|
249
249
|
openDeepLink: (link) => post(outgoing.openDeepLink(link)),
|
|
250
250
|
customPlacement: (name, params = {}) => post(outgoing.customPlacement(name, params)),
|
|
251
|
+
setUserAttributes: (attributes) => {
|
|
252
|
+
const entries = Object.entries(attributes).map(([key, value]) => ({
|
|
253
|
+
key,
|
|
254
|
+
value: String(value),
|
|
255
|
+
}));
|
|
256
|
+
if (entries.length === 0)
|
|
257
|
+
return;
|
|
258
|
+
post(outgoing.userAttributeUpdated(entries));
|
|
259
|
+
const user = {
|
|
260
|
+
...snapshot.user,
|
|
261
|
+
...Object.fromEntries(entries.map(({ key, value }) => [key, value])),
|
|
262
|
+
};
|
|
263
|
+
commit({ user, variables: { ...snapshot.variables, user } });
|
|
264
|
+
},
|
|
251
265
|
requestStoreReview: (reviewType) => post(outgoing.requestStoreReview(reviewType)),
|
|
252
266
|
pageView: (input) => post(outgoing.pageView(input)),
|
|
253
267
|
hapticFeedback: (hapticType) => post(outgoing.hapticFeedback(hapticType)),
|
|
@@ -264,7 +278,7 @@ const createSession = (options) => {
|
|
|
264
278
|
pendingPurchase: { reference: product.reference, product },
|
|
265
279
|
lastPurchaseFailure: undefined,
|
|
266
280
|
});
|
|
267
|
-
post(outgoing.purchase(product, purchaseOptions.
|
|
281
|
+
post(outgoing.purchase(product, dismissesOnPurchase(purchaseOptions.postPurchase ?? snapshot.config?.postPurchase)));
|
|
268
282
|
return result;
|
|
269
283
|
},
|
|
270
284
|
};
|
|
@@ -11,6 +11,7 @@ export declare const openUrl: (url: string, browserType?: "payment_sheet") => Ou
|
|
|
11
11
|
export declare const openExternalUrl: (url: string) => Outgoing<"open_url_external">;
|
|
12
12
|
export declare const openDeepLink: (link: string) => Outgoing<"open_deep_link">;
|
|
13
13
|
export declare const customPlacement: (name: string, params: Record<string, unknown>) => Outgoing<"custom_placement">;
|
|
14
|
+
export declare const userAttributeUpdated: (attributes: Outgoing<"user_attribute_updated">["attributes"]) => Outgoing<"user_attribute_updated">;
|
|
14
15
|
export declare const requestStoreReview: (reviewType: Outgoing<"request_store_review">["review_type"]) => Outgoing<"request_store_review">;
|
|
15
16
|
export declare const hapticFeedback: (hapticType: Outgoing<"haptic_feedback">["haptic_type"]) => Outgoing<"haptic_feedback">;
|
|
16
17
|
export type PageViewInput = {
|
package/dist/session/outgoing.js
CHANGED
|
@@ -21,6 +21,7 @@ export const openDeepLink = (link) => ({
|
|
|
21
21
|
link,
|
|
22
22
|
});
|
|
23
23
|
export const customPlacement = (name, params) => ({ event_name: "custom_placement", name, params });
|
|
24
|
+
export const userAttributeUpdated = (attributes) => ({ event_name: "user_attribute_updated", attributes });
|
|
24
25
|
export const requestStoreReview = (reviewType) => ({
|
|
25
26
|
event_name: "request_store_review",
|
|
26
27
|
review_type: reviewType,
|
package/dist/session/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ITransport, TEventTrackingBehavior, TIncomingMessage, TIncomingMessageCallbackResult, TIncomingMessageTransactionAbandon, TIncomingMessageTransactionComplete, TOutgoingMessage, TPermissionStatus, TPermissionTypeMessage, TransportKind } from "@superwall/protocol";
|
|
2
|
-
import type { SuperwallConfig } from "../config";
|
|
2
|
+
import type { SuperwallConfig, SuperwallPostPurchase } from "../config";
|
|
3
3
|
import type { SuperwallTranslateValues } from "../localization";
|
|
4
4
|
import type { SuperwallProduct, SuperwallProductInput } from "../products";
|
|
5
5
|
import type { Outgoing, PageViewInput } from "./outgoing";
|
|
@@ -75,7 +75,18 @@ export type SuperwallRuntimeSnapshot<out TProduct extends SuperwallProduct = Sup
|
|
|
75
75
|
lastPurchaseFailure?: SuperwallPurchaseFailure<TProduct>;
|
|
76
76
|
};
|
|
77
77
|
export type SuperwallPurchaseOptions = {
|
|
78
|
-
|
|
78
|
+
/**
|
|
79
|
+
* What happens once this purchase completes, overriding the config's
|
|
80
|
+
* `postPurchase` for this call. See `SuperwallPostPurchase`.
|
|
81
|
+
*/
|
|
82
|
+
postPurchase?: SuperwallPostPurchase;
|
|
83
|
+
/**
|
|
84
|
+
* Key/value pairs written onto the Stripe subscription (or payment) once
|
|
85
|
+
* a web purchase completes — a quiz session id, a plan name, anything you
|
|
86
|
+
* want to see in Stripe and in webhooks. Values are strings. Ignored by
|
|
87
|
+
* store purchases, which have no Stripe object to write to.
|
|
88
|
+
*/
|
|
89
|
+
stripeMetadata?: Readonly<Record<string, string>>;
|
|
79
90
|
};
|
|
80
91
|
export type SuperwallCallbackOptions = {
|
|
81
92
|
behavior?: CallbackBehavior;
|
|
@@ -96,6 +107,7 @@ export type SuperwallSessionOptions = {
|
|
|
96
107
|
transportKind?: TransportKind;
|
|
97
108
|
onDecodeError?: (error: unknown) => void;
|
|
98
109
|
};
|
|
110
|
+
export type SuperwallUserAttributes = Readonly<Record<string, string | number | boolean>>;
|
|
99
111
|
export interface SuperwallSession<out TProduct extends SuperwallProduct = SuperwallProduct> {
|
|
100
112
|
on: <E extends SuperwallEventName>(event: E, handler: (payload: SuperwallEventMap[E]) => void) => () => void;
|
|
101
113
|
subscribe: (listener: () => void) => () => void;
|
|
@@ -115,6 +127,7 @@ export interface SuperwallSession<out TProduct extends SuperwallProduct = Superw
|
|
|
115
127
|
openExternalUrl: (url: string) => void;
|
|
116
128
|
openDeepLink: (link: string) => void;
|
|
117
129
|
customPlacement: (name: string, params?: Record<string, unknown>) => void;
|
|
130
|
+
setUserAttributes: (attributes: SuperwallUserAttributes) => void;
|
|
118
131
|
requestStoreReview: (reviewType: Outgoing<"request_store_review">["review_type"]) => void;
|
|
119
132
|
pageView: (input: PageViewInput) => void;
|
|
120
133
|
hapticFeedback: (hapticType: Outgoing<"haptic_feedback">["haptic_type"]) => void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superwall/runtime",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.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": "1.5.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@superwall/host": "workspace:*",
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hosts this surface is allowed to reach over the network.
|
|
3
|
+
*
|
|
4
|
+
* A published surface runs under a Content-Security-Policy the platform
|
|
5
|
+
* enforces at serve time, and by default it may only talk to Superwall and
|
|
6
|
+
* Stripe. Anything else — a customer's own API, a lead-capture endpoint, a
|
|
7
|
+
* custom analytics collector — has to be named here, or the browser refuses
|
|
8
|
+
* the request before it reaches the network.
|
|
9
|
+
*
|
|
10
|
+
* The allowlist is deliberately narrow rather than absent. The policy permits
|
|
11
|
+
* `unsafe-eval` and `blob:` scripts (the bundler output needs both), so the
|
|
12
|
+
* set of reachable hosts is what keeps a published surface from fetching and
|
|
13
|
+
* evaluating code nobody reviewed. An open policy would make the bytes that
|
|
14
|
+
* run unknowable; a named host keeps them attributable.
|
|
15
|
+
*
|
|
16
|
+
* Write bare hostnames — `https` is implied and enforced:
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* export default definePaywall({
|
|
20
|
+
* allowedHosts: ["api.example.com"],
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* A full `https://` origin is accepted too and normalizes to the same thing,
|
|
25
|
+
* so pasting a URL from a fetch call does the expected thing.
|
|
26
|
+
*/
|
|
27
|
+
export type SuperwallAllowedHostsConfig = readonly string[];
|
|
28
|
+
|
|
29
|
+
export type ResolvedAllowedHosts = {
|
|
30
|
+
/** CSP source expressions, `https://host[:port]`, deduped and sorted. */
|
|
31
|
+
origins: string[];
|
|
32
|
+
/** Entries that could not be read as a host, verbatim, in input order. */
|
|
33
|
+
invalid: string[];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* How many hosts one surface may name. Not a technical ceiling — a surface
|
|
38
|
+
* reaching more than a handful of origins is usually a sign the work belongs
|
|
39
|
+
* on a backend, and every entry widens the policy for every shopper.
|
|
40
|
+
*/
|
|
41
|
+
export const MAX_ALLOWED_HOSTS = 20;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Host plus optional port. Deliberately strict: no scheme (stripped before
|
|
45
|
+
* this runs), no userinfo, no path, no query, no wildcard. A wildcard would
|
|
46
|
+
* re-open the hole the allowlist exists to close, and `*.example.com` is not
|
|
47
|
+
* reviewable — the point is naming the host you actually call.
|
|
48
|
+
*/
|
|
49
|
+
const HOST_PATTERN = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*(:\d{1,5})?$/;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The loopback interface, which is never a valid entry.
|
|
53
|
+
*
|
|
54
|
+
* Not a security judgement so much as an arithmetic one: a pushed paywall
|
|
55
|
+
* runs on somebody else's device, where `localhost` is *their* machine, not
|
|
56
|
+
* the developer's. An entry naming it can only ever widen the policy for
|
|
57
|
+
* every shopper in exchange for a request that cannot succeed.
|
|
58
|
+
*
|
|
59
|
+
* Local development does not need it either — `superwall dev` serves the
|
|
60
|
+
* surface with no policy at all, so a `fetch` to a local API works there
|
|
61
|
+
* whether or not it is named here. To exercise a local API from a *pushed*
|
|
62
|
+
* build, put it behind an https tunnel and name that hostname.
|
|
63
|
+
*
|
|
64
|
+
* Detected only to say so plainly; see `allowedHostsWire`.
|
|
65
|
+
*/
|
|
66
|
+
export const LOOPBACK_HOST = /^(https?:\/\/)?(localhost|127\.0\.0\.1|\[::1\])(:\d{1,5})?\/?$/;
|
|
67
|
+
|
|
68
|
+
const originOf = (entry: string): string | undefined => {
|
|
69
|
+
const trimmed = entry.trim().toLowerCase();
|
|
70
|
+
if (trimmed === "") return undefined;
|
|
71
|
+
|
|
72
|
+
const withoutSlash = trimmed.endsWith("/") ? trimmed.slice(0, -1) : trimmed;
|
|
73
|
+
|
|
74
|
+
// https only, everywhere, with no exception — a served policy should never
|
|
75
|
+
// be able to carry a plaintext origin. `http://` is refused rather than
|
|
76
|
+
// upgraded: silently rewriting it would produce an origin the author did
|
|
77
|
+
// not ask for and every request against it would fail unexplained.
|
|
78
|
+
if (LOOPBACK_HOST.test(withoutSlash)) return undefined;
|
|
79
|
+
|
|
80
|
+
// A pasted `https://api.example.com` and a bare `api.example.com` mean the
|
|
81
|
+
// same thing. Anything else with a scheme is a mistake worth surfacing.
|
|
82
|
+
const host = withoutSlash.startsWith("https://")
|
|
83
|
+
? withoutSlash.slice("https://".length)
|
|
84
|
+
: withoutSlash;
|
|
85
|
+
|
|
86
|
+
if (!HOST_PATTERN.test(host)) return undefined;
|
|
87
|
+
return `https://${host}`;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Total by design — it never throws, so a malformed entry cannot white-screen
|
|
92
|
+
* a paywall at runtime. Rejects are returned rather than dropped so the build
|
|
93
|
+
* can fail loudly instead, which is the only moment anyone is looking.
|
|
94
|
+
*/
|
|
95
|
+
export const allowedHostsOf = (
|
|
96
|
+
allowedHosts: SuperwallAllowedHostsConfig | undefined,
|
|
97
|
+
): ResolvedAllowedHosts => {
|
|
98
|
+
if (allowedHosts === undefined) return { origins: [], invalid: [] };
|
|
99
|
+
|
|
100
|
+
const origins = new Set<string>();
|
|
101
|
+
const invalid: string[] = [];
|
|
102
|
+
|
|
103
|
+
for (const entry of allowedHosts) {
|
|
104
|
+
const origin = typeof entry === "string" ? originOf(entry) : undefined;
|
|
105
|
+
if (origin === undefined) {
|
|
106
|
+
invalid.push(String(entry));
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
origins.add(origin);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Sorted so the same config always produces the same wire bytes, whatever
|
|
113
|
+
// order the author listed them in.
|
|
114
|
+
return { origins: [...origins].sort(), invalid };
|
|
115
|
+
};
|
package/src/config/checkout.ts
CHANGED
|
@@ -15,13 +15,19 @@
|
|
|
15
15
|
export type SuperwallCheckoutMode = "external" | "sheet" | "applePay";
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
* The mode alone, or the object form to steer
|
|
19
|
-
*
|
|
20
|
-
*
|
|
18
|
+
* The mode alone, or the object form to steer it:
|
|
19
|
+
*
|
|
20
|
+
* - `prefetch` names the Stripe product warmed on paywall open (default: the
|
|
21
|
+
* first Stripe product); `false` disables automatic prefetch.
|
|
22
|
+
* - `managedPayments` overrides the account's Stripe managed-payments
|
|
23
|
+
* setting for this paywall: `true` forces it on, `false` forces it off.
|
|
24
|
+
* Omitted, the account setting applies.
|
|
21
25
|
*/
|
|
22
26
|
export type SuperwallCheckoutConfig<TReference extends string = string> =
|
|
23
27
|
| SuperwallCheckoutMode
|
|
24
|
-
| { mode: SuperwallCheckoutMode; prefetch?: TReference | false };
|
|
28
|
+
| { mode: SuperwallCheckoutMode; prefetch?: TReference | false; managedPayments?: boolean };
|
|
29
|
+
|
|
30
|
+
export type SuperwallManagedPaymentsOverride = "USE_MANAGED_PAYMENTS" | "USE_DEFAULT_PAYMENTS";
|
|
25
31
|
|
|
26
32
|
export const checkoutModeOf = (
|
|
27
33
|
checkout: SuperwallCheckoutConfig | undefined,
|
|
@@ -30,3 +36,11 @@ export const checkoutModeOf = (
|
|
|
30
36
|
export const checkoutPrefetchOf = (
|
|
31
37
|
checkout: SuperwallCheckoutConfig | undefined,
|
|
32
38
|
): string | false | undefined => (typeof checkout === "object" ? checkout.prefetch : undefined);
|
|
39
|
+
|
|
40
|
+
export const checkoutManagedPaymentsOf = (
|
|
41
|
+
checkout: SuperwallCheckoutConfig | undefined,
|
|
42
|
+
): SuperwallManagedPaymentsOverride | undefined => {
|
|
43
|
+
const override = typeof checkout === "object" ? checkout.managedPayments : undefined;
|
|
44
|
+
if (override === undefined) return undefined;
|
|
45
|
+
return override ? "USE_MANAGED_PAYMENTS" : "USE_DEFAULT_PAYMENTS";
|
|
46
|
+
};
|
package/src/config/index.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
+
import type { SuperwallAllowedHostsConfig } from "./allowedHosts";
|
|
1
2
|
import type { SuperwallBackgroundConfig } from "./background";
|
|
2
3
|
import type { SuperwallCheckoutConfig } from "./checkout";
|
|
4
|
+
import type { SuperwallInsetsConfig } from "./insets";
|
|
3
5
|
import type { SuperwallLocalizationConfig } from "./localization";
|
|
4
6
|
import type { SuperwallNotificationsConfig } from "./notifications";
|
|
5
7
|
import type { SuperwallPresentationConfig } from "./presentation";
|
|
8
|
+
import type { SuperwallPostPurchase } from "./postPurchase";
|
|
6
9
|
import type { SuperwallProductSlotInput } from "./products";
|
|
7
10
|
|
|
11
|
+
export type SuperwallIntroductoryOfferEligibility = "automatic" | "eligible" | "ineligible";
|
|
12
|
+
|
|
8
13
|
/**
|
|
9
14
|
* A paywall's contract: product slots, presentation, gating, notifications,
|
|
10
15
|
* and localization. The session reads it at render time and the build reads
|
|
@@ -36,6 +41,14 @@ export type SuperwallConfig<
|
|
|
36
41
|
*/
|
|
37
42
|
background?: SuperwallBackgroundConfig;
|
|
38
43
|
|
|
44
|
+
/**
|
|
45
|
+
* What every page is padded with: `"safe-area"` (the device's safe area,
|
|
46
|
+
* floored per platform and presentation), `"none"`, pixels, a CSS length,
|
|
47
|
+
* or one value per edge. Chrome that sits over the bars on purpose reads
|
|
48
|
+
* `--sw-safe-area-inset-*` instead. Default: `"safe-area"`.
|
|
49
|
+
*/
|
|
50
|
+
insets?: SuperwallInsetsConfig;
|
|
51
|
+
|
|
39
52
|
/**
|
|
40
53
|
* Whether the feature behind the placement is gated on a purchase.
|
|
41
54
|
* Default: `"nonGated"`.
|
|
@@ -52,12 +65,20 @@ export type SuperwallConfig<
|
|
|
52
65
|
onDeviceCacheEnabled?: boolean;
|
|
53
66
|
|
|
54
67
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
68
|
+
* How the SDK decides introductory-offer (trial) eligibility for this
|
|
69
|
+
* paywall's products: `"automatic"` asks the store, `"eligible"` and
|
|
70
|
+
* `"ineligible"` force the answer — useful to preview or ship the one
|
|
71
|
+
* variant regardless of the shopper's history. Default: `"automatic"`.
|
|
72
|
+
*/
|
|
73
|
+
introductoryOfferEligibility?: SuperwallIntroductoryOfferEligibility;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* What happens once a purchase completes: `"dismiss"` (default), `"stay"`
|
|
77
|
+
* to keep the paywall in control, and on the web also `"redeem"` or
|
|
78
|
+
* `{ redirect: url }`. See `SuperwallPostPurchase`. A `purchase()` call can
|
|
79
|
+
* override it per button.
|
|
59
80
|
*/
|
|
60
|
-
|
|
81
|
+
postPurchase?: SuperwallPostPurchase;
|
|
61
82
|
|
|
62
83
|
/**
|
|
63
84
|
* Local notifications the paywall schedules (e.g. trial reminders).
|
|
@@ -70,19 +91,44 @@ export type SuperwallConfig<
|
|
|
70
91
|
* `{ defaultLocale: "en" }`; file-based catalogs need no config.
|
|
71
92
|
*/
|
|
72
93
|
localization?: SuperwallLocalizationConfig<TLocale>;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Hosts this surface may reach over the network, as bare hostnames —
|
|
97
|
+
* `["api.example.com"]`. Superwall and Stripe are always reachable;
|
|
98
|
+
* everything else is refused by the browser until it is named here.
|
|
99
|
+
* Default: none. See `SuperwallAllowedHostsConfig`.
|
|
100
|
+
*/
|
|
101
|
+
allowedHosts?: SuperwallAllowedHostsConfig;
|
|
73
102
|
};
|
|
74
103
|
|
|
104
|
+
export {
|
|
105
|
+
allowedHostsOf,
|
|
106
|
+
LOOPBACK_HOST,
|
|
107
|
+
MAX_ALLOWED_HOSTS,
|
|
108
|
+
type ResolvedAllowedHosts,
|
|
109
|
+
type SuperwallAllowedHostsConfig,
|
|
110
|
+
} from "./allowedHosts";
|
|
75
111
|
export {
|
|
76
112
|
backgroundOf,
|
|
77
113
|
type ResolvedBackground,
|
|
78
114
|
type SuperwallBackgroundConfig,
|
|
79
115
|
} from "./background";
|
|
80
116
|
export {
|
|
117
|
+
checkoutManagedPaymentsOf,
|
|
81
118
|
checkoutModeOf,
|
|
82
119
|
checkoutPrefetchOf,
|
|
83
120
|
type SuperwallCheckoutConfig,
|
|
84
121
|
type SuperwallCheckoutMode,
|
|
122
|
+
type SuperwallManagedPaymentsOverride,
|
|
85
123
|
} from "./checkout";
|
|
124
|
+
export {
|
|
125
|
+
INSET_EDGES,
|
|
126
|
+
insetsOf,
|
|
127
|
+
type InsetEdge,
|
|
128
|
+
type ResolvedInsets,
|
|
129
|
+
type SuperwallInsetSide,
|
|
130
|
+
type SuperwallInsetsConfig,
|
|
131
|
+
} from "./insets";
|
|
86
132
|
export type { SuperwallLocalizationConfig, SuperwallMessages } from "./localization";
|
|
87
133
|
export type {
|
|
88
134
|
SuperwallNotificationsConfig,
|
|
@@ -92,6 +138,7 @@ export type {
|
|
|
92
138
|
SuperwallTrialReminderStaticConfig,
|
|
93
139
|
} from "./notifications";
|
|
94
140
|
export { normalizePlatform, SUPERWALL_PLATFORMS, type SuperwallPlatform } from "./platforms";
|
|
141
|
+
export { dismissesOnPurchase, type SuperwallPostPurchase } from "./postPurchase";
|
|
95
142
|
export type { SuperwallPresentationConfig, SuperwallPresentationStyle } from "./presentation";
|
|
96
143
|
export {
|
|
97
144
|
isPlatformProductIds,
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One edge of the padding the framework puts around every page:
|
|
3
|
+
* `"safe-area"` (the device's safe area, floored per platform and
|
|
4
|
+
* presentation), `"none"` (flush to the edge), a number of pixels, or any
|
|
5
|
+
* CSS length.
|
|
6
|
+
*/
|
|
7
|
+
export type SuperwallInsetSide = "safe-area" | "none" | number | string;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* What every page is padded with. One value applies to all four edges; the
|
|
11
|
+
* object form sets edges individually and leaves the rest at `"safe-area"`.
|
|
12
|
+
*/
|
|
13
|
+
export type SuperwallInsetsConfig =
|
|
14
|
+
| SuperwallInsetSide
|
|
15
|
+
| {
|
|
16
|
+
/** Default: `"safe-area"`. */
|
|
17
|
+
top?: SuperwallInsetSide;
|
|
18
|
+
/** Default: `"safe-area"`. */
|
|
19
|
+
right?: SuperwallInsetSide;
|
|
20
|
+
/** Default: `"safe-area"`. */
|
|
21
|
+
bottom?: SuperwallInsetSide;
|
|
22
|
+
/** Default: `"safe-area"`. */
|
|
23
|
+
left?: SuperwallInsetSide;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const INSET_EDGES = ["top", "right", "bottom", "left"] as const;
|
|
27
|
+
|
|
28
|
+
export type InsetEdge = (typeof INSET_EDGES)[number];
|
|
29
|
+
|
|
30
|
+
export type ResolvedInsets = Record<InsetEdge, string | undefined>;
|
|
31
|
+
|
|
32
|
+
const lengthOf = (side: SuperwallInsetSide | undefined): string | undefined => {
|
|
33
|
+
if (side === undefined || side === "safe-area") return undefined;
|
|
34
|
+
if (side === "none") return "0px";
|
|
35
|
+
if (typeof side === "number") return `${side}px`;
|
|
36
|
+
const length = side.trim();
|
|
37
|
+
return length === "" ? undefined : length;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const insetsOf = (insets: SuperwallInsetsConfig | undefined): ResolvedInsets => {
|
|
41
|
+
if (insets === undefined || typeof insets !== "object") {
|
|
42
|
+
const all = lengthOf(insets);
|
|
43
|
+
return { top: all, right: all, bottom: all, left: all };
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
top: lengthOf(insets.top),
|
|
47
|
+
right: lengthOf(insets.right),
|
|
48
|
+
bottom: lengthOf(insets.bottom),
|
|
49
|
+
left: lengthOf(insets.left),
|
|
50
|
+
};
|
|
51
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What happens once a purchase completes. One vocabulary for both platforms;
|
|
3
|
+
* each host reads the part it can act on:
|
|
4
|
+
*
|
|
5
|
+
* - `"dismiss"` — the default. Natively the SDK dismisses the paywall
|
|
6
|
+
* (`should_dismiss` on the purchase message; the app can still veto it
|
|
7
|
+
* with `SuperwallOptions.paywalls.automaticallyDismiss = false`). On the
|
|
8
|
+
* web the host does what the placement's delivery mode says: grant access
|
|
9
|
+
* and close inside a Web SDK overlay, redeem on a hosted page.
|
|
10
|
+
* - `"stay"` — the paywall keeps control. Natively nothing is dismissed. On
|
|
11
|
+
* the web the host hands the completed purchase back to the paywall, which
|
|
12
|
+
* finishes it with `useCheckoutRedemption()` — a success page, a "Continue
|
|
13
|
+
* to app" button, whatever the flow needs.
|
|
14
|
+
* - `"redeem"` — web only: send the shopper to the redemption page, which
|
|
15
|
+
* opens the app with their code. Natively the same as `"dismiss"`.
|
|
16
|
+
* - `{ redirect: url }` — web only: send the shopper to that URL with their
|
|
17
|
+
* redemption code attached. Natively the same as `"dismiss"`.
|
|
18
|
+
*
|
|
19
|
+
* A `purchase()` call can override it per button.
|
|
20
|
+
*/
|
|
21
|
+
export type SuperwallPostPurchase = "dismiss" | "stay" | "redeem" | { redirect: string };
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Whether the native SDK should dismiss after the purchase — everything but
|
|
25
|
+
* `"stay"`. Undefined when nothing was chosen, so the purchase message omits
|
|
26
|
+
* the flag and the SDK keeps its own default, as before.
|
|
27
|
+
*/
|
|
28
|
+
export const dismissesOnPurchase = (
|
|
29
|
+
postPurchase: SuperwallPostPurchase | undefined,
|
|
30
|
+
): boolean | undefined => (postPurchase === undefined ? undefined : postPurchase !== "stay");
|
package/src/session/index.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
} from "@superwall/protocol";
|
|
7
7
|
import { decodeIncomingMessages } from "../bridge/base64";
|
|
8
8
|
import { createRuntimeTransport } from "../bridge/transport";
|
|
9
|
-
import { normalizePlatform, productIdFor } from "../config";
|
|
9
|
+
import { dismissesOnPurchase, normalizePlatform, productIdFor } from "../config";
|
|
10
10
|
import { createTranslator, languageCandidates, resolveLocale } from "../localization";
|
|
11
11
|
import {
|
|
12
12
|
createProductDeriver,
|
|
@@ -307,6 +307,19 @@ const createSession = (options: SuperwallSessionOptions): SuperwallSession => {
|
|
|
307
307
|
openExternalUrl: (url) => post(outgoing.openExternalUrl(url)),
|
|
308
308
|
openDeepLink: (link) => post(outgoing.openDeepLink(link)),
|
|
309
309
|
customPlacement: (name, params = {}) => post(outgoing.customPlacement(name, params)),
|
|
310
|
+
setUserAttributes: (attributes) => {
|
|
311
|
+
const entries = Object.entries(attributes).map(([key, value]) => ({
|
|
312
|
+
key,
|
|
313
|
+
value: String(value),
|
|
314
|
+
}));
|
|
315
|
+
if (entries.length === 0) return;
|
|
316
|
+
post(outgoing.userAttributeUpdated(entries));
|
|
317
|
+
const user = {
|
|
318
|
+
...snapshot.user,
|
|
319
|
+
...Object.fromEntries(entries.map(({ key, value }) => [key, value])),
|
|
320
|
+
};
|
|
321
|
+
commit({ user, variables: { ...snapshot.variables, user } });
|
|
322
|
+
},
|
|
310
323
|
requestStoreReview: (reviewType) => post(outgoing.requestStoreReview(reviewType)),
|
|
311
324
|
pageView: (input) => post(outgoing.pageView(input)),
|
|
312
325
|
hapticFeedback: (hapticType) => post(outgoing.hapticFeedback(hapticType)),
|
|
@@ -326,7 +339,7 @@ const createSession = (options: SuperwallSessionOptions): SuperwallSession => {
|
|
|
326
339
|
post(
|
|
327
340
|
outgoing.purchase(
|
|
328
341
|
product,
|
|
329
|
-
purchaseOptions.
|
|
342
|
+
dismissesOnPurchase(purchaseOptions.postPurchase ?? snapshot.config?.postPurchase),
|
|
330
343
|
),
|
|
331
344
|
);
|
|
332
345
|
return result;
|
package/src/session/outgoing.ts
CHANGED
|
@@ -40,6 +40,10 @@ export const customPlacement = (
|
|
|
40
40
|
params: Record<string, unknown>,
|
|
41
41
|
): Outgoing<"custom_placement"> => ({ event_name: "custom_placement", name, params });
|
|
42
42
|
|
|
43
|
+
export const userAttributeUpdated = (
|
|
44
|
+
attributes: Outgoing<"user_attribute_updated">["attributes"],
|
|
45
|
+
): Outgoing<"user_attribute_updated"> => ({ event_name: "user_attribute_updated", attributes });
|
|
46
|
+
|
|
43
47
|
export const requestStoreReview = (
|
|
44
48
|
reviewType: Outgoing<"request_store_review">["review_type"],
|
|
45
49
|
): Outgoing<"request_store_review"> => ({
|
package/src/session/types.ts
CHANGED
|
@@ -10,7 +10,7 @@ import type {
|
|
|
10
10
|
TPermissionTypeMessage,
|
|
11
11
|
TransportKind,
|
|
12
12
|
} from "@superwall/protocol";
|
|
13
|
-
import type { SuperwallConfig } from "../config";
|
|
13
|
+
import type { SuperwallConfig, SuperwallPostPurchase } from "../config";
|
|
14
14
|
import type { SuperwallTranslateValues } from "../localization";
|
|
15
15
|
import type { SuperwallProduct, SuperwallProductInput } from "../products";
|
|
16
16
|
import type { Outgoing, PageViewInput } from "./outgoing";
|
|
@@ -109,7 +109,18 @@ export type SuperwallRuntimeSnapshot<out TProduct extends SuperwallProduct = Sup
|
|
|
109
109
|
};
|
|
110
110
|
|
|
111
111
|
export type SuperwallPurchaseOptions = {
|
|
112
|
-
|
|
112
|
+
/**
|
|
113
|
+
* What happens once this purchase completes, overriding the config's
|
|
114
|
+
* `postPurchase` for this call. See `SuperwallPostPurchase`.
|
|
115
|
+
*/
|
|
116
|
+
postPurchase?: SuperwallPostPurchase;
|
|
117
|
+
/**
|
|
118
|
+
* Key/value pairs written onto the Stripe subscription (or payment) once
|
|
119
|
+
* a web purchase completes — a quiz session id, a plan name, anything you
|
|
120
|
+
* want to see in Stripe and in webhooks. Values are strings. Ignored by
|
|
121
|
+
* store purchases, which have no Stripe object to write to.
|
|
122
|
+
*/
|
|
123
|
+
stripeMetadata?: Readonly<Record<string, string>>;
|
|
113
124
|
};
|
|
114
125
|
|
|
115
126
|
export type SuperwallCallbackOptions = {
|
|
@@ -136,6 +147,8 @@ export type SuperwallSessionOptions = {
|
|
|
136
147
|
onDecodeError?: (error: unknown) => void;
|
|
137
148
|
};
|
|
138
149
|
|
|
150
|
+
export type SuperwallUserAttributes = Readonly<Record<string, string | number | boolean>>;
|
|
151
|
+
|
|
139
152
|
export interface SuperwallSession<out TProduct extends SuperwallProduct = SuperwallProduct> {
|
|
140
153
|
on: <E extends SuperwallEventName>(
|
|
141
154
|
event: E,
|
|
@@ -156,6 +169,7 @@ export interface SuperwallSession<out TProduct extends SuperwallProduct = Superw
|
|
|
156
169
|
openExternalUrl: (url: string) => void;
|
|
157
170
|
openDeepLink: (link: string) => void;
|
|
158
171
|
customPlacement: (name: string, params?: Record<string, unknown>) => void;
|
|
172
|
+
setUserAttributes: (attributes: SuperwallUserAttributes) => void;
|
|
159
173
|
requestStoreReview: (reviewType: Outgoing<"request_store_review">["review_type"]) => void;
|
|
160
174
|
pageView: (input: PageViewInput) => void;
|
|
161
175
|
hapticFeedback: (hapticType: Outgoing<"haptic_feedback">["haptic_type"]) => void;
|