@app-swiss-knife/react-native-sdk 0.2.5 → 0.3.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 +100 -47
- package/SwissKnifePurchases.podspec +8 -5
- package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt +18 -1
- package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +1 -1
- package/android/vendor/java/com/appswissknife/net/IngestClient.kt +1 -1
- package/android/vendor/java/com/appswissknife/purchases/BillingClientGateway.kt +53 -9
- package/ios/SwissKnifeCrashReporting.swift +59 -18
- package/ios/SwissKnifePurchases.swift +16 -1
- 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 +24 -0
- package/ios/vendor/SwissKnifeiOS/DeviceID.swift +22 -1
- package/ios/vendor/SwissKnifeiOS/Identity/Identity.swift +49 -0
- 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 +324 -218
- 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 +309 -212
- 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 +103 -95
- 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 +103 -95
- 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 +320 -228
- package/src/swiss-knife.ts +134 -0
- package/src/types.ts +14 -0
package/src/purchases.ts
CHANGED
|
@@ -1,11 +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";
|
|
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";
|
|
2
60
|
import { getNativeModule, getEventEmitter } from "./native/module";
|
|
3
61
|
import { CUSTOMER_INFO_UPDATED_EVENT } from "./native/spec";
|
|
4
|
-
import {
|
|
62
|
+
import { VERIFICATION_RESULT } from "./types";
|
|
5
63
|
import type { EmitterSubscription } from "react-native";
|
|
6
64
|
import type {
|
|
7
|
-
PurchasesConfiguration,
|
|
8
|
-
LOG_LEVEL,
|
|
9
65
|
CustomerInfo,
|
|
10
66
|
PurchasesOfferings,
|
|
11
67
|
PurchasesPackage,
|
|
@@ -14,41 +70,6 @@ import type {
|
|
|
14
70
|
LogInResult,
|
|
15
71
|
CustomerInfoUpdateListener,
|
|
16
72
|
} from "./types";
|
|
17
|
-
import type {
|
|
18
|
-
NativeConfigureOptions,
|
|
19
|
-
SwissKnifePurchasesNativeModule,
|
|
20
|
-
} from "./native/spec";
|
|
21
|
-
|
|
22
|
-
let configured = false;
|
|
23
|
-
let configuredApiKey: string | null = null;
|
|
24
|
-
let coreConfigured = false;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Read a boolean off the native module without ever letting a missing native
|
|
28
|
-
* module escape as an exception.
|
|
29
|
-
*
|
|
30
|
-
* `getNativeModule()` THROWS when the module is not linked (Expo Go, a bare RN
|
|
31
|
-
* app that skipped `pod install` / a Gradle sync, web) — precisely the state a
|
|
32
|
-
* developer is in while wiring the SDK up, and precisely when they are most
|
|
33
|
-
* likely to be calling one of these accessors to find out what is wrong. A
|
|
34
|
-
* boolean accessor rejecting with "native module is not linked" is a worse
|
|
35
|
-
* answer than `false`: an unlinked module cannot have configured anything, so
|
|
36
|
-
* `false` is not merely safe, it is correct.
|
|
37
|
-
*
|
|
38
|
-
* The `.catch` covers the adjacent case where the module IS linked but predates
|
|
39
|
-
* the method (JS upgraded without rebuilding the app) — same reasoning, same
|
|
40
|
-
* answer. Mirrors the tolerance `Analytics.init` already applies to
|
|
41
|
-
* `configureObservability`.
|
|
42
|
-
*/
|
|
43
|
-
function nativeBoolean(
|
|
44
|
-
read: (native: SwissKnifePurchasesNativeModule) => Promise<boolean>
|
|
45
|
-
): Promise<boolean> {
|
|
46
|
-
try {
|
|
47
|
-
return Promise.resolve(read(getNativeModule())).catch(() => false);
|
|
48
|
-
} catch {
|
|
49
|
-
return Promise.resolve(false);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
73
|
|
|
53
74
|
// Maps a caller's listener fn to its native subscription so we can remove it.
|
|
54
75
|
const customerInfoSubscriptions = new Map<
|
|
@@ -57,160 +78,130 @@ const customerInfoSubscriptions = new Map<
|
|
|
57
78
|
>();
|
|
58
79
|
|
|
59
80
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
81
|
+
* The `CustomerInfo` RC's `logOut()` resolves with, for a device that just
|
|
82
|
+
* became anonymous.
|
|
83
|
+
*
|
|
84
|
+
* The store engine is the preferred source, so an app that owns the store
|
|
85
|
+
* keeps getting real post-logout state. Whether it can answer is decided by
|
|
86
|
+
* `isConfigured()` — native truth on both platforms — rather than by trying
|
|
87
|
+
* the read and interpreting the failure, because an *unconfigured* store does
|
|
88
|
+
* not fail the same way on the two platforms: iOS rejects `getCustomerInfo`
|
|
89
|
+
* with `notConfigured`, Android's unconfigured gateway resolves an empty
|
|
90
|
+
* record of its own construction. Discovering the state from the error would
|
|
91
|
+
* hand back two different objects for one situation, in the very module that
|
|
92
|
+
* exists to stop that happening.
|
|
93
|
+
*
|
|
94
|
+
* With no store engine — or with one that cannot answer right now (cold cache
|
|
95
|
+
* plus offline) — the caller gets the entitlement-free record below, built in
|
|
96
|
+
* JS so it is identical on both platforms. Logging out is not a store
|
|
97
|
+
* operation and must not fail because the store is unreachable.
|
|
63
98
|
*/
|
|
64
|
-
function
|
|
65
|
-
if (
|
|
66
|
-
|
|
99
|
+
async function customerInfoAfterLogOut(): Promise<CustomerInfo> {
|
|
100
|
+
if (await nativeBoolean((native) => native.isConfigured())) {
|
|
101
|
+
try {
|
|
102
|
+
return await getNativeModule().getCustomerInfo();
|
|
103
|
+
} catch {
|
|
104
|
+
// Fall through to the empty record.
|
|
105
|
+
}
|
|
67
106
|
}
|
|
107
|
+
const snapshot = await Identity.current();
|
|
108
|
+
return emptyCustomerInfo(snapshot.userId ?? snapshot.anonymousId);
|
|
68
109
|
}
|
|
69
110
|
|
|
70
|
-
/**
|
|
71
|
-
*
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (await nativeBoolean((native) => native.isCoreConfigured())) return;
|
|
98
|
-
throw new UninitializedPurchasesError(
|
|
99
|
-
"Purchases core is not configured. Call Purchases.configure(), " +
|
|
100
|
-
"Purchases.configureCore(), or Analytics.init() first."
|
|
101
|
-
);
|
|
111
|
+
/** An entitlement-free `CustomerInfo` for `subjectId`. Mirrors the native
|
|
112
|
+
* `CustomerInfo.empty(originalAppUserId:)` constructors. */
|
|
113
|
+
function emptyCustomerInfo(subjectId: string): CustomerInfo {
|
|
114
|
+
const now = new Date();
|
|
115
|
+
return {
|
|
116
|
+
entitlements: {
|
|
117
|
+
all: {},
|
|
118
|
+
active: {},
|
|
119
|
+
verification: VERIFICATION_RESULT.NOT_REQUESTED,
|
|
120
|
+
},
|
|
121
|
+
activeSubscriptions: [],
|
|
122
|
+
allPurchasedProductIdentifiers: [],
|
|
123
|
+
allPurchaseDates: {},
|
|
124
|
+
subscriptionsByProductIdentifier: {},
|
|
125
|
+
nonSubscriptionTransactions: [],
|
|
126
|
+
firstSeen: now.toISOString(),
|
|
127
|
+
firstSeenMillis: now.getTime(),
|
|
128
|
+
originalAppUserId: subjectId,
|
|
129
|
+
requestDate: now.toISOString(),
|
|
130
|
+
requestDateMillis: now.getTime(),
|
|
131
|
+
latestExpirationDate: null,
|
|
132
|
+
latestExpirationDateMillis: null,
|
|
133
|
+
originalPurchaseDate: null,
|
|
134
|
+
originalPurchaseDateMillis: null,
|
|
135
|
+
originalApplicationVersion: null,
|
|
136
|
+
managementURL: null,
|
|
137
|
+
};
|
|
102
138
|
}
|
|
103
139
|
|
|
104
140
|
/**
|
|
105
|
-
* RC-compatible subscriptions SDK entry point. Process-wide singleton
|
|
106
|
-
*
|
|
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.
|
|
107
147
|
*/
|
|
108
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
|
+
|
|
109
155
|
/**
|
|
110
156
|
* Configure the SDK. Must be called before any other method. Calling again
|
|
111
|
-
* with the same API key is a no-op
|
|
112
|
-
*
|
|
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.
|
|
113
161
|
*/
|
|
114
|
-
static configure
|
|
115
|
-
options: PurchasesConfiguration & { logLevel?: LOG_LEVEL }
|
|
116
|
-
): void {
|
|
117
|
-
if (configured && configuredApiKey === options.apiKey) return;
|
|
118
|
-
const native = getNativeModule();
|
|
119
|
-
const nativeOptions: NativeConfigureOptions = {
|
|
120
|
-
apiKey: options.apiKey,
|
|
121
|
-
appUserID: options.appUserID ?? null,
|
|
122
|
-
// proxyURL points the native SDK at the Swiss Knife backend. Without it the
|
|
123
|
-
// native layer falls back to its hardcoded default host (api.swiss-knife.dev),
|
|
124
|
-
// which is unreachable for self-hosted tenants. Forward it so callers can
|
|
125
|
-
// target their own backend (e.g. https://swiss.remneys.com).
|
|
126
|
-
proxyURL: options.proxyURL ?? null,
|
|
127
|
-
// Hardening pass (spec): diagnostics opt-in only — default OFF.
|
|
128
|
-
diagnosticsEnabled: options.diagnosticsEnabled ?? false,
|
|
129
|
-
storeKitVersion: options.storeKitVersion as string | undefined,
|
|
130
|
-
useAmazon: options.useAmazon,
|
|
131
|
-
// iOS ATT — default off (no tracking prompt) unless the host opts in.
|
|
132
|
-
autoPromptATT: options.autoPromptATT,
|
|
133
|
-
sendInstallOnFirstLaunch: options.sendInstallOnFirstLaunch,
|
|
134
|
-
logLevel: options.logLevel,
|
|
135
|
-
};
|
|
136
|
-
// Fire-and-forget: native configure resolves async but RC's configure is sync.
|
|
137
|
-
void native.configure(nativeOptions);
|
|
138
|
-
configured = true;
|
|
139
|
-
coreConfigured = true;
|
|
140
|
-
configuredApiKey = options.apiKey;
|
|
141
|
-
}
|
|
162
|
+
static configure = configure;
|
|
142
163
|
|
|
143
164
|
/**
|
|
144
165
|
* Configure everything EXCEPT the store: ingest, `Identity`, `Attribution`,
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* Use this when another library already owns in-app purchases. `configure`
|
|
148
|
-
* starts a `StoreKit.Transaction.updates` observer on iOS (whose handler
|
|
149
|
-
* calls `transaction.finish()`) and builds a `BillingClient` on Android —
|
|
150
|
-
* both of which contend with an incumbent IAP SDK. `configureCore` creates
|
|
151
|
-
* neither, and registers no install as a side effect.
|
|
166
|
+
* `Attributes`. Use it when another library already owns in-app purchases.
|
|
152
167
|
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
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.
|
|
156
172
|
*/
|
|
157
|
-
static configureCore
|
|
158
|
-
options: PurchasesConfiguration & { logLevel?: LOG_LEVEL }
|
|
159
|
-
): void {
|
|
160
|
-
if (coreConfigured && configuredApiKey === options.apiKey) return;
|
|
161
|
-
const native = getNativeModule();
|
|
162
|
-
const nativeOptions: NativeConfigureOptions = {
|
|
163
|
-
apiKey: options.apiKey,
|
|
164
|
-
appUserID: options.appUserID ?? null,
|
|
165
|
-
proxyURL: options.proxyURL ?? null,
|
|
166
|
-
diagnosticsEnabled: options.diagnosticsEnabled ?? false,
|
|
167
|
-
autoPromptATT: options.autoPromptATT,
|
|
168
|
-
// Forwarded but inert on this path — core never auto-registers an
|
|
169
|
-
// install. Kept on the payload so the same options object can be handed
|
|
170
|
-
// to `configure` later without surprises.
|
|
171
|
-
sendInstallOnFirstLaunch: options.sendInstallOnFirstLaunch,
|
|
172
|
-
logLevel: options.logLevel,
|
|
173
|
-
};
|
|
174
|
-
void native.configureCore(nativeOptions);
|
|
175
|
-
coreConfigured = true;
|
|
176
|
-
configuredApiKey = options.apiKey;
|
|
177
|
-
}
|
|
173
|
+
static configureCore = configureCore;
|
|
178
174
|
|
|
179
175
|
/**
|
|
180
176
|
* Whether the PURCHASE engine has been configured — i.e. whether the store
|
|
181
|
-
* APIs
|
|
177
|
+
* APIs below can work. Native is the source of truth; never throws or rejects.
|
|
182
178
|
*
|
|
183
|
-
*
|
|
184
|
-
* "configure was called" flag: a brownfield app can configure the SDK from
|
|
185
|
-
* native code, and `Purchases.configure` itself is fire-and-forget, so the
|
|
186
|
-
* JS flag is a request, not a fact.
|
|
187
|
-
*
|
|
188
|
-
* Never throws and never rejects. Resolves `false` when the native module is
|
|
189
|
-
* not linked — see {@link nativeBoolean}. Exactly the same failure semantics
|
|
190
|
-
* as {@link isCoreConfigured}; the two differ only in which native flag they
|
|
191
|
-
* report.
|
|
179
|
+
* RC-compatibility alias for `SwissKnife.isConfigured()`.
|
|
192
180
|
*/
|
|
193
|
-
static isConfigured
|
|
194
|
-
return nativeBoolean((native) => native.isConfigured());
|
|
195
|
-
}
|
|
181
|
+
static isConfigured = isConfigured;
|
|
196
182
|
|
|
197
183
|
/**
|
|
198
|
-
* Whether the CORE (ingest / Identity / Attribution /
|
|
184
|
+
* Whether the CORE (ingest / Identity / Attribution / Attributes) is
|
|
199
185
|
* configured. True after `configure`, `configureCore`, or `Analytics.init`.
|
|
186
|
+
* Native is the source of truth; never throws or rejects.
|
|
200
187
|
*
|
|
201
|
-
*
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
*
|
|
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.
|
|
207
195
|
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
196
|
+
* Alias for `SwissKnife.setLogLevel(...)`, which is where a process-wide
|
|
197
|
+
* setting belongs (and where both native SDKs already put it).
|
|
210
198
|
*/
|
|
211
|
-
static
|
|
212
|
-
|
|
213
|
-
|
|
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.
|
|
214
205
|
|
|
215
206
|
/** Latest CustomerInfo (entitlements + subscriptions), computed server-side. */
|
|
216
207
|
static async getCustomerInfo(): Promise<CustomerInfo> {
|
|
@@ -245,91 +236,186 @@ export class Purchases {
|
|
|
245
236
|
return getNativeModule().restorePurchases();
|
|
246
237
|
}
|
|
247
238
|
|
|
248
|
-
//
|
|
249
|
-
//
|
|
250
|
-
//
|
|
251
|
-
//
|
|
252
|
-
//
|
|
253
|
-
//
|
|
254
|
-
//
|
|
255
|
-
//
|
|
256
|
-
//
|
|
257
|
-
//
|
|
258
|
-
// Identity on
|
|
239
|
+
// MARK: - RC-parity identity aliases
|
|
240
|
+
//
|
|
241
|
+
// ## The guarantee
|
|
242
|
+
//
|
|
243
|
+
// Each of the four aliases below is *exactly* its {@link Identity}
|
|
244
|
+
// counterpart with the RevenueCat name and the RevenueCat return type, on
|
|
245
|
+
// BOTH platforms. There is no per-platform behaviour left in them: they are
|
|
246
|
+
// one-line delegations to `Identity`, which reaches the `identityLogIn` /
|
|
247
|
+
// `identityLogOut` / `identityCurrent` native bridge methods — and those go
|
|
248
|
+
// to the unified native `Identity` (`SwissKnife.Identity` on iOS,
|
|
249
|
+
// `com.appswissknife.identity.Identity` on Android) on both. Concretely, a
|
|
250
|
+
// `Purchases.logIn(...)` POSTs `/api/sdk/users/identify` on iOS AND on
|
|
251
|
+
// Android. Pinned by `identity-parity.test.ts`.
|
|
252
|
+
//
|
|
253
|
+
// ## Why they are written as delegations rather than bridge calls
|
|
254
|
+
//
|
|
255
|
+
// They used to call the legacy `logIn` / `logOut` / `getAppUserID` /
|
|
256
|
+
// `isAnonymous` bridge methods, which bottom out in the *deprecated native
|
|
257
|
+
// facades* — and those facades do NOT agree across platforms:
|
|
258
|
+
//
|
|
259
|
+
// - `logOut`: iOS `SwissKnife.logOut` routes to `Identity.logOut`;
|
|
260
|
+
// Android's routes straight to the purchases `gateway` and only
|
|
261
|
+
// back-syncs Identity's snapshot afterwards.
|
|
262
|
+
// - `getAppUserID`: iOS returns the Identity-synced subject (a stable
|
|
263
|
+
// `$SKAnonymousID:`+DeviceID value when anonymous); Android returns
|
|
264
|
+
// `gateway.getAppUserID()`, whose anonymous form is a fresh random UUID
|
|
265
|
+
// minted per process and unrelated to `Identity.current().anonymousId`.
|
|
266
|
+
// - `isAnonymous`: iOS reads Identity-synced state; Android reads the
|
|
267
|
+
// gateway's, which `Identity.reset()` / `Identity.wipe()` never touch.
|
|
268
|
+
//
|
|
269
|
+
// Delegating in JS makes the RN surface immune to all of that, and it is the
|
|
270
|
+
// only version of the fix that is verifiable from this repo's test suites.
|
|
271
|
+
// The legacy bridge methods stay on the native-module spec (removing them
|
|
272
|
+
// would break the native contract) but nothing in this package calls them
|
|
273
|
+
// any more.
|
|
274
|
+
//
|
|
275
|
+
// ## Second bug this closes
|
|
276
|
+
//
|
|
277
|
+
// `Identity` keeps JS-side caches (`lastSnapshot`, `lastLogIn`) that let a
|
|
278
|
+
// repeat `Identity.logIn(sameId)` skip the bridge. An alias that mutated
|
|
279
|
+
// native identity behind `Identity`'s back left those caches describing a
|
|
280
|
+
// world that no longer existed — after `Identity.logIn("u")` +
|
|
281
|
+
// `Purchases.logOut()`, a following `Identity.logIn("u")` short-circuited on
|
|
282
|
+
// the stale cache and resolved "logged in" without the device ever being
|
|
283
|
+
// re-identified. Routing through `Identity` means one owner for that state.
|
|
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
|
+
//
|
|
296
|
+
// ## The guard
|
|
297
|
+
//
|
|
298
|
+
// These sit on `throwIfCoreNotConfigured`, not on the purchase guard.
|
|
299
|
+
// Identity is a core capability — natively both platforms gate it on
|
|
300
|
+
// `isCoreConfigured` — and the single most likely caller of
|
|
301
|
+
// `Purchases.logIn` is an app migrating off RevenueCat that still lets
|
|
302
|
+
// RevenueCat own the store, i.e. one that called `configureCore`. Gating the
|
|
303
|
+
// RC-named alias on the purchase engine would reject exactly that app while
|
|
304
|
+
// the identical `Identity.logIn` call succeeded. The guard is not dropped
|
|
305
|
+
// entirely, because `Identity.current()` before any configure returns a
|
|
306
|
+
// real snapshot on iOS and a `{anonymousId: ""}` placeholder on Android —
|
|
307
|
+
// requiring core is what makes even that case agree.
|
|
259
308
|
|
|
260
309
|
/**
|
|
261
|
-
* Identify the user
|
|
310
|
+
* Identify the user, merging the anonymous subject into `appUserID`
|
|
311
|
+
* server-side.
|
|
312
|
+
*
|
|
313
|
+
* Exactly `Identity.logIn(appUserID)` with the RevenueCat name. On both
|
|
314
|
+
* platforms this POSTs `(anonymous_id, user_id)` to
|
|
315
|
+
* `/api/sdk/users/identify` and resolves once the backend ack and the
|
|
316
|
+
* post-identify entitlements refresh complete; a rejection means the device
|
|
317
|
+
* is NOT merged server-side and the call must be retried. Repeat calls with
|
|
318
|
+
* the id already in effect are free (no bridge hop, no request).
|
|
262
319
|
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
* `Identity.logIn(
|
|
320
|
+
* Works after `SwissKnife.configure`, `SwissKnife.configureCore`, or
|
|
321
|
+
* `Analytics.init` — the store does not have to be ours.
|
|
322
|
+
*
|
|
323
|
+
* @deprecated Wave 1 — prefer `Identity.logIn(userId)`. This alias exists so
|
|
324
|
+
* RevenueCat migrations compile unchanged; it is a pure delegation, so there
|
|
325
|
+
* is nothing to gain by keeping it and one less name to learn by dropping it.
|
|
267
326
|
*/
|
|
268
327
|
static async logIn(appUserID: string): Promise<LogInResult> {
|
|
269
|
-
|
|
270
|
-
return
|
|
328
|
+
await throwIfCoreNotConfigured();
|
|
329
|
+
return Identity.logIn(appUserID);
|
|
271
330
|
}
|
|
272
331
|
|
|
273
332
|
/**
|
|
274
|
-
* Log out the current user and revert to anonymous.
|
|
333
|
+
* Log out the current user and revert to anonymous. The device id survives,
|
|
334
|
+
* so analytics continuity for the same physical install is preserved.
|
|
275
335
|
*
|
|
276
|
-
*
|
|
336
|
+
* Exactly `Identity.logOut()` with the RevenueCat name, plus RC's
|
|
337
|
+
* `CustomerInfo` return: after the logout lands, the post-logout customer
|
|
338
|
+
* info is read back from the store engine. **When no store engine is
|
|
339
|
+
* configured** (`configureCore`, `Analytics.init`) there is nothing to read,
|
|
340
|
+
* and you get an entitlement-free `CustomerInfo` for the anonymous subject —
|
|
341
|
+
* the same record, built in JS, on both platforms. Do not read that as a
|
|
342
|
+
* store error; if you want real entitlements, call `getCustomerInfo()`.
|
|
343
|
+
*
|
|
344
|
+
* @deprecated Wave 1 — prefer `Identity.logOut()`, which does not pretend to
|
|
345
|
+
* return store state.
|
|
277
346
|
*/
|
|
278
347
|
static async logOut(): Promise<CustomerInfo> {
|
|
279
|
-
|
|
280
|
-
|
|
348
|
+
await throwIfCoreNotConfigured();
|
|
349
|
+
await Identity.logOut();
|
|
350
|
+
return customerInfoAfterLogOut();
|
|
281
351
|
}
|
|
282
352
|
|
|
283
|
-
/**
|
|
353
|
+
/**
|
|
354
|
+
* The current subject id — the identified user id if there is one, else the
|
|
355
|
+
* SDK's anonymous id (`$SKAnonymousID:`-prefixed, stable across launches).
|
|
356
|
+
*
|
|
357
|
+
* Exactly `(await Identity.current()).userId ?? .anonymousId`. Note this is
|
|
358
|
+
* the *Identity* subject on both platforms; on Android the purchase
|
|
359
|
+
* gateway's own anonymous id is a different, per-process value, and this
|
|
360
|
+
* alias deliberately does not report it.
|
|
361
|
+
*
|
|
362
|
+
* @deprecated Wave 1 — prefer `Identity.current()`, which hands back the
|
|
363
|
+
* anonymous id, the user id and `isAnonymous` in one read instead of forcing
|
|
364
|
+
* two calls that can interleave.
|
|
365
|
+
*/
|
|
284
366
|
static async getAppUserID(): Promise<string> {
|
|
285
|
-
|
|
286
|
-
|
|
367
|
+
await throwIfCoreNotConfigured();
|
|
368
|
+
const snapshot = await Identity.current();
|
|
369
|
+
return snapshot.userId ?? snapshot.anonymousId;
|
|
287
370
|
}
|
|
288
371
|
|
|
289
|
-
/**
|
|
372
|
+
/**
|
|
373
|
+
* Whether the current subject is the SDK-generated anonymous id.
|
|
374
|
+
*
|
|
375
|
+
* Exactly `(await Identity.current()).isAnonymous`, so it tracks
|
|
376
|
+
* `Identity.reset()` and `Identity.wipe()` on both platforms.
|
|
377
|
+
*
|
|
378
|
+
* @deprecated Wave 1 — prefer `Identity.current()`; see
|
|
379
|
+
* {@link getAppUserID}.
|
|
380
|
+
*/
|
|
290
381
|
static async isAnonymous(): Promise<boolean> {
|
|
291
|
-
|
|
292
|
-
return
|
|
382
|
+
await throwIfCoreNotConfigured();
|
|
383
|
+
return (await Identity.current()).isAnonymous;
|
|
293
384
|
}
|
|
294
385
|
|
|
295
|
-
// Subscriber attributes
|
|
296
|
-
//
|
|
297
|
-
//
|
|
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`.
|
|
298
395
|
|
|
299
|
-
/**
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
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;
|
|
304
402
|
|
|
305
|
-
/** Reserved attribute: email. */
|
|
306
|
-
static
|
|
307
|
-
await throwIfCoreNotConfigured();
|
|
308
|
-
return getNativeModule().setEmail(email);
|
|
309
|
-
}
|
|
403
|
+
/** Reserved attribute: email. Alias for `Attributes.setEmail(...)`. */
|
|
404
|
+
static setEmail = Attributes.setEmail;
|
|
310
405
|
|
|
311
|
-
/** Reserved attribute: display name. */
|
|
312
|
-
static
|
|
313
|
-
await throwIfCoreNotConfigured();
|
|
314
|
-
return getNativeModule().setDisplayName(displayName);
|
|
315
|
-
}
|
|
406
|
+
/** Reserved attribute: display name. Alias for `Attributes.setDisplayName(...)`. */
|
|
407
|
+
static setDisplayName = Attributes.setDisplayName;
|
|
316
408
|
|
|
317
|
-
/** Reserved attribute: phone number. */
|
|
318
|
-
static
|
|
319
|
-
await throwIfCoreNotConfigured();
|
|
320
|
-
return getNativeModule().setPhoneNumber(phoneNumber);
|
|
321
|
-
}
|
|
409
|
+
/** Reserved attribute: phone number. Alias for `Attributes.setPhoneNumber(...)`. */
|
|
410
|
+
static setPhoneNumber = Attributes.setPhoneNumber;
|
|
322
411
|
|
|
323
|
-
/** Reserved attribute: push token. */
|
|
324
|
-
static
|
|
325
|
-
await throwIfCoreNotConfigured();
|
|
326
|
-
return getNativeModule().setPushToken(pushToken);
|
|
327
|
-
}
|
|
412
|
+
/** Reserved attribute: push token. Alias for `Attributes.setPushToken(...)`. */
|
|
413
|
+
static setPushToken = Attributes.setPushToken;
|
|
328
414
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
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.
|
|
333
419
|
|
|
334
420
|
/** Subscribe to CustomerInfo changes (purchase, renewal, dashboard grant). */
|
|
335
421
|
static addCustomerInfoUpdateListener(
|
|
@@ -353,12 +439,18 @@ export class Purchases {
|
|
|
353
439
|
}
|
|
354
440
|
}
|
|
355
441
|
|
|
356
|
-
/**
|
|
442
|
+
/**
|
|
443
|
+
* @internal Test seam: reset singleton state between tests.
|
|
444
|
+
*
|
|
445
|
+
* Also resets `Identity`'s caches, because the RC-parity aliases delegate
|
|
446
|
+
* there: without it, a `Purchases.logIn("u")` in one test would leave
|
|
447
|
+
* `Identity`'s `lastLogIn` primed and the next test's identical call would
|
|
448
|
+
* short-circuit before reaching the (freshly mocked) bridge.
|
|
449
|
+
*/
|
|
357
450
|
static __resetForTests(): void {
|
|
358
|
-
|
|
359
|
-
coreConfigured = false;
|
|
360
|
-
configuredApiKey = null;
|
|
451
|
+
resetConfigForTests();
|
|
361
452
|
customerInfoSubscriptions.clear();
|
|
453
|
+
Identity.__resetForTests();
|
|
362
454
|
}
|
|
363
455
|
}
|
|
364
456
|
|