@app-swiss-knife/react-native-sdk 0.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.
Files changed (218) hide show
  1. package/README.md +182 -0
  2. package/SwissKnifePurchases.podspec +40 -0
  3. package/android/build.gradle +89 -0
  4. package/android/gradle.properties +3 -0
  5. package/android/src/main/AndroidManifest.xml +26 -0
  6. package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingModule.kt +216 -0
  7. package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingPackage.kt +17 -0
  8. package/android/src/main/java/com/swissknife/purchases/Conversions.kt +15 -0
  9. package/android/src/main/java/com/swissknife/purchases/SwissKnifeJson.kt +371 -0
  10. package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt +497 -0
  11. package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesPackage.kt +25 -0
  12. package/android/vendor/GENERATED.md +10 -0
  13. package/android/vendor/java/com/appswissknife/SwissKnife.kt +533 -0
  14. package/android/vendor/java/com/appswissknife/analytics/EventQueueStore.kt +326 -0
  15. package/android/vendor/java/com/appswissknife/analytics/FlushEventsWorker.kt +296 -0
  16. package/android/vendor/java/com/appswissknife/analytics/SKAnalytics.kt +503 -0
  17. package/android/vendor/java/com/appswissknife/attributes/AttributesClient.kt +188 -0
  18. package/android/vendor/java/com/appswissknife/attributes/SetAttributesWorker.kt +204 -0
  19. package/android/vendor/java/com/appswissknife/attribution/Attribution.kt +447 -0
  20. package/android/vendor/java/com/appswissknife/context/AppLifecycle.kt +120 -0
  21. package/android/vendor/java/com/appswissknife/context/Clock.kt +70 -0
  22. package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +138 -0
  23. package/android/vendor/java/com/appswissknife/context/IDs.kt +55 -0
  24. package/android/vendor/java/com/appswissknife/context/Reachability.kt +145 -0
  25. package/android/vendor/java/com/appswissknife/context/Session.kt +192 -0
  26. package/android/vendor/java/com/appswissknife/crashreporting/SKCrashReporting.kt +945 -0
  27. package/android/vendor/java/com/appswissknife/crashreporting/SKScreenshot.kt +201 -0
  28. package/android/vendor/java/com/appswissknife/device/DeviceID.kt +172 -0
  29. package/android/vendor/java/com/appswissknife/diagnostics/SKLog.kt +97 -0
  30. package/android/vendor/java/com/appswissknife/entitlements/EntitlementsRepository.kt +495 -0
  31. package/android/vendor/java/com/appswissknife/identity/Identity.kt +522 -0
  32. package/android/vendor/java/com/appswissknife/models/CustomerInfo.kt +67 -0
  33. package/android/vendor/java/com/appswissknife/models/Enums.kt +35 -0
  34. package/android/vendor/java/com/appswissknife/models/Offerings.kt +80 -0
  35. package/android/vendor/java/com/appswissknife/models/PurchasesError.kt +89 -0
  36. package/android/vendor/java/com/appswissknife/models/StoreTransaction.kt +17 -0
  37. package/android/vendor/java/com/appswissknife/net/BackendHttpClient.kt +507 -0
  38. package/android/vendor/java/com/appswissknife/net/ExponentialBackoff.kt +69 -0
  39. package/android/vendor/java/com/appswissknife/net/IngestClient.kt +582 -0
  40. package/android/vendor/java/com/appswissknife/net/OfflineQueueStore.kt +320 -0
  41. package/android/vendor/java/com/appswissknife/offerings/OfferingsRepository.kt +219 -0
  42. package/android/vendor/java/com/appswissknife/performance/SKFrameTracker.kt +94 -0
  43. package/android/vendor/java/com/appswissknife/performance/SKPerformance.kt +428 -0
  44. package/android/vendor/java/com/appswissknife/purchases/BillingClientGateway.kt +560 -0
  45. package/android/vendor/java/com/appswissknife/purchases/PlayPriceEnrichmentProvider.kt +100 -0
  46. package/android/vendor/java/com/appswissknife/purchases/PriceEnrichmentProvider.kt +36 -0
  47. package/android/vendor/java/com/appswissknife/purchases/PurchasesGateway.kt +120 -0
  48. package/android/vendor/res/xml/swissknife_data_extraction_rules.xml +20 -0
  49. package/ios/SwissKnifeCrashReporting.m +32 -0
  50. package/ios/SwissKnifeCrashReporting.swift +186 -0
  51. package/ios/SwissKnifeJSON.swift +347 -0
  52. package/ios/SwissKnifePurchases.m +124 -0
  53. package/ios/SwissKnifePurchases.swift +523 -0
  54. package/ios/vendor/GENERATED.md +10 -0
  55. package/ios/vendor/SwissKnifeiOS/Analytics/SKAnalytics.swift +488 -0
  56. package/ios/vendor/SwissKnifeiOS/AttributesClient.swift +460 -0
  57. package/ios/vendor/SwissKnifeiOS/Attribution/Attribution.swift +307 -0
  58. package/ios/vendor/SwissKnifeiOS/Attribution/AttributionNetwork.swift +92 -0
  59. package/ios/vendor/SwissKnifeiOS/Attribution/AttributionState.swift +141 -0
  60. package/ios/vendor/SwissKnifeiOS/Context/AppLifecycle.swift +130 -0
  61. package/ios/vendor/SwissKnifeiOS/Context/Clock.swift +100 -0
  62. package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +190 -0
  63. package/ios/vendor/SwissKnifeiOS/Context/IDs.swift +64 -0
  64. package/ios/vendor/SwissKnifeiOS/Context/Reachability.swift +121 -0
  65. package/ios/vendor/SwissKnifeiOS/Context/Session.swift +223 -0
  66. package/ios/vendor/SwissKnifeiOS/CrashReporting/SKCrashReporting.swift +1287 -0
  67. package/ios/vendor/SwissKnifeiOS/CrashReporting/SKScreenshot.swift +175 -0
  68. package/ios/vendor/SwissKnifeiOS/DeviceID.swift +161 -0
  69. package/ios/vendor/SwissKnifeiOS/Diagnostics/SKLog.swift +128 -0
  70. package/ios/vendor/SwissKnifeiOS/Identity/Identity.swift +323 -0
  71. package/ios/vendor/SwissKnifeiOS/Identity/IdentityNetwork.swift +70 -0
  72. package/ios/vendor/SwissKnifeiOS/Net/BackendHttpClient.swift +329 -0
  73. package/ios/vendor/SwissKnifeiOS/Net/ExponentialBackoff.swift +87 -0
  74. package/ios/vendor/SwissKnifeiOS/Net/IngestClient.swift +681 -0
  75. package/ios/vendor/SwissKnifeiOS/Net/OfflineQueueStore.swift +409 -0
  76. package/ios/vendor/SwissKnifeiOS/Performance/SKFrameTracker.swift +89 -0
  77. package/ios/vendor/SwissKnifeiOS/Performance/SKPerformance.swift +440 -0
  78. package/ios/vendor/SwissKnifeiOS/Performance/SKURLSessionDelegate.swift +87 -0
  79. package/ios/vendor/SwissKnifeiOS/Services/EntitlementsService.swift +646 -0
  80. package/ios/vendor/SwissKnifeiOS/Services/OfferingsService.swift +458 -0
  81. package/ios/vendor/SwissKnifeiOS/Services/PurchaseService.swift +374 -0
  82. package/ios/vendor/SwissKnifeiOS/SwissKnife+Analytics.swift +46 -0
  83. package/ios/vendor/SwissKnifeiOS/SwissKnife+DeviceID.swift +30 -0
  84. package/ios/vendor/SwissKnifeiOS/SwissKnife+Session.swift +47 -0
  85. package/ios/vendor/SwissKnifeiOS/SwissKnife.swift +417 -0
  86. package/ios/vendor/SwissKnifeiOS/Types.swift +496 -0
  87. package/lib/commonjs/attribution.js +55 -0
  88. package/lib/commonjs/attribution.js.map +1 -0
  89. package/lib/commonjs/identity.js +126 -0
  90. package/lib/commonjs/identity.js.map +1 -0
  91. package/lib/commonjs/index.js +118 -0
  92. package/lib/commonjs/index.js.map +1 -0
  93. package/lib/commonjs/modules/analytics.js +646 -0
  94. package/lib/commonjs/modules/analytics.js.map +1 -0
  95. package/lib/commonjs/modules/attachments.js +169 -0
  96. package/lib/commonjs/modules/attachments.js.map +1 -0
  97. package/lib/commonjs/modules/crash-reporting.js +1327 -0
  98. package/lib/commonjs/modules/crash-reporting.js.map +1 -0
  99. package/lib/commonjs/modules/ids.js +50 -0
  100. package/lib/commonjs/modules/ids.js.map +1 -0
  101. package/lib/commonjs/modules/log.js +59 -0
  102. package/lib/commonjs/modules/log.js.map +1 -0
  103. package/lib/commonjs/modules/offline-store.js +44 -0
  104. package/lib/commonjs/modules/offline-store.js.map +1 -0
  105. package/lib/commonjs/modules/performance.js +494 -0
  106. package/lib/commonjs/modules/performance.js.map +1 -0
  107. package/lib/commonjs/native/module.js +41 -0
  108. package/lib/commonjs/native/module.js.map +1 -0
  109. package/lib/commonjs/native/spec.js +26 -0
  110. package/lib/commonjs/native/spec.js.map +1 -0
  111. package/lib/commonjs/package.json +1 -0
  112. package/lib/commonjs/purchases.js +194 -0
  113. package/lib/commonjs/purchases.js.map +1 -0
  114. package/lib/commonjs/types.js +236 -0
  115. package/lib/commonjs/types.js.map +1 -0
  116. package/lib/module/attribution.js +51 -0
  117. package/lib/module/attribution.js.map +1 -0
  118. package/lib/module/identity.js +122 -0
  119. package/lib/module/identity.js.map +1 -0
  120. package/lib/module/index.js +36 -0
  121. package/lib/module/index.js.map +1 -0
  122. package/lib/module/modules/analytics.js +642 -0
  123. package/lib/module/modules/analytics.js.map +1 -0
  124. package/lib/module/modules/attachments.js +162 -0
  125. package/lib/module/modules/attachments.js.map +1 -0
  126. package/lib/module/modules/crash-reporting.js +1320 -0
  127. package/lib/module/modules/crash-reporting.js.map +1 -0
  128. package/lib/module/modules/ids.js +44 -0
  129. package/lib/module/modules/ids.js.map +1 -0
  130. package/lib/module/modules/log.js +54 -0
  131. package/lib/module/modules/log.js.map +1 -0
  132. package/lib/module/modules/offline-store.js +39 -0
  133. package/lib/module/modules/offline-store.js.map +1 -0
  134. package/lib/module/modules/performance.js +491 -0
  135. package/lib/module/modules/performance.js.map +1 -0
  136. package/lib/module/native/module.js +34 -0
  137. package/lib/module/native/module.js.map +1 -0
  138. package/lib/module/native/spec.js +22 -0
  139. package/lib/module/native/spec.js.map +1 -0
  140. package/lib/module/package.json +1 -0
  141. package/lib/module/purchases.js +187 -0
  142. package/lib/module/purchases.js.map +1 -0
  143. package/lib/module/types.js +230 -0
  144. package/lib/module/types.js.map +1 -0
  145. package/lib/typescript/commonjs/attribution.d.ts +24 -0
  146. package/lib/typescript/commonjs/attribution.d.ts.map +1 -0
  147. package/lib/typescript/commonjs/identity.d.ts +37 -0
  148. package/lib/typescript/commonjs/identity.d.ts.map +1 -0
  149. package/lib/typescript/commonjs/index.d.ts +15 -0
  150. package/lib/typescript/commonjs/index.d.ts.map +1 -0
  151. package/lib/typescript/commonjs/modules/analytics.d.ts +127 -0
  152. package/lib/typescript/commonjs/modules/analytics.d.ts.map +1 -0
  153. package/lib/typescript/commonjs/modules/attachments.d.ts +50 -0
  154. package/lib/typescript/commonjs/modules/attachments.d.ts.map +1 -0
  155. package/lib/typescript/commonjs/modules/crash-reporting.d.ts +175 -0
  156. package/lib/typescript/commonjs/modules/crash-reporting.d.ts.map +1 -0
  157. package/lib/typescript/commonjs/modules/ids.d.ts +17 -0
  158. package/lib/typescript/commonjs/modules/ids.d.ts.map +1 -0
  159. package/lib/typescript/commonjs/modules/log.d.ts +12 -0
  160. package/lib/typescript/commonjs/modules/log.d.ts.map +1 -0
  161. package/lib/typescript/commonjs/modules/offline-store.d.ts +9 -0
  162. package/lib/typescript/commonjs/modules/offline-store.d.ts.map +1 -0
  163. package/lib/typescript/commonjs/modules/performance.d.ts +75 -0
  164. package/lib/typescript/commonjs/modules/performance.d.ts.map +1 -0
  165. package/lib/typescript/commonjs/native/module.d.ts +6 -0
  166. package/lib/typescript/commonjs/native/module.d.ts.map +1 -0
  167. package/lib/typescript/commonjs/native/spec.d.ts +119 -0
  168. package/lib/typescript/commonjs/native/spec.d.ts.map +1 -0
  169. package/lib/typescript/commonjs/package.json +1 -0
  170. package/lib/typescript/commonjs/purchases.d.ts +68 -0
  171. package/lib/typescript/commonjs/purchases.d.ts.map +1 -0
  172. package/lib/typescript/commonjs/types.d.ts +384 -0
  173. package/lib/typescript/commonjs/types.d.ts.map +1 -0
  174. package/lib/typescript/module/attribution.d.ts +24 -0
  175. package/lib/typescript/module/attribution.d.ts.map +1 -0
  176. package/lib/typescript/module/identity.d.ts +37 -0
  177. package/lib/typescript/module/identity.d.ts.map +1 -0
  178. package/lib/typescript/module/index.d.ts +15 -0
  179. package/lib/typescript/module/index.d.ts.map +1 -0
  180. package/lib/typescript/module/modules/analytics.d.ts +127 -0
  181. package/lib/typescript/module/modules/analytics.d.ts.map +1 -0
  182. package/lib/typescript/module/modules/attachments.d.ts +50 -0
  183. package/lib/typescript/module/modules/attachments.d.ts.map +1 -0
  184. package/lib/typescript/module/modules/crash-reporting.d.ts +175 -0
  185. package/lib/typescript/module/modules/crash-reporting.d.ts.map +1 -0
  186. package/lib/typescript/module/modules/ids.d.ts +17 -0
  187. package/lib/typescript/module/modules/ids.d.ts.map +1 -0
  188. package/lib/typescript/module/modules/log.d.ts +12 -0
  189. package/lib/typescript/module/modules/log.d.ts.map +1 -0
  190. package/lib/typescript/module/modules/offline-store.d.ts +9 -0
  191. package/lib/typescript/module/modules/offline-store.d.ts.map +1 -0
  192. package/lib/typescript/module/modules/performance.d.ts +75 -0
  193. package/lib/typescript/module/modules/performance.d.ts.map +1 -0
  194. package/lib/typescript/module/native/module.d.ts +6 -0
  195. package/lib/typescript/module/native/module.d.ts.map +1 -0
  196. package/lib/typescript/module/native/spec.d.ts +119 -0
  197. package/lib/typescript/module/native/spec.d.ts.map +1 -0
  198. package/lib/typescript/module/package.json +1 -0
  199. package/lib/typescript/module/purchases.d.ts +68 -0
  200. package/lib/typescript/module/purchases.d.ts.map +1 -0
  201. package/lib/typescript/module/types.d.ts +384 -0
  202. package/lib/typescript/module/types.d.ts.map +1 -0
  203. package/package.json +115 -0
  204. package/react-native.config.js +23 -0
  205. package/src/attribution.ts +56 -0
  206. package/src/identity.ts +133 -0
  207. package/src/index.ts +54 -0
  208. package/src/modules/analytics.ts +689 -0
  209. package/src/modules/attachments.ts +222 -0
  210. package/src/modules/crash-reporting.ts +1577 -0
  211. package/src/modules/ids.ts +41 -0
  212. package/src/modules/log.ts +56 -0
  213. package/src/modules/offline-store.ts +51 -0
  214. package/src/modules/performance.ts +589 -0
  215. package/src/native/module.ts +47 -0
  216. package/src/native/spec.ts +183 -0
  217. package/src/purchases.ts +214 -0
  218. package/src/types.ts +470 -0
@@ -0,0 +1,183 @@
1
+ // src/native/spec.ts
2
+ // The contract the native module must implement. All params/returns are
3
+ // JSON-serializable plain objects — the RN bridge marshals them. Phase 7 of
4
+ // the SDK rip-and-replace cut the implementation over from PHC to our own
5
+ // SwissKnifeiOS (StoreKit 2) and swiss-knife-android (Play Billing v8) AARs;
6
+ // this TS spec is what both sides of the bridge now agree on.
7
+ import type { TurboModule } from "react-native";
8
+ import type {
9
+ CustomerInfo,
10
+ PurchasesOfferings,
11
+ PurchasesPackage,
12
+ PurchasesStoreProduct,
13
+ MakePurchaseResult,
14
+ LogInResult,
15
+ IdentitySnapshot,
16
+ ResolvedAttribution,
17
+ } from "../types";
18
+
19
+ // Mirrors RC's PurchasesConfiguration but only the fields the native layer accepts
20
+ // over the bridge. apiKey is required; the rest are optional.
21
+ export interface NativeConfigureOptions {
22
+ apiKey: string;
23
+ appUserID?: string | null;
24
+ // RC's `proxyURL` points the native SDK at our backend; surfaced here so SP-2b wires it.
25
+ proxyURL?: string | null;
26
+ // Diagnostics MUST default off (hardening pass, see spec). Passed through to native.
27
+ diagnosticsEnabled?: boolean;
28
+ storeKitVersion?: string;
29
+ useAmazon?: boolean;
30
+ // iOS App Tracking Transparency. autoPromptATT defaults FALSE natively — the
31
+ // SDK never shows the tracking prompt unless the host opts in. iOS-only.
32
+ autoPromptATT?: boolean;
33
+ sendInstallOnFirstLaunch?: boolean;
34
+ // string value of LOG_LEVEL; native sets its log level on configure.
35
+ logLevel?: string;
36
+ }
37
+
38
+ // The event name RC emits on CustomerInfo changes; SP-2b must emit the same name.
39
+ export const CUSTOMER_INFO_UPDATED_EVENT = "Purchases-CustomerInfoUpdated";
40
+
41
+ // Resolved via `TurboModuleRegistry.get<T>` on the New Architecture, so it must
42
+ // be a `TurboModule` (RN constrains `get<T extends TurboModule>`). `TurboModule`
43
+ // only adds an optional `getConstants?(): {}`, which the native side doesn't use.
44
+ export interface SwissKnifePurchasesNativeModule extends TurboModule {
45
+ configure(options: NativeConfigureOptions): Promise<void>;
46
+ isConfigured(): Promise<boolean>;
47
+
48
+ getCustomerInfo(): Promise<CustomerInfo>;
49
+ getOfferings(): Promise<PurchasesOfferings>;
50
+
51
+ purchasePackage(
52
+ packageIdentifier: string,
53
+ presentedOfferingIdentifier: string
54
+ ): Promise<MakePurchaseResult>;
55
+ purchaseStoreProduct(productIdentifier: string): Promise<MakePurchaseResult>;
56
+
57
+ restorePurchases(): Promise<CustomerInfo>;
58
+
59
+ logIn(appUserID: string): Promise<LogInResult>;
60
+ logOut(): Promise<CustomerInfo>;
61
+ getAppUserID(): Promise<string>;
62
+ isAnonymous(): Promise<boolean>;
63
+
64
+ // Wave 1 — unified Identity surface. Mirrors `SwissKnife.Identity` on
65
+ // iOS and `com.appswissknife.identity.Identity` on Android. JS code
66
+ // should prefer these over the per-leg `logIn` / `logOut` /
67
+ // `getAppUserID` / `isAnonymous` above. The legacy methods stay on
68
+ // the spec for back-compat and route through Identity natively.
69
+
70
+ /** Single source of truth for "who is this device pretending to be?" */
71
+ identityCurrent(): Promise<IdentitySnapshot>;
72
+
73
+ /**
74
+ * Identify this device as a known user. Native posts the
75
+ * (anonymous_id, user_id) pair to /api/sdk/users/identify (the
76
+ * bug-fixed behaviour on Android — pre-Wave-1 Android did NOT POST)
77
+ * and rebinds entitlements + analytics user_id from the same callsite.
78
+ */
79
+ identityLogIn(userId: string): Promise<LogInResult>;
80
+
81
+ /** Revert to anonymous. DeviceID stays stable across logout. */
82
+ identityLogOut(): Promise<void>;
83
+
84
+ /** Rotate in-memory identity state; keep persistent storage. */
85
+ identityReset(): Promise<void>;
86
+
87
+ /** Hard wipe: clear Keychain/EncryptedSharedPreferences DeviceID. */
88
+ identityWipe(): Promise<void>;
89
+
90
+ /**
91
+ * Current session snapshot from the native canonical `Session.shared`
92
+ * primitive — the same source that stamps envelope-level
93
+ * `ContextSnapshot.session_id` on every ingested item. JS uses this
94
+ * for analytics' legacy `session_id` field so it agrees with the
95
+ * envelope. Replaces the JS-side `Date.now()` fallback that diverged
96
+ * from native truth.
97
+ *
98
+ * `startedAt` is unix-millis-epoch; `sessionNumber` is the monotonic
99
+ * counter persisted across launches (1, 2, 3, …).
100
+ */
101
+ sessionCurrent(): Promise<{ id: string; sessionNumber: number; startedAt: number }>;
102
+
103
+ // Wave 2 — Attribution. Native owns the disk-backed cache + the
104
+ // ATT prompt orchestration (iOS) and the AdvertisingId/InstallReferrer
105
+ // collectors (Android). JS is a thin passthrough; see
106
+ // `packages/react-native-sdk/src/attribution.ts`.
107
+
108
+ /** Most recent resolved attribution from the native cache. Null if no install POST has succeeded yet. */
109
+ attributionResolved(): Promise<ResolvedAttribution | null>;
110
+
111
+ /**
112
+ * Auto-prompt ATT (iOS) / auto-collect signals (Android) and POST
113
+ * `/api/sdk/attribution/install`. Idempotent — second call returns
114
+ * the cached resolution.
115
+ */
116
+ attributionRequestAndRegister(): Promise<ResolvedAttribution | null>;
117
+
118
+ /**
119
+ * Manual install registration. Pass the IDFA / advertising_id the
120
+ * host already collected, or null when consent was denied.
121
+ */
122
+ attributionRegisterInstall(
123
+ advertisingId: string | null,
124
+ ): Promise<ResolvedAttribution | null>;
125
+
126
+ setAttributes(attributes: Record<string, string | null>): Promise<void>;
127
+ setEmail(email: string | null): Promise<void>;
128
+ setDisplayName(displayName: string | null): Promise<void>;
129
+ setPhoneNumber(phoneNumber: string | null): Promise<void>;
130
+ setPushToken(pushToken: string | null): Promise<void>;
131
+
132
+ setLogLevel(level: string): Promise<void>;
133
+
134
+ /**
135
+ * Persistent per-device identifier, sourced from the native SDK (iOS Keychain
136
+ * / Android EncryptedSharedPreferences). Survives process restart so analytics
137
+ * `device_id` stays stable across app cold starts. JS MUST NOT generate this —
138
+ * an in-memory UUID would fragment every user's analytics timeline on every
139
+ * launch. See `packages/react-native-sdk/src/modules/analytics.ts`.
140
+ */
141
+ getDeviceId(): Promise<string>;
142
+
143
+ /**
144
+ * Persist a pre-built analytics event onto the native disk-backed queue.
145
+ *
146
+ * Phase 2.3 of the RC rip-and-replace: JS owns the payload shape (event_id,
147
+ * device_id, super properties, timed-event duration, identify merges) and
148
+ * native owns the queue (disk persistence on iOS Caches / Android cacheDir,
149
+ * exponential back-off, dead-letter on permanent failure). Once this
150
+ * promise resolves the event has hit disk and survives process kill.
151
+ *
152
+ * The event MUST already carry an `event_id` (UUID idempotency key) and a
153
+ * `device_id` — the backend dedupes on `(app_project_id, event_id)`.
154
+ */
155
+ /**
156
+ * Wire the observability backend (analytics/crash/perf ingest) WITHOUT the
157
+ * purchase engine, so a non-SDK-store build (e.g. RuStore, where
158
+ * `Purchases.configure` is never called) still sends analytics. Idempotent.
159
+ *
160
+ * OPTIONAL: implemented on Android (the platform that needs it); absent on
161
+ * iOS, where the backend is wired by `Purchases.configure`. Call via optional
162
+ * chaining so the iOS-absent case is a safe no-op.
163
+ */
164
+ configureObservability?(apiKey: string, serverURL: string): Promise<void>;
165
+
166
+ trackEvent(event: Record<string, unknown>): Promise<void>;
167
+
168
+ /**
169
+ * Kick off a flush of the native disk-backed queue. Fire-and-forget — the
170
+ * actual HTTP work runs asynchronously on the native side (iOS GCD queue
171
+ * with back-off; Android WorkManager OneTimeWorkRequest). Resolves as soon
172
+ * as the flush job is scheduled.
173
+ */
174
+ flushEvents(): Promise<void>;
175
+
176
+ // RN NativeEventEmitter book-keeping (no-op on some platforms; required on iOS).
177
+ addListener(eventType: string): void;
178
+ removeListeners(count: number): void;
179
+ }
180
+
181
+ // Re-export the unbound package/product shapes callers pass into purchase methods,
182
+ // so callers don't need to reach into ../types for these specific ones.
183
+ export type { PurchasesPackage, PurchasesStoreProduct };
@@ -0,0 +1,214 @@
1
+ // src/purchases.ts
2
+ import { getNativeModule, getEventEmitter } from "./native/module";
3
+ import { CUSTOMER_INFO_UPDATED_EVENT } from "./native/spec";
4
+ import { UninitializedPurchasesError } from "./types";
5
+ import type { EmitterSubscription } from "react-native";
6
+ import type {
7
+ PurchasesConfiguration,
8
+ LOG_LEVEL,
9
+ CustomerInfo,
10
+ PurchasesOfferings,
11
+ PurchasesPackage,
12
+ PurchasesStoreProduct,
13
+ MakePurchaseResult,
14
+ LogInResult,
15
+ CustomerInfoUpdateListener,
16
+ } from "./types";
17
+ import type { NativeConfigureOptions } from "./native/spec";
18
+
19
+ let configured = false;
20
+ let configuredApiKey: string | null = null;
21
+
22
+ // Maps a caller's listener fn to its native subscription so we can remove it.
23
+ const customerInfoSubscriptions = new Map<
24
+ CustomerInfoUpdateListener,
25
+ EmitterSubscription
26
+ >();
27
+
28
+ function throwIfNotConfigured(): void {
29
+ if (!configured) {
30
+ throw new UninitializedPurchasesError();
31
+ }
32
+ }
33
+
34
+ /**
35
+ * RC-compatible subscriptions SDK entry point. Process-wide singleton; call
36
+ * `Purchases.configure(...)` once at app start, then use the static methods.
37
+ */
38
+ export class Purchases {
39
+ /**
40
+ * Configure the SDK. Must be called before any other method. Calling again
41
+ * with the same API key is a no-op (matches the common "configure once" guard);
42
+ * the native layer owns full reconfiguration.
43
+ */
44
+ static configure(
45
+ options: PurchasesConfiguration & { logLevel?: LOG_LEVEL }
46
+ ): void {
47
+ if (configured && configuredApiKey === options.apiKey) return;
48
+ const native = getNativeModule();
49
+ const nativeOptions: NativeConfigureOptions = {
50
+ apiKey: options.apiKey,
51
+ appUserID: options.appUserID ?? null,
52
+ // proxyURL points the native SDK at the Swiss Knife backend. Without it the
53
+ // native layer falls back to its hardcoded default host (api.swiss-knife.dev),
54
+ // which is unreachable for self-hosted tenants. Forward it so callers can
55
+ // target their own backend (e.g. https://swiss.remneys.com).
56
+ proxyURL: options.proxyURL ?? null,
57
+ // Hardening pass (spec): diagnostics opt-in only — default OFF.
58
+ diagnosticsEnabled: options.diagnosticsEnabled ?? false,
59
+ storeKitVersion: options.storeKitVersion as string | undefined,
60
+ useAmazon: options.useAmazon,
61
+ // iOS ATT — default off (no tracking prompt) unless the host opts in.
62
+ autoPromptATT: options.autoPromptATT,
63
+ sendInstallOnFirstLaunch: options.sendInstallOnFirstLaunch,
64
+ logLevel: options.logLevel,
65
+ };
66
+ // Fire-and-forget: native configure resolves async but RC's configure is sync.
67
+ void native.configure(nativeOptions);
68
+ configured = true;
69
+ configuredApiKey = options.apiKey;
70
+ }
71
+
72
+ /** Whether the SDK has been configured. Delegates to native. */
73
+ static isConfigured(): Promise<boolean> {
74
+ if (!configured) return Promise.resolve(false);
75
+ return getNativeModule().isConfigured();
76
+ }
77
+
78
+ /** Latest CustomerInfo (entitlements + subscriptions), computed server-side. */
79
+ static async getCustomerInfo(): Promise<CustomerInfo> {
80
+ throwIfNotConfigured();
81
+ return getNativeModule().getCustomerInfo();
82
+ }
83
+
84
+ /** Current + all offerings configured for this app. */
85
+ static async getOfferings(): Promise<PurchasesOfferings> {
86
+ throwIfNotConfigured();
87
+ return getNativeModule().getOfferings();
88
+ }
89
+
90
+ /** Purchase a package from an offering. Drives the native store payment sheet. */
91
+ static async purchasePackage(pkg: PurchasesPackage): Promise<MakePurchaseResult> {
92
+ throwIfNotConfigured();
93
+ const offeringId = pkg.presentedOfferingContext?.offeringIdentifier ?? '';
94
+ return getNativeModule().purchasePackage(pkg.identifier, offeringId);
95
+ }
96
+
97
+ /** Purchase a store product directly by its store SKU. */
98
+ static async purchaseStoreProduct(
99
+ product: PurchasesStoreProduct
100
+ ): Promise<MakePurchaseResult> {
101
+ throwIfNotConfigured();
102
+ return getNativeModule().purchaseStoreProduct(product.identifier);
103
+ }
104
+
105
+ /** Restore purchases tied to the underlying store account. */
106
+ static async restorePurchases(): Promise<CustomerInfo> {
107
+ throwIfNotConfigured();
108
+ return getNativeModule().restorePurchases();
109
+ }
110
+
111
+ /**
112
+ * Identify the user. Aliases/merges anonymous purchases server-side.
113
+ *
114
+ * @deprecated Wave 1 — prefer `Identity.logIn(userId)`. Routes through
115
+ * the unified Identity surface natively, so the bug fix (Android now
116
+ * POSTs `/api/sdk/users/identify`) applies whether you call this or
117
+ * `Identity.logIn(...)`.
118
+ */
119
+ static async logIn(appUserID: string): Promise<LogInResult> {
120
+ throwIfNotConfigured();
121
+ return getNativeModule().logIn(appUserID);
122
+ }
123
+
124
+ /**
125
+ * Log out the current user and revert to anonymous.
126
+ *
127
+ * @deprecated Wave 1 — prefer `Identity.logOut()`.
128
+ */
129
+ static async logOut(): Promise<CustomerInfo> {
130
+ throwIfNotConfigured();
131
+ return getNativeModule().logOut();
132
+ }
133
+
134
+ /** The current app user id (anonymous or identified). */
135
+ static async getAppUserID(): Promise<string> {
136
+ throwIfNotConfigured();
137
+ return getNativeModule().getAppUserID();
138
+ }
139
+
140
+ /** Whether the current user id is an SDK-generated anonymous id. */
141
+ static async isAnonymous(): Promise<boolean> {
142
+ throwIfNotConfigured();
143
+ return getNativeModule().isAnonymous();
144
+ }
145
+
146
+ /** Set arbitrary subscriber attributes. null clears an attribute. */
147
+ static async setAttributes(attributes: Record<string, string | null>): Promise<void> {
148
+ throwIfNotConfigured();
149
+ return getNativeModule().setAttributes(attributes);
150
+ }
151
+
152
+ /** Reserved attribute: email. */
153
+ static async setEmail(email: string | null): Promise<void> {
154
+ throwIfNotConfigured();
155
+ return getNativeModule().setEmail(email);
156
+ }
157
+
158
+ /** Reserved attribute: display name. */
159
+ static async setDisplayName(displayName: string | null): Promise<void> {
160
+ throwIfNotConfigured();
161
+ return getNativeModule().setDisplayName(displayName);
162
+ }
163
+
164
+ /** Reserved attribute: phone number. */
165
+ static async setPhoneNumber(phoneNumber: string | null): Promise<void> {
166
+ throwIfNotConfigured();
167
+ return getNativeModule().setPhoneNumber(phoneNumber);
168
+ }
169
+
170
+ /** Reserved attribute: push token. */
171
+ static async setPushToken(pushToken: string | null): Promise<void> {
172
+ throwIfNotConfigured();
173
+ return getNativeModule().setPushToken(pushToken);
174
+ }
175
+
176
+ /** Set the native log level. Accepts a LOG_LEVEL enum value. */
177
+ static async setLogLevel(level: LOG_LEVEL): Promise<void> {
178
+ return getNativeModule().setLogLevel(level as unknown as string);
179
+ }
180
+
181
+ /** Subscribe to CustomerInfo changes (purchase, renewal, dashboard grant). */
182
+ static addCustomerInfoUpdateListener(
183
+ listener: CustomerInfoUpdateListener
184
+ ): void {
185
+ const sub = getEventEmitter().addListener(
186
+ CUSTOMER_INFO_UPDATED_EVENT,
187
+ (event) => listener(event as Parameters<CustomerInfoUpdateListener>[0])
188
+ );
189
+ customerInfoSubscriptions.set(listener, sub);
190
+ }
191
+
192
+ /** Remove a previously-registered CustomerInfo update listener. */
193
+ static removeCustomerInfoUpdateListener(
194
+ listener: CustomerInfoUpdateListener
195
+ ): void {
196
+ const sub = customerInfoSubscriptions.get(listener);
197
+ if (sub) {
198
+ sub.remove();
199
+ customerInfoSubscriptions.delete(listener);
200
+ }
201
+ }
202
+
203
+ /** @internal Test seam: reset singleton state between tests. */
204
+ static __resetForTests(): void {
205
+ configured = false;
206
+ configuredApiKey = null;
207
+ customerInfoSubscriptions.clear();
208
+ }
209
+ }
210
+
211
+ // Internal helper shared with the rest of the class (defined across SP-2a tasks).
212
+ export { throwIfNotConfigured as __throwIfNotConfigured };
213
+
214
+ export default Purchases;