@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,523 @@
1
+ // packages/react-native-sdk/ios/SwissKnifePurchases.swift
2
+ //
3
+ // Phase 7 of the RC rip-and-replace: the RN bridge now talks ONLY to our own
4
+ // `SwissKnife` iOS SDK (StoreKit 2 + our backend). PurchasesHybridCommon /
5
+ // RevenueCat are gone — every bridge method invokes `SwissKnife.<method>` and
6
+ // JSON-encodes the resulting Swift value via `SwissKnifeJSON.encode(...)`
7
+ // into the dictionary shape that `packages/react-native-sdk/src/types.ts`
8
+ // declares.
9
+
10
+ import Foundation
11
+ import React
12
+ // When built as the `SwissKnifePurchases` pod the SDK's Swift sources are
13
+ // vendored into THIS module (ios/vendor/SwissKnifeiOS/**), so there is no
14
+ // separate `SwissKnifeiOS` module to import — the types are already in scope.
15
+ // The import survives for native/SPM consumers that link the SDK as a module.
16
+ #if canImport(SwissKnifeiOS)
17
+ import SwissKnifeiOS
18
+ #endif
19
+
20
+ // Native module backing src/native/spec.ts. Registered (via the .m bridge) under
21
+ // the JS name "SwissKnifePurchases" — must match src/native/module.ts MODULE_NAME.
22
+ @objc(SwissKnifePurchases)
23
+ class SwissKnifePurchases: RCTEventEmitter {
24
+
25
+ private var hasListeners = false
26
+
27
+ // The CustomerInfo stream bridge task. Cancelled + replaced on every configure()
28
+ // so re-configuring does not leak/stack duplicate stream subscribers.
29
+ private var customerInfoTask: Task<Void, Never>?
30
+
31
+ deinit {
32
+ customerInfoTask?.cancel()
33
+ }
34
+
35
+ // MARK: RCTEventEmitter
36
+
37
+ // Event names this module can emit. Must include the CustomerInfo updated event
38
+ // whose JS constant is CUSTOMER_INFO_UPDATED_EVENT in src/native/spec.ts.
39
+ override func supportedEvents() -> [String]! {
40
+ return ["Purchases-CustomerInfoUpdated"]
41
+ }
42
+
43
+ override static func requiresMainQueueSetup() -> Bool {
44
+ return true
45
+ }
46
+
47
+ override func startObserving() { hasListeners = true }
48
+ override func stopObserving() { hasListeners = false }
49
+
50
+ private func sendCustomerInfo(_ customerInfo: [String: Any]) {
51
+ // RCTEventEmitter is not thread-safe; the stream callback runs off-main.
52
+ DispatchQueue.main.async {
53
+ guard self.hasListeners else { return }
54
+ self.sendEvent(withName: "Purchases-CustomerInfoUpdated", body: customerInfo)
55
+ }
56
+ }
57
+
58
+ // MARK: Lifecycle
59
+
60
+ @objc(configure:resolver:rejecter:)
61
+ func configure(_ options: NSDictionary,
62
+ resolver resolve: @escaping RCTPromiseResolveBlock,
63
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
64
+ guard let apiKey = options["apiKey"] as? String, !apiKey.isEmpty else {
65
+ reject("CONFIGURATION", "configure requires a non-empty apiKey", nil)
66
+ return
67
+ }
68
+ let appUserID = options["appUserID"] as? String
69
+ let proxyURL = options["proxyURL"] as? String
70
+ let storeKitVersion = (options["storeKitVersion"] as? String) ?? "DEFAULT"
71
+ let logLevel = options["logLevel"] as? String
72
+
73
+ // Attribution / App Tracking Transparency. Default is NO ATT prompt
74
+ // (autoPromptATT: false) — the SDK never asks for tracking consent unless
75
+ // the host opts in by passing `autoPromptATT: true` (which also requires
76
+ // NSUserTrackingUsageDescription in Info.plist). `sendInstallOnFirstLaunch`
77
+ // still registers an organic install (no IDFA, no prompt) unless disabled.
78
+ let attribution = SwissKnife.Attribution.Configuration(
79
+ autoPromptATT: (options["autoPromptATT"] as? Bool) ?? false,
80
+ sendInstallOnFirstLaunch: (options["sendInstallOnFirstLaunch"] as? Bool) ?? true
81
+ )
82
+
83
+ // Phase 5: SwissKnife owns the entire native subscription stack — StoreKit 2,
84
+ // proxyURL, diagnostics, identity. RN + native-app paths configure identically.
85
+ SwissKnife.configure(
86
+ apiKey: apiKey,
87
+ appUserID: appUserID,
88
+ proxyURL: proxyURL,
89
+ storeKitVersion: storeKitVersion,
90
+ logLevel: logLevel,
91
+ attribution: attribution
92
+ )
93
+
94
+ // Bridge the native SDK's CustomerInfo stream to the JS event channel.
95
+ // Cancel any prior task first so re-configure does not stack duplicate
96
+ // stream subscribers (which would emit duplicate CustomerInfo events).
97
+ customerInfoTask?.cancel()
98
+ customerInfoTask = Task { [weak self] in
99
+ for await info in SwissKnife.customerInfoStream {
100
+ self?.sendCustomerInfo(SwissKnifeJSON.encode(customerInfo: info))
101
+ }
102
+ }
103
+
104
+ resolve(nil)
105
+ }
106
+
107
+ @objc(isConfigured:rejecter:)
108
+ func isConfigured(_ resolve: @escaping RCTPromiseResolveBlock,
109
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
110
+ resolve(SwissKnife.isConfigured)
111
+ }
112
+
113
+ // MARK: CustomerInfo / Offerings
114
+
115
+ @objc(getCustomerInfo:rejecter:)
116
+ func getCustomerInfo(_ resolve: @escaping RCTPromiseResolveBlock,
117
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
118
+ Task {
119
+ do {
120
+ let info = try await SwissKnife.getCustomerInfo()
121
+ resolve(SwissKnifeJSON.encode(customerInfo: info))
122
+ } catch {
123
+ Self.rejectWithPurchasesError(error, reject)
124
+ }
125
+ }
126
+ }
127
+
128
+ @objc(getOfferings:rejecter:)
129
+ func getOfferings(_ resolve: @escaping RCTPromiseResolveBlock,
130
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
131
+ Task {
132
+ do {
133
+ let offerings = try await SwissKnife.getOfferings()
134
+ resolve(SwissKnifeJSON.encode(offerings: offerings))
135
+ } catch {
136
+ Self.rejectWithPurchasesError(error, reject)
137
+ }
138
+ }
139
+ }
140
+
141
+ // MARK: Purchasing
142
+
143
+ @objc(purchasePackage:presentedOfferingIdentifier:resolver:rejecter:)
144
+ func purchasePackage(_ packageIdentifier: String,
145
+ presentedOfferingIdentifier: String,
146
+ resolver resolve: @escaping RCTPromiseResolveBlock,
147
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
148
+ Task {
149
+ do {
150
+ // We don't have the full Package value across the bridge — JS sent us
151
+ // identifiers. Fetch offerings to resolve the matching Package, then
152
+ // hand it to PurchaseService.
153
+ let offerings = try await SwissKnife.getOfferings()
154
+ guard
155
+ let offering = offerings.all[presentedOfferingIdentifier],
156
+ let pkg = offering.availablePackages.first(where: { $0.identifier == packageIdentifier })
157
+ else {
158
+ reject(
159
+ String(describing: PurchasesErrorCodeBridge.productNotAvailable),
160
+ "Package \(packageIdentifier) not found in offering \(presentedOfferingIdentifier)",
161
+ nil
162
+ )
163
+ return
164
+ }
165
+ let result = try await SwissKnife.purchasePackage(pkg)
166
+ resolve(SwissKnifeJSON.encode(purchaseResult: result, productIdentifier: pkg.storeProduct.productIdentifier))
167
+ } catch {
168
+ Self.rejectWithPurchasesError(error, reject)
169
+ }
170
+ }
171
+ }
172
+
173
+ @objc(purchaseStoreProduct:resolver:rejecter:)
174
+ func purchaseStoreProduct(_ productIdentifier: String,
175
+ resolver resolve: @escaping RCTPromiseResolveBlock,
176
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
177
+ Task {
178
+ do {
179
+ // Synthesize a minimal StoreProduct — PurchaseService only needs the
180
+ // identifier on this path; the localized metadata is filled in by
181
+ // StoreKit 2 inside PurchaseService.purchase(productIdentifier:).
182
+ let product = StoreProduct(
183
+ productIdentifier: productIdentifier,
184
+ localizedTitle: "",
185
+ localizedDescription: "",
186
+ price: 0,
187
+ currencyCode: "",
188
+ displayPrice: "",
189
+ subscriptionPeriod: nil,
190
+ introductoryDiscount: nil,
191
+ discounts: []
192
+ )
193
+ let result = try await SwissKnife.purchaseStoreProduct(product)
194
+ resolve(SwissKnifeJSON.encode(purchaseResult: result, productIdentifier: productIdentifier))
195
+ } catch {
196
+ Self.rejectWithPurchasesError(error, reject)
197
+ }
198
+ }
199
+ }
200
+
201
+ @objc(restorePurchases:rejecter:)
202
+ func restorePurchases(_ resolve: @escaping RCTPromiseResolveBlock,
203
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
204
+ Task {
205
+ do {
206
+ let info = try await SwissKnife.restorePurchases()
207
+ resolve(SwissKnifeJSON.encode(customerInfo: info))
208
+ } catch {
209
+ Self.rejectWithPurchasesError(error, reject)
210
+ }
211
+ }
212
+ }
213
+
214
+ // MARK: Identity
215
+
216
+ @objc(logIn:resolver:rejecter:)
217
+ func logIn(_ appUserID: String,
218
+ resolver resolve: @escaping RCTPromiseResolveBlock,
219
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
220
+ Task {
221
+ do {
222
+ let outcome = try await SwissKnife.logIn(appUserID)
223
+ resolve([
224
+ "customerInfo": SwissKnifeJSON.encode(customerInfo: outcome.customerInfo),
225
+ "created": outcome.created,
226
+ ])
227
+ } catch {
228
+ Self.rejectWithPurchasesError(error, reject)
229
+ }
230
+ }
231
+ }
232
+
233
+ @objc(logOut:rejecter:)
234
+ func logOut(_ resolve: @escaping RCTPromiseResolveBlock,
235
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
236
+ Task {
237
+ do {
238
+ let info = try await SwissKnife.logOut()
239
+ resolve(SwissKnifeJSON.encode(customerInfo: info))
240
+ } catch {
241
+ Self.rejectWithPurchasesError(error, reject)
242
+ }
243
+ }
244
+ }
245
+
246
+ @objc(getAppUserID:rejecter:)
247
+ func getAppUserID(_ resolve: @escaping RCTPromiseResolveBlock,
248
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
249
+ resolve(SwissKnife.appUserID)
250
+ }
251
+
252
+ @objc(isAnonymous:rejecter:)
253
+ func isAnonymous(_ resolve: @escaping RCTPromiseResolveBlock,
254
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
255
+ resolve(SwissKnife.isAnonymous)
256
+ }
257
+
258
+ // MARK: Identity (Wave 1)
259
+ //
260
+ // Unified identity bridge. All five methods delegate to
261
+ // `SwissKnife.Identity` so iOS / Android / RN share one source of
262
+ // truth. The legacy `logIn` / `logOut` bridge methods above stay on
263
+ // the spec for back-compat and route through the SwissKnife facade,
264
+ // which itself routes through Identity.
265
+
266
+ @objc(identityCurrent:rejecter:)
267
+ func identityCurrent(_ resolve: @escaping RCTPromiseResolveBlock,
268
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
269
+ let snapshot = SwissKnife.Identity.current
270
+ resolve([
271
+ "anonymousId": snapshot.anonymousId,
272
+ "userId": snapshot.userId as Any? ?? NSNull(),
273
+ "isAnonymous": snapshot.isAnonymous,
274
+ ])
275
+ }
276
+
277
+ @objc(identityLogIn:resolver:rejecter:)
278
+ func identityLogIn(_ userId: String,
279
+ resolver resolve: @escaping RCTPromiseResolveBlock,
280
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
281
+ Task {
282
+ do {
283
+ let result = try await SwissKnife.Identity.logIn(userId)
284
+ resolve([
285
+ "customerInfo": SwissKnifeJSON.encode(customerInfo: result.customerInfo),
286
+ "created": result.created,
287
+ ])
288
+ } catch {
289
+ Self.rejectWithPurchasesError(error, reject)
290
+ }
291
+ }
292
+ }
293
+
294
+ @objc(identityLogOut:rejecter:)
295
+ func identityLogOut(_ resolve: @escaping RCTPromiseResolveBlock,
296
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
297
+ Task {
298
+ do {
299
+ try await SwissKnife.Identity.logOut()
300
+ resolve(nil)
301
+ } catch {
302
+ Self.rejectWithPurchasesError(error, reject)
303
+ }
304
+ }
305
+ }
306
+
307
+ @objc(identityReset:rejecter:)
308
+ func identityReset(_ resolve: @escaping RCTPromiseResolveBlock,
309
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
310
+ SwissKnife.Identity.reset()
311
+ resolve(nil)
312
+ }
313
+
314
+ @objc(identityWipe:rejecter:)
315
+ func identityWipe(_ resolve: @escaping RCTPromiseResolveBlock,
316
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
317
+ SwissKnife.Identity.wipe()
318
+ resolve(nil)
319
+ }
320
+
321
+ // Post-#136 audit: JS-side analytics used `Date.now()` for session_id which
322
+ // diverged from the envelope. This bridge surfaces the canonical
323
+ // `Session.shared.current` snapshot so JS' legacy `session_id` field
324
+ // agrees with the envelope.
325
+ @objc(sessionCurrent:rejecter:)
326
+ func sessionCurrent(_ resolve: @escaping RCTPromiseResolveBlock,
327
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
328
+ let info = SwissKnife.currentSession()
329
+ resolve([
330
+ "id": info.id,
331
+ "sessionNumber": info.sessionNumber,
332
+ "startedAt": info.startedAtMillis,
333
+ ])
334
+ }
335
+
336
+ // MARK: Attribution (Wave 2)
337
+ //
338
+ // Unified attribution bridge. All three methods delegate to
339
+ // `SwissKnife.Attribution`, which owns the disk-backed idempotency
340
+ // cache, the ATT prompt, and the install POST.
341
+
342
+ @objc(attributionResolved:rejecter:)
343
+ func attributionResolved(_ resolve: @escaping RCTPromiseResolveBlock,
344
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
345
+ resolve(Self.encodeAttribution(SwissKnife.Attribution.resolved))
346
+ }
347
+
348
+ @objc(attributionRequestAndRegister:rejecter:)
349
+ func attributionRequestAndRegister(_ resolve: @escaping RCTPromiseResolveBlock,
350
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
351
+ Task { @MainActor in
352
+ do {
353
+ let resolved = try await SwissKnife.Attribution.requestTrackingAuthorizationAndRegister()
354
+ resolve(Self.encodeAttribution(resolved))
355
+ } catch {
356
+ Self.rejectWithPurchasesError(error, reject)
357
+ }
358
+ }
359
+ }
360
+
361
+ @objc(attributionRegisterInstall:resolver:rejecter:)
362
+ func attributionRegisterInstall(_ advertisingId: NSString?,
363
+ resolver resolve: @escaping RCTPromiseResolveBlock,
364
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
365
+ let idfa = (advertisingId as String?).flatMap { $0.isEmpty ? nil : $0 }
366
+ Task {
367
+ do {
368
+ let resolved = try await SwissKnife.Attribution.registerInstall(advertisingId: idfa)
369
+ resolve(Self.encodeAttribution(resolved))
370
+ } catch {
371
+ Self.rejectWithPurchasesError(error, reject)
372
+ }
373
+ }
374
+ }
375
+
376
+ /// Encode the ResolvedAttribution into the RN bridge dict shape that
377
+ /// `packages/react-native-sdk/src/types.ts.ResolvedAttribution`
378
+ /// expects (`method`, `campaignId`, `resolvedAt: epoch-ms`).
379
+ private static func encodeAttribution(
380
+ _ resolved: SwissKnife.Attribution.ResolvedAttribution?
381
+ ) -> Any {
382
+ guard let resolved else { return NSNull() }
383
+ return [
384
+ "method": resolved.method,
385
+ "campaignId": resolved.campaignId as Any? ?? NSNull(),
386
+ "resolvedAt": Int(resolved.resolvedAt.timeIntervalSince1970 * 1000),
387
+ ]
388
+ }
389
+
390
+ // MARK: Subscriber attributes
391
+ //
392
+ // Phase 3 of the RC rip-and-replace: attribute setters delegate to
393
+ // SwissKnife (which routes via AttributesClient → /api/sdk/users/attributes).
394
+ // Phase 7 leaves this path untouched — we only removed the PHC import.
395
+
396
+ @objc(setAttributes:resolver:rejecter:)
397
+ func setAttributes(_ attributes: NSDictionary,
398
+ resolver resolve: @escaping RCTPromiseResolveBlock,
399
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
400
+ // JS passes Record<string, string | null>. Across the bridge nulls land
401
+ // as NSNull, strings as NSString. Translate to a non-nullable
402
+ // [String: String] so SwissKnife.setAttributes() can pass it through.
403
+ var nonNull: [String: String] = [:]
404
+ for case let (key as String, value) in attributes {
405
+ if value is NSNull { continue }
406
+ if let s = value as? String {
407
+ nonNull[key] = s
408
+ } else {
409
+ // Non-string, non-null value — coerce via description rather than
410
+ // dropping it silently, matching prior lenient behavior.
411
+ nonNull[key] = String(describing: value)
412
+ }
413
+ }
414
+ SwissKnife.setAttributes(nonNull)
415
+ // Note: SwissKnife.setAttributes takes [String: String] today, so a
416
+ // null-clear via this bulk path falls back to per-key setEmail/etc.
417
+ // Callers wanting to clear a value should call the dedicated setter
418
+ // with a null argument.
419
+ resolve(nil)
420
+ }
421
+
422
+ @objc(setEmail:resolver:rejecter:)
423
+ func setEmail(_ email: NSString?,
424
+ resolver resolve: @escaping RCTPromiseResolveBlock,
425
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
426
+ SwissKnife.setEmail(email as String?)
427
+ resolve(nil)
428
+ }
429
+
430
+ @objc(setDisplayName:resolver:rejecter:)
431
+ func setDisplayName(_ displayName: NSString?,
432
+ resolver resolve: @escaping RCTPromiseResolveBlock,
433
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
434
+ SwissKnife.setDisplayName(displayName as String?)
435
+ resolve(nil)
436
+ }
437
+
438
+ @objc(setPhoneNumber:resolver:rejecter:)
439
+ func setPhoneNumber(_ phoneNumber: NSString?,
440
+ resolver resolve: @escaping RCTPromiseResolveBlock,
441
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
442
+ SwissKnife.setPhoneNumber(phoneNumber as String?)
443
+ resolve(nil)
444
+ }
445
+
446
+ @objc(setPushToken:resolver:rejecter:)
447
+ func setPushToken(_ pushToken: NSString?,
448
+ resolver resolve: @escaping RCTPromiseResolveBlock,
449
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
450
+ SwissKnife.setPushToken(pushToken as String?)
451
+ resolve(nil)
452
+ }
453
+
454
+ @objc(setLogLevel:resolver:rejecter:)
455
+ func setLogLevel(_ level: String,
456
+ resolver resolve: @escaping RCTPromiseResolveBlock,
457
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
458
+ SwissKnife.setLogLevel(level)
459
+ resolve(nil)
460
+ }
461
+
462
+ // MARK: Device identifier
463
+
464
+ // Delegates to SwissKnifeiOS's `getDeviceId()` facade (Keychain-backed UUID).
465
+ // The native SDK owns persistence; JS must NOT generate this id — see
466
+ // src/modules/analytics.ts for the rationale.
467
+ @objc(getDeviceId:rejecter:)
468
+ func getDeviceId(_ resolve: @escaping RCTPromiseResolveBlock,
469
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
470
+ resolve(SwissKnife.getDeviceId())
471
+ }
472
+
473
+ // MARK: Analytics persistent queue (Phase 2.3 — RN bridge → native)
474
+
475
+ // Persist a pre-built event dictionary onto the native disk-backed queue.
476
+ // JS owns the payload shape (event_id, device_id, super props, timed events,
477
+ // identify merges); native owns the queue (disk persistence, back-off,
478
+ // dead-letter). See src/modules/analytics.ts and Sources/SwissKnifeiOS/
479
+ // SwissKnife+Analytics.swift for the full rationale.
480
+ @objc(trackEvent:resolver:rejecter:)
481
+ func trackEvent(_ event: NSDictionary,
482
+ resolver resolve: @escaping RCTPromiseResolveBlock,
483
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
484
+ SwissKnife.trackEvent(event as? [String: Any] ?? [:])
485
+ resolve(nil)
486
+ }
487
+
488
+ // Kick off a flush of the native queue. Wrapped in a Task so the underlying
489
+ // `async` facade can evolve into a real awaitable form without breaking the
490
+ // bridge signature.
491
+ @objc(flushEvents:rejecter:)
492
+ func flushEvents(_ resolve: @escaping RCTPromiseResolveBlock,
493
+ rejecter reject: @escaping RCTPromiseRejectBlock) {
494
+ Task {
495
+ await SwissKnife.flushEvents()
496
+ resolve(nil)
497
+ }
498
+ }
499
+
500
+ // MARK: Helpers
501
+
502
+ // Map a typed `PurchasesError` to the RN promise rejection shape that JS
503
+ // (`src/types.ts`) expects. The numeric code matches our own
504
+ // `PURCHASES_ERROR_CODE` mapping so the JS PurchasesError mapper works
505
+ // unchanged.
506
+ private static func rejectWithPurchasesError(_ error: Error,
507
+ _ reject: RCTPromiseRejectBlock) {
508
+ if let pe = error as? PurchasesError {
509
+ let codeAndMessage = PurchasesErrorCodeBridge.toJSCodeAndMessage(pe)
510
+ reject(codeAndMessage.code, codeAndMessage.message, error)
511
+ } else {
512
+ reject(
513
+ PurchasesErrorCodeBridge.unknown,
514
+ error.localizedDescription,
515
+ error
516
+ )
517
+ }
518
+ }
519
+ }
520
+
521
+ // NOTE: No CustomerInfo delegate plumbing here — SwissKnife (the native iOS SDK)
522
+ // owns the stream and re-emits via `customerInfoStream`. The Task block in
523
+ // configure() above subscribes to that stream and calls sendCustomerInfo().
@@ -0,0 +1,10 @@
1
+ GENERATED — DO NOT EDIT.
2
+
3
+ Copied verbatim by packages/react-native-sdk/scripts/vendor-native.mjs from the
4
+ standalone native SDK sources in the monorepo. Edit those instead:
5
+
6
+ packages/swiss-knife-ios/Sources/SwissKnifeiOS/**
7
+ packages/swiss-knife-android/src/main/{java,res}/**
8
+
9
+ This directory is gitignored and regenerated on every `build` / `prepack`.
10
+ It ships inside the npm tarball so the package is self-contained.