launchdarkly-js-sdk-common 5.0.0-alpha.3 → 5.0.0-alpha.5
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 +1 -1
- package/src/EventProcessor.js +4 -21
- package/src/__tests__/context-test.js +109 -1
- package/src/__tests__/utils-test.js +1 -1
- package/src/context.js +38 -0
- package/src/index.js +2 -1
- package/test-types.ts +2 -0
- package/typings.d.ts +5 -0
package/package.json
CHANGED
package/src/EventProcessor.js
CHANGED
|
@@ -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 =
|
|
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
|
|
64
|
-
|
|
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
|
|
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
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
* @param {string} kind
|
|
4
4
|
* @returns true if the kind is valid.
|
|
5
5
|
*/
|
|
6
|
+
const { commonBasicLogger } = require('./loggers');
|
|
7
|
+
|
|
6
8
|
function validKind(kind) {
|
|
7
9
|
return typeof kind === 'string' && kind !== 'kind' && kind.match(/^(\w|\.|-)+$/);
|
|
8
10
|
}
|
|
@@ -89,8 +91,44 @@ function getCanonicalKey(context) {
|
|
|
89
91
|
}
|
|
90
92
|
}
|
|
91
93
|
|
|
94
|
+
function getContextKeys(context, logger = commonBasicLogger()) {
|
|
95
|
+
if (!context) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const keys = {};
|
|
100
|
+
const { kind, key } = context;
|
|
101
|
+
|
|
102
|
+
switch (kind) {
|
|
103
|
+
case typeof kind === 'undefined':
|
|
104
|
+
keys.user = `${key}`;
|
|
105
|
+
break;
|
|
106
|
+
case 'multi':
|
|
107
|
+
Object.entries(context)
|
|
108
|
+
.filter(([key]) => key !== 'kind')
|
|
109
|
+
.forEach(([key, value]) => {
|
|
110
|
+
if (value?.key) {
|
|
111
|
+
keys[key] = value.key;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
break;
|
|
115
|
+
case null:
|
|
116
|
+
logger.warn(`null is not a valid context kind: ${context}`);
|
|
117
|
+
break;
|
|
118
|
+
case '':
|
|
119
|
+
logger.warn(`'' is not a valid context kind: ${context}`);
|
|
120
|
+
break;
|
|
121
|
+
default:
|
|
122
|
+
keys[kind] = `${key}`;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return keys;
|
|
127
|
+
}
|
|
128
|
+
|
|
92
129
|
module.exports = {
|
|
93
130
|
checkContext,
|
|
131
|
+
getContextKeys,
|
|
94
132
|
getContextKinds,
|
|
95
133
|
getCanonicalKey,
|
|
96
134
|
};
|
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
|
};
|
package/test-types.ts
CHANGED
package/typings.d.ts
CHANGED
|
@@ -984,6 +984,11 @@ declare module 'launchdarkly-js-sdk-common' {
|
|
|
984
984
|
*/
|
|
985
985
|
export type LDLogLevel = 'debug' | 'info' | 'warn' | 'error' | 'none';
|
|
986
986
|
|
|
987
|
+
export function getContextKeys(
|
|
988
|
+
context: LDContext,
|
|
989
|
+
logger?: LDLogger,
|
|
990
|
+
): {[attribute: string]: string} | undefined;
|
|
991
|
+
|
|
987
992
|
/**
|
|
988
993
|
* Callback interface for collecting information about the SDK at runtime.
|
|
989
994
|
*
|