@altcraft/react-native-sdk 0.0.4 → 1.0.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/LICENSE +48 -20
- package/README.md +21 -120
- package/autolink.js +36 -0
- package/lib/commonjs/altcraft_sdk.js +297 -0
- package/lib/commonjs/altcraft_sdk.js.map +1 -0
- package/lib/commonjs/index.js +192 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/module/altcraft_sdk.js +292 -0
- package/lib/module/altcraft_sdk.js.map +1 -0
- package/lib/module/index.js +13 -427
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/src/altcraft_sdk.d.ts +143 -0
- package/lib/typescript/commonjs/src/altcraft_sdk.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/index.d.ts +24 -0
- package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
- package/lib/typescript/module/src/altcraft_sdk.d.ts +143 -0
- package/lib/typescript/module/src/altcraft_sdk.d.ts.map +1 -0
- package/lib/typescript/module/src/index.d.ts +24 -0
- package/lib/typescript/module/src/index.d.ts.map +1 -0
- package/package.json +49 -95
- package/scripts/postinstall.cjs +195 -0
- package/src/altcraft_sdk.ts +337 -0
- package/src/index.ts +91 -0
- package/Sdk.podspec +0 -48
- package/android/build.gradle +0 -77
- package/android/gradle.properties +0 -5
- package/android/src/main/AndroidManifest.xml +0 -2
- package/android/src/main/java/com/altcraft/sdk/SdkEventEmitter.kt +0 -50
- package/android/src/main/java/com/altcraft/sdk/SdkModule.kt +0 -949
- package/android/src/main/java/com/altcraft/sdk/SdkPackage.kt +0 -41
- package/android/src/main/java/com/altcraft/sdk/utilities/Converter.kt +0 -326
- package/android/src/main/java/com/altcraft/sdk/utilities/Extension.kt +0 -72
- package/ios/Converter.swift +0 -640
- package/ios/Sdk.h +0 -20
- package/ios/Sdk.mm +0 -979
- package/ios/SdkModule.swift +0 -778
- package/lib/module/NativeSdk.js +0 -48
- package/lib/module/NativeSdk.js.map +0 -1
- package/lib/module/Utilities.js +0 -178
- package/lib/module/Utilities.js.map +0 -1
- package/lib/typescript/src/NativeSdk.d.ts +0 -215
- package/lib/typescript/src/NativeSdk.d.ts.map +0 -1
- package/lib/typescript/src/Utilities.d.ts +0 -47
- package/lib/typescript/src/Utilities.d.ts.map +0 -1
- package/lib/typescript/src/index.d.ts +0 -252
- package/lib/typescript/src/index.d.ts.map +0 -1
- package/react-native.config.js +0 -10
- package/src/NativeSdk.ts +0 -313
- package/src/Utilities.ts +0 -180
- package/src/index.tsx +0 -570
- /package/lib/typescript/{package.json → module/package.json} +0 -0
package/ios/Converter.swift
DELETED
|
@@ -1,640 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Converter.swift
|
|
3
|
-
// AltcraftRnBridge
|
|
4
|
-
//
|
|
5
|
-
// Created by Andrey Pogodin.
|
|
6
|
-
//
|
|
7
|
-
// Copyright © 2026 Altcraft. All rights reserved.
|
|
8
|
-
|
|
9
|
-
import Foundation
|
|
10
|
-
import Altcraft
|
|
11
|
-
|
|
12
|
-
/// Utility namespace for bridging and normalizing values between React Native (Obj-C) and Swift.
|
|
13
|
-
///
|
|
14
|
-
/// `Converter` provides:
|
|
15
|
-
/// - Recursive normalization of Obj-C containers (`NSDictionary`/`NSArray`) into Swift-friendly
|
|
16
|
-
/// structures (`[String: Any?]`, `[Any]`).
|
|
17
|
-
/// - Safe conversion of RN values (`NSNull`, `NSNumber`, `NSString`) into predictable Swift types.
|
|
18
|
-
/// - Optional JSON parsing for strings that look like JSON objects/arrays.
|
|
19
|
-
/// - Helpers for building Altcraft SDK models (`AppInfo`, `UTM`, `Subscription`) from RN payloads.
|
|
20
|
-
/// - Conversion of arbitrary values into `UserDefaults`-compatible property list values.
|
|
21
|
-
///
|
|
22
|
-
/// All functions are stateless and intended to be used from the React Native bridge layer.
|
|
23
|
-
enum Converter: @unchecked Sendable {
|
|
24
|
-
|
|
25
|
-
nonisolated(unsafe) static let iso8601: ISO8601DateFormatter = {
|
|
26
|
-
let f = ISO8601DateFormatter()
|
|
27
|
-
return f
|
|
28
|
-
}()
|
|
29
|
-
|
|
30
|
-
// MARK: - Public (Generic normalization)
|
|
31
|
-
|
|
32
|
-
/// Converts arbitrary values into RN-safe Swift types.
|
|
33
|
-
///
|
|
34
|
-
/// Rules:
|
|
35
|
-
/// - `NSNull` -> `nil`
|
|
36
|
-
/// - `NSNumber` -> `Bool` (if CFBoolean) or normalized numeric (`Int`/`Int64`/`Double`)
|
|
37
|
-
/// - `NSString` -> `String` (and if it looks like JSON `{...}` or `[...]` it will be parsed)
|
|
38
|
-
/// - `NSDictionary`/`NSArray` -> recursively converted
|
|
39
|
-
/// - `Date` -> ISO8601 string
|
|
40
|
-
/// - `Data` -> base64 string
|
|
41
|
-
///
|
|
42
|
-
/// - Parameter value: Input value.
|
|
43
|
-
/// - Returns: Normalized value.
|
|
44
|
-
static func toAny(_ value: Any?) -> Any? {
|
|
45
|
-
guard let value else { return nil }
|
|
46
|
-
if value is NSNull { return nil }
|
|
47
|
-
|
|
48
|
-
if let n = value as? NSNumber {
|
|
49
|
-
if isCFBoolean(n) { return n.boolValue }
|
|
50
|
-
return normalizeNumber(n)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if let s = value as? NSString {
|
|
54
|
-
return parseJSONIfPossible(String(s))
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if let d = value as? Date {
|
|
58
|
-
return iso8601.string(from: d)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if let data = value as? Data {
|
|
62
|
-
return data.base64EncodedString()
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if let dict = value as? NSDictionary {
|
|
66
|
-
return toAnyDict(dict)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if let arr = value as? NSArray {
|
|
70
|
-
return toAnyArray(arr)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if let dict = value as? [String: Any?] {
|
|
74
|
-
var out: [String: Any?] = [:]
|
|
75
|
-
out.reserveCapacity(dict.count)
|
|
76
|
-
for (k, v) in dict {
|
|
77
|
-
out[k] = toAny(v)
|
|
78
|
-
}
|
|
79
|
-
return out
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if let dict = value as? [String: Any] {
|
|
83
|
-
var out: [String: Any] = [:]
|
|
84
|
-
out.reserveCapacity(dict.count)
|
|
85
|
-
for (k, v) in dict {
|
|
86
|
-
out[k] = toAny(v) ?? NSNull()
|
|
87
|
-
}
|
|
88
|
-
return out
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if let arr = value as? [Any] {
|
|
92
|
-
var out: [Any] = []
|
|
93
|
-
out.reserveCapacity(arr.count)
|
|
94
|
-
for v in arr {
|
|
95
|
-
out.append(toAny(v) ?? NSNull())
|
|
96
|
-
}
|
|
97
|
-
return out
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return String(describing: value)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/// Converts `NSDictionary` into `[String: Any?]` recursively.
|
|
104
|
-
///
|
|
105
|
-
/// - Parameter dict: Source dictionary.
|
|
106
|
-
/// - Returns: Converted dictionary or `nil`.
|
|
107
|
-
static func toAnyDict(_ dict: NSDictionary?) -> [String: Any?]? {
|
|
108
|
-
guard let dict, dict.count > 0 else { return nil }
|
|
109
|
-
var out: [String: Any?] = [:]
|
|
110
|
-
out.reserveCapacity(dict.count)
|
|
111
|
-
|
|
112
|
-
for (k, v) in dict {
|
|
113
|
-
guard let key = k as? String else { continue }
|
|
114
|
-
out[key] = toAny(v)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return out.isEmpty ? nil : out
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/// Converts `NSArray` into `[Any]` recursively.
|
|
121
|
-
///
|
|
122
|
-
/// - Parameter arr: Source array.
|
|
123
|
-
/// - Returns: Converted array or `nil`.
|
|
124
|
-
static func toAnyArray(_ arr: NSArray?) -> [Any]? {
|
|
125
|
-
guard let arr, arr.count > 0 else { return nil }
|
|
126
|
-
var out: [Any] = []
|
|
127
|
-
out.reserveCapacity(arr.count)
|
|
128
|
-
|
|
129
|
-
for item in arr {
|
|
130
|
-
out.append(toAny(item) ?? NSNull())
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return out.isEmpty ? nil : out
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// MARK: - UserDefaults helpers
|
|
137
|
-
|
|
138
|
-
/// Converts value into a type supported by UserDefaults:
|
|
139
|
-
/// String/Bool/Int/Double/Data/Date/Array/Dictionary (property list).
|
|
140
|
-
///
|
|
141
|
-
/// If it cannot be represented as a property list, it falls back to JSON string when possible.
|
|
142
|
-
///
|
|
143
|
-
/// - Parameter value: Input value.
|
|
144
|
-
/// - Returns: Property-list compatible value or `nil`.
|
|
145
|
-
static func toPropertyListValue(_ value: Any?) -> Any? {
|
|
146
|
-
guard let value = toAny(value) else { return nil }
|
|
147
|
-
|
|
148
|
-
if value is String
|
|
149
|
-
|| value is Bool
|
|
150
|
-
|| value is Int
|
|
151
|
-
|| value is Int64
|
|
152
|
-
|| value is Double
|
|
153
|
-
|| value is Float
|
|
154
|
-
|| value is Data
|
|
155
|
-
|| value is Date {
|
|
156
|
-
if let i64 = value as? Int64 { return NSNumber(value: i64) }
|
|
157
|
-
if let f = value as? Float { return NSNumber(value: f) }
|
|
158
|
-
return value
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if let n = value as? NSNumber {
|
|
162
|
-
if isCFBoolean(n) { return n.boolValue }
|
|
163
|
-
return n
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if let arr = value as? [Any] {
|
|
167
|
-
var mapped: [Any] = []
|
|
168
|
-
mapped.reserveCapacity(arr.count)
|
|
169
|
-
for element in arr {
|
|
170
|
-
if element is NSNull { continue }
|
|
171
|
-
if let pv = toPropertyListValue(element) { mapped.append(pv) }
|
|
172
|
-
}
|
|
173
|
-
return mapped
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if let dict = value as? [String: Any?] {
|
|
177
|
-
var mapped: [String: Any] = [:]
|
|
178
|
-
mapped.reserveCapacity(dict.count)
|
|
179
|
-
for (k, v) in dict {
|
|
180
|
-
if let pv = toPropertyListValue(v) { mapped[k] = pv }
|
|
181
|
-
}
|
|
182
|
-
return mapped
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if let dict = value as? [String: Any] {
|
|
186
|
-
var mapped: [String: Any] = [:]
|
|
187
|
-
mapped.reserveCapacity(dict.count)
|
|
188
|
-
for (k, v) in dict {
|
|
189
|
-
if let pv = toPropertyListValue(v) { mapped[k] = pv }
|
|
190
|
-
}
|
|
191
|
-
return mapped
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
if JSONSerialization.isValidJSONObject(value) {
|
|
195
|
-
if let data = try? JSONSerialization.data(withJSONObject: value, options: []),
|
|
196
|
-
let json = String(data: data, encoding: .utf8) {
|
|
197
|
-
return json
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
return String(describing: value)
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/// Converts `[String: Any?]` into `[String: String]` by stringifying values.
|
|
205
|
-
///
|
|
206
|
-
/// - Parameter dict: Input dictionary.
|
|
207
|
-
/// - Returns: A string map or `nil`.
|
|
208
|
-
static func toStringMap(_ dict: [String: Any?]?) -> [String: String]? {
|
|
209
|
-
guard let dict, !dict.isEmpty else { return nil }
|
|
210
|
-
var out: [String: String] = [:]
|
|
211
|
-
out.reserveCapacity(dict.count)
|
|
212
|
-
for (k, v) in dict {
|
|
213
|
-
guard let v else { continue }
|
|
214
|
-
out[k] = String(describing: v)
|
|
215
|
-
}
|
|
216
|
-
return out.isEmpty ? nil : out
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
/// Builds `AppInfo` from a config value.
|
|
221
|
-
///
|
|
222
|
-
/// - Parameter any: `NSDictionary` / `[String: Any]` / `[String: Any?]` or `nil`.
|
|
223
|
-
/// - Returns: `AppInfo` or `nil` if all fields are blank.
|
|
224
|
-
static func appInfo(from any: Any?) -> AppInfo? {
|
|
225
|
-
guard let map = normalizeStringAnyMap(any) else { return nil }
|
|
226
|
-
|
|
227
|
-
let appID = (map["appID"] as? String) ?? ""
|
|
228
|
-
let appIID = (map["appIID"] as? String) ?? ""
|
|
229
|
-
let appVer = (map["appVer"] as? String) ?? ""
|
|
230
|
-
|
|
231
|
-
if appID.isEmpty && appIID.isEmpty && appVer.isEmpty { return nil }
|
|
232
|
-
return AppInfo(appID: appID, appIID: appIID, appVer: appVer)
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/// Converts an Objective-C array of category items into `[CategoryData]`.
|
|
236
|
-
///
|
|
237
|
-
/// Supported item formats:
|
|
238
|
-
/// - String: treated as category `name` with `active = true`
|
|
239
|
-
/// - Map: supports `name`, `title`, `steady`, `active`
|
|
240
|
-
///
|
|
241
|
-
/// - Parameter cats: Source array.
|
|
242
|
-
/// - Returns: Category list or `nil`.
|
|
243
|
-
static func toCats(_ cats: NSArray?) -> [CategoryData]? {
|
|
244
|
-
guard let arr = cats, arr.count > 0 else { return nil }
|
|
245
|
-
|
|
246
|
-
var out: [CategoryData] = []
|
|
247
|
-
out.reserveCapacity(arr.count)
|
|
248
|
-
|
|
249
|
-
for item in arr {
|
|
250
|
-
if let s = item as? String, !s.isEmpty {
|
|
251
|
-
out.append(CategoryData(name: s, title: nil, steady: nil, active: true))
|
|
252
|
-
continue
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
if let m = item as? NSDictionary {
|
|
256
|
-
let mm = toAnyDict(m) ?? [:]
|
|
257
|
-
let name = mm["name"] as? String
|
|
258
|
-
if name == nil { continue }
|
|
259
|
-
|
|
260
|
-
let title = mm["title"] as? String
|
|
261
|
-
let steady = mm["steady"] as? Bool
|
|
262
|
-
let active = mm["active"] as? Bool
|
|
263
|
-
|
|
264
|
-
out.append(CategoryData(name: name, title: title, steady: steady, active: active))
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
return out.isEmpty ? nil : out
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/// Converts `utm` dictionary into `UTM`.
|
|
272
|
-
///
|
|
273
|
-
/// - Parameter utm: Source map (`NSDictionary`) from RN.
|
|
274
|
-
/// - Returns: `UTM` or `nil`.
|
|
275
|
-
static func toUTM(_ utm: NSDictionary?) -> UTM? {
|
|
276
|
-
guard let map = toAnyDict(utm) else { return nil }
|
|
277
|
-
return UTM(
|
|
278
|
-
campaign: map["campaign"] as? String,
|
|
279
|
-
content: map["content"] as? String,
|
|
280
|
-
keyword: map["keyword"] as? String,
|
|
281
|
-
medium: map["medium"] as? String,
|
|
282
|
-
source: map["source"] as? String,
|
|
283
|
-
temp: map["temp"] as? String
|
|
284
|
-
)
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/// Converts a React Native subscription map into a native `Subscription` instance.
|
|
288
|
-
///
|
|
289
|
-
/// Supported `type` values:
|
|
290
|
-
/// - `email`, `sms`, `push`, `cc_data`
|
|
291
|
-
///
|
|
292
|
-
/// - Parameter subscriptionDict: Subscription dictionary from JS.
|
|
293
|
-
/// - Returns: A native subscription object or `nil` if the input is invalid.
|
|
294
|
-
static func toSubscription(_ subscriptionDict: NSDictionary?) -> Subscription? {
|
|
295
|
-
guard let dict = subscriptionDict else { return nil }
|
|
296
|
-
guard let type = stringFromAny(dict["type"]) else { return nil }
|
|
297
|
-
|
|
298
|
-
let resourceId =
|
|
299
|
-
intFromAny(dict["resource_id"]) ??
|
|
300
|
-
intFromAny(dict["resourceId"])
|
|
301
|
-
guard let resourceId else { return nil }
|
|
302
|
-
|
|
303
|
-
let status = stringFromAny(dict["status"])
|
|
304
|
-
let priority = intFromAny(dict["priority"])
|
|
305
|
-
|
|
306
|
-
let customFields: [String: JSONValue]? =
|
|
307
|
-
jsonValueDictFromAny(dict["custom_fields"]) ??
|
|
308
|
-
jsonValueDictFromAny(dict["customFields"])
|
|
309
|
-
|
|
310
|
-
let cats: [String]? = stringArrayFromAny(dict["cats"])
|
|
311
|
-
|
|
312
|
-
switch type {
|
|
313
|
-
case "email":
|
|
314
|
-
guard let email = stringFromAny(
|
|
315
|
-
dict["email"]
|
|
316
|
-
) else {
|
|
317
|
-
return nil
|
|
318
|
-
}
|
|
319
|
-
return EmailSubscription(
|
|
320
|
-
resourceId: resourceId,
|
|
321
|
-
email: email, status: status,
|
|
322
|
-
priority: priority,
|
|
323
|
-
customFields: customFields,
|
|
324
|
-
cats: cats
|
|
325
|
-
)
|
|
326
|
-
|
|
327
|
-
case "sms":
|
|
328
|
-
guard let phone = stringFromAny(
|
|
329
|
-
dict["phone"]
|
|
330
|
-
) else {
|
|
331
|
-
return nil
|
|
332
|
-
}
|
|
333
|
-
return SmsSubscription(
|
|
334
|
-
resourceId: resourceId,
|
|
335
|
-
phone: phone,
|
|
336
|
-
status: status,
|
|
337
|
-
priority: priority,
|
|
338
|
-
customFields: customFields,
|
|
339
|
-
cats: cats
|
|
340
|
-
)
|
|
341
|
-
|
|
342
|
-
case "push":
|
|
343
|
-
let provider = stringFromAny(dict["provider"])
|
|
344
|
-
let subscriptionId =
|
|
345
|
-
stringFromAny(dict["subscription_id"]) ??
|
|
346
|
-
stringFromAny(dict["subscriptionId"])
|
|
347
|
-
guard let provider, let subscriptionId else {
|
|
348
|
-
return nil
|
|
349
|
-
}
|
|
350
|
-
return PushSubscription(
|
|
351
|
-
resourceId: resourceId,
|
|
352
|
-
provider: provider,
|
|
353
|
-
subscriptionId: subscriptionId,
|
|
354
|
-
status: status,
|
|
355
|
-
priority: priority,
|
|
356
|
-
customFields: customFields,
|
|
357
|
-
cats: cats
|
|
358
|
-
)
|
|
359
|
-
|
|
360
|
-
case "cc_data":
|
|
361
|
-
guard let channel = stringFromAny(
|
|
362
|
-
dict["channel"]
|
|
363
|
-
), !channel.isEmpty else {
|
|
364
|
-
return nil
|
|
365
|
-
}
|
|
366
|
-
guard let ccData = jsonValueDictFromAny(
|
|
367
|
-
dict["cc_data"]
|
|
368
|
-
) ?? jsonValueDictFromAny(
|
|
369
|
-
dict["ccData"]
|
|
370
|
-
) else {
|
|
371
|
-
return nil
|
|
372
|
-
}
|
|
373
|
-
return CcDataSubscription(
|
|
374
|
-
resourceId: resourceId,
|
|
375
|
-
channel: channel,
|
|
376
|
-
ccData: ccData,
|
|
377
|
-
status: status,
|
|
378
|
-
priority: priority,
|
|
379
|
-
customFields: customFields,
|
|
380
|
-
cats: cats
|
|
381
|
-
)
|
|
382
|
-
|
|
383
|
-
default:
|
|
384
|
-
return nil
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
/// Parses a JSON string into a `[String: JSONValue]` dictionary.
|
|
389
|
-
///
|
|
390
|
-
/// - Parameter jsonString: JSON string representing an object.
|
|
391
|
-
/// - Returns: A JSONValue dictionary or `nil` if parsing fails or the root is not an object.
|
|
392
|
-
static func jsonStringToJSONValueDict(_ jsonString: String) -> [String: JSONValue]? {
|
|
393
|
-
guard let data = jsonString.data(using: .utf8) else { return nil }
|
|
394
|
-
|
|
395
|
-
do {
|
|
396
|
-
let obj = try JSONSerialization.jsonObject(with: data, options: [])
|
|
397
|
-
guard let dict = obj as? [String: Any] else { return nil }
|
|
398
|
-
|
|
399
|
-
var out: [String: JSONValue] = [:]
|
|
400
|
-
out.reserveCapacity(dict.count)
|
|
401
|
-
|
|
402
|
-
for (k, v) in dict {
|
|
403
|
-
out[k] = anyToJSONValuePublic(v)
|
|
404
|
-
}
|
|
405
|
-
return out
|
|
406
|
-
} catch {
|
|
407
|
-
return nil
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
/// Parses a JSON string into a `[String]` array.
|
|
412
|
-
///
|
|
413
|
-
/// - Parameter jsonString: JSON string representing an array.
|
|
414
|
-
/// - Returns: A string array or `nil` if parsing fails or elements are not strings.
|
|
415
|
-
static func jsonStringToStringArray(_ jsonString: String) -> [String]? {
|
|
416
|
-
guard let data = jsonString.data(using: .utf8) else { return nil }
|
|
417
|
-
|
|
418
|
-
do {
|
|
419
|
-
let obj = try JSONSerialization.jsonObject(with: data, options: [])
|
|
420
|
-
guard let array = obj as? [Any] else { return nil }
|
|
421
|
-
if array.isEmpty { return [] }
|
|
422
|
-
return array.compactMap { $0 as? String }
|
|
423
|
-
} catch {
|
|
424
|
-
return nil
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
/// Converts an arbitrary Swift value into a `JSONValue` suitable for SDK APIs.
|
|
429
|
-
///
|
|
430
|
-
/// - Parameter value: Input value.
|
|
431
|
-
/// - Returns: Converted `JSONValue`.
|
|
432
|
-
static func anyToJSONValuePublic(_ value: Any) -> JSONValue {
|
|
433
|
-
if value is NSNull { return .null }
|
|
434
|
-
if let s = value as? String { return .string(s) }
|
|
435
|
-
if let b = value as? Bool { return .bool(b) }
|
|
436
|
-
|
|
437
|
-
if let n = value as? NSNumber {
|
|
438
|
-
if isCFBoolean(n) { return .bool(n.boolValue) }
|
|
439
|
-
return .number(n.doubleValue)
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
if let dict = value as? [String: Any] {
|
|
443
|
-
var out: [String: JSONValue] = [:]
|
|
444
|
-
out.reserveCapacity(dict.count)
|
|
445
|
-
for (k, v) in dict { out[k] = anyToJSONValuePublic(v) }
|
|
446
|
-
return .object(out)
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
if let dict = value as? [String: Any?] {
|
|
450
|
-
var out: [String: JSONValue] = [:]
|
|
451
|
-
out.reserveCapacity(dict.count)
|
|
452
|
-
for (k, v) in dict {
|
|
453
|
-
if let v { out[k] = anyToJSONValuePublic(v) }
|
|
454
|
-
else { out[k] = .null }
|
|
455
|
-
}
|
|
456
|
-
return .object(out)
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
if let arr = value as? [Any] {
|
|
460
|
-
var out: [JSONValue] = []
|
|
461
|
-
out.reserveCapacity(arr.count)
|
|
462
|
-
for v in arr { out.append(anyToJSONValuePublic(v)) }
|
|
463
|
-
return .array(out)
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
if let arr = value as? NSArray {
|
|
467
|
-
var out: [JSONValue] = []
|
|
468
|
-
out.reserveCapacity(arr.count)
|
|
469
|
-
for v in arr { out.append(anyToJSONValuePublic(v)) }
|
|
470
|
-
return .array(out)
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
return .string(String(describing: value))
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
/// Attempts to convert a value into `Int`.
|
|
477
|
-
///
|
|
478
|
-
/// Supports `Int`, `NSNumber`, and numeric `String` values. Returns `nil` for `nil`/`NSNull` or
|
|
479
|
-
/// non-numeric inputs.
|
|
480
|
-
private static func intFromAny(_ any: Any?) -> Int? {
|
|
481
|
-
guard let any else { return nil }
|
|
482
|
-
if any is NSNull { return nil }
|
|
483
|
-
if let i = any as? Int { return i }
|
|
484
|
-
if let n = any as? NSNumber { return n.intValue }
|
|
485
|
-
if let s = any as? String {
|
|
486
|
-
let t = s.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
487
|
-
if t.isEmpty { return nil }
|
|
488
|
-
return Int(t)
|
|
489
|
-
}
|
|
490
|
-
return nil
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
/// Converts a value into `String` if possible.
|
|
494
|
-
///
|
|
495
|
-
/// Supports `String`, `NSNumber`, and falls back to `String(describing:)`. Returns `nil` for
|
|
496
|
-
/// `nil`/`NSNull`.
|
|
497
|
-
private static func stringFromAny(_ any: Any?) -> String? {
|
|
498
|
-
guard let any else { return nil }
|
|
499
|
-
if any is NSNull { return nil }
|
|
500
|
-
if let s = any as? String { return s }
|
|
501
|
-
if let n = any as? NSNumber { return n.stringValue }
|
|
502
|
-
return String(describing: any)
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
/// Converts a value into a `[String: JSONValue]` dictionary.
|
|
506
|
-
///
|
|
507
|
-
/// Accepts a JSON object string, `NSDictionary`, `[String: Any]`, or `[String: Any?]`. Returns `nil`
|
|
508
|
-
/// for `nil`/`NSNull` or incompatible inputs.
|
|
509
|
-
private static func jsonValueDictFromAny(_ any: Any?) -> [String: JSONValue]? {
|
|
510
|
-
guard let any else { return nil }
|
|
511
|
-
if any is NSNull { return nil }
|
|
512
|
-
|
|
513
|
-
if let s = any as? String {
|
|
514
|
-
return jsonStringToJSONValueDict(s)
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
if let d = any as? NSDictionary {
|
|
518
|
-
let dict = toAnyDict(d) ?? [:]
|
|
519
|
-
var out: [String: JSONValue] = [:]
|
|
520
|
-
out.reserveCapacity(dict.count)
|
|
521
|
-
for (k, v) in dict {
|
|
522
|
-
if let v { out[k] = anyToJSONValuePublic(v) }
|
|
523
|
-
else { out[k] = .null }
|
|
524
|
-
}
|
|
525
|
-
return out
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
if let d = any as? [String: Any] {
|
|
529
|
-
var out: [String: JSONValue] = [:]
|
|
530
|
-
out.reserveCapacity(d.count)
|
|
531
|
-
for (k, v) in d {
|
|
532
|
-
out[k] = anyToJSONValuePublic(v)
|
|
533
|
-
}
|
|
534
|
-
return out
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
if let d = any as? [String: Any?] {
|
|
538
|
-
var out: [String: JSONValue] = [:]
|
|
539
|
-
out.reserveCapacity(d.count)
|
|
540
|
-
for (k, v) in d {
|
|
541
|
-
if let v { out[k] = anyToJSONValuePublic(v) }
|
|
542
|
-
else { out[k] = .null }
|
|
543
|
-
}
|
|
544
|
-
return out
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
return nil
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
/// Converts a value into an array of `String`.
|
|
551
|
-
///
|
|
552
|
-
/// Accepts a JSON array string, `[Any]`, or `NSArray`. Returns `nil` for `nil`/`NSNull` or
|
|
553
|
-
/// incompatible inputs. Non-string elements are ignored.
|
|
554
|
-
private static func stringArrayFromAny(_ any: Any?) -> [String]? {
|
|
555
|
-
guard let any else { return nil }
|
|
556
|
-
if any is NSNull { return nil }
|
|
557
|
-
|
|
558
|
-
if let s = any as? String {
|
|
559
|
-
return jsonStringToStringArray(s)
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
if let arr = any as? [Any] {
|
|
563
|
-
if arr.isEmpty { return [] }
|
|
564
|
-
return arr.compactMap { $0 as? String }
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
if let arr = any as? NSArray {
|
|
568
|
-
if arr.count == 0 { return [] }
|
|
569
|
-
return arr.compactMap { $0 as? String }
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
return nil
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
/// Normalizes a value into a `[String: Any]` map.
|
|
576
|
-
///
|
|
577
|
-
/// Accepts `[String: Any]`, `[String: Any?]` (dropping `nil` values), or `NSDictionary`. Returns
|
|
578
|
-
/// `nil` for `nil`/`NSNull` or incompatible inputs.
|
|
579
|
-
private static func normalizeStringAnyMap(_ any: Any?) -> [String: Any]? {
|
|
580
|
-
guard let any else { return nil }
|
|
581
|
-
if any is NSNull { return nil }
|
|
582
|
-
|
|
583
|
-
if let m = any as? [String: Any] { return m }
|
|
584
|
-
|
|
585
|
-
if let mOpt = any as? [String: Any?] {
|
|
586
|
-
var out: [String: Any] = [:]
|
|
587
|
-
out.reserveCapacity(mOpt.count)
|
|
588
|
-
for (k, v) in mOpt { if let v { out[k] = v } }
|
|
589
|
-
return out.isEmpty ? nil : out
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
if let ns = any as? NSDictionary {
|
|
593
|
-
return ns as? [String: Any]
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
return nil
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
/// Returns `true` if the given `NSNumber` represents a CoreFoundation boolean.
|
|
600
|
-
private static func isCFBoolean(_ n: NSNumber) -> Bool {
|
|
601
|
-
CFGetTypeID(n) == CFBooleanGetTypeID()
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
/// Normalizes `NSNumber` into `Int`, `Int64`, or `Double`.
|
|
605
|
-
///
|
|
606
|
-
/// If the value has a fractional part, returns `Double`. If it is an integer, returns `Int` when it
|
|
607
|
-
/// fits, otherwise `Int64`.
|
|
608
|
-
private static func normalizeNumber(_ n: NSNumber) -> Any {
|
|
609
|
-
let d = n.doubleValue
|
|
610
|
-
if !d.isFinite { return d }
|
|
611
|
-
|
|
612
|
-
let asInt64 = Int64(d)
|
|
613
|
-
if d != Double(asInt64) { return d }
|
|
614
|
-
|
|
615
|
-
if asInt64 >= Int64(Int.min) && asInt64 <= Int64(Int.max) {
|
|
616
|
-
return Int(asInt64)
|
|
617
|
-
} else {
|
|
618
|
-
return asInt64
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
/// Parses a string that looks like JSON (`{...}` or `[...]`) and normalizes the result.
|
|
623
|
-
///
|
|
624
|
-
/// If parsing fails, returns the original string.
|
|
625
|
-
private static func parseJSONIfPossible(_ s: String) -> Any {
|
|
626
|
-
let trimmed = s.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
627
|
-
if trimmed.isEmpty { return "" }
|
|
628
|
-
|
|
629
|
-
guard let first = trimmed.first, first == "{" || first == "[" else {
|
|
630
|
-
return s
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
guard let data = trimmed.data(using: .utf8) else { return s }
|
|
634
|
-
guard let obj = try? JSONSerialization.jsonObject(
|
|
635
|
-
with: data, options: []
|
|
636
|
-
) else { return s }
|
|
637
|
-
|
|
638
|
-
return toAny(obj) ?? s
|
|
639
|
-
}
|
|
640
|
-
}
|
package/ios/Sdk.h
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#ifdef __OBJC__
|
|
4
|
-
|
|
5
|
-
#import <Foundation/Foundation.h>
|
|
6
|
-
#import <React/RCTBridgeModule.h>
|
|
7
|
-
|
|
8
|
-
#ifdef RCT_NEW_ARCH_ENABLED
|
|
9
|
-
#import <Sdk/Sdk.h>
|
|
10
|
-
#endif
|
|
11
|
-
|
|
12
|
-
NS_EXTENSION_UNAVAILABLE_IOS("Altcraft RN bridge is unavailable in app extensions.")
|
|
13
|
-
#ifdef RCT_NEW_ARCH_ENABLED
|
|
14
|
-
@interface Sdk : NSObject <NativeSdkSpec>
|
|
15
|
-
#else
|
|
16
|
-
@interface Sdk : NSObject
|
|
17
|
-
#endif
|
|
18
|
-
@end
|
|
19
|
-
|
|
20
|
-
#endif // __OBJC__
|