diff --git a/packages/shared/common/src/Context.ts b/packages/shared/common/src/Context.ts index 42b4918a5..60560a32e 100644 --- a/packages/shared/common/src/Context.ts +++ b/packages/shared/common/src/Context.ts @@ -1,8 +1,14 @@ /* eslint-disable no-underscore-dangle */ // eslint-disable-next-line max-classes-per-file -import { LDContextCommon, LDMultiKindContext, LDSingleKindContext, LDUser } from './api/context'; -import { LDContext } from './api/context/LDContext'; +import type { + LDContext, + LDContextCommon, + LDMultiKindContext, + LDSingleKindContext, + LDUser, +} from './api'; import AttributeReference from './AttributeReference'; +import { isLegacyUser, isMultiKind, isSingleKind } from './internal/context'; import { TypeValidators } from './validators'; // The general strategy for the context is to transform the passed in context @@ -38,39 +44,6 @@ function encodeKey(key: string): string { return key; } -/** - * Check if a context is a single kind context. - * @param context - * @returns true if the context is a single kind context. - */ -export function isSingleKind(context: LDContext): context is LDSingleKindContext { - if ('kind' in context) { - return TypeValidators.String.is(context.kind) && context.kind !== 'multi'; - } - return false; -} - -/** - * Check if a context is a multi-kind context. - * @param context - * @returns true if it is a multi-kind context. - */ -export function isMultiKind(context: LDContext): context is LDMultiKindContext { - if ('kind' in context) { - return TypeValidators.String.is(context.kind) && context.kind === 'multi'; - } - return false; -} - -/** - * Check if a context is a legacy user context. - * @param context - * @returns true if it is a legacy user context. - */ -export function isLegacyUser(context: LDContext): context is LDUser { - return !('kind' in context) || context.kind === null || context.kind === undefined; -} - /** * Check if the given value is a LDContextCommon. * @param kindOrContext diff --git a/packages/shared/common/src/internal/context/index.ts b/packages/shared/common/src/internal/context/index.ts new file mode 100644 index 000000000..f8d2ddd4e --- /dev/null +++ b/packages/shared/common/src/internal/context/index.ts @@ -0,0 +1,39 @@ +/** + * Internal use only. These functions should only be used as part of the initial validation of + * the LDContext object. Thereafter, the Context object should be used. + */ +import type { LDContext, LDMultiKindContext, LDSingleKindContext, LDUser } from '../../api'; +import { TypeValidators } from '../../validators'; + +/** + * Check if a context is a single kind context. + * @param context + * @returns true if the context is a single kind context. + */ +export function isSingleKind(context: LDContext): context is LDSingleKindContext { + if ('kind' in context) { + return TypeValidators.String.is(context.kind) && context.kind !== 'multi'; + } + return false; +} + +/** + * Check if a context is a multi-kind context. + * @param context + * @returns true if it is a multi-kind context. + */ +export function isMultiKind(context: LDContext): context is LDMultiKindContext { + if ('kind' in context) { + return TypeValidators.String.is(context.kind) && context.kind === 'multi'; + } + return false; +} + +/** + * Check if a context is a legacy user context. + * @param context + * @returns true if it is a legacy user context. + */ +export function isLegacyUser(context: LDContext): context is LDUser { + return !('kind' in context) || context.kind === null || context.kind === undefined; +} diff --git a/packages/shared/common/src/internal/index.ts b/packages/shared/common/src/internal/index.ts index db6af8042..ae8a03362 100644 --- a/packages/shared/common/src/internal/index.ts +++ b/packages/shared/common/src/internal/index.ts @@ -2,3 +2,4 @@ export * from './diagnostics'; export * from './evaluation'; export * from './events'; export * from './stream'; +export * from './context'; diff --git a/packages/shared/sdk-client/src/utils/ensureKey.ts b/packages/shared/sdk-client/src/utils/ensureKey.ts index 75a3817da..c234c0372 100644 --- a/packages/shared/sdk-client/src/utils/ensureKey.ts +++ b/packages/shared/sdk-client/src/utils/ensureKey.ts @@ -6,7 +6,9 @@ import type { LDUser, Platform, } from '@launchdarkly/js-sdk-common'; -import { isLegacyUser, isMultiKind, isSingleKind } from '@launchdarkly/js-sdk-common'; +import { internal } from '@launchdarkly/js-sdk-common'; + +const { isLegacyUser, isMultiKind, isSingleKind } = internal; export const addNamespace = (s: string) => `LaunchDarkly_AnonKeys_${s}`;