@app-swiss-knife/react-native-sdk 0.2.7 → 0.3.1
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 +100 -47
- package/SwissKnifePurchases.podspec +8 -5
- package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +1 -1
- package/android/vendor/java/com/appswissknife/net/IngestClient.kt +1 -1
- package/ios/SwissKnifeCrashReporting.swift +59 -18
- package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +1 -1
- package/ios/vendor/SwissKnifeiOS/Context/Reachability.swift +1 -1
- package/ios/vendor/SwissKnifeiOS/CrashReporting/SKCrashReporting.swift +51 -1
- package/ios/vendor/SwissKnifeiOS/DeviceID.swift +22 -1
- package/ios/vendor/SwissKnifeiOS/Identity/Identity.swift +105 -7
- package/ios/vendor/SwissKnifeiOS/Net/IngestClient.swift +14 -0
- package/lib/commonjs/attributes.js +103 -0
- package/lib/commonjs/attributes.js.map +1 -0
- package/lib/commonjs/index.js +31 -2
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/internal/config.js +237 -0
- package/lib/commonjs/internal/config.js.map +1 -0
- package/lib/commonjs/purchases.js +152 -209
- package/lib/commonjs/purchases.js.map +1 -1
- package/lib/commonjs/swiss-knife.js +115 -0
- package/lib/commonjs/swiss-knife.js.map +1 -0
- package/lib/commonjs/types.js +11 -0
- package/lib/commonjs/types.js.map +1 -1
- package/lib/module/attributes.js +100 -0
- package/lib/module/attributes.js.map +1 -0
- package/lib/module/index.js +19 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/internal/config.js +225 -0
- package/lib/module/internal/config.js.map +1 -0
- package/lib/module/purchases.js +132 -198
- package/lib/module/purchases.js.map +1 -1
- package/lib/module/swiss-knife.js +112 -0
- package/lib/module/swiss-knife.js.map +1 -0
- package/lib/module/types.js +11 -0
- package/lib/module/types.js.map +1 -1
- package/lib/typescript/commonjs/attributes.d.ts +41 -0
- package/lib/typescript/commonjs/attributes.d.ts.map +1 -0
- package/lib/typescript/commonjs/index.d.ts +2 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/internal/config.d.ts +121 -0
- package/lib/typescript/commonjs/internal/config.d.ts.map +1 -0
- package/lib/typescript/commonjs/purchases.d.ts +47 -91
- package/lib/typescript/commonjs/purchases.d.ts.map +1 -1
- package/lib/typescript/commonjs/swiss-knife.d.ts +56 -0
- package/lib/typescript/commonjs/swiss-knife.d.ts.map +1 -0
- package/lib/typescript/commonjs/types.d.ts +13 -0
- package/lib/typescript/commonjs/types.d.ts.map +1 -1
- package/lib/typescript/module/attributes.d.ts +41 -0
- package/lib/typescript/module/attributes.d.ts.map +1 -0
- package/lib/typescript/module/index.d.ts +2 -0
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/internal/config.d.ts +121 -0
- package/lib/typescript/module/internal/config.d.ts.map +1 -0
- package/lib/typescript/module/purchases.d.ts +47 -91
- package/lib/typescript/module/purchases.d.ts.map +1 -1
- package/lib/typescript/module/swiss-knife.d.ts +56 -0
- package/lib/typescript/module/swiss-knife.d.ts.map +1 -0
- package/lib/typescript/module/types.d.ts +13 -0
- package/lib/typescript/module/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/attributes.ts +97 -0
- package/src/index.ts +19 -2
- package/src/internal/config.ts +233 -0
- package/src/purchases.ts +142 -213
- package/src/swiss-knife.ts +134 -0
- package/src/types.ts +14 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
// src/internal/config.ts
|
|
2
|
+
//
|
|
3
|
+
// The SDK's bootstrap: the process-wide "have we been configured?" state, the
|
|
4
|
+
// two configure entry points, the two readback flags, the log level, and the
|
|
5
|
+
// guards every public surface is gated on.
|
|
6
|
+
//
|
|
7
|
+
// ## Why this is a module of its own rather than statics on a namespace
|
|
8
|
+
//
|
|
9
|
+
// `SwissKnife.configure(...)` and `Purchases.configure(...)` are one call under
|
|
10
|
+
// two names. If each namespace owned its own copy of `configured` /
|
|
11
|
+
// `coreConfigured`, an app that bootstrapped through one name and then used a
|
|
12
|
+
// surface guarded by the other would be told it had never configured anything —
|
|
13
|
+
// precisely the class of bug a namespace split invites. There is exactly one
|
|
14
|
+
// implementation here, and every public namespace exports a *reference to it*.
|
|
15
|
+
// That makes `SwissKnife.configure === Purchases.configure` literally true (and
|
|
16
|
+
// `Attributes.set === Purchases.setAttributes`, and so on), so the compatibility
|
|
17
|
+
// aliases cannot drift from the canonical methods by construction rather than by
|
|
18
|
+
// diligence. Pinned in `src/namespaces.test.ts`.
|
|
19
|
+
//
|
|
20
|
+
// Nothing in here is part of the published API. It is reachable via the
|
|
21
|
+
// package's `./*` subpath export, as every file under `src/` is; the `internal/`
|
|
22
|
+
// segment is the signal not to.
|
|
23
|
+
|
|
24
|
+
import { getNativeModule } from "../native/module";
|
|
25
|
+
import { UninitializedPurchasesError } from "../types";
|
|
26
|
+
import type { LOG_LEVEL, ConfigureOptions } from "../types";
|
|
27
|
+
import type {
|
|
28
|
+
NativeConfigureOptions,
|
|
29
|
+
SwissKnifePurchasesNativeModule,
|
|
30
|
+
} from "../native/spec";
|
|
31
|
+
|
|
32
|
+
let configured = false;
|
|
33
|
+
let configuredApiKey: string | null = null;
|
|
34
|
+
let coreConfigured = false;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Read a boolean off the native module without ever letting a missing native
|
|
38
|
+
* module escape as an exception.
|
|
39
|
+
*
|
|
40
|
+
* `getNativeModule()` THROWS when the module is not linked (Expo Go, a bare RN
|
|
41
|
+
* app that skipped `pod install` / a Gradle sync, web) — precisely the state a
|
|
42
|
+
* developer is in while wiring the SDK up, and precisely when they are most
|
|
43
|
+
* likely to be calling one of these accessors to find out what is wrong. A
|
|
44
|
+
* boolean accessor rejecting with "native module is not linked" is a worse
|
|
45
|
+
* answer than `false`: an unlinked module cannot have configured anything, so
|
|
46
|
+
* `false` is not merely safe, it is correct.
|
|
47
|
+
*
|
|
48
|
+
* The `.catch` covers the adjacent case where the module IS linked but predates
|
|
49
|
+
* the method (JS upgraded without rebuilding the app) — same reasoning, same
|
|
50
|
+
* answer. Mirrors the tolerance `Analytics.init` already applies to
|
|
51
|
+
* `configureObservability`.
|
|
52
|
+
*/
|
|
53
|
+
export function nativeBoolean(
|
|
54
|
+
read: (native: SwissKnifePurchasesNativeModule) => Promise<boolean>
|
|
55
|
+
): Promise<boolean> {
|
|
56
|
+
try {
|
|
57
|
+
return Promise.resolve(read(getNativeModule())).catch(() => false);
|
|
58
|
+
} catch {
|
|
59
|
+
return Promise.resolve(false);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Configure the SDK. Must be called before any other method. Calling again
|
|
65
|
+
* with the same API key is a no-op (matches the common "configure once" guard);
|
|
66
|
+
* the native layer owns full reconfiguration.
|
|
67
|
+
*/
|
|
68
|
+
export function configure(options: ConfigureOptions): void {
|
|
69
|
+
if (configured && configuredApiKey === options.apiKey) return;
|
|
70
|
+
const native = getNativeModule();
|
|
71
|
+
const nativeOptions: NativeConfigureOptions = {
|
|
72
|
+
apiKey: options.apiKey,
|
|
73
|
+
appUserID: options.appUserID ?? null,
|
|
74
|
+
// proxyURL points the native SDK at the Swiss Knife backend. Without it the
|
|
75
|
+
// native layer falls back to its hardcoded default host (api.swiss-knife.dev),
|
|
76
|
+
// which is unreachable for self-hosted tenants. Forward it so callers can
|
|
77
|
+
// target their own backend (e.g. https://swiss.remneys.com).
|
|
78
|
+
proxyURL: options.proxyURL ?? null,
|
|
79
|
+
// Hardening pass (spec): diagnostics opt-in only — default OFF.
|
|
80
|
+
diagnosticsEnabled: options.diagnosticsEnabled ?? false,
|
|
81
|
+
storeKitVersion: options.storeKitVersion as string | undefined,
|
|
82
|
+
useAmazon: options.useAmazon,
|
|
83
|
+
// iOS ATT — default off (no tracking prompt) unless the host opts in.
|
|
84
|
+
autoPromptATT: options.autoPromptATT,
|
|
85
|
+
sendInstallOnFirstLaunch: options.sendInstallOnFirstLaunch,
|
|
86
|
+
logLevel: options.logLevel,
|
|
87
|
+
};
|
|
88
|
+
// Fire-and-forget: native configure resolves async but RC's configure is sync.
|
|
89
|
+
void native.configure(nativeOptions);
|
|
90
|
+
configured = true;
|
|
91
|
+
coreConfigured = true;
|
|
92
|
+
configuredApiKey = options.apiKey;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Configure everything EXCEPT the store: ingest, `Identity`, `Attribution`,
|
|
97
|
+
* `Attributes`.
|
|
98
|
+
*
|
|
99
|
+
* Use this when another library already owns in-app purchases. `configure`
|
|
100
|
+
* starts a `StoreKit.Transaction.updates` observer on iOS (whose handler
|
|
101
|
+
* calls `transaction.finish()`) and builds a `BillingClient` on Android —
|
|
102
|
+
* both of which contend with an incumbent IAP SDK. `configureCore` creates
|
|
103
|
+
* neither, and registers no install as a side effect.
|
|
104
|
+
*
|
|
105
|
+
* Analytics-only apps don't even need this: `Analytics.init(...)` already
|
|
106
|
+
* wires core natively. Reach for `configureCore` when you want to pass an
|
|
107
|
+
* `appUserID`, an attribution configuration, or a log level up front.
|
|
108
|
+
*/
|
|
109
|
+
export function configureCore(options: ConfigureOptions): void {
|
|
110
|
+
if (coreConfigured && configuredApiKey === options.apiKey) return;
|
|
111
|
+
const native = getNativeModule();
|
|
112
|
+
const nativeOptions: NativeConfigureOptions = {
|
|
113
|
+
apiKey: options.apiKey,
|
|
114
|
+
appUserID: options.appUserID ?? null,
|
|
115
|
+
proxyURL: options.proxyURL ?? null,
|
|
116
|
+
diagnosticsEnabled: options.diagnosticsEnabled ?? false,
|
|
117
|
+
autoPromptATT: options.autoPromptATT,
|
|
118
|
+
// Forwarded but inert on this path — core never auto-registers an
|
|
119
|
+
// install. Kept on the payload so the same options object can be handed
|
|
120
|
+
// to `configure` later without surprises.
|
|
121
|
+
sendInstallOnFirstLaunch: options.sendInstallOnFirstLaunch,
|
|
122
|
+
logLevel: options.logLevel,
|
|
123
|
+
};
|
|
124
|
+
void native.configureCore(nativeOptions);
|
|
125
|
+
coreConfigured = true;
|
|
126
|
+
configuredApiKey = options.apiKey;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Whether the PURCHASE engine has been configured — i.e. whether the store
|
|
131
|
+
* APIs (`getOfferings`, `purchasePackage`, `restorePurchases`, …) can work.
|
|
132
|
+
*
|
|
133
|
+
* **Native is the source of truth.** This does not consult the JS-side
|
|
134
|
+
* "configure was called" flag: a brownfield app can configure the SDK from
|
|
135
|
+
* native code, and `configure` itself is fire-and-forget, so the JS flag is a
|
|
136
|
+
* request, not a fact.
|
|
137
|
+
*
|
|
138
|
+
* Never throws and never rejects. Resolves `false` when the native module is
|
|
139
|
+
* not linked — see {@link nativeBoolean}. Exactly the same failure semantics
|
|
140
|
+
* as {@link isCoreConfigured}; the two differ only in which native flag they
|
|
141
|
+
* report.
|
|
142
|
+
*/
|
|
143
|
+
export function isConfigured(): Promise<boolean> {
|
|
144
|
+
return nativeBoolean((native) => native.isConfigured());
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Whether the CORE (ingest / Identity / Attribution / Attributes) is
|
|
149
|
+
* configured. True after `configure`, `configureCore`, or `Analytics.init`.
|
|
150
|
+
*
|
|
151
|
+
* **Native is the source of truth**, and here that is load-bearing rather
|
|
152
|
+
* than merely principled: `Analytics.init` wires core natively via
|
|
153
|
+
* `configureObservability` and touches no JS configure flag, so an
|
|
154
|
+
* analytics-only app has core configured while every JS flag reads false.
|
|
155
|
+
* Short-circuiting on JS state would report `false` in the single most
|
|
156
|
+
* common core-only integration.
|
|
157
|
+
*
|
|
158
|
+
* Never throws and never rejects. Resolves `false` when the native module is
|
|
159
|
+
* not linked — see {@link nativeBoolean}.
|
|
160
|
+
*/
|
|
161
|
+
export function isCoreConfigured(): Promise<boolean> {
|
|
162
|
+
return nativeBoolean((native) => native.isCoreConfigured());
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Set the native log level. Process-wide: it is read by the store engine, the
|
|
167
|
+
* ingest client, Identity, Attribution and the observability modules alike,
|
|
168
|
+
* which is why it belongs on the SDK root and not on any one namespace.
|
|
169
|
+
*
|
|
170
|
+
* Deliberately ungated — a log level you can only raise *after* a successful
|
|
171
|
+
* configure is useless for debugging a configure that is going wrong. It does
|
|
172
|
+
* still require the native module to be linked, and rejects if it is not.
|
|
173
|
+
*/
|
|
174
|
+
export function setLogLevel(level: LOG_LEVEL): Promise<void> {
|
|
175
|
+
return getNativeModule().setLogLevel(level as unknown as string);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Guard for surfaces that genuinely need the PURCHASE engine — `getOfferings`,
|
|
180
|
+
* `purchasePackage`, `restorePurchases` and friends. Checks the JS "configure
|
|
181
|
+
* was called" flag.
|
|
182
|
+
*
|
|
183
|
+
* The RC-parity identity aliases used to sit here too, on the grounds that the
|
|
184
|
+
* legacy native bridge methods they called meant different things on iOS and
|
|
185
|
+
* Android. They now delegate to `Identity` and sit on
|
|
186
|
+
* {@link throwIfCoreNotConfigured} — see the block above `Purchases.logIn`.
|
|
187
|
+
*/
|
|
188
|
+
export function throwIfNotConfigured(): void {
|
|
189
|
+
if (!configured) {
|
|
190
|
+
throw new UninitializedPurchasesError();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Guard for surfaces that only need CORE — the `Attributes` setters.
|
|
196
|
+
*
|
|
197
|
+
* Attributes describe the *person*, not the purchase: natively they land in
|
|
198
|
+
* `AttributesClient` → `POST /api/sdk/users/attributes` → `app_users.properties`,
|
|
199
|
+
* which is what the dashboard person card reads. Neither platform's attributes
|
|
200
|
+
* path touches StoreKit or Play Billing — iOS gates on `SwissKnife.isCoreConfigured`
|
|
201
|
+
* (`SwissKnife.currentAnonymousIdForAttributes`), and Android's `configureCore`
|
|
202
|
+
* wires `AttributesClient` with an `Identity`-snapshot id fallback precisely so a
|
|
203
|
+
* store-less app still resolves ids. Gating these on the purchase flag left an app
|
|
204
|
+
* that uses `configureCore` (because another library owns the store) with **no way
|
|
205
|
+
* at all** to populate the person card.
|
|
206
|
+
*
|
|
207
|
+
* Two-step check, and the second step is load-bearing:
|
|
208
|
+
*
|
|
209
|
+
* 1. The JS flag, set by `configure` / `configureCore`. Free, no bridge hop.
|
|
210
|
+
* 2. Native truth. `Analytics.init(...)` wires core natively via
|
|
211
|
+
* `configureObservability` and touches no JS flag, so an analytics-only app
|
|
212
|
+
* has core configured while every JS flag reads `false` — exactly the case
|
|
213
|
+
* {@link isCoreConfigured} documents. Skipping this step would keep the most
|
|
214
|
+
* common core-only integration broken.
|
|
215
|
+
*
|
|
216
|
+
* Never consults the purchase flag. Strictly more permissive than
|
|
217
|
+
* {@link throwIfNotConfigured}, so no existing caller can break.
|
|
218
|
+
*/
|
|
219
|
+
export async function throwIfCoreNotConfigured(): Promise<void> {
|
|
220
|
+
if (coreConfigured) return;
|
|
221
|
+
if (await nativeBoolean((native) => native.isCoreConfigured())) return;
|
|
222
|
+
throw new UninitializedPurchasesError(
|
|
223
|
+
"Swiss Knife core is not configured. Call SwissKnife.configure(), " +
|
|
224
|
+
"SwissKnife.configureCore(), or Analytics.init() first."
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** @internal Test seam: drop the process-wide configure state. */
|
|
229
|
+
export function resetConfigForTests(): void {
|
|
230
|
+
configured = false;
|
|
231
|
+
coreConfigured = false;
|
|
232
|
+
configuredApiKey = null;
|
|
233
|
+
}
|
package/src/purchases.ts
CHANGED
|
@@ -1,12 +1,67 @@
|
|
|
1
1
|
// src/purchases.ts
|
|
2
|
+
//
|
|
3
|
+
// The RevenueCat-compatible surface. Everything a `react-native-purchases`
|
|
4
|
+
// migration can call, spelled the way RevenueCat spells it.
|
|
5
|
+
//
|
|
6
|
+
// ## What this file is, after the namespace split
|
|
7
|
+
//
|
|
8
|
+
// `Purchases` used to be the whole SDK: the store, both bootstraps, subscriber
|
|
9
|
+
// attributes and the identity aliases, twenty-one statics across four unrelated
|
|
10
|
+
// concerns — while `Identity`, `Attribution`, `Analytics`, `CrashReporting` and
|
|
11
|
+
// `Performance` sat beside it as peer namespaces. Two organising principles at
|
|
12
|
+
// once, and the seams showed: `Purchases.configureCore()` is a call whose entire
|
|
13
|
+
// meaning is "I do not want the store", and an app with no purchases in it set
|
|
14
|
+
// its user data through `Purchases.setAttributes`.
|
|
15
|
+
//
|
|
16
|
+
// The store methods below are the ones that genuinely belong here. The rest are
|
|
17
|
+
// **RC-compatibility aliases**, and they are not going anywhere:
|
|
18
|
+
//
|
|
19
|
+
// - bootstrap → `SwissKnife.configure` / `configureCore` / `isConfigured` /
|
|
20
|
+
// `isCoreConfigured` / `setLogLevel` (src/swiss-knife.ts)
|
|
21
|
+
// - attributes → `Attributes.set` / `setEmail` / `setDisplayName` /
|
|
22
|
+
// `setPhoneNumber` / `setPushToken` (src/attributes.ts)
|
|
23
|
+
// - identity → `Identity.logIn` / `logOut` / `current` (src/identity.ts)
|
|
24
|
+
//
|
|
25
|
+
// ## Why the aliases are not deprecated
|
|
26
|
+
//
|
|
27
|
+
// This class exists so that someone migrating off `react-native-purchases` can
|
|
28
|
+
// keep their call sites. Emitting deprecation warnings at exactly those call
|
|
29
|
+
// sites would punish the users the compatibility surface was built for — the
|
|
30
|
+
// migration would arrive pre-broken, with a console full of warnings for code
|
|
31
|
+
// that is doing the right thing. They are documented as aliases and left alone.
|
|
32
|
+
//
|
|
33
|
+
// The four identity aliases are the exception and keep their `@deprecated` tags,
|
|
34
|
+
// because they are a different case: `Identity` is genuinely the canonical home,
|
|
35
|
+
// and the RC names carry return types (`logOut` resolving a `CustomerInfo`) that
|
|
36
|
+
// misdescribe what the call does in a store-less app.
|
|
37
|
+
//
|
|
38
|
+
// ## Why they cannot drift
|
|
39
|
+
//
|
|
40
|
+
// The aliases are not re-implementations and not one-line wrappers: they are the
|
|
41
|
+
// *same function objects* as the canonical methods, assigned as static fields
|
|
42
|
+
// from the modules that own them. `Purchases.configure === SwissKnife.configure`
|
|
43
|
+
// and `Purchases.setAttributes === Attributes.set` are literally true. There is
|
|
44
|
+
// no second copy of the bootstrap state either — see `src/internal/config.ts`.
|
|
45
|
+
// Pinned by `src/namespaces.test.ts`.
|
|
46
|
+
|
|
47
|
+
import { Attributes } from "./attributes";
|
|
2
48
|
import { Identity } from "./identity";
|
|
49
|
+
import {
|
|
50
|
+
configure,
|
|
51
|
+
configureCore,
|
|
52
|
+
isConfigured,
|
|
53
|
+
isCoreConfigured,
|
|
54
|
+
nativeBoolean,
|
|
55
|
+
setLogLevel,
|
|
56
|
+
throwIfCoreNotConfigured,
|
|
57
|
+
throwIfNotConfigured,
|
|
58
|
+
resetConfigForTests,
|
|
59
|
+
} from "./internal/config";
|
|
3
60
|
import { getNativeModule, getEventEmitter } from "./native/module";
|
|
4
61
|
import { CUSTOMER_INFO_UPDATED_EVENT } from "./native/spec";
|
|
5
|
-
import {
|
|
62
|
+
import { VERIFICATION_RESULT } from "./types";
|
|
6
63
|
import type { EmitterSubscription } from "react-native";
|
|
7
64
|
import type {
|
|
8
|
-
PurchasesConfiguration,
|
|
9
|
-
LOG_LEVEL,
|
|
10
65
|
CustomerInfo,
|
|
11
66
|
PurchasesOfferings,
|
|
12
67
|
PurchasesPackage,
|
|
@@ -15,41 +70,6 @@ import type {
|
|
|
15
70
|
LogInResult,
|
|
16
71
|
CustomerInfoUpdateListener,
|
|
17
72
|
} from "./types";
|
|
18
|
-
import type {
|
|
19
|
-
NativeConfigureOptions,
|
|
20
|
-
SwissKnifePurchasesNativeModule,
|
|
21
|
-
} from "./native/spec";
|
|
22
|
-
|
|
23
|
-
let configured = false;
|
|
24
|
-
let configuredApiKey: string | null = null;
|
|
25
|
-
let coreConfigured = false;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Read a boolean off the native module without ever letting a missing native
|
|
29
|
-
* module escape as an exception.
|
|
30
|
-
*
|
|
31
|
-
* `getNativeModule()` THROWS when the module is not linked (Expo Go, a bare RN
|
|
32
|
-
* app that skipped `pod install` / a Gradle sync, web) — precisely the state a
|
|
33
|
-
* developer is in while wiring the SDK up, and precisely when they are most
|
|
34
|
-
* likely to be calling one of these accessors to find out what is wrong. A
|
|
35
|
-
* boolean accessor rejecting with "native module is not linked" is a worse
|
|
36
|
-
* answer than `false`: an unlinked module cannot have configured anything, so
|
|
37
|
-
* `false` is not merely safe, it is correct.
|
|
38
|
-
*
|
|
39
|
-
* The `.catch` covers the adjacent case where the module IS linked but predates
|
|
40
|
-
* the method (JS upgraded without rebuilding the app) — same reasoning, same
|
|
41
|
-
* answer. Mirrors the tolerance `Analytics.init` already applies to
|
|
42
|
-
* `configureObservability`.
|
|
43
|
-
*/
|
|
44
|
-
function nativeBoolean(
|
|
45
|
-
read: (native: SwissKnifePurchasesNativeModule) => Promise<boolean>
|
|
46
|
-
): Promise<boolean> {
|
|
47
|
-
try {
|
|
48
|
-
return Promise.resolve(read(getNativeModule())).catch(() => false);
|
|
49
|
-
} catch {
|
|
50
|
-
return Promise.resolve(false);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
73
|
|
|
54
74
|
// Maps a caller's listener fn to its native subscription so we can remove it.
|
|
55
75
|
const customerInfoSubscriptions = new Map<
|
|
@@ -57,56 +77,6 @@ const customerInfoSubscriptions = new Map<
|
|
|
57
77
|
EmitterSubscription
|
|
58
78
|
>();
|
|
59
79
|
|
|
60
|
-
/**
|
|
61
|
-
* Guard for surfaces that genuinely need the PURCHASE engine — `getOfferings`,
|
|
62
|
-
* `purchasePackage`, `restorePurchases` and friends. Checks the JS "configure
|
|
63
|
-
* was called" flag.
|
|
64
|
-
*
|
|
65
|
-
* The RC-parity identity aliases used to sit here too, on the grounds that the
|
|
66
|
-
* legacy native bridge methods they called meant different things on iOS and
|
|
67
|
-
* Android. They now delegate to `Identity` and sit on
|
|
68
|
-
* {@link throwIfCoreNotConfigured} — see the block above `logIn`.
|
|
69
|
-
*/
|
|
70
|
-
function throwIfNotConfigured(): void {
|
|
71
|
-
if (!configured) {
|
|
72
|
-
throw new UninitializedPurchasesError();
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Guard for surfaces that only need CORE — the subscriber-attribute setters.
|
|
78
|
-
*
|
|
79
|
-
* Attributes describe the *person*, not the purchase: natively they land in
|
|
80
|
-
* `AttributesClient` → `POST /api/sdk/users/attributes` → `app_users.properties`,
|
|
81
|
-
* which is what the dashboard person card reads. Neither platform's attributes
|
|
82
|
-
* path touches StoreKit or Play Billing — iOS gates on `SwissKnife.isCoreConfigured`
|
|
83
|
-
* (`SwissKnife.currentAnonymousIdForAttributes`), and Android's `configureCore`
|
|
84
|
-
* wires `AttributesClient` with an `Identity`-snapshot id fallback precisely so a
|
|
85
|
-
* store-less app still resolves ids. Gating these on the purchase flag left an app
|
|
86
|
-
* that uses `configureCore` (because another library owns the store) with **no way
|
|
87
|
-
* at all** to populate the person card.
|
|
88
|
-
*
|
|
89
|
-
* Two-step check, and the second step is load-bearing:
|
|
90
|
-
*
|
|
91
|
-
* 1. The JS flag, set by `configure` / `configureCore`. Free, no bridge hop.
|
|
92
|
-
* 2. Native truth. `Analytics.init(...)` wires core natively via
|
|
93
|
-
* `configureObservability` and touches no JS flag, so an analytics-only app
|
|
94
|
-
* has core configured while every JS flag reads `false` — exactly the case
|
|
95
|
-
* {@link isCoreConfigured} documents. Skipping this step would keep the most
|
|
96
|
-
* common core-only integration broken.
|
|
97
|
-
*
|
|
98
|
-
* Never consults the purchase flag. Strictly more permissive than
|
|
99
|
-
* {@link throwIfNotConfigured}, so no existing caller can break.
|
|
100
|
-
*/
|
|
101
|
-
async function throwIfCoreNotConfigured(): Promise<void> {
|
|
102
|
-
if (coreConfigured) return;
|
|
103
|
-
if (await nativeBoolean((native) => native.isCoreConfigured())) return;
|
|
104
|
-
throw new UninitializedPurchasesError(
|
|
105
|
-
"Purchases core is not configured. Call Purchases.configure(), " +
|
|
106
|
-
"Purchases.configureCore(), or Analytics.init() first."
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
80
|
/**
|
|
111
81
|
* The `CustomerInfo` RC's `logOut()` resolves with, for a device that just
|
|
112
82
|
* became anonymous.
|
|
@@ -168,115 +138,70 @@ function emptyCustomerInfo(subjectId: string): CustomerInfo {
|
|
|
168
138
|
}
|
|
169
139
|
|
|
170
140
|
/**
|
|
171
|
-
* RC-compatible subscriptions SDK entry point. Process-wide singleton
|
|
172
|
-
*
|
|
141
|
+
* RC-compatible subscriptions SDK entry point. Process-wide singleton.
|
|
142
|
+
*
|
|
143
|
+
* New code should bootstrap through {@link SwissKnife} — `SwissKnife.configure(…)`
|
|
144
|
+
* — and reach the store through this class. Code migrating from
|
|
145
|
+
* `react-native-purchases` can keep calling `Purchases.configure(…)`: it is the
|
|
146
|
+
* same function.
|
|
173
147
|
*/
|
|
174
148
|
export class Purchases {
|
|
149
|
+
// MARK: - Bootstrap (RC-compatibility aliases for `SwissKnife.*`)
|
|
150
|
+
//
|
|
151
|
+
// Assigned, not wrapped: these ARE `SwissKnife.configure` and friends. The
|
|
152
|
+
// configure state lives in one module (`./internal/config`), so mixing the two
|
|
153
|
+
// spellings in one app is not merely allowed, it is indistinguishable.
|
|
154
|
+
|
|
175
155
|
/**
|
|
176
156
|
* Configure the SDK. Must be called before any other method. Calling again
|
|
177
|
-
* with the same API key is a no-op
|
|
178
|
-
*
|
|
157
|
+
* with the same API key is a no-op; the native layer owns full reconfiguration.
|
|
158
|
+
*
|
|
159
|
+
* RC-compatibility alias for `SwissKnife.configure(...)` — the same function
|
|
160
|
+
* under the name `react-native-purchases` uses. Neither is deprecated.
|
|
179
161
|
*/
|
|
180
|
-
static configure
|
|
181
|
-
options: PurchasesConfiguration & { logLevel?: LOG_LEVEL }
|
|
182
|
-
): void {
|
|
183
|
-
if (configured && configuredApiKey === options.apiKey) return;
|
|
184
|
-
const native = getNativeModule();
|
|
185
|
-
const nativeOptions: NativeConfigureOptions = {
|
|
186
|
-
apiKey: options.apiKey,
|
|
187
|
-
appUserID: options.appUserID ?? null,
|
|
188
|
-
// proxyURL points the native SDK at the Swiss Knife backend. Without it the
|
|
189
|
-
// native layer falls back to its hardcoded default host (api.swiss-knife.dev),
|
|
190
|
-
// which is unreachable for self-hosted tenants. Forward it so callers can
|
|
191
|
-
// target their own backend (e.g. https://swiss.remneys.com).
|
|
192
|
-
proxyURL: options.proxyURL ?? null,
|
|
193
|
-
// Hardening pass (spec): diagnostics opt-in only — default OFF.
|
|
194
|
-
diagnosticsEnabled: options.diagnosticsEnabled ?? false,
|
|
195
|
-
storeKitVersion: options.storeKitVersion as string | undefined,
|
|
196
|
-
useAmazon: options.useAmazon,
|
|
197
|
-
// iOS ATT — default off (no tracking prompt) unless the host opts in.
|
|
198
|
-
autoPromptATT: options.autoPromptATT,
|
|
199
|
-
sendInstallOnFirstLaunch: options.sendInstallOnFirstLaunch,
|
|
200
|
-
logLevel: options.logLevel,
|
|
201
|
-
};
|
|
202
|
-
// Fire-and-forget: native configure resolves async but RC's configure is sync.
|
|
203
|
-
void native.configure(nativeOptions);
|
|
204
|
-
configured = true;
|
|
205
|
-
coreConfigured = true;
|
|
206
|
-
configuredApiKey = options.apiKey;
|
|
207
|
-
}
|
|
162
|
+
static configure = configure;
|
|
208
163
|
|
|
209
164
|
/**
|
|
210
165
|
* Configure everything EXCEPT the store: ingest, `Identity`, `Attribution`,
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
* Use this when another library already owns in-app purchases. `configure`
|
|
214
|
-
* starts a `StoreKit.Transaction.updates` observer on iOS (whose handler
|
|
215
|
-
* calls `transaction.finish()`) and builds a `BillingClient` on Android —
|
|
216
|
-
* both of which contend with an incumbent IAP SDK. `configureCore` creates
|
|
217
|
-
* neither, and registers no install as a side effect.
|
|
166
|
+
* `Attributes`. Use it when another library already owns in-app purchases.
|
|
218
167
|
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
168
|
+
* Alias for `SwissKnife.configureCore(...)`, which is the name that actually
|
|
169
|
+
* describes the call — nothing about this method concerns purchases, and it is
|
|
170
|
+
* only spelled `Purchases.configureCore` here so that no existing call site
|
|
171
|
+
* breaks. Prefer the `SwissKnife` spelling in new code.
|
|
222
172
|
*/
|
|
223
|
-
static configureCore
|
|
224
|
-
options: PurchasesConfiguration & { logLevel?: LOG_LEVEL }
|
|
225
|
-
): void {
|
|
226
|
-
if (coreConfigured && configuredApiKey === options.apiKey) return;
|
|
227
|
-
const native = getNativeModule();
|
|
228
|
-
const nativeOptions: NativeConfigureOptions = {
|
|
229
|
-
apiKey: options.apiKey,
|
|
230
|
-
appUserID: options.appUserID ?? null,
|
|
231
|
-
proxyURL: options.proxyURL ?? null,
|
|
232
|
-
diagnosticsEnabled: options.diagnosticsEnabled ?? false,
|
|
233
|
-
autoPromptATT: options.autoPromptATT,
|
|
234
|
-
// Forwarded but inert on this path — core never auto-registers an
|
|
235
|
-
// install. Kept on the payload so the same options object can be handed
|
|
236
|
-
// to `configure` later without surprises.
|
|
237
|
-
sendInstallOnFirstLaunch: options.sendInstallOnFirstLaunch,
|
|
238
|
-
logLevel: options.logLevel,
|
|
239
|
-
};
|
|
240
|
-
void native.configureCore(nativeOptions);
|
|
241
|
-
coreConfigured = true;
|
|
242
|
-
configuredApiKey = options.apiKey;
|
|
243
|
-
}
|
|
173
|
+
static configureCore = configureCore;
|
|
244
174
|
|
|
245
175
|
/**
|
|
246
176
|
* Whether the PURCHASE engine has been configured — i.e. whether the store
|
|
247
|
-
* APIs
|
|
177
|
+
* APIs below can work. Native is the source of truth; never throws or rejects.
|
|
248
178
|
*
|
|
249
|
-
*
|
|
250
|
-
* "configure was called" flag: a brownfield app can configure the SDK from
|
|
251
|
-
* native code, and `Purchases.configure` itself is fire-and-forget, so the
|
|
252
|
-
* JS flag is a request, not a fact.
|
|
253
|
-
*
|
|
254
|
-
* Never throws and never rejects. Resolves `false` when the native module is
|
|
255
|
-
* not linked — see {@link nativeBoolean}. Exactly the same failure semantics
|
|
256
|
-
* as {@link isCoreConfigured}; the two differ only in which native flag they
|
|
257
|
-
* report.
|
|
179
|
+
* RC-compatibility alias for `SwissKnife.isConfigured()`.
|
|
258
180
|
*/
|
|
259
|
-
static isConfigured
|
|
260
|
-
return nativeBoolean((native) => native.isConfigured());
|
|
261
|
-
}
|
|
181
|
+
static isConfigured = isConfigured;
|
|
262
182
|
|
|
263
183
|
/**
|
|
264
|
-
* Whether the CORE (ingest / Identity / Attribution /
|
|
184
|
+
* Whether the CORE (ingest / Identity / Attribution / Attributes) is
|
|
265
185
|
* configured. True after `configure`, `configureCore`, or `Analytics.init`.
|
|
186
|
+
* Native is the source of truth; never throws or rejects.
|
|
266
187
|
*
|
|
267
|
-
*
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
*
|
|
188
|
+
* Alias for `SwissKnife.isCoreConfigured()`.
|
|
189
|
+
*/
|
|
190
|
+
static isCoreConfigured = isCoreConfigured;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Set the native log level. Process-wide — it governs the store engine, the
|
|
194
|
+
* ingest client, Identity, Attribution and the observability modules alike.
|
|
273
195
|
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
196
|
+
* Alias for `SwissKnife.setLogLevel(...)`, which is where a process-wide
|
|
197
|
+
* setting belongs (and where both native SDKs already put it).
|
|
276
198
|
*/
|
|
277
|
-
static
|
|
278
|
-
|
|
279
|
-
|
|
199
|
+
static setLogLevel = setLogLevel;
|
|
200
|
+
|
|
201
|
+
// MARK: - The store
|
|
202
|
+
//
|
|
203
|
+
// These are the methods that genuinely belong on `Purchases`. All of them are
|
|
204
|
+
// gated on the PURCHASE flag.
|
|
280
205
|
|
|
281
206
|
/** Latest CustomerInfo (entitlements + subscriptions), computed server-side. */
|
|
282
207
|
static async getCustomerInfo(): Promise<CustomerInfo> {
|
|
@@ -357,9 +282,20 @@ export class Purchases {
|
|
|
357
282
|
// the stale cache and resolved "logged in" without the device ever being
|
|
358
283
|
// re-identified. Routing through `Identity` means one owner for that state.
|
|
359
284
|
//
|
|
285
|
+
// ## Why these four ARE deprecated, when the aliases above are not
|
|
286
|
+
//
|
|
287
|
+
// The bootstrap and attribute aliases are pure renames: same arguments, same
|
|
288
|
+
// return type, same behaviour, and keeping them costs a migrating app nothing.
|
|
289
|
+
// These four are not. `logOut` returns a `CustomerInfo` that is fabricated in
|
|
290
|
+
// a store-less app, `getAppUserID` collapses a three-field snapshot into one
|
|
291
|
+
// string, and `isAnonymous` forces a second round trip to learn what
|
|
292
|
+
// `Identity.current()` already returned. The RC names describe the RC model,
|
|
293
|
+
// not ours, so `Identity.*` is genuinely the better call and not merely the
|
|
294
|
+
// better-organised one. They still work, and will keep working.
|
|
295
|
+
//
|
|
360
296
|
// ## The guard
|
|
361
297
|
//
|
|
362
|
-
// These sit on
|
|
298
|
+
// These sit on `throwIfCoreNotConfigured`, not on the purchase guard.
|
|
363
299
|
// Identity is a core capability — natively both platforms gate it on
|
|
364
300
|
// `isCoreConfigured` — and the single most likely caller of
|
|
365
301
|
// `Purchases.logIn` is an app migrating off RevenueCat that still lets
|
|
@@ -381,7 +317,7 @@ export class Purchases {
|
|
|
381
317
|
* is NOT merged server-side and the call must be retried. Repeat calls with
|
|
382
318
|
* the id already in effect are free (no bridge hop, no request).
|
|
383
319
|
*
|
|
384
|
-
* Works after `
|
|
320
|
+
* Works after `SwissKnife.configure`, `SwissKnife.configureCore`, or
|
|
385
321
|
* `Analytics.init` — the store does not have to be ours.
|
|
386
322
|
*
|
|
387
323
|
* @deprecated Wave 1 — prefer `Identity.logIn(userId)`. This alias exists so
|
|
@@ -447,44 +383,39 @@ export class Purchases {
|
|
|
447
383
|
return (await Identity.current()).isAnonymous;
|
|
448
384
|
}
|
|
449
385
|
|
|
450
|
-
// Subscriber attributes
|
|
451
|
-
//
|
|
452
|
-
//
|
|
386
|
+
// MARK: - Subscriber attributes (RC-compatibility aliases for `Attributes.*`)
|
|
387
|
+
//
|
|
388
|
+
// CORE, not store: all five are gated on `throwIfCoreNotConfigured`, so they
|
|
389
|
+
// work in an app that only called `configureCore` (another library owns the
|
|
390
|
+
// store) or only `Analytics.init`. That is the tell that they never belonged
|
|
391
|
+
// on this class — an app with no purchases at all still needs them, because
|
|
392
|
+
// they are the only path to the dashboard person card.
|
|
393
|
+
//
|
|
394
|
+
// Assigned, not wrapped: `Purchases.setAttributes === Attributes.set`.
|
|
453
395
|
|
|
454
|
-
/**
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
396
|
+
/**
|
|
397
|
+
* Set arbitrary subscriber attributes. `null` clears an attribute.
|
|
398
|
+
*
|
|
399
|
+
* RC-compatibility alias for `Attributes.set(...)`. Not deprecated.
|
|
400
|
+
*/
|
|
401
|
+
static setAttributes = Attributes.set;
|
|
459
402
|
|
|
460
|
-
/** Reserved attribute: email. */
|
|
461
|
-
static
|
|
462
|
-
await throwIfCoreNotConfigured();
|
|
463
|
-
return getNativeModule().setEmail(email);
|
|
464
|
-
}
|
|
403
|
+
/** Reserved attribute: email. Alias for `Attributes.setEmail(...)`. */
|
|
404
|
+
static setEmail = Attributes.setEmail;
|
|
465
405
|
|
|
466
|
-
/** Reserved attribute: display name. */
|
|
467
|
-
static
|
|
468
|
-
await throwIfCoreNotConfigured();
|
|
469
|
-
return getNativeModule().setDisplayName(displayName);
|
|
470
|
-
}
|
|
406
|
+
/** Reserved attribute: display name. Alias for `Attributes.setDisplayName(...)`. */
|
|
407
|
+
static setDisplayName = Attributes.setDisplayName;
|
|
471
408
|
|
|
472
|
-
/** Reserved attribute: phone number. */
|
|
473
|
-
static
|
|
474
|
-
await throwIfCoreNotConfigured();
|
|
475
|
-
return getNativeModule().setPhoneNumber(phoneNumber);
|
|
476
|
-
}
|
|
409
|
+
/** Reserved attribute: phone number. Alias for `Attributes.setPhoneNumber(...)`. */
|
|
410
|
+
static setPhoneNumber = Attributes.setPhoneNumber;
|
|
477
411
|
|
|
478
|
-
/** Reserved attribute: push token. */
|
|
479
|
-
static
|
|
480
|
-
await throwIfCoreNotConfigured();
|
|
481
|
-
return getNativeModule().setPushToken(pushToken);
|
|
482
|
-
}
|
|
412
|
+
/** Reserved attribute: push token. Alias for `Attributes.setPushToken(...)`. */
|
|
413
|
+
static setPushToken = Attributes.setPushToken;
|
|
483
414
|
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
415
|
+
// MARK: - CustomerInfo listeners
|
|
416
|
+
//
|
|
417
|
+
// Store surface: these fire on purchase, renewal and dashboard grant, so they
|
|
418
|
+
// stay here rather than moving to the root.
|
|
488
419
|
|
|
489
420
|
/** Subscribe to CustomerInfo changes (purchase, renewal, dashboard grant). */
|
|
490
421
|
static addCustomerInfoUpdateListener(
|
|
@@ -517,9 +448,7 @@ export class Purchases {
|
|
|
517
448
|
* short-circuit before reaching the (freshly mocked) bridge.
|
|
518
449
|
*/
|
|
519
450
|
static __resetForTests(): void {
|
|
520
|
-
|
|
521
|
-
coreConfigured = false;
|
|
522
|
-
configuredApiKey = null;
|
|
451
|
+
resetConfigForTests();
|
|
523
452
|
customerInfoSubscriptions.clear();
|
|
524
453
|
Identity.__resetForTests();
|
|
525
454
|
}
|