@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
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { TIncomingMessageCallbackResult, TPermissionStatus } from "@superwall/protocol";
|
|
2
|
+
import type { SuperwallProduct } from "../products";
|
|
3
|
+
|
|
4
|
+
export type SuperwallSdkState = Readonly<Record<string, unknown>>;
|
|
5
|
+
|
|
6
|
+
export type SuperwallPermissionStatuses = Readonly<Record<string, TPermissionStatus>>;
|
|
7
|
+
|
|
8
|
+
export type SuperwallCallbackState = Readonly<{
|
|
9
|
+
result: TIncomingMessageCallbackResult["status"];
|
|
10
|
+
data?: Record<string, unknown>;
|
|
11
|
+
}>;
|
|
12
|
+
|
|
13
|
+
export type SuperwallCallbackStates = Readonly<Record<string, SuperwallCallbackState>>;
|
|
14
|
+
|
|
15
|
+
export type BuildSuperwallStateOptions<TProduct extends SuperwallProduct = SuperwallProduct> = {
|
|
16
|
+
variables: Readonly<Record<string, unknown>>;
|
|
17
|
+
products: readonly TProduct[];
|
|
18
|
+
paywall?: {
|
|
19
|
+
id: string;
|
|
20
|
+
identifier: string;
|
|
21
|
+
};
|
|
22
|
+
lastTransaction?: {
|
|
23
|
+
status: "completed" | "abandoned";
|
|
24
|
+
product: TProduct;
|
|
25
|
+
};
|
|
26
|
+
permissionStatuses?: SuperwallPermissionStatuses;
|
|
27
|
+
callbackStates?: SuperwallCallbackStates;
|
|
28
|
+
trial?: { eligible?: boolean };
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
32
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
33
|
+
|
|
34
|
+
const productStateFromVariables = (variables: Readonly<Record<string, unknown>>) => {
|
|
35
|
+
const products: Record<string, unknown> = {};
|
|
36
|
+
|
|
37
|
+
if (Array.isArray(variables.products)) {
|
|
38
|
+
for (const entry of variables.products) {
|
|
39
|
+
if (!isRecord(entry)) continue;
|
|
40
|
+
|
|
41
|
+
for (const [reference, productVariables] of Object.entries(entry)) {
|
|
42
|
+
products[reference] = productVariables;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
for (const reference of ["primary", "secondary", "tertiary"]) {
|
|
48
|
+
const productVariables = variables[reference];
|
|
49
|
+
if (isRecord(productVariables) && !products[reference]) {
|
|
50
|
+
products[reference] = productVariables;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return products;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const buildSuperwallState = <TProduct extends SuperwallProduct = SuperwallProduct>({
|
|
58
|
+
variables,
|
|
59
|
+
products,
|
|
60
|
+
paywall,
|
|
61
|
+
lastTransaction,
|
|
62
|
+
permissionStatuses = {},
|
|
63
|
+
callbackStates = {},
|
|
64
|
+
trial,
|
|
65
|
+
}: BuildSuperwallStateOptions<TProduct>): SuperwallSdkState => {
|
|
66
|
+
const productState = productStateFromVariables(variables);
|
|
67
|
+
|
|
68
|
+
if (trial?.eligible !== undefined) {
|
|
69
|
+
productState.hasIntroductoryOffer = trial.eligible;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
for (const product of products) {
|
|
73
|
+
const existingProductState = productState[product.reference];
|
|
74
|
+
productState[product.reference] = {
|
|
75
|
+
...(isRecord(existingProductState) ? existingProductState : {}),
|
|
76
|
+
...product.variables,
|
|
77
|
+
productId: product.productId,
|
|
78
|
+
productIdentifier: product.productIdentifier,
|
|
79
|
+
source: product.source,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const transactionState = {
|
|
84
|
+
didAbandonTransaction: lastTransaction?.status === "abandoned",
|
|
85
|
+
didCompleteTransaction: lastTransaction?.status === "completed",
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
productState.abandonedProductId =
|
|
89
|
+
lastTransaction?.status === "abandoned" ? lastTransaction.product.reference : "";
|
|
90
|
+
productState.purchasedProductId =
|
|
91
|
+
lastTransaction?.status === "completed" ? lastTransaction.product.reference : "";
|
|
92
|
+
|
|
93
|
+
if (lastTransaction) {
|
|
94
|
+
const transactionProduct = lastTransaction.product;
|
|
95
|
+
productState[lastTransaction.status === "completed" ? "purchased" : "abandoned"] =
|
|
96
|
+
transactionProduct.variables;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const state: Record<string, unknown> = {
|
|
100
|
+
...variables,
|
|
101
|
+
state: transactionState,
|
|
102
|
+
products: productState,
|
|
103
|
+
permissions: permissionStatuses,
|
|
104
|
+
callbacks: callbackStates,
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
if (paywall) {
|
|
108
|
+
state.paywall = paywall;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return state;
|
|
112
|
+
};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
TIncomingMessage,
|
|
3
|
+
TIncomingMessageCallbackResult,
|
|
4
|
+
TIncomingMessageTransactionAbandon,
|
|
5
|
+
TIncomingMessageTransactionComplete,
|
|
6
|
+
TOutgoingMessage,
|
|
7
|
+
} from "@superwall/protocol";
|
|
8
|
+
import type { SuperwallBridge, SuperwallBridgeOptions, SuperwallMessageLogEntry } from "../bridge";
|
|
9
|
+
import type { SuperwallConfig } from "../config";
|
|
10
|
+
import type { SuperwallProduct } from "../products";
|
|
11
|
+
import type {
|
|
12
|
+
SuperwallCallbackStates,
|
|
13
|
+
SuperwallPermissionStatuses,
|
|
14
|
+
SuperwallSdkState,
|
|
15
|
+
} from "./state";
|
|
16
|
+
import type {
|
|
17
|
+
SuperwallDeviceVariables,
|
|
18
|
+
SuperwallExperiment,
|
|
19
|
+
SuperwallPlacementParams,
|
|
20
|
+
SuperwallUserVariables,
|
|
21
|
+
} from "./variables";
|
|
22
|
+
|
|
23
|
+
export type SuperwallRuntimeStatus = "idle" | "listening";
|
|
24
|
+
|
|
25
|
+
export type SuperwallPaywallIdentity = {
|
|
26
|
+
id: string;
|
|
27
|
+
identifier: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type SuperwallPendingPurchase<out TProduct extends SuperwallProduct = SuperwallProduct> = {
|
|
31
|
+
reference: string;
|
|
32
|
+
product: TProduct;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type SuperwallTransaction<TProduct extends SuperwallProduct = SuperwallProduct> =
|
|
36
|
+
| {
|
|
37
|
+
status: "completed";
|
|
38
|
+
product: TProduct;
|
|
39
|
+
message: TIncomingMessageTransactionComplete;
|
|
40
|
+
}
|
|
41
|
+
| {
|
|
42
|
+
status: "abandoned";
|
|
43
|
+
product: TProduct;
|
|
44
|
+
message: TIncomingMessageTransactionAbandon;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type SuperwallPurchaseFailure<out TProduct extends SuperwallProduct = SuperwallProduct> = {
|
|
48
|
+
status: "failed";
|
|
49
|
+
product: TProduct;
|
|
50
|
+
/**
|
|
51
|
+
* "timeout": no transaction message arrived in time. "superseded": a newer
|
|
52
|
+
* purchase for the same product, or a paywall re-open, replaced this one.
|
|
53
|
+
*/
|
|
54
|
+
reason: "timeout" | "superseded";
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type SuperwallPurchaseResult<TProduct extends SuperwallProduct = SuperwallProduct> =
|
|
58
|
+
| SuperwallTransaction<TProduct>
|
|
59
|
+
| SuperwallPurchaseFailure<TProduct>;
|
|
60
|
+
|
|
61
|
+
export type SuperwallCallbackResult<TData = Record<string, unknown>> = {
|
|
62
|
+
status: TIncomingMessageCallbackResult["status"];
|
|
63
|
+
data?: TData;
|
|
64
|
+
message: TIncomingMessageCallbackResult;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export type SuperwallTrialState = {
|
|
68
|
+
/**
|
|
69
|
+
* Whether the user is eligible for an introductory offer. `undefined` until
|
|
70
|
+
* the SDK signals eligibility (or the config forces it).
|
|
71
|
+
*/
|
|
72
|
+
eligible?: boolean;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export type SuperwallRuntimeSnapshot<out TProduct extends SuperwallProduct = SuperwallProduct> = {
|
|
76
|
+
status: SuperwallRuntimeStatus;
|
|
77
|
+
config?: SuperwallConfig;
|
|
78
|
+
paywall?: SuperwallPaywallIdentity;
|
|
79
|
+
products: readonly TProduct[];
|
|
80
|
+
productsByReference: Readonly<Record<string, TProduct>>;
|
|
81
|
+
variables: Readonly<Record<string, unknown>>;
|
|
82
|
+
device: SuperwallDeviceVariables;
|
|
83
|
+
user: SuperwallUserVariables;
|
|
84
|
+
params: SuperwallPlacementParams;
|
|
85
|
+
experiment?: SuperwallExperiment;
|
|
86
|
+
trial: SuperwallTrialState;
|
|
87
|
+
locale: string;
|
|
88
|
+
state: SuperwallSdkState;
|
|
89
|
+
permissionStatuses: SuperwallPermissionStatuses;
|
|
90
|
+
callbackStates: SuperwallCallbackStates;
|
|
91
|
+
messages: readonly SuperwallMessageLogEntry[];
|
|
92
|
+
pendingPurchase?: SuperwallPendingPurchase<TProduct>;
|
|
93
|
+
lastTransaction?: SuperwallTransaction<TProduct>;
|
|
94
|
+
lastPurchaseFailure?: SuperwallPurchaseFailure<TProduct>;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export type SuperwallPurchaseOptions = {
|
|
98
|
+
shouldDismiss?: boolean;
|
|
99
|
+
/** Resolve as `{ status: "failed", reason: "timeout" }` if no transaction message arrives in time. */
|
|
100
|
+
timeoutMs?: number;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export type SuperwallEventMap = {
|
|
104
|
+
[E in TIncomingMessage["event_name"]]: Extract<TIncomingMessage, { event_name: E }>;
|
|
105
|
+
} & {
|
|
106
|
+
/** Fires for every incoming message. */
|
|
107
|
+
message: TIncomingMessage;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export type SuperwallEventName = keyof SuperwallEventMap & string;
|
|
111
|
+
|
|
112
|
+
export type SuperwallSessionStartOptions = {
|
|
113
|
+
sendPing?: boolean;
|
|
114
|
+
ping?: Partial<Extract<TOutgoingMessage, { event_name: "ping" }>>;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export type SuperwallSessionOptions<TProduct extends SuperwallProduct = SuperwallProduct> =
|
|
118
|
+
SuperwallBridgeOptions & {
|
|
119
|
+
bridge?: SuperwallBridge;
|
|
120
|
+
config?: SuperwallConfig;
|
|
121
|
+
products?: readonly TProduct[];
|
|
122
|
+
variables?: Record<string, unknown>;
|
|
123
|
+
};
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type { TIncomingMessageExperiment } from "@superwall/protocol";
|
|
2
|
+
|
|
3
|
+
export type SuperwallDeviceEntitlement = {
|
|
4
|
+
identifier: string;
|
|
5
|
+
type?: string;
|
|
6
|
+
store?: string;
|
|
7
|
+
state?: string;
|
|
8
|
+
isActive?: boolean;
|
|
9
|
+
isLifetime?: boolean;
|
|
10
|
+
willRenew?: boolean;
|
|
11
|
+
latestProductId?: string;
|
|
12
|
+
productIds?: readonly string[];
|
|
13
|
+
startsAt?: number;
|
|
14
|
+
expiresAt?: number;
|
|
15
|
+
renewedAt?: number;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type SuperwallDeviceTransaction = {
|
|
19
|
+
productId: string;
|
|
20
|
+
transactionId?: string;
|
|
21
|
+
subscriptionGroupId?: string;
|
|
22
|
+
store?: string;
|
|
23
|
+
isActive?: boolean;
|
|
24
|
+
isRevoked?: boolean;
|
|
25
|
+
isInGracePeriod?: boolean;
|
|
26
|
+
isInBillingRetryPeriod?: boolean;
|
|
27
|
+
willRenew?: boolean;
|
|
28
|
+
purchaseDate?: number;
|
|
29
|
+
expirationDate?: number;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type SuperwallCustomerInfo = {
|
|
33
|
+
entitlements?: readonly SuperwallDeviceEntitlement[];
|
|
34
|
+
subscriptions?: readonly SuperwallDeviceTransaction[];
|
|
35
|
+
nonSubscriptions?: readonly SuperwallDeviceTransaction[];
|
|
36
|
+
isPlaceholder?: boolean;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type SuperwallCapability = {
|
|
40
|
+
name: string;
|
|
41
|
+
eventNames?: readonly string[];
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Device variables the native SDK delivers via `template_variables`. The names
|
|
46
|
+
* mirror the SDK's device template exactly — there is no index signature, so a
|
|
47
|
+
* key the SDK does not send is a compile error rather than `undefined` on a
|
|
48
|
+
* device. Every field is optional: availability varies by platform and SDK
|
|
49
|
+
* version, and a paywall must render without any of them.
|
|
50
|
+
*/
|
|
51
|
+
export type SuperwallDeviceVariables = {
|
|
52
|
+
publicApiKey?: string;
|
|
53
|
+
platform?: string;
|
|
54
|
+
appUserId?: string;
|
|
55
|
+
aliases?: readonly string[];
|
|
56
|
+
vendorId?: string;
|
|
57
|
+
deviceId?: string;
|
|
58
|
+
appVersion?: string;
|
|
59
|
+
appVersionPadded?: string;
|
|
60
|
+
appBuildString?: string;
|
|
61
|
+
appBuildStringNumber?: number;
|
|
62
|
+
bundleId?: string;
|
|
63
|
+
osVersion?: string;
|
|
64
|
+
deviceModel?: string;
|
|
65
|
+
deviceLocale?: string;
|
|
66
|
+
preferredLocale?: string;
|
|
67
|
+
deviceLanguageCode?: string;
|
|
68
|
+
preferredLanguageCode?: string;
|
|
69
|
+
regionCode?: string;
|
|
70
|
+
preferredRegionCode?: string;
|
|
71
|
+
deviceCurrencyCode?: string;
|
|
72
|
+
deviceCurrencySymbol?: string;
|
|
73
|
+
interfaceType?: string;
|
|
74
|
+
interfaceStyle?: string;
|
|
75
|
+
interfaceStyleMode?: string;
|
|
76
|
+
timezoneOffset?: number;
|
|
77
|
+
radioType?: string;
|
|
78
|
+
isLowPowerModeEnabled?: boolean;
|
|
79
|
+
isApplePayAvailable?: boolean;
|
|
80
|
+
isMac?: boolean;
|
|
81
|
+
isSandbox?: string;
|
|
82
|
+
isFirstAppOpen?: boolean;
|
|
83
|
+
appInstallDate?: string;
|
|
84
|
+
daysSinceInstall?: number;
|
|
85
|
+
minutesSinceInstall?: number;
|
|
86
|
+
daysSinceLastPaywallView?: number;
|
|
87
|
+
minutesSinceLastPaywallView?: number;
|
|
88
|
+
totalPaywallViews?: number;
|
|
89
|
+
totalReviewRequests?: number;
|
|
90
|
+
utcDate?: string;
|
|
91
|
+
localDate?: string;
|
|
92
|
+
utcTime?: string;
|
|
93
|
+
localTime?: string;
|
|
94
|
+
utcDateTime?: string;
|
|
95
|
+
localDateTime?: string;
|
|
96
|
+
subscriptionStatus?: string;
|
|
97
|
+
activeEntitlements?: readonly string[];
|
|
98
|
+
activeEntitlementObjects?: readonly SuperwallDeviceEntitlement[];
|
|
99
|
+
activeProducts?: readonly string[];
|
|
100
|
+
customerInfo?: SuperwallCustomerInfo;
|
|
101
|
+
capabilitiesConfig?: readonly SuperwallCapability[];
|
|
102
|
+
sdkVersion?: string;
|
|
103
|
+
sdkVersionPadded?: string;
|
|
104
|
+
capabilities?: string;
|
|
105
|
+
platformWrapper?: string;
|
|
106
|
+
platformWrapperVersion?: string;
|
|
107
|
+
swiftVersion?: string;
|
|
108
|
+
compilerVersion?: string;
|
|
109
|
+
localResourceIds?: string;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export type SuperwallUserVariables = {
|
|
113
|
+
seed?: number;
|
|
114
|
+
appUserId?: string;
|
|
115
|
+
aliasId?: string;
|
|
116
|
+
applicationInstalledAt?: string;
|
|
117
|
+
} & Record<string, unknown>;
|
|
118
|
+
|
|
119
|
+
export type SuperwallPlacementParams = {
|
|
120
|
+
$event_name?: string;
|
|
121
|
+
event_name?: string;
|
|
122
|
+
$is_standard_event?: boolean;
|
|
123
|
+
$is_feature_gatable?: boolean;
|
|
124
|
+
$app_session_id?: string;
|
|
125
|
+
} & Record<string, unknown>;
|
|
126
|
+
|
|
127
|
+
/** Experiment assignment, mirroring the SDK's `experiment` message payload. */
|
|
128
|
+
export type SuperwallExperiment = Omit<TIncomingMessageExperiment, "event_name">;
|
|
129
|
+
|
|
130
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
131
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
132
|
+
|
|
133
|
+
export const recordFromVariables = (
|
|
134
|
+
variables: Readonly<Record<string, unknown>>,
|
|
135
|
+
key: string,
|
|
136
|
+
): Record<string, unknown> => {
|
|
137
|
+
const value = variables[key];
|
|
138
|
+
return isRecord(value) ? value : {};
|
|
139
|
+
};
|