@datadog/mobile-react-native 3.0.2 → 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.
- package/DatadogSDKReactNative.podspec +10 -9
- package/android/build.gradle +11 -8
- package/android/src/main/kotlin/com/datadog/reactnative/DdFlagsImplementation.kt +237 -0
- package/android/src/main/kotlin/com/datadog/reactnative/DdSdkBridgeExt.kt +262 -78
- package/android/src/main/kotlin/com/datadog/reactnative/DdSdkImplementation.kt +1 -1
- package/android/src/main/kotlin/com/datadog/reactnative/DdSdkReactNativePackage.kt +3 -1
- package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
- package/android/src/newarch/kotlin/com/datadog/reactnative/DdFlags.kt +66 -0
- package/android/src/oldarch/kotlin/com/datadog/reactnative/DdFlags.kt +63 -0
- package/android/src/test/kotlin/com/datadog/reactnative/DdSdkBridgeExtTest.kt +242 -0
- package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +28 -0
- package/android/src/test/kotlin/com/datadog/tools/unit/MapExt.kt +38 -0
- package/android/src/test/kotlin/com/datadog/tools/unit/MockRumMonitor.kt +3 -0
- package/ios/Sources/DatadogSDKReactNative.h +2 -0
- package/ios/Sources/DdFlags.h +24 -0
- package/ios/Sources/DdFlags.mm +83 -0
- package/ios/Sources/DdFlagsImplementation.swift +263 -0
- package/ios/Sources/RNDdSdkConfiguration.swift +2 -1
- package/ios/Sources/SdkVersion.swift +1 -1
- package/lib/commonjs/flags/DdFlags.js +51 -0
- package/lib/commonjs/flags/DdFlags.js.map +1 -0
- package/lib/commonjs/flags/FlagsClient.js +218 -0
- package/lib/commonjs/flags/FlagsClient.js.map +1 -0
- package/lib/commonjs/flags/internal.js +40 -0
- package/lib/commonjs/flags/internal.js.map +1 -0
- package/lib/commonjs/flags/types.js +6 -0
- package/lib/commonjs/flags/types.js.map +1 -0
- package/lib/commonjs/index.js +7 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/specs/NativeDdFlags.js +19 -0
- package/lib/commonjs/specs/NativeDdFlags.js.map +1 -0
- package/lib/commonjs/version.js +1 -1
- package/lib/module/flags/DdFlags.js +45 -0
- package/lib/module/flags/DdFlags.js.map +1 -0
- package/lib/module/flags/FlagsClient.js +211 -0
- package/lib/module/flags/FlagsClient.js.map +1 -0
- package/lib/module/flags/internal.js +33 -0
- package/lib/module/flags/internal.js.map +1 -0
- package/lib/module/flags/types.js +2 -0
- package/lib/module/flags/types.js.map +1 -0
- package/lib/module/index.js +2 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/specs/NativeDdFlags.js +17 -0
- package/lib/module/specs/NativeDdFlags.js.map +1 -0
- package/lib/module/version.js +1 -1
- package/lib/typescript/flags/DdFlags.d.ts +3 -0
- package/lib/typescript/flags/DdFlags.d.ts.map +1 -0
- package/lib/typescript/flags/FlagsClient.d.ts +114 -0
- package/lib/typescript/flags/FlagsClient.d.ts.map +1 -0
- package/lib/typescript/flags/internal.d.ts +14 -0
- package/lib/typescript/flags/internal.d.ts.map +1 -0
- package/lib/typescript/flags/types.d.ts +202 -0
- package/lib/typescript/flags/types.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +5 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/nativeModulesTypes.d.ts +5 -0
- package/lib/typescript/nativeModulesTypes.d.ts.map +1 -1
- package/lib/typescript/specs/NativeDdFlags.d.ts +15 -0
- package/lib/typescript/specs/NativeDdFlags.d.ts.map +1 -0
- package/lib/typescript/version.d.ts +1 -1
- package/package.json +2 -2
- package/src/flags/DdFlags.ts +59 -0
- package/src/flags/FlagsClient.ts +266 -0
- package/src/flags/__tests__/DdFlags.test.ts +65 -0
- package/src/flags/__tests__/FlagsClient.test.ts +320 -0
- package/src/flags/internal.ts +63 -0
- package/src/flags/types.ts +215 -0
- package/src/index.tsx +15 -1
- package/src/nativeModulesTypes.ts +6 -0
- package/src/specs/NativeDdFlags.ts +35 -0
- package/src/version.ts +1 -1
|
@@ -0,0 +1,65 @@
|
|
|
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 { NativeModules } from 'react-native';
|
|
8
|
+
|
|
9
|
+
import { InternalLog } from '../../InternalLog';
|
|
10
|
+
import { SdkVerbosity } from '../../config/types/SdkVerbosity';
|
|
11
|
+
import { DdFlags } from '../DdFlags';
|
|
12
|
+
|
|
13
|
+
jest.mock('../../InternalLog', () => {
|
|
14
|
+
return {
|
|
15
|
+
InternalLog: {
|
|
16
|
+
log: jest.fn()
|
|
17
|
+
},
|
|
18
|
+
DATADOG_MESSAGE_PREFIX: 'DATADOG:'
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('DdFlags', () => {
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
jest.clearAllMocks();
|
|
25
|
+
// Reset state of DdFlags instance.
|
|
26
|
+
Object.assign(DdFlags, {
|
|
27
|
+
isFeatureEnabled: false,
|
|
28
|
+
clients: {}
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should always call the native enable method with enabled set to true', async () => {
|
|
33
|
+
await DdFlags.enable();
|
|
34
|
+
|
|
35
|
+
expect(NativeModules.DdFlags.enable).toHaveBeenCalledWith({
|
|
36
|
+
enabled: true
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should call the native enable method with the correct configuration', async () => {
|
|
41
|
+
await DdFlags.enable({
|
|
42
|
+
customExposureEndpoint: 'https://example.com',
|
|
43
|
+
customFlagsEndpoint: 'https://example.com',
|
|
44
|
+
trackExposures: false,
|
|
45
|
+
rumIntegrationEnabled: false
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
expect(NativeModules.DdFlags.enable).toHaveBeenCalledWith({
|
|
49
|
+
enabled: true,
|
|
50
|
+
customExposureEndpoint: 'https://example.com',
|
|
51
|
+
customFlagsEndpoint: 'https://example.com',
|
|
52
|
+
trackExposures: false,
|
|
53
|
+
rumIntegrationEnabled: false
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should print an error when trying to retrieve a client before DdFlags.enable() has been called', async () => {
|
|
58
|
+
DdFlags.getClient();
|
|
59
|
+
|
|
60
|
+
expect(InternalLog.log).toHaveBeenCalledWith(
|
|
61
|
+
'`DdFlags.getClient()` called before Datadog Flags feature have been enabled. Client will fall back to serving default flag values.',
|
|
62
|
+
SdkVerbosity.ERROR
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,320 @@
|
|
|
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 { NativeModules } from 'react-native';
|
|
8
|
+
|
|
9
|
+
import { InternalLog } from '../../InternalLog';
|
|
10
|
+
import { SdkVerbosity } from '../../config/types/SdkVerbosity';
|
|
11
|
+
import { DdFlags } from '../DdFlags';
|
|
12
|
+
|
|
13
|
+
jest.spyOn(NativeModules.DdFlags, 'setEvaluationContext').mockResolvedValue({
|
|
14
|
+
'test-boolean-flag': {
|
|
15
|
+
key: 'test-boolean-flag',
|
|
16
|
+
value: true,
|
|
17
|
+
allocationKey: '',
|
|
18
|
+
variationKey: 'true',
|
|
19
|
+
reason: 'STATIC',
|
|
20
|
+
doLog: true,
|
|
21
|
+
// Internal fields for Android.
|
|
22
|
+
variationType: '',
|
|
23
|
+
variationValue: '',
|
|
24
|
+
extraLogging: {}
|
|
25
|
+
},
|
|
26
|
+
'test-string-flag': {
|
|
27
|
+
key: 'test-string-flag',
|
|
28
|
+
value: 'hello world',
|
|
29
|
+
allocationKey: '',
|
|
30
|
+
variationKey: 'Hello World',
|
|
31
|
+
reason: 'STATIC',
|
|
32
|
+
doLog: true,
|
|
33
|
+
// Internal fields for Android.
|
|
34
|
+
variationType: '',
|
|
35
|
+
variationValue: '',
|
|
36
|
+
extraLogging: {}
|
|
37
|
+
},
|
|
38
|
+
'test-number-flag': {
|
|
39
|
+
key: 'test-number-flag',
|
|
40
|
+
value: 42,
|
|
41
|
+
allocationKey: '',
|
|
42
|
+
variationKey: '42',
|
|
43
|
+
reason: 'STATIC',
|
|
44
|
+
doLog: true,
|
|
45
|
+
// Internal fields for Android.
|
|
46
|
+
variationType: '',
|
|
47
|
+
variationValue: '',
|
|
48
|
+
extraLogging: {}
|
|
49
|
+
},
|
|
50
|
+
'test-object-flag': {
|
|
51
|
+
key: 'test-object-flag',
|
|
52
|
+
value: { greeting: 'Greeting from the native side!' },
|
|
53
|
+
allocationKey: '',
|
|
54
|
+
variationKey: 'Native Greeting',
|
|
55
|
+
reason: 'STATIC',
|
|
56
|
+
doLog: true,
|
|
57
|
+
// Internal fields for Android.
|
|
58
|
+
variationType: '',
|
|
59
|
+
variationValue: '',
|
|
60
|
+
extraLogging: {}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
jest.mock('../../InternalLog', () => {
|
|
65
|
+
return {
|
|
66
|
+
InternalLog: { log: jest.fn() },
|
|
67
|
+
DATADOG_MESSAGE_PREFIX: 'DATADOG:'
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('FlagsClient', () => {
|
|
72
|
+
beforeEach(async () => {
|
|
73
|
+
jest.clearAllMocks();
|
|
74
|
+
|
|
75
|
+
// Reset state of the global DdFlags instance.
|
|
76
|
+
Object.assign(DdFlags, {
|
|
77
|
+
isFeatureEnabled: false,
|
|
78
|
+
clients: {}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
await DdFlags.enable();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('setEvaluationContext', () => {
|
|
85
|
+
it('should set the evaluation context', async () => {
|
|
86
|
+
const flagsClient = DdFlags.getClient();
|
|
87
|
+
await flagsClient.setEvaluationContext({
|
|
88
|
+
targetingKey: 'test-user-1',
|
|
89
|
+
attributes: { country: 'US' }
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
expect(
|
|
93
|
+
NativeModules.DdFlags.setEvaluationContext
|
|
94
|
+
).toHaveBeenCalledWith('default', 'test-user-1', { country: 'US' });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should throw an error if there is an error setting the evaluation context', async () => {
|
|
98
|
+
NativeModules.DdFlags.setEvaluationContext.mockRejectedValueOnce(
|
|
99
|
+
new Error(
|
|
100
|
+
"A network error occurred while fetching feature flags for client 'default'."
|
|
101
|
+
)
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
const flagsClient = DdFlags.getClient();
|
|
105
|
+
|
|
106
|
+
await expect(
|
|
107
|
+
flagsClient.setEvaluationContext({
|
|
108
|
+
targetingKey: 'test-user-1',
|
|
109
|
+
attributes: { country: 'US' }
|
|
110
|
+
})
|
|
111
|
+
).rejects.toThrow(
|
|
112
|
+
"A network error occurred while fetching feature flags for client 'default'."
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
expect(InternalLog.log).toHaveBeenCalledWith(
|
|
116
|
+
"Error setting flag evaluation context: A network error occurred while fetching feature flags for client 'default'.",
|
|
117
|
+
SdkVerbosity.WARN
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('getDetails', () => {
|
|
123
|
+
it('should succesfully return flag details for flags', async () => {
|
|
124
|
+
// Flag values are mocked in the __mocks__/react-native.ts file.
|
|
125
|
+
const flagsClient = DdFlags.getClient();
|
|
126
|
+
await flagsClient.setEvaluationContext({
|
|
127
|
+
targetingKey: 'test-user-1',
|
|
128
|
+
attributes: { country: 'US' }
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const booleanDetails = flagsClient.getBooleanDetails(
|
|
132
|
+
'test-boolean-flag',
|
|
133
|
+
false
|
|
134
|
+
);
|
|
135
|
+
const stringDetails = flagsClient.getStringDetails(
|
|
136
|
+
'test-string-flag',
|
|
137
|
+
'Default value'
|
|
138
|
+
);
|
|
139
|
+
const numberDetails = flagsClient.getNumberDetails(
|
|
140
|
+
'test-number-flag',
|
|
141
|
+
-2
|
|
142
|
+
);
|
|
143
|
+
const objectDetails = flagsClient.getObjectDetails(
|
|
144
|
+
'test-object-flag',
|
|
145
|
+
{ greeting: 'Default value' }
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
expect(booleanDetails).toMatchObject({
|
|
149
|
+
value: true,
|
|
150
|
+
variant: 'true',
|
|
151
|
+
reason: 'STATIC'
|
|
152
|
+
});
|
|
153
|
+
expect(stringDetails).toMatchObject({
|
|
154
|
+
value: 'hello world',
|
|
155
|
+
variant: 'Hello World',
|
|
156
|
+
reason: 'STATIC'
|
|
157
|
+
});
|
|
158
|
+
expect(numberDetails).toMatchObject({
|
|
159
|
+
value: 42,
|
|
160
|
+
variant: '42',
|
|
161
|
+
reason: 'STATIC'
|
|
162
|
+
});
|
|
163
|
+
expect(objectDetails).toMatchObject({
|
|
164
|
+
value: { greeting: 'Greeting from the native side!' },
|
|
165
|
+
variant: 'Native Greeting',
|
|
166
|
+
reason: 'STATIC'
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should return PROVIDER_NOT_READY if evaluation context is not set', () => {
|
|
171
|
+
const flagsClient = DdFlags.getClient();
|
|
172
|
+
// Skip `setEvaluationContext` call here.
|
|
173
|
+
|
|
174
|
+
const details = flagsClient.getBooleanDetails(
|
|
175
|
+
'test-boolean-flag',
|
|
176
|
+
false
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
expect(details).toMatchObject({
|
|
180
|
+
value: false,
|
|
181
|
+
reason: 'ERROR',
|
|
182
|
+
errorCode: 'PROVIDER_NOT_READY',
|
|
183
|
+
errorMessage: expect.stringContaining(
|
|
184
|
+
'The evaluation context is not set'
|
|
185
|
+
)
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('should return FLAG_NOT_FOUND if flag is missing from context', async () => {
|
|
190
|
+
const flagsClient = DdFlags.getClient();
|
|
191
|
+
await flagsClient.setEvaluationContext({
|
|
192
|
+
targetingKey: 'test-user-1',
|
|
193
|
+
attributes: { country: 'US' }
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// 'unknown-flag' is not defined in the __mocks__/react-native.ts
|
|
197
|
+
const details = flagsClient.getBooleanDetails(
|
|
198
|
+
'unknown-flag',
|
|
199
|
+
false
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
expect(details).toMatchObject({
|
|
203
|
+
value: false,
|
|
204
|
+
reason: 'ERROR',
|
|
205
|
+
errorCode: 'FLAG_NOT_FOUND'
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('should return TYPE_MISMATCH when using wrong typed accessor method', async () => {
|
|
210
|
+
// Flag values are mocked in the __mocks__/react-native.ts file.
|
|
211
|
+
const flagsClient = DdFlags.getClient();
|
|
212
|
+
await flagsClient.setEvaluationContext({
|
|
213
|
+
targetingKey: 'test-user-1',
|
|
214
|
+
attributes: { country: 'US' }
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// Call getStringDetails on a boolean flag.
|
|
218
|
+
const booleanFlagAsString = flagsClient.getStringDetails(
|
|
219
|
+
'test-boolean-flag',
|
|
220
|
+
'default'
|
|
221
|
+
);
|
|
222
|
+
// Call getBooleanDetails on a string flag.
|
|
223
|
+
const stringFlagAsBoolean = flagsClient.getBooleanDetails(
|
|
224
|
+
'test-string-flag',
|
|
225
|
+
false
|
|
226
|
+
);
|
|
227
|
+
// Call getStringDetails on a number flag.
|
|
228
|
+
const numberFlagAsString = flagsClient.getStringDetails(
|
|
229
|
+
'test-number-flag',
|
|
230
|
+
'default'
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
expect(booleanFlagAsString).toMatchObject({
|
|
234
|
+
key: 'test-boolean-flag',
|
|
235
|
+
value: 'default',
|
|
236
|
+
errorCode: 'TYPE_MISMATCH',
|
|
237
|
+
reason: 'ERROR',
|
|
238
|
+
errorMessage: expect.stringContaining('boolean')
|
|
239
|
+
});
|
|
240
|
+
expect(stringFlagAsBoolean).toMatchObject({
|
|
241
|
+
key: 'test-string-flag',
|
|
242
|
+
value: false,
|
|
243
|
+
errorCode: 'TYPE_MISMATCH',
|
|
244
|
+
reason: 'ERROR',
|
|
245
|
+
errorMessage: expect.stringContaining('string')
|
|
246
|
+
});
|
|
247
|
+
expect(numberFlagAsString).toMatchObject({
|
|
248
|
+
key: 'test-number-flag',
|
|
249
|
+
value: 'default',
|
|
250
|
+
errorCode: 'TYPE_MISMATCH',
|
|
251
|
+
reason: 'ERROR',
|
|
252
|
+
errorMessage: expect.stringContaining('number')
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
describe('getValue', () => {
|
|
258
|
+
it('should succesfully return flag values', async () => {
|
|
259
|
+
// Flag values are mocked in the __mocks__/react-native.ts file.
|
|
260
|
+
const flagsClient = DdFlags.getClient();
|
|
261
|
+
await flagsClient.setEvaluationContext({
|
|
262
|
+
targetingKey: 'test-user-1',
|
|
263
|
+
attributes: { country: 'US' }
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
const booleanValue = flagsClient.getBooleanValue(
|
|
267
|
+
'test-boolean-flag',
|
|
268
|
+
false
|
|
269
|
+
);
|
|
270
|
+
const stringValue = flagsClient.getStringValue(
|
|
271
|
+
'test-string-flag',
|
|
272
|
+
'Default value'
|
|
273
|
+
);
|
|
274
|
+
const numberValue = flagsClient.getNumberValue(
|
|
275
|
+
'test-number-flag',
|
|
276
|
+
-2
|
|
277
|
+
);
|
|
278
|
+
const objectValue = flagsClient.getObjectValue('test-object-flag', {
|
|
279
|
+
greeting: 'Default value'
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
expect(booleanValue).toBe(true);
|
|
283
|
+
expect(stringValue).toBe('hello world');
|
|
284
|
+
expect(numberValue).toBe(42);
|
|
285
|
+
expect(objectValue).toStrictEqual({
|
|
286
|
+
greeting: 'Greeting from the native side!'
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('should return the default value when using wrong typed accessor method', async () => {
|
|
291
|
+
// Flag values are mocked in the __mocks__/react-native.ts file.
|
|
292
|
+
const flagsClient = DdFlags.getClient();
|
|
293
|
+
await flagsClient.setEvaluationContext({
|
|
294
|
+
targetingKey: 'test-user-1',
|
|
295
|
+
attributes: { country: 'US' }
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
// Call getStringValue on a boolean flag.
|
|
299
|
+
const booleanFlagAsString = flagsClient.getStringValue(
|
|
300
|
+
'test-boolean-flag',
|
|
301
|
+
'default'
|
|
302
|
+
);
|
|
303
|
+
// Call getBooleanValue on a string flag.
|
|
304
|
+
const stringFlagAsBoolean = flagsClient.getBooleanValue(
|
|
305
|
+
'test-string-flag',
|
|
306
|
+
false
|
|
307
|
+
);
|
|
308
|
+
// Call getStringValue on a number flag.
|
|
309
|
+
const numberFlagAsString = flagsClient.getStringValue(
|
|
310
|
+
'test-number-flag',
|
|
311
|
+
'default'
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
// The default value is returned due to TYPE_MISMATCH.
|
|
315
|
+
expect(booleanFlagAsString).toBe('default');
|
|
316
|
+
expect(stringFlagAsBoolean).toBe(false);
|
|
317
|
+
expect(numberFlagAsString).toBe('default');
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
});
|
|
@@ -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
|
};
|