Skip to content

Commit

Permalink
chore: move kind helpers to internal.
Browse files Browse the repository at this point in the history
  • Loading branch information
yusinto committed Jan 18, 2024
1 parent 24c9ea3 commit cfdcb7f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 36 deletions.
43 changes: 8 additions & 35 deletions packages/shared/common/src/Context.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions packages/shared/common/src/internal/context/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions packages/shared/common/src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './diagnostics';
export * from './evaluation';
export * from './events';
export * from './stream';
export * from './context';
4 changes: 3 additions & 1 deletion packages/shared/sdk-client/src/utils/ensureKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

Expand Down

0 comments on commit cfdcb7f

Please sign in to comment.