@datadog/mobile-react-native-openfeature 3.7.0 → 3.9.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 (41) hide show
  1. package/README.md +187 -15
  2. package/lib/commonjs/index.js +7 -0
  3. package/lib/commonjs/index.js.map +1 -1
  4. package/lib/commonjs/provider.js +1 -1
  5. package/lib/commonjs/provider.js.map +1 -1
  6. package/lib/commonjs/rumContext.js +44 -0
  7. package/lib/commonjs/rumContext.js.map +1 -0
  8. package/lib/module/index.js +2 -1
  9. package/lib/module/index.js.map +1 -1
  10. package/lib/module/provider.js +1 -1
  11. package/lib/module/provider.js.map +1 -1
  12. package/lib/module/rumContext.js +37 -0
  13. package/lib/module/rumContext.js.map +1 -0
  14. package/lib/typescript/core/src/flags/internal.d.ts.map +1 -1
  15. package/lib/typescript/core/src/flags/rumIntegration.d.ts +14 -0
  16. package/lib/typescript/core/src/flags/rumIntegration.d.ts.map +1 -0
  17. package/lib/typescript/core/src/index.d.ts +2 -1
  18. package/lib/typescript/core/src/index.d.ts.map +1 -1
  19. package/lib/typescript/core/src/sdk/AttributesEncoding/defaultEncoders.d.ts.map +1 -1
  20. package/lib/typescript/core/src/sdk/AttributesEncoding/utils.d.ts +1 -0
  21. package/lib/typescript/core/src/sdk/AttributesEncoding/utils.d.ts.map +1 -1
  22. package/lib/typescript/core/src/sdk/DdSdkInternal.d.ts +1 -8
  23. package/lib/typescript/core/src/sdk/DdSdkInternal.d.ts.map +1 -1
  24. package/lib/typescript/core/src/utils/time-provider/TimeProvider.d.ts.map +1 -1
  25. package/lib/typescript/core/src/version.d.ts +1 -1
  26. package/lib/typescript/react-native-openfeature/src/index.d.ts +4 -2
  27. package/lib/typescript/react-native-openfeature/src/index.d.ts.map +1 -1
  28. package/lib/typescript/react-native-openfeature/src/provider.d.ts.map +1 -1
  29. package/lib/typescript/react-native-openfeature/src/rumContext.d.ts +23 -0
  30. package/lib/typescript/react-native-openfeature/src/rumContext.d.ts.map +1 -0
  31. package/package.json +3 -3
  32. package/src/__tests__/__utils__/rumContext.types.ts +55 -0
  33. package/src/__tests__/__utils__/tsconfig.json +8 -0
  34. package/src/__tests__/provider.test.ts +30 -0
  35. package/src/__tests__/rumContext.compatibility.test.ts +122 -0
  36. package/src/__tests__/rumContext.integration.test.ts +414 -0
  37. package/src/__tests__/rumContext.test.ts +100 -0
  38. package/src/__tests__/rumContext.types.test.ts +27 -0
  39. package/src/index.ts +4 -1
  40. package/src/provider.ts +5 -3
  41. package/src/rumContext.ts +54 -0
@@ -0,0 +1,100 @@
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 { UserInfoSingleton } from '../../../core/src/sdk/UserInfoSingleton/UserInfoSingleton';
8
+ import type { EnrichableEvaluationContext } from '../index';
9
+ import { enrichWithRumUser } from '../index';
10
+
11
+ describe('enrichWithRumUser', () => {
12
+ beforeEach(() => {
13
+ UserInfoSingleton.reset();
14
+ });
15
+
16
+ it('explicitly adds the current RUM user to application context', () => {
17
+ UserInfoSingleton.getInstance().setUserInfo({
18
+ id: 'rum-user',
19
+ name: 'RUM Name',
20
+ email: 'rum@example.com',
21
+ extraInfo: {
22
+ plan: 'pro',
23
+ profile: { tier: 'enterprise' }
24
+ }
25
+ });
26
+
27
+ expect(
28
+ enrichWithRumUser({
29
+ targetingKey: 'device-subject',
30
+ email: 'application@example.com',
31
+ region: 'us-east-1'
32
+ })
33
+ ).toStrictEqual({
34
+ targetingKey: 'device-subject',
35
+ name: 'RUM Name',
36
+ email: 'application@example.com',
37
+ plan: 'pro',
38
+ region: 'us-east-1'
39
+ });
40
+ });
41
+
42
+ it('uses undefined application fields as tombstones', () => {
43
+ UserInfoSingleton.getInstance().setUserInfo({
44
+ id: 'rum-user',
45
+ email: 'rum@example.com',
46
+ extraInfo: { plan: 'pro' }
47
+ });
48
+
49
+ expect(
50
+ enrichWithRumUser({
51
+ email: undefined,
52
+ plan: undefined
53
+ })
54
+ ).toStrictEqual({ targetingKey: 'rum-user' });
55
+ });
56
+
57
+ it.each([undefined, 'application-user'])(
58
+ 'uses an optional application targeting key of %s without falling back to the RUM user',
59
+ targetingKey => {
60
+ UserInfoSingleton.getInstance().setUserInfo({ id: 'rum-user' });
61
+ const context: EnrichableEvaluationContext = {
62
+ targetingKey,
63
+ region: 'us'
64
+ };
65
+
66
+ expect(enrichWithRumUser(context)).toStrictEqual(
67
+ targetingKey === undefined
68
+ ? { region: 'us' }
69
+ : { targetingKey, region: 'us' }
70
+ );
71
+ expect(context).toHaveProperty('targetingKey', targetingKey);
72
+ }
73
+ );
74
+
75
+ it('normalizes undefined fields when no RUM user is available', () => {
76
+ expect(
77
+ enrichWithRumUser({
78
+ targetingKey: 'application-subject',
79
+ email: undefined
80
+ })
81
+ ).toStrictEqual({ targetingKey: 'application-subject' });
82
+ });
83
+
84
+ it('reads the latest RUM user without mutating the application context', () => {
85
+ const applicationContext = { region: 'us-east-1' };
86
+
87
+ UserInfoSingleton.getInstance().setUserInfo({ id: 'rum-user-a' });
88
+ expect(enrichWithRumUser(applicationContext)).toStrictEqual({
89
+ targetingKey: 'rum-user-a',
90
+ region: 'us-east-1'
91
+ });
92
+
93
+ UserInfoSingleton.getInstance().setUserInfo({ id: 'rum-user-b' });
94
+ expect(enrichWithRumUser(applicationContext)).toStrictEqual({
95
+ targetingKey: 'rum-user-b',
96
+ region: 'us-east-1'
97
+ });
98
+ expect(applicationContext).toStrictEqual({ region: 'us-east-1' });
99
+ });
100
+ });
@@ -0,0 +1,27 @@
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 { spawnSync } from 'child_process';
8
+ import { resolve } from 'path';
9
+
10
+ it('typechecks RUM context inputs and outputs against the public API', () => {
11
+ // Run tsc separately so Jest's React Native transforms do not process the compiler itself.
12
+ const result = spawnSync(
13
+ process.execPath,
14
+ [
15
+ require.resolve('typescript/bin/tsc'),
16
+ '--project',
17
+ resolve(__dirname, '__utils__/tsconfig.json'),
18
+ '--pretty',
19
+ 'false'
20
+ ],
21
+ { encoding: 'utf8', timeout: 120000 }
22
+ );
23
+
24
+ expect(result.error).toBeUndefined();
25
+ expect(result.stdout + result.stderr).toBe('');
26
+ expect(result.status).toBe(0);
27
+ });
package/src/index.ts CHANGED
@@ -9,10 +9,13 @@ import { configurationFromString } from '@datadog/mobile-react-native';
9
9
  import { DatadogOfflineOpenFeatureProvider } from './offlineProvider';
10
10
  import { DatadogOpenFeatureProvider } from './provider';
11
11
  import type { DatadogOpenFeatureProviderOptions } from './provider';
12
+ import { enrichWithRumUser } from './rumContext';
13
+ import type { EnrichableEvaluationContext } from './rumContext';
12
14
 
13
15
  export {
14
16
  DatadogOpenFeatureProvider,
15
17
  DatadogOfflineOpenFeatureProvider,
18
+ enrichWithRumUser,
16
19
  configurationFromString
17
20
  };
18
- export type { DatadogOpenFeatureProviderOptions };
21
+ export type { DatadogOpenFeatureProviderOptions, EnrichableEvaluationContext };
package/src/provider.ts CHANGED
@@ -41,9 +41,11 @@ export class DatadogOpenFeatureProvider extends DatadogCoreOpenFeatureProvider {
41
41
  const newDdContext = toDdContext(newContext);
42
42
 
43
43
  // Promise chain in case `onContextChange` is called multiple times.
44
- this.contextChangePromise = this.contextChangePromise.then(() => {
45
- return this.flagsClient.setEvaluationContext(newDdContext);
46
- });
44
+ this.contextChangePromise = this.contextChangePromise
45
+ .catch(() => undefined)
46
+ .then(() => {
47
+ return this.flagsClient.setEvaluationContext(newDdContext);
48
+ });
47
49
 
48
50
  // Wait for the current context change to complete.
49
51
  await this.contextChangePromise;
@@ -0,0 +1,54 @@
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 * as DatadogSdk from '@datadog/mobile-react-native';
8
+ import type {
9
+ EvaluationContext,
10
+ EvaluationContextValue
11
+ } from '@openfeature/web-sdk';
12
+
13
+ /**
14
+ * An application context that permits top-level undefined values to remove RUM defaults.
15
+ * Unlike OpenFeature's EvaluationContext, this input can contain these explicit tombstones.
16
+ * If enrichment cannot run, the context is returned unchanged, including undefined values.
17
+ */
18
+ export type EnrichableEvaluationContext = {
19
+ targetingKey?: string | undefined;
20
+ } & Record<string, EvaluationContextValue | undefined>;
21
+
22
+ type RumContextEnricher = typeof DatadogSdk.__ddEnrichEvaluationContextWithRumUser;
23
+
24
+ /**
25
+ * Explicitly add the current RUM user to an OpenFeature evaluation context.
26
+ *
27
+ * The helper reads the RUM user each time it is called and returns a new context; it does not keep
28
+ * the OpenFeature context synchronized when the RUM user changes. The RUM user ID supplies the
29
+ * targeting key, while flat primitive user properties supply attributes. Application fields take
30
+ * precedence, and an explicitly undefined application field removes the corresponding RUM value
31
+ * from the returned context when enrichment succeeds.
32
+ *
33
+ * If the core SDK's enrichment helper is unavailable, logs a warning and returns the application
34
+ * context unchanged so OpenFeature initialization and evaluation can continue without RUM values.
35
+ */
36
+ export const enrichWithRumUser = (
37
+ context: EnrichableEvaluationContext
38
+ ): EvaluationContext => {
39
+ const enricher = (DatadogSdk as {
40
+ __ddEnrichEvaluationContextWithRumUser?: RumContextEnricher;
41
+ }).__ddEnrichEvaluationContextWithRumUser;
42
+
43
+ if (typeof enricher !== 'function') {
44
+ // InternalLog may also be absent from the core module, or have verbosity disabled.
45
+ // eslint-disable-next-line no-console
46
+ console.warn(
47
+ 'DATADOG: `enrichWithRumUser` could not access the core RUM enrichment helper. Returning the application context unchanged. Check SDK compatibility.'
48
+ );
49
+
50
+ return context as EvaluationContext;
51
+ }
52
+
53
+ return enricher(context) as EvaluationContext;
54
+ };