@datadog/mobile-react-native 3.0.3 → 3.1.1

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 (93) 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/DdSdkConfiguration.kt +3 -3
  6. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfigurationExt.kt +18 -18
  7. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkNativeInitialization.kt +3 -2
  8. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkReactNativePackage.kt +3 -1
  9. package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
  10. package/android/src/newarch/kotlin/com/datadog/reactnative/DdFlags.kt +66 -0
  11. package/android/src/oldarch/kotlin/com/datadog/reactnative/DdFlags.kt +63 -0
  12. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkBridgeExtTest.kt +242 -0
  13. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkNativeInitializationTest.kt +4 -2
  14. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +6 -3
  15. package/android/src/test/kotlin/com/datadog/tools/unit/DdSdkConfigurationExt.kt +3 -4
  16. package/android/src/test/kotlin/com/datadog/tools/unit/MapExt.kt +38 -0
  17. package/android/src/test/kotlin/com/datadog/tools/unit/MockRumMonitor.kt +3 -0
  18. package/android/src/test/kotlin/com/datadog/tools/unit/forge/DdSdkConfigurationForgeryFactory.kt +1 -1
  19. package/android/src/test/resources/input/complete-configuration.json +10 -10
  20. package/ios/Sources/DatadogSDKReactNative.h +2 -0
  21. package/ios/Sources/DdFlags.h +24 -0
  22. package/ios/Sources/DdFlags.mm +83 -0
  23. package/ios/Sources/DdFlagsImplementation.swift +263 -0
  24. package/ios/Sources/DdSdkConfiguration.swift +4 -4
  25. package/ios/Sources/DdSdkNativeInitialization.swift +2 -2
  26. package/ios/Sources/RNDdSdkConfiguration.swift +11 -9
  27. package/ios/Sources/SdkVersion.swift +1 -1
  28. package/lib/commonjs/InternalLog.js +9 -7
  29. package/lib/commonjs/InternalLog.js.map +1 -1
  30. package/lib/commonjs/flags/DdFlags.js +51 -0
  31. package/lib/commonjs/flags/DdFlags.js.map +1 -0
  32. package/lib/commonjs/flags/FlagsClient.js +218 -0
  33. package/lib/commonjs/flags/FlagsClient.js.map +1 -0
  34. package/lib/commonjs/flags/internal.js +40 -0
  35. package/lib/commonjs/flags/internal.js.map +1 -0
  36. package/lib/commonjs/flags/types.js +6 -0
  37. package/lib/commonjs/flags/types.js.map +1 -0
  38. package/lib/commonjs/index.js +7 -0
  39. package/lib/commonjs/index.js.map +1 -1
  40. package/lib/commonjs/sdk/DatadogProvider/Buffer/BufferSingleton.js +10 -8
  41. package/lib/commonjs/sdk/DatadogProvider/Buffer/BufferSingleton.js.map +1 -1
  42. package/lib/commonjs/specs/NativeDdFlags.js +19 -0
  43. package/lib/commonjs/specs/NativeDdFlags.js.map +1 -0
  44. package/lib/commonjs/version.js +1 -1
  45. package/lib/module/InternalLog.js +9 -6
  46. package/lib/module/InternalLog.js.map +1 -1
  47. package/lib/module/flags/DdFlags.js +45 -0
  48. package/lib/module/flags/DdFlags.js.map +1 -0
  49. package/lib/module/flags/FlagsClient.js +211 -0
  50. package/lib/module/flags/FlagsClient.js.map +1 -0
  51. package/lib/module/flags/internal.js +33 -0
  52. package/lib/module/flags/internal.js.map +1 -0
  53. package/lib/module/flags/types.js +2 -0
  54. package/lib/module/flags/types.js.map +1 -0
  55. package/lib/module/index.js +2 -1
  56. package/lib/module/index.js.map +1 -1
  57. package/lib/module/sdk/DatadogProvider/Buffer/BufferSingleton.js +10 -7
  58. package/lib/module/sdk/DatadogProvider/Buffer/BufferSingleton.js.map +1 -1
  59. package/lib/module/specs/NativeDdFlags.js +17 -0
  60. package/lib/module/specs/NativeDdFlags.js.map +1 -0
  61. package/lib/module/version.js +1 -1
  62. package/lib/typescript/InternalLog.d.ts +6 -4
  63. package/lib/typescript/InternalLog.d.ts.map +1 -1
  64. package/lib/typescript/flags/DdFlags.d.ts +3 -0
  65. package/lib/typescript/flags/DdFlags.d.ts.map +1 -0
  66. package/lib/typescript/flags/FlagsClient.d.ts +114 -0
  67. package/lib/typescript/flags/FlagsClient.d.ts.map +1 -0
  68. package/lib/typescript/flags/internal.d.ts +14 -0
  69. package/lib/typescript/flags/internal.d.ts.map +1 -0
  70. package/lib/typescript/flags/types.d.ts +202 -0
  71. package/lib/typescript/flags/types.d.ts.map +1 -0
  72. package/lib/typescript/index.d.ts +5 -2
  73. package/lib/typescript/index.d.ts.map +1 -1
  74. package/lib/typescript/nativeModulesTypes.d.ts +5 -0
  75. package/lib/typescript/nativeModulesTypes.d.ts.map +1 -1
  76. package/lib/typescript/sdk/DatadogProvider/Buffer/BufferSingleton.d.ts +7 -5
  77. package/lib/typescript/sdk/DatadogProvider/Buffer/BufferSingleton.d.ts.map +1 -1
  78. package/lib/typescript/specs/NativeDdFlags.d.ts +15 -0
  79. package/lib/typescript/specs/NativeDdFlags.d.ts.map +1 -0
  80. package/lib/typescript/version.d.ts +1 -1
  81. package/package.json +2 -2
  82. package/src/InternalLog.tsx +14 -6
  83. package/src/flags/DdFlags.ts +59 -0
  84. package/src/flags/FlagsClient.ts +266 -0
  85. package/src/flags/__tests__/DdFlags.test.ts +65 -0
  86. package/src/flags/__tests__/FlagsClient.test.ts +320 -0
  87. package/src/flags/internal.ts +63 -0
  88. package/src/flags/types.ts +215 -0
  89. package/src/index.tsx +15 -1
  90. package/src/nativeModulesTypes.ts +6 -0
  91. package/src/sdk/DatadogProvider/Buffer/BufferSingleton.ts +16 -7
  92. package/src/specs/NativeDdFlags.ts +35 -0
  93. package/src/version.ts +1 -1
@@ -0,0 +1,63 @@
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 { InternalLog } from '../InternalLog';
8
+ import { SdkVerbosity } from '../config/types/SdkVerbosity';
9
+
10
+ import type { EvaluationContext, PrimitiveValue } from './types';
11
+
12
+ export interface FlagCacheEntry {
13
+ key: string;
14
+ value: unknown;
15
+ allocationKey: string;
16
+ variationKey: string;
17
+ variationType: string;
18
+ variationValue: string;
19
+ reason: string;
20
+ doLog: boolean;
21
+ extraLogging: Record<string, unknown>;
22
+ }
23
+
24
+ export const processEvaluationContext = (
25
+ context: EvaluationContext
26
+ ): EvaluationContext => {
27
+ const { targetingKey } = context;
28
+
29
+ // We should ignore non-primitive values in the context as per FFE SDK requirements OF.3.
30
+ const providedAttributes: Record<string, unknown> =
31
+ context.attributes ?? {};
32
+
33
+ const attributes: Record<string, PrimitiveValue> = {};
34
+
35
+ for (const [key, value] of Object.entries(providedAttributes)) {
36
+ const isPrimitiveValue =
37
+ typeof value === 'boolean' ||
38
+ typeof value === 'string' ||
39
+ typeof value === 'number' ||
40
+ value === undefined ||
41
+ value === null;
42
+
43
+ if (!isPrimitiveValue) {
44
+ InternalLog.log(
45
+ `Non-primitive context value under "${key}" is not supported. Omitting this attribute from the evaluation context.`,
46
+ SdkVerbosity.WARN
47
+ );
48
+
49
+ continue;
50
+ }
51
+
52
+ if (value === undefined) {
53
+ continue;
54
+ }
55
+
56
+ attributes[key] = value;
57
+ }
58
+
59
+ return {
60
+ targetingKey,
61
+ attributes
62
+ };
63
+ };
@@ -0,0 +1,215 @@
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 type { FlagsClient } from './FlagsClient';
8
+
9
+ export interface DdFlagsType {
10
+ /**
11
+ * Enables the Datadog Flags feature in your application.
12
+ *
13
+ * Call this method after initializing the Datadog SDK to enable feature flag evaluation.
14
+ * This method must be called before creating any `FlagsClient` instances via `DdFlags.getClient()`.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { DdSdkReactNativeConfiguration, DdSdkReactNative, DdFlags } from '@datadog/mobile-react-native';
19
+ *
20
+ * // Initialize the Datadog SDK.
21
+ * await DdSdkReactNative.initialize(...);
22
+ *
23
+ * // Optinal flags configuration object.
24
+ * const flagsConfig = {
25
+ * customFlagsEndpoint: 'https://flags.example.com'
26
+ * };
27
+ *
28
+ * // Enable the feature.
29
+ * await DdFlags.enable(flagsConfig);
30
+ *
31
+ * // Retrieve the client and access feature flags.
32
+ * const flagsClient = DdFlags.getClient();
33
+ * const flagValue = await flagsClient.getBooleanValue('new-feature', false);
34
+ * ```
35
+ *
36
+ * @param configuration Configuration options for the Datadog Flags feature.
37
+ */
38
+ enable: (configuration?: FlagsConfiguration) => Promise<void>;
39
+ /**
40
+ * Returns a `FlagsClient` instance for further feature flag evaluation.
41
+ *
42
+ * For most applications, you would need only one client. If you need multiple clients,
43
+ * you can retrieve a couple of clients with different names.
44
+ *
45
+ * @param clientName An optional name of the client to retrieve. Defaults to `'default'`.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * const flagsClient = DdFlags.getClient();
50
+ *
51
+ * // Set the evaluation context.
52
+ * await flagsClient.setEvaluationContext({
53
+ * targetingKey: 'user-123',
54
+ * attributes: {
55
+ * favoriteFruit: 'apple'
56
+ * }
57
+ * });
58
+ *
59
+ * const flagValue = flagsClient.getBooleanValue('new-feature', false);
60
+ * ```
61
+ */
62
+ getClient: (clientName?: string) => FlagsClient;
63
+ }
64
+
65
+ /**
66
+ * Configuration options for the Datadog Flags feature.
67
+ */
68
+ export interface FlagsConfiguration {
69
+ /**
70
+ * Custom server URL for retrieving flag assignments.
71
+ *
72
+ * The provided value should only include the base URL, and the endpoint will be appended automatically.
73
+ * For example, if you provide 'https://flags.example.com', the SDK will use 'https://flags.example.com/precompute-assignments'.
74
+ *
75
+ * If not set, the SDK uses the default Datadog Flags endpoint for the configured site.
76
+ *
77
+ * @default undefined
78
+ */
79
+ customFlagsEndpoint?: string;
80
+ /**
81
+ * Custom server URL for sending Flags exposure data.
82
+ *
83
+ * The provided value should only include the base URL, and the endpoint will be appended automatically.
84
+ * For example, if you provide 'https://flags.example.com', the SDK will use 'https://flags.example.com/api/v2/exposures'.
85
+ *
86
+ * If not set, the SDK uses the default Datadog Flags exposure endpoint.
87
+ *
88
+ * @default undefined
89
+ */
90
+ customExposureEndpoint?: string;
91
+ /**
92
+ * Enables exposure logging via the dedicated exposures intake endpoint.
93
+ *
94
+ * When enabled, flag evaluation events are sent to the exposures endpoint for analytics and monitoring.
95
+ *
96
+ * @default true
97
+ */
98
+ trackExposures?: boolean;
99
+ /**
100
+ * Enables the RUM integration.
101
+ *
102
+ * When enabled, flag evaluation events are sent to RUM for correlation with user sessions.
103
+ *
104
+ * @default true
105
+ */
106
+ rumIntegrationEnabled?: boolean;
107
+ }
108
+
109
+ export type PrimitiveValue = null | boolean | string | number;
110
+ type JsonObject = { [key: string]: JsonValue };
111
+ type JsonArray = JsonValue[];
112
+ /**
113
+ * Represents a JSON node value.
114
+ */
115
+ export type JsonValue = PrimitiveValue | JsonObject | JsonArray;
116
+
117
+ /**
118
+ * Context information used for feature flag targeting and evaluation.
119
+ *
120
+ * Contains user or session information that determines which flag variations are returned.
121
+ * This typically includes a unique identifier (targeting key) and optional custom attributes
122
+ * for granular targeting.
123
+ *
124
+ * Note: Evaluation context should be set before flag evaluations.
125
+ *
126
+ * @example
127
+ * ```ts
128
+ * const context: EvaluationContext = {
129
+ * targetingKey: "user-123",
130
+ * attributes: {
131
+ * "region": "US",
132
+ * "plan": "premium",
133
+ * "age": 25,
134
+ * "beta_tester": true
135
+ * }
136
+ * };
137
+ *
138
+ * await client.setEvaluationContext(context);
139
+ * ```
140
+ */
141
+ export interface EvaluationContext {
142
+ /**
143
+ * The unique identifier used for targeting this user or session.
144
+ *
145
+ * This is typically a user ID, session ID, or device ID. The targeting key is used
146
+ * by the feature flag service to determine which variation to serve.
147
+ *
148
+ * Pass an empty string if you don't have such an identifier.
149
+ */
150
+ targetingKey: string;
151
+
152
+ /**
153
+ * Custom attributes for more granular targeting.
154
+ *
155
+ * Attributes can include user properties, session data, or any other contextual information
156
+ * needed for flag evaluation rules.
157
+ *
158
+ * NOTE: Nested object values are not supported and will be dropped from the evaluation context.
159
+ */
160
+ attributes?: Record<string, PrimitiveValue>;
161
+ }
162
+
163
+ /**
164
+ * An error that occurs during feature flag evaluation.
165
+ *
166
+ * Indicates why a flag evaluation may have failed or returned a default value.
167
+ */
168
+ type FlagErrorCode =
169
+ | 'PROVIDER_NOT_READY'
170
+ | 'FLAG_NOT_FOUND'
171
+ | 'PARSE_ERROR'
172
+ | 'TYPE_MISMATCH';
173
+
174
+ /**
175
+ * Detailed information about a feature flag evaluation.
176
+ *
177
+ * Contains both the evaluated flag value and metadata about the evaluation,
178
+ * including the variant served, evaluation reason, and any errors that occurred.
179
+ */
180
+ export interface FlagDetails<T> {
181
+ /**
182
+ * The feature flag key that was evaluated.
183
+ */
184
+ key: string;
185
+ /**
186
+ * The evaluated flag value.
187
+ *
188
+ * Falls back to the default value if evaluation failed.
189
+ */
190
+ value: T;
191
+ /**
192
+ * The reason why this evaluation result was returned.
193
+ */
194
+ reason: string;
195
+ /**
196
+ * The variant key for the evaluated flag.
197
+ *
198
+ * Variants identify which version of the flag was served.
199
+ */
200
+ variant?: string;
201
+ /**
202
+ * The allocation key for the evaluated flag.
203
+ *
204
+ * Useful for debugging targeting rules.
205
+ */
206
+ allocationKey?: string;
207
+ /**
208
+ * Code of the error that occurred during evaluation, if any.
209
+ */
210
+ errorCode?: FlagErrorCode;
211
+ /**
212
+ * Detailed explanation of the occurred error, if any.
213
+ */
214
+ errorMessage?: string;
215
+ }
package/src/index.tsx CHANGED
@@ -30,6 +30,14 @@ import {
30
30
  UploadFrequency,
31
31
  VitalsUpdateFrequency
32
32
  } from './config/types';
33
+ import { DdFlags } from './flags/DdFlags';
34
+ import type { FlagsClient } from './flags/FlagsClient';
35
+ import type {
36
+ FlagsConfiguration,
37
+ FlagDetails,
38
+ EvaluationContext,
39
+ PrimitiveValue
40
+ } from './flags/types';
33
41
  import { DdLogs } from './logs/DdLogs';
34
42
  import { DdRum } from './rum/DdRum';
35
43
  import { DdBabelInteractionTracking } from './rum/instrumentation/interactionTracking/DdBabelInteractionTracking';
@@ -61,6 +69,7 @@ export {
61
69
  FileBasedConfiguration,
62
70
  InitializationMode,
63
71
  DdLogs,
72
+ DdFlags,
64
73
  DdTrace,
65
74
  DdRum,
66
75
  RumActionType,
@@ -102,5 +111,10 @@ export type {
102
111
  CoreConfigurationOptions,
103
112
  RumConfigurationOptions,
104
113
  LogsConfigurationOptions,
105
- TraceConfigurationOptions
114
+ TraceConfigurationOptions,
115
+ FlagsConfiguration,
116
+ FlagsClient,
117
+ EvaluationContext,
118
+ PrimitiveValue,
119
+ FlagDetails
106
120
  };
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  import type { DdSdkNativeConfiguration } from './config/features/CoreConfigurationNative';
8
+ import type { Spec as NativeDdFlags } from './specs/NativeDdFlags';
8
9
  import type { Spec as NativeDdLogs } from './specs/NativeDdLogs';
9
10
  import type { Spec as NativeDdRum } from './specs/NativeDdRum';
10
11
  import type { Spec as NativeDdSdk } from './specs/NativeDdSdk';
@@ -25,6 +26,11 @@ export type DdNativeLogsType = NativeDdLogs;
25
26
  */
26
27
  export type DdNativeTraceType = NativeDdTrace;
27
28
 
29
+ /**
30
+ * The entry point to use Datadog's Flags feature.
31
+ */
32
+ export type DdNativeFlagsType = NativeDdFlags;
33
+
28
34
  /**
29
35
  * The entry point to initialize Datadog's features.
30
36
  */
@@ -4,23 +4,32 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
+ import { getGlobalInstance } from '../../../utils/singletonUtils';
8
+
7
9
  import { BoundedBuffer } from './BoundedBuffer';
8
10
  import type { DatadogBuffer } from './DatadogBuffer';
9
11
  import { PassThroughBuffer } from './PassThroughBuffer';
10
12
 
11
- export class BufferSingleton {
12
- private static bufferInstance: DatadogBuffer = new BoundedBuffer();
13
+ const BUFFER_SINGLETON_MODULE = 'com.datadog.reactnative.buffer_singleton';
14
+
15
+ class _BufferSingleton {
16
+ private bufferInstance: DatadogBuffer = new BoundedBuffer();
13
17
 
14
- static getInstance = (): DatadogBuffer => {
18
+ getInstance = (): DatadogBuffer => {
15
19
  return BufferSingleton.bufferInstance;
16
20
  };
17
21
 
18
- static onInitialization = () => {
19
- BufferSingleton.bufferInstance.drain();
20
- BufferSingleton.bufferInstance = new PassThroughBuffer();
22
+ onInitialization = () => {
23
+ this.bufferInstance.drain();
24
+ this.bufferInstance = new PassThroughBuffer();
21
25
  };
22
26
 
23
- static reset = () => {
27
+ reset = () => {
24
28
  BufferSingleton.bufferInstance = new BoundedBuffer();
25
29
  };
26
30
  }
31
+
32
+ export const BufferSingleton = getGlobalInstance(
33
+ BUFFER_SINGLETON_MODULE,
34
+ () => new _BufferSingleton()
35
+ );
@@ -0,0 +1,35 @@
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
+ /* eslint-disable @typescript-eslint/ban-types */
8
+ import type { TurboModule } from 'react-native';
9
+ import { TurboModuleRegistry } from 'react-native';
10
+
11
+ import type { FlagCacheEntry } from '../flags/internal';
12
+
13
+ /**
14
+ * Do not import this Spec directly, use DdNativeFlagsType instead.
15
+ */
16
+ export interface Spec extends TurboModule {
17
+ readonly enable: (configuration: Object) => Promise<void>;
18
+
19
+ readonly setEvaluationContext: (
20
+ clientName: string,
21
+ targetingKey: string,
22
+ attributes: Object
23
+ ) => Promise<{ [key: string]: FlagCacheEntry }>;
24
+
25
+ readonly trackEvaluation: (
26
+ clientName: string,
27
+ key: string,
28
+ rawFlag: Object,
29
+ targetingKey: string,
30
+ attributes: Object
31
+ ) => Promise<void>;
32
+ }
33
+
34
+ // eslint-disable-next-line import/no-default-export
35
+ export default TurboModuleRegistry.get<Spec>('DdFlags');
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // generated by genversion
2
- export const version = '3.0.3';
2
+ export const version = '3.1.1';