launchdarkly-js-sdk-common
Advanced tools
Comparing version 5.0.0-alpha.3 to 5.0.0-alpha.4
{ | ||
"name": "launchdarkly-js-sdk-common", | ||
"version": "5.0.0-alpha.3", | ||
"version": "5.0.0-alpha.4", | ||
"description": "LaunchDarkly SDK for JavaScript - common code", | ||
@@ -5,0 +5,0 @@ "author": "LaunchDarkly <team@launchdarkly.com>", |
@@ -1,2 +0,2 @@ | ||
const { checkContext, getContextKinds, getCanonicalKey } = require('../context'); | ||
const { checkContext, getContextKeys, getContextKinds, getCanonicalKey } = require('../context'); | ||
@@ -94,1 +94,109 @@ describe.each([{ key: 'test' }, { kind: 'user', key: 'test' }, { kind: 'multi', user: { key: 'test' } }])( | ||
}); | ||
describe('getContextKeys', () => { | ||
it('returns undefined if argument is undefined', () => { | ||
const context = undefined; | ||
const keys = getContextKeys(context); | ||
expect(keys).toBeUndefined(); | ||
}); | ||
it('works with legacy user without kind attribute', () => { | ||
const user = { | ||
key: 'legacy-user-key', | ||
name: 'Test User', | ||
custom: { | ||
customAttribute1: true, | ||
}, | ||
}; | ||
const keys = getContextKeys(user); | ||
expect(keys).toEqual({ user: 'legacy-user-key' }); | ||
}); | ||
it('gets keys from multi context', () => { | ||
const context = { | ||
kind: 'multi', | ||
user: { | ||
key: 'test-user-key', | ||
name: 'Test User', | ||
isPremiumCustomer: true, | ||
}, | ||
organization: { | ||
key: 'test-organization-key', | ||
name: 'Test Company', | ||
industry: 'technology', | ||
}, | ||
}; | ||
const keys = getContextKeys(context); | ||
expect(keys).toEqual({ user: 'test-user-key', organization: 'test-organization-key' }); | ||
}); | ||
it('ignores undefined keys from multi context', () => { | ||
const context = { | ||
kind: 'multi', | ||
user: { | ||
key: 'test-user-key', | ||
name: 'Test User', | ||
isPremiumCustomer: true, | ||
}, | ||
organization: { | ||
name: 'Test Company', | ||
industry: 'technology', | ||
}, | ||
rogueAttribute: undefined, | ||
}; | ||
const keys = getContextKeys(context); | ||
expect(keys).toEqual({ user: 'test-user-key' }); | ||
}); | ||
it.only('ignores empty string and null keys from multi context', () => { | ||
const context = { | ||
kind: 'multi', | ||
user: { | ||
key: 'test-user-key', | ||
name: 'Test User', | ||
isPremiumCustomer: true, | ||
}, | ||
organization: { | ||
key: '', | ||
name: 'Test Company', | ||
industry: 'technology', | ||
}, | ||
drone: { | ||
key: null, | ||
name: 'test-drone', | ||
}, | ||
}; | ||
const keys = getContextKeys(context); | ||
expect(keys).toEqual({ user: 'test-user-key' }); | ||
}); | ||
it('gets keys from single context', () => { | ||
const context = { | ||
kind: 'drone', | ||
key: 'test-drone-key', | ||
name: 'test-drone', | ||
}; | ||
const keys = getContextKeys(context); | ||
expect(keys).toEqual({ drone: 'test-drone-key' }); | ||
}); | ||
it('ignores kind when it is an empty string', () => { | ||
const context = { | ||
kind: '', | ||
key: 'test-drone-key', | ||
name: 'test-drone', | ||
}; | ||
const keys = getContextKeys(context); | ||
expect(keys).toEqual({}); | ||
}); | ||
it('ignores kind when it is null', () => { | ||
const context = { | ||
kind: null, | ||
key: 'test-drone-key', | ||
name: 'test-drone', | ||
}; | ||
const keys = getContextKeys(context); | ||
expect(keys).toEqual({}); | ||
}); | ||
}); |
@@ -1,2 +0,2 @@ | ||
import { appendUrlPath, base64URLEncode, getLDUserAgentString, wrapPromiseCallback, chunkEventsForUrl } from '../utils'; | ||
import { appendUrlPath, base64URLEncode, chunkEventsForUrl, getLDUserAgentString, wrapPromiseCallback } from '../utils'; | ||
@@ -3,0 +3,0 @@ import * as stubPlatform from './stubPlatform'; |
@@ -92,6 +92,42 @@ /** | ||
function getContextKeys(context, logger) { | ||
if (!context) { | ||
return undefined; | ||
} | ||
const keys = {}; | ||
const { kind, key } = context; | ||
switch (kind) { | ||
case typeof kind === 'undefined': | ||
keys.user = `${key}`; | ||
break; | ||
case 'multi': | ||
Object.entries(context) | ||
.filter(([key]) => key !== 'kind') | ||
.forEach(([key, value]) => { | ||
if (value?.key) { | ||
keys[key] = value.key; | ||
} | ||
}); | ||
break; | ||
case null: | ||
logger?.warn(`null is not a valid context kind: ${context}`); | ||
break; | ||
case '': | ||
logger?.warn(`'' is not a valid context kind: ${context}`); | ||
break; | ||
default: | ||
keys[kind] = `${key}`; | ||
break; | ||
} | ||
return keys; | ||
} | ||
module.exports = { | ||
checkContext, | ||
getContextKeys, | ||
getContextKinds, | ||
getCanonicalKey, | ||
}; |
@@ -7,2 +7,3 @@ const EventSender = require('./EventSender'); | ||
const utils = require('./utils'); | ||
const { getContextKeys } = require('./context'); | ||
@@ -54,3 +55,3 @@ function EventProcessor( | ||
} else { | ||
ret.contextKeys = getContextKeys(e); | ||
ret.contextKeys = getContextKeysFromEvent(e); | ||
delete ret['context']; | ||
@@ -65,22 +66,4 @@ } | ||
function getContextKeys(event) { | ||
const keys = {}; | ||
const context = event.context; | ||
if (context !== undefined) { | ||
if (context.kind === undefined) { | ||
keys.user = String(context.key); | ||
} else if (context.kind === 'multi') { | ||
Object.entries(context) | ||
.filter(([key]) => key !== 'kind') | ||
.forEach(([key, value]) => { | ||
if (value !== undefined && value.key !== undefined) { | ||
keys[key] = value.key; | ||
} | ||
}); | ||
} else { | ||
keys[context.kind] = String(context.key); | ||
} | ||
return keys; | ||
} | ||
return undefined; | ||
function getContextKeysFromEvent(event) { | ||
return getContextKeys(event.context, logger); | ||
} | ||
@@ -87,0 +70,0 @@ |
@@ -17,3 +17,3 @@ const EventProcessor = require('./EventProcessor'); | ||
const messages = require('./messages'); | ||
const { checkContext } = require('./context'); | ||
const { checkContext, getContextKeys } = require('./context'); | ||
const { InspectorTypes, InspectorManager } = require('./InspectorManager'); | ||
@@ -813,2 +813,3 @@ | ||
utils, | ||
getContextKeys, | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
435729
10339