@datadog/mobile-react-native 3.0.3 → 3.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 (69) hide show
  1. package/DatadogSDKReactNative.podspec +10 -9
  2. package/android/build.gradle +10 -8
  3. package/android/src/main/kotlin/com/datadog/reactnative/DdFlagsImplementation.kt +237 -0
  4. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkBridgeExt.kt +262 -78
  5. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkReactNativePackage.kt +3 -1
  6. package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
  7. package/android/src/newarch/kotlin/com/datadog/reactnative/DdFlags.kt +66 -0
  8. package/android/src/oldarch/kotlin/com/datadog/reactnative/DdFlags.kt +63 -0
  9. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkBridgeExtTest.kt +242 -0
  10. package/android/src/test/kotlin/com/datadog/tools/unit/MapExt.kt +38 -0
  11. package/android/src/test/kotlin/com/datadog/tools/unit/MockRumMonitor.kt +3 -0
  12. package/ios/Sources/DatadogSDKReactNative.h +2 -0
  13. package/ios/Sources/DdFlags.h +24 -0
  14. package/ios/Sources/DdFlags.mm +83 -0
  15. package/ios/Sources/DdFlagsImplementation.swift +263 -0
  16. package/ios/Sources/RNDdSdkConfiguration.swift +2 -1
  17. package/ios/Sources/SdkVersion.swift +1 -1
  18. package/lib/commonjs/flags/DdFlags.js +51 -0
  19. package/lib/commonjs/flags/DdFlags.js.map +1 -0
  20. package/lib/commonjs/flags/FlagsClient.js +218 -0
  21. package/lib/commonjs/flags/FlagsClient.js.map +1 -0
  22. package/lib/commonjs/flags/internal.js +40 -0
  23. package/lib/commonjs/flags/internal.js.map +1 -0
  24. package/lib/commonjs/flags/types.js +6 -0
  25. package/lib/commonjs/flags/types.js.map +1 -0
  26. package/lib/commonjs/index.js +7 -0
  27. package/lib/commonjs/index.js.map +1 -1
  28. package/lib/commonjs/specs/NativeDdFlags.js +19 -0
  29. package/lib/commonjs/specs/NativeDdFlags.js.map +1 -0
  30. package/lib/commonjs/version.js +1 -1
  31. package/lib/module/flags/DdFlags.js +45 -0
  32. package/lib/module/flags/DdFlags.js.map +1 -0
  33. package/lib/module/flags/FlagsClient.js +211 -0
  34. package/lib/module/flags/FlagsClient.js.map +1 -0
  35. package/lib/module/flags/internal.js +33 -0
  36. package/lib/module/flags/internal.js.map +1 -0
  37. package/lib/module/flags/types.js +2 -0
  38. package/lib/module/flags/types.js.map +1 -0
  39. package/lib/module/index.js +2 -1
  40. package/lib/module/index.js.map +1 -1
  41. package/lib/module/specs/NativeDdFlags.js +17 -0
  42. package/lib/module/specs/NativeDdFlags.js.map +1 -0
  43. package/lib/module/version.js +1 -1
  44. package/lib/typescript/flags/DdFlags.d.ts +3 -0
  45. package/lib/typescript/flags/DdFlags.d.ts.map +1 -0
  46. package/lib/typescript/flags/FlagsClient.d.ts +114 -0
  47. package/lib/typescript/flags/FlagsClient.d.ts.map +1 -0
  48. package/lib/typescript/flags/internal.d.ts +14 -0
  49. package/lib/typescript/flags/internal.d.ts.map +1 -0
  50. package/lib/typescript/flags/types.d.ts +202 -0
  51. package/lib/typescript/flags/types.d.ts.map +1 -0
  52. package/lib/typescript/index.d.ts +5 -2
  53. package/lib/typescript/index.d.ts.map +1 -1
  54. package/lib/typescript/nativeModulesTypes.d.ts +5 -0
  55. package/lib/typescript/nativeModulesTypes.d.ts.map +1 -1
  56. package/lib/typescript/specs/NativeDdFlags.d.ts +15 -0
  57. package/lib/typescript/specs/NativeDdFlags.d.ts.map +1 -0
  58. package/lib/typescript/version.d.ts +1 -1
  59. package/package.json +2 -2
  60. package/src/flags/DdFlags.ts +59 -0
  61. package/src/flags/FlagsClient.ts +266 -0
  62. package/src/flags/__tests__/DdFlags.test.ts +65 -0
  63. package/src/flags/__tests__/FlagsClient.test.ts +320 -0
  64. package/src/flags/internal.ts +63 -0
  65. package/src/flags/types.ts +215 -0
  66. package/src/index.tsx +15 -1
  67. package/src/nativeModulesTypes.ts +6 -0
  68. package/src/specs/NativeDdFlags.ts +35 -0
  69. package/src/version.ts +1 -1
@@ -9,11 +9,14 @@ package com.datadog.reactnative
9
9
  import com.datadog.tools.unit.keys
10
10
  import com.datadog.tools.unit.toReadableArray
11
11
  import com.datadog.tools.unit.toReadableMap
12
+ import com.datadog.tools.unit.toReadableMapDeep
12
13
  import com.facebook.react.bridge.JavaOnlyArray
13
14
  import com.facebook.react.bridge.JavaOnlyMap
14
15
  import com.facebook.react.bridge.WritableArray
15
16
  import com.facebook.react.bridge.WritableMap
16
17
  import org.assertj.core.api.Assertions.assertThat
18
+ import org.json.JSONArray
19
+ import org.json.JSONObject
17
20
  import org.junit.jupiter.api.Test
18
21
 
19
22
  internal class DdSdkBridgeExtTest {
@@ -276,6 +279,245 @@ internal class DdSdkBridgeExtTest {
276
279
  assertThat(value).isNull()
277
280
  }
278
281
 
282
+ @Test
283
+ fun `M do a proper conversion W JSONObject toMap { with raw types }`() {
284
+ // Given
285
+ val jsonObject = JSONObject().apply {
286
+ put("null", JSONObject.NULL)
287
+ put("int", 1)
288
+ put("long", 2L)
289
+ put("double", 3.0)
290
+ put("string", "test")
291
+ put("boolean", true)
292
+ }
293
+
294
+ // When
295
+ val map = jsonObject.toMap()
296
+
297
+ // Then
298
+ assertThat(map).hasSize(6)
299
+ assertThat(map["null"]).isNull()
300
+ assertThat(map["int"]).isEqualTo(1)
301
+ assertThat(map["long"]).isEqualTo(2L)
302
+ assertThat(map["double"]).isEqualTo(3.0)
303
+ assertThat(map["string"]).isEqualTo("test")
304
+ assertThat(map["boolean"]).isEqualTo(true)
305
+ }
306
+
307
+ @Test
308
+ fun `M do a proper conversion W JSONObject toMap { with nested objects }`() {
309
+ // Given
310
+ val nestedObject = JSONObject().apply {
311
+ put("nestedKey", "nestedValue")
312
+ }
313
+ val nestedArray = JSONArray().apply {
314
+ put("item1")
315
+ put("item2")
316
+ }
317
+ val jsonObject = JSONObject().apply {
318
+ put("object", nestedObject)
319
+ put("array", nestedArray)
320
+ }
321
+
322
+ // When
323
+ val map = jsonObject.toMap()
324
+
325
+ // Then
326
+ assertThat(map).hasSize(2)
327
+ assertThat(map["object"]).isInstanceOf(Map::class.java)
328
+ assertThat((map["object"] as Map<*, *>)["nestedKey"]).isEqualTo("nestedValue")
329
+ assertThat(map["array"]).isInstanceOf(List::class.java)
330
+ assertThat((map["array"] as List<*>)).hasSize(2)
331
+ assertThat((map["array"] as List<*>)[0]).isEqualTo("item1")
332
+ assertThat((map["array"] as List<*>)[1]).isEqualTo("item2")
333
+ }
334
+
335
+ @Test
336
+ fun `M do a proper conversion W JSONObject toWritableMap { with raw types }`() {
337
+ // Given
338
+ val jsonObject = JSONObject().apply {
339
+ put("int", 1)
340
+ put("double", 2.0)
341
+ put("string", "test")
342
+ put("boolean", true)
343
+ }
344
+
345
+ // When
346
+ // Use the parameterized version to avoid native library requirements in unit tests
347
+ val writableMap = jsonObject.toMap().toWritableMap(createWritableMap, createWritableArray)
348
+
349
+ // Then
350
+ assertThat(writableMap.getInt("int")).isEqualTo(1)
351
+ assertThat(writableMap.getDouble("double")).isEqualTo(2.0)
352
+ assertThat(writableMap.getString("string")).isEqualTo("test")
353
+ assertThat(writableMap.getBoolean("boolean")).isTrue()
354
+ }
355
+
356
+ @Test
357
+ fun `M do a proper conversion W JSONArray toList { with raw types }`() {
358
+ // Given
359
+ val jsonArray = JSONArray().apply {
360
+ put(JSONObject.NULL)
361
+ put(1)
362
+ put(2.0)
363
+ put("test")
364
+ put(true)
365
+ }
366
+
367
+ // When
368
+ val list = jsonArray.toList()
369
+
370
+ // Then
371
+ assertThat(list).hasSize(5)
372
+ assertThat(list[0]).isNull()
373
+ assertThat(list[1]).isEqualTo(1)
374
+ assertThat(list[2]).isEqualTo(2.0)
375
+ assertThat(list[3]).isEqualTo("test")
376
+ assertThat(list[4]).isEqualTo(true)
377
+ }
378
+
379
+ @Test
380
+ fun `M do a proper conversion W JSONArray toList { with nested objects }`() {
381
+ // Given
382
+ val nestedObject = JSONObject().apply {
383
+ put("key", "value")
384
+ }
385
+ val nestedArray = JSONArray().apply {
386
+ put("nested")
387
+ }
388
+ val jsonArray = JSONArray().apply {
389
+ put(nestedObject)
390
+ put(nestedArray)
391
+ }
392
+
393
+ // When
394
+ val list = jsonArray.toList()
395
+
396
+ // Then
397
+ assertThat(list).hasSize(2)
398
+ assertThat(list[0]).isInstanceOf(Map::class.java)
399
+ assertThat((list[0] as Map<*, *>)["key"]).isEqualTo("value")
400
+ assertThat(list[1]).isInstanceOf(List::class.java)
401
+ assertThat((list[1] as List<*>)[0]).isEqualTo("nested")
402
+ }
403
+
404
+ @Test
405
+ fun `M do a proper conversion W ReadableMap toJSONObject { with raw types }`() {
406
+ // Given
407
+ val readableMap = mapOf(
408
+ "int" to 1,
409
+ "double" to 2.0,
410
+ "string" to "test",
411
+ "boolean" to true
412
+ ).toReadableMapDeep()
413
+
414
+ // When
415
+ val jsonObject = readableMap.toJSONObject()
416
+
417
+ // Then
418
+ assertThat(jsonObject.length()).isEqualTo(4)
419
+ assertThat(jsonObject.getInt("int")).isEqualTo(1)
420
+ assertThat(jsonObject.getDouble("double")).isEqualTo(2.0)
421
+ assertThat(jsonObject.getString("string")).isEqualTo("test")
422
+ assertThat(jsonObject.getBoolean("boolean")).isTrue()
423
+ }
424
+
425
+ @Test
426
+ fun `M do a proper conversion W ReadableMap toJSONObject { with nested objects }`() {
427
+ // Given
428
+ val readableMap = mapOf(
429
+ "map" to mapOf("nestedKey" to "nestedValue"),
430
+ "list" to listOf("item1", "item2")
431
+ ).toReadableMapDeep()
432
+
433
+ // When
434
+ val jsonObject = readableMap.toJSONObject()
435
+
436
+ // Then
437
+ assertThat(jsonObject.length()).isEqualTo(2)
438
+ assertThat(jsonObject.getJSONObject("map").getString("nestedKey")).isEqualTo("nestedValue")
439
+ assertThat(jsonObject.getJSONArray("list").length()).isEqualTo(2)
440
+ assertThat(jsonObject.getJSONArray("list").getString(0)).isEqualTo("item1")
441
+ assertThat(jsonObject.getJSONArray("list").getString(1)).isEqualTo("item2")
442
+ }
443
+
444
+ @Test
445
+ fun `M do a proper conversion W Map toJSONObject { with raw types }`() {
446
+ // Given
447
+ val map: Map<String, Any> = mapOf(
448
+ "int" to 1,
449
+ "double" to 2.0,
450
+ "string" to "test",
451
+ "boolean" to true
452
+ )
453
+
454
+ // When
455
+ val jsonObject = map.toJSONObject()
456
+
457
+ // Then
458
+ assertThat(jsonObject.length()).isEqualTo(4)
459
+ assertThat(jsonObject.getInt("int")).isEqualTo(1)
460
+ assertThat(jsonObject.getDouble("double")).isEqualTo(2.0)
461
+ assertThat(jsonObject.getString("string")).isEqualTo("test")
462
+ assertThat(jsonObject.getBoolean("boolean")).isTrue()
463
+ }
464
+
465
+ @Test
466
+ fun `M do a proper conversion W Map toJSONObject { with nested objects }`() {
467
+ // Given
468
+ val map: Map<String, Any> = mapOf(
469
+ "nestedMap" to mapOf("key" to "value"),
470
+ "nestedList" to listOf(1, 2, 3)
471
+ )
472
+
473
+ // When
474
+ val jsonObject = map.toJSONObject()
475
+
476
+ // Then
477
+ assertThat(jsonObject.length()).isEqualTo(2)
478
+ assertThat(jsonObject.getJSONObject("nestedMap").getString("key")).isEqualTo("value")
479
+ assertThat(jsonObject.getJSONArray("nestedList").length()).isEqualTo(3)
480
+ assertThat(jsonObject.getJSONArray("nestedList").getInt(0)).isEqualTo(1)
481
+ assertThat(jsonObject.getJSONArray("nestedList").getInt(1)).isEqualTo(2)
482
+ assertThat(jsonObject.getJSONArray("nestedList").getInt(2)).isEqualTo(3)
483
+ }
484
+
485
+ @Test
486
+ fun `M do a proper conversion W List toJSONArray { with raw types }`() {
487
+ // Given
488
+ val list = listOf(null, 1, 2.0, "test", true)
489
+
490
+ // When
491
+ val jsonArray = list.toJSONArray()
492
+
493
+ // Then
494
+ assertThat(jsonArray.length()).isEqualTo(5)
495
+ assertThat(jsonArray.isNull(0)).isTrue()
496
+ assertThat(jsonArray.getInt(1)).isEqualTo(1)
497
+ assertThat(jsonArray.getDouble(2)).isEqualTo(2.0)
498
+ assertThat(jsonArray.getString(3)).isEqualTo("test")
499
+ assertThat(jsonArray.getBoolean(4)).isTrue()
500
+ }
501
+
502
+ @Test
503
+ fun `M do a proper conversion W List toJSONArray { with nested objects }`() {
504
+ // Given
505
+ val list = listOf(
506
+ mapOf("key" to "value"),
507
+ listOf("nested1", "nested2")
508
+ )
509
+
510
+ // When
511
+ val jsonArray = list.toJSONArray()
512
+
513
+ // Then
514
+ assertThat(jsonArray.length()).isEqualTo(2)
515
+ assertThat(jsonArray.getJSONObject(0).getString("key")).isEqualTo("value")
516
+ assertThat(jsonArray.getJSONArray(1).length()).isEqualTo(2)
517
+ assertThat(jsonArray.getJSONArray(1).getString(0)).isEqualTo("nested1")
518
+ assertThat(jsonArray.getJSONArray(1).getString(1)).isEqualTo("nested2")
519
+ }
520
+
279
521
  private fun getTestMap(): MutableMap<String, Any?> = mutableMapOf(
280
522
  "null" to null,
281
523
  "int" to 1,
@@ -25,6 +25,28 @@ fun Map<*, *>.toReadableMap(): ReadableMap {
25
25
  return JavaOnlyMap.of(*keysAndValues.toTypedArray())
26
26
  }
27
27
 
28
+ /**
29
+ * Recursively converts the [Map] to a [ReadableMap], including nested maps and lists.
30
+ */
31
+ fun Map<*, *>.toReadableMapDeep(): ReadableMap {
32
+ val keysAndValues = mutableListOf<Any?>()
33
+
34
+ entries.forEach {
35
+ keysAndValues.add(it.key)
36
+ keysAndValues.add(
37
+ when (val value = it.value) {
38
+ is ReadableMap -> value
39
+ is ReadableArray -> value
40
+ is Map<*, *> -> value.toReadableMapDeep()
41
+ is List<*> -> value.toReadableArrayDeep()
42
+ else -> value
43
+ }
44
+ )
45
+ }
46
+
47
+ return JavaOnlyMap.of(*keysAndValues.toTypedArray())
48
+ }
49
+
28
50
  fun Map<String, Set<TracingHeaderType>>.toFirstPartyHostsReadableArray(): ReadableArray {
29
51
  val list = mutableListOf<Any>()
30
52
 
@@ -48,6 +70,22 @@ fun List<*>.toReadableArray(): ReadableArray {
48
70
  return JavaOnlyArray.from(this)
49
71
  }
50
72
 
73
+ /**
74
+ * Recursively converts the [List] to a [ReadableArray], including nested maps and lists.
75
+ */
76
+ fun List<*>.toReadableArrayDeep(): ReadableArray {
77
+ val convertedList = this.map { value ->
78
+ when (value) {
79
+ is ReadableMap -> value
80
+ is ReadableArray -> value
81
+ is Map<*, *> -> value.toReadableMapDeep()
82
+ is List<*> -> value.toReadableArrayDeep()
83
+ else -> value
84
+ }
85
+ }
86
+ return JavaOnlyArray.from(convertedList)
87
+ }
88
+
51
89
  fun Set<*>.toReadableArray(): ReadableArray {
52
90
  // this FB implementation is not backed by Android-specific .so library, so ok for unit tests
53
91
  return JavaOnlyArray.from(this.toList())
@@ -147,4 +147,7 @@ class MockRumMonitor : RumMonitor {
147
147
  failureReason: FailureReason,
148
148
  attributes: Map<String, Any?>
149
149
  ) {}
150
+
151
+ @ExperimentalRumApi
152
+ override fun reportAppFullyDisplayed() {}
150
153
  }
@@ -6,3 +6,5 @@
6
6
 
7
7
  // This file is imported in the auto-generated DatadogSDKReactNative-Swift.h header file.
8
8
  // Deleting it could result in iOS builds failing.
9
+
10
+ #import <React/RCTBridgeModule.h>
@@ -0,0 +1,24 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ #import <Foundation/Foundation.h>
8
+ @class DdFlagsImplementation;
9
+
10
+ #ifdef RCT_NEW_ARCH_ENABLED
11
+
12
+ #import <DdSdkReactNative/DdSdkReactNative.h>
13
+ @interface DdFlags: NSObject <NativeDdFlagsSpec>
14
+
15
+ #else
16
+
17
+ #import <React/RCTBridgeModule.h>
18
+ @interface DdFlags : NSObject <RCTBridgeModule>
19
+
20
+ #endif
21
+
22
+ @property (nonatomic, strong) DdFlagsImplementation* ddFlagsImplementation;
23
+
24
+ @end
@@ -0,0 +1,83 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ // Import this first to prevent require cycles
8
+ #if __has_include("DatadogSDKReactNative-Swift.h")
9
+ #import <DatadogSDKReactNative-Swift.h>
10
+ #else
11
+ #import <DatadogSDKReactNative/DatadogSDKReactNative-Swift.h>
12
+ #endif
13
+ #import "DdFlags.h"
14
+
15
+
16
+ @implementation DdFlags
17
+
18
+ RCT_EXPORT_MODULE()
19
+
20
+ RCT_EXPORT_METHOD(enable:(NSDictionary *)configuration
21
+ withResolver:(RCTPromiseResolveBlock)resolve
22
+ withRejecter:(RCTPromiseRejectBlock)reject)
23
+ {
24
+ [self enable:configuration resolve:resolve reject:reject];
25
+ }
26
+
27
+ RCT_EXPORT_METHOD(setEvaluationContext:(NSString *)clientName
28
+ targetingKey:(NSString *)targetingKey
29
+ attributes:(NSDictionary *)attributes
30
+ withResolver:(RCTPromiseResolveBlock)resolve
31
+ withRejecter:(RCTPromiseRejectBlock)reject)
32
+ {
33
+ [self setEvaluationContext:clientName targetingKey:targetingKey attributes:attributes resolve:resolve reject:reject];
34
+ }
35
+
36
+ RCT_EXPORT_METHOD(trackEvaluation:(NSString *)clientName
37
+ withKey:(NSString *)key
38
+ withRawFlag:(NSDictionary *)rawFlag
39
+ targetingKey:(NSString *)targetingKey
40
+ attributes:(NSDictionary *)attributes
41
+ withResolver:(RCTPromiseResolveBlock)resolve
42
+ withRejecter:(RCTPromiseRejectBlock)reject)
43
+ {
44
+ [self trackEvaluation:clientName key:key rawFlag:rawFlag targetingKey:targetingKey attributes:attributes resolve:resolve reject:reject];
45
+ }
46
+
47
+ // Thanks to this guard, we won't compile this code when we build for the new architecture.
48
+ #ifdef RCT_NEW_ARCH_ENABLED
49
+ - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
50
+ (const facebook::react::ObjCTurboModule::InitParams &)params
51
+ {
52
+ return std::make_shared<facebook::react::NativeDdFlagsSpecJSI>(params);
53
+ }
54
+ #endif
55
+
56
+ - (DdFlagsImplementation*)ddFlagsImplementation
57
+ {
58
+ if (_ddFlagsImplementation == nil) {
59
+ _ddFlagsImplementation = [[DdFlagsImplementation alloc] init];
60
+ }
61
+ return _ddFlagsImplementation;
62
+ }
63
+
64
+ + (BOOL)requiresMainQueueSetup {
65
+ return NO;
66
+ }
67
+
68
+ - (dispatch_queue_t)methodQueue {
69
+ return [RNQueue getSharedQueue];
70
+ }
71
+
72
+ - (void)enable:(NSDictionary *)configuration resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
73
+ [self.ddFlagsImplementation enable:configuration resolve:resolve reject:reject];
74
+ }
75
+
76
+ - (void)setEvaluationContext:(NSString *)clientName targetingKey:(NSString *)targetingKey attributes:(NSDictionary *)attributes resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
77
+ [self.ddFlagsImplementation setEvaluationContext:clientName targetingKey:targetingKey attributes:attributes resolve:resolve reject:reject];
78
+ }
79
+
80
+ - (void)trackEvaluation:(NSString *)clientName key:(NSString *)key rawFlag:(NSDictionary *)rawFlag targetingKey:(NSString *)targetingKey attributes:(NSDictionary *)attributes resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
81
+ [self.ddFlagsImplementation trackEvaluation:clientName key:key rawFlag:rawFlag targetingKey:targetingKey attributes:attributes resolve:resolve reject:reject];
82
+ }
83
+ @end