@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.
Files changed (69) hide show
  1. package/README.md +100 -47
  2. package/SwissKnifePurchases.podspec +8 -5
  3. package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt +18 -1
  4. package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +1 -1
  5. package/android/vendor/java/com/appswissknife/net/IngestClient.kt +1 -1
  6. package/android/vendor/java/com/appswissknife/purchases/BillingClientGateway.kt +53 -9
  7. package/ios/SwissKnifeCrashReporting.swift +59 -18
  8. package/ios/SwissKnifePurchases.swift +16 -1
  9. package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +1 -1
  10. package/ios/vendor/SwissKnifeiOS/Context/Reachability.swift +1 -1
  11. package/ios/vendor/SwissKnifeiOS/CrashReporting/SKCrashReporting.swift +24 -0
  12. package/ios/vendor/SwissKnifeiOS/DeviceID.swift +22 -1
  13. package/ios/vendor/SwissKnifeiOS/Identity/Identity.swift +49 -0
  14. package/ios/vendor/SwissKnifeiOS/Net/IngestClient.swift +14 -0
  15. package/lib/commonjs/attributes.js +103 -0
  16. package/lib/commonjs/attributes.js.map +1 -0
  17. package/lib/commonjs/index.js +31 -2
  18. package/lib/commonjs/index.js.map +1 -1
  19. package/lib/commonjs/internal/config.js +237 -0
  20. package/lib/commonjs/internal/config.js.map +1 -0
  21. package/lib/commonjs/purchases.js +324 -218
  22. package/lib/commonjs/purchases.js.map +1 -1
  23. package/lib/commonjs/swiss-knife.js +115 -0
  24. package/lib/commonjs/swiss-knife.js.map +1 -0
  25. package/lib/commonjs/types.js +11 -0
  26. package/lib/commonjs/types.js.map +1 -1
  27. package/lib/module/attributes.js +100 -0
  28. package/lib/module/attributes.js.map +1 -0
  29. package/lib/module/index.js +19 -2
  30. package/lib/module/index.js.map +1 -1
  31. package/lib/module/internal/config.js +225 -0
  32. package/lib/module/internal/config.js.map +1 -0
  33. package/lib/module/purchases.js +309 -212
  34. package/lib/module/purchases.js.map +1 -1
  35. package/lib/module/swiss-knife.js +112 -0
  36. package/lib/module/swiss-knife.js.map +1 -0
  37. package/lib/module/types.js +11 -0
  38. package/lib/module/types.js.map +1 -1
  39. package/lib/typescript/commonjs/attributes.d.ts +41 -0
  40. package/lib/typescript/commonjs/attributes.d.ts.map +1 -0
  41. package/lib/typescript/commonjs/index.d.ts +2 -0
  42. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  43. package/lib/typescript/commonjs/internal/config.d.ts +121 -0
  44. package/lib/typescript/commonjs/internal/config.d.ts.map +1 -0
  45. package/lib/typescript/commonjs/purchases.d.ts +103 -95
  46. package/lib/typescript/commonjs/purchases.d.ts.map +1 -1
  47. package/lib/typescript/commonjs/swiss-knife.d.ts +56 -0
  48. package/lib/typescript/commonjs/swiss-knife.d.ts.map +1 -0
  49. package/lib/typescript/commonjs/types.d.ts +13 -0
  50. package/lib/typescript/commonjs/types.d.ts.map +1 -1
  51. package/lib/typescript/module/attributes.d.ts +41 -0
  52. package/lib/typescript/module/attributes.d.ts.map +1 -0
  53. package/lib/typescript/module/index.d.ts +2 -0
  54. package/lib/typescript/module/index.d.ts.map +1 -1
  55. package/lib/typescript/module/internal/config.d.ts +121 -0
  56. package/lib/typescript/module/internal/config.d.ts.map +1 -0
  57. package/lib/typescript/module/purchases.d.ts +103 -95
  58. package/lib/typescript/module/purchases.d.ts.map +1 -1
  59. package/lib/typescript/module/swiss-knife.d.ts +56 -0
  60. package/lib/typescript/module/swiss-knife.d.ts.map +1 -0
  61. package/lib/typescript/module/types.d.ts +13 -0
  62. package/lib/typescript/module/types.d.ts.map +1 -1
  63. package/package.json +1 -1
  64. package/src/attributes.ts +97 -0
  65. package/src/index.ts +19 -2
  66. package/src/internal/config.ts +233 -0
  67. package/src/purchases.ts +320 -228
  68. package/src/swiss-knife.ts +134 -0
  69. 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
+ }