launchdarkly-js-sdk-common 5.0.0-alpha.3 → 5.0.0-alpha.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "launchdarkly-js-sdk-common",
3
- "version": "5.0.0-alpha.3",
3
+ "version": "5.0.0-alpha.4",
4
4
  "description": "LaunchDarkly SDK for JavaScript - common code",
5
5
  "author": "LaunchDarkly <team@launchdarkly.com>",
6
6
  "license": "Apache-2.0",
@@ -4,6 +4,7 @@ const ContextFilter = require('./ContextFilter');
4
4
  const errors = require('./errors');
5
5
  const messages = require('./messages');
6
6
  const utils = require('./utils');
7
+ const { getContextKeys } = require('./context');
7
8
 
8
9
  function EventProcessor(
9
10
  platform,
@@ -50,7 +51,7 @@ function EventProcessor(
50
51
  // identify events always have an inline context
51
52
  ret.context = contextFilter.filter(e.context);
52
53
  } else {
53
- ret.contextKeys = getContextKeys(e);
54
+ ret.contextKeys = getContextKeysFromEvent(e);
54
55
  delete ret['context'];
55
56
  }
56
57
  if (e.kind === 'feature') {
@@ -60,26 +61,8 @@ function EventProcessor(
60
61
  return ret;
61
62
  }
62
63
 
63
- function getContextKeys(event) {
64
- const keys = {};
65
- const context = event.context;
66
- if (context !== undefined) {
67
- if (context.kind === undefined) {
68
- keys.user = String(context.key);
69
- } else if (context.kind === 'multi') {
70
- Object.entries(context)
71
- .filter(([key]) => key !== 'kind')
72
- .forEach(([key, value]) => {
73
- if (value !== undefined && value.key !== undefined) {
74
- keys[key] = value.key;
75
- }
76
- });
77
- } else {
78
- keys[context.kind] = String(context.key);
79
- }
80
- return keys;
81
- }
82
- return undefined;
64
+ function getContextKeysFromEvent(event) {
65
+ return getContextKeys(event.context, logger);
83
66
  }
84
67
 
85
68
  function addToOutbox(event) {
@@ -1,4 +1,4 @@
1
- const { checkContext, getContextKinds, getCanonicalKey } = require('../context');
1
+ const { checkContext, getContextKeys, getContextKinds, getCanonicalKey } = require('../context');
2
2
 
3
3
  describe.each([{ key: 'test' }, { kind: 'user', key: 'test' }, { kind: 'multi', user: { key: 'test' } }])(
4
4
  'given a context which contains a single kind',
@@ -91,3 +91,111 @@ describe('when determining canonical keys', () => {
91
91
  expect(getCanonicalKey(null)).toBeUndefined();
92
92
  });
93
93
  });
94
+
95
+ describe('getContextKeys', () => {
96
+ it('returns undefined if argument is undefined', () => {
97
+ const context = undefined;
98
+ const keys = getContextKeys(context);
99
+ expect(keys).toBeUndefined();
100
+ });
101
+
102
+ it('works with legacy user without kind attribute', () => {
103
+ const user = {
104
+ key: 'legacy-user-key',
105
+ name: 'Test User',
106
+ custom: {
107
+ customAttribute1: true,
108
+ },
109
+ };
110
+ const keys = getContextKeys(user);
111
+ expect(keys).toEqual({ user: 'legacy-user-key' });
112
+ });
113
+
114
+ it('gets keys from multi context', () => {
115
+ const context = {
116
+ kind: 'multi',
117
+ user: {
118
+ key: 'test-user-key',
119
+ name: 'Test User',
120
+ isPremiumCustomer: true,
121
+ },
122
+ organization: {
123
+ key: 'test-organization-key',
124
+ name: 'Test Company',
125
+ industry: 'technology',
126
+ },
127
+ };
128
+ const keys = getContextKeys(context);
129
+ expect(keys).toEqual({ user: 'test-user-key', organization: 'test-organization-key' });
130
+ });
131
+
132
+ it('ignores undefined keys from multi context', () => {
133
+ const context = {
134
+ kind: 'multi',
135
+ user: {
136
+ key: 'test-user-key',
137
+ name: 'Test User',
138
+ isPremiumCustomer: true,
139
+ },
140
+ organization: {
141
+ name: 'Test Company',
142
+ industry: 'technology',
143
+ },
144
+ rogueAttribute: undefined,
145
+ };
146
+ const keys = getContextKeys(context);
147
+ expect(keys).toEqual({ user: 'test-user-key' });
148
+ });
149
+
150
+ it.only('ignores empty string and null keys from multi context', () => {
151
+ const context = {
152
+ kind: 'multi',
153
+ user: {
154
+ key: 'test-user-key',
155
+ name: 'Test User',
156
+ isPremiumCustomer: true,
157
+ },
158
+ organization: {
159
+ key: '',
160
+ name: 'Test Company',
161
+ industry: 'technology',
162
+ },
163
+ drone: {
164
+ key: null,
165
+ name: 'test-drone',
166
+ },
167
+ };
168
+ const keys = getContextKeys(context);
169
+ expect(keys).toEqual({ user: 'test-user-key' });
170
+ });
171
+
172
+ it('gets keys from single context', () => {
173
+ const context = {
174
+ kind: 'drone',
175
+ key: 'test-drone-key',
176
+ name: 'test-drone',
177
+ };
178
+ const keys = getContextKeys(context);
179
+ expect(keys).toEqual({ drone: 'test-drone-key' });
180
+ });
181
+
182
+ it('ignores kind when it is an empty string', () => {
183
+ const context = {
184
+ kind: '',
185
+ key: 'test-drone-key',
186
+ name: 'test-drone',
187
+ };
188
+ const keys = getContextKeys(context);
189
+ expect(keys).toEqual({});
190
+ });
191
+
192
+ it('ignores kind when it is null', () => {
193
+ const context = {
194
+ kind: null,
195
+ key: 'test-drone-key',
196
+ name: 'test-drone',
197
+ };
198
+ const keys = getContextKeys(context);
199
+ expect(keys).toEqual({});
200
+ });
201
+ });
@@ -1,4 +1,4 @@
1
- import { appendUrlPath, base64URLEncode, getLDUserAgentString, wrapPromiseCallback, chunkEventsForUrl } from '../utils';
1
+ import { appendUrlPath, base64URLEncode, chunkEventsForUrl, getLDUserAgentString, wrapPromiseCallback } from '../utils';
2
2
 
3
3
  import * as stubPlatform from './stubPlatform';
4
4
 
package/src/context.js CHANGED
@@ -89,8 +89,44 @@ function getCanonicalKey(context) {
89
89
  }
90
90
  }
91
91
 
92
+ function getContextKeys(context, logger) {
93
+ if (!context) {
94
+ return undefined;
95
+ }
96
+
97
+ const keys = {};
98
+ const { kind, key } = context;
99
+
100
+ switch (kind) {
101
+ case typeof kind === 'undefined':
102
+ keys.user = `${key}`;
103
+ break;
104
+ case 'multi':
105
+ Object.entries(context)
106
+ .filter(([key]) => key !== 'kind')
107
+ .forEach(([key, value]) => {
108
+ if (value?.key) {
109
+ keys[key] = value.key;
110
+ }
111
+ });
112
+ break;
113
+ case null:
114
+ logger?.warn(`null is not a valid context kind: ${context}`);
115
+ break;
116
+ case '':
117
+ logger?.warn(`'' is not a valid context kind: ${context}`);
118
+ break;
119
+ default:
120
+ keys[kind] = `${key}`;
121
+ break;
122
+ }
123
+
124
+ return keys;
125
+ }
126
+
92
127
  module.exports = {
93
128
  checkContext,
129
+ getContextKeys,
94
130
  getContextKinds,
95
131
  getCanonicalKey,
96
132
  };
package/src/index.js CHANGED
@@ -14,7 +14,7 @@ const { commonBasicLogger } = require('./loggers');
14
14
  const utils = require('./utils');
15
15
  const errors = require('./errors');
16
16
  const messages = require('./messages');
17
- const { checkContext } = require('./context');
17
+ const { checkContext, getContextKeys } = require('./context');
18
18
  const { InspectorTypes, InspectorManager } = require('./InspectorManager');
19
19
 
20
20
  const changeEvent = 'change';
@@ -809,4 +809,5 @@ module.exports = {
809
809
  errors,
810
810
  messages,
811
811
  utils,
812
+ getContextKeys,
812
813
  };