UNPKG

@launchdarkly/js-sdk-common

Version:
104 lines 3.73 kB
import type { LDContext, LDContextCommon } from './api'; import AttributeReference from './AttributeReference'; /** * Container for a context/contexts. Because contexts come from external code * they must be thoroughly validated and then formed to comply with * the type system. */ export default class Context { private _context?; private _isMulti; private _isUser; private _wasLegacy; private _contexts; private _privateAttributeReferences?; private _cachedCanonicalJson?; readonly kind: string; /** * Is this a valid context. If a valid context cannot be created, then this flag will be true. * The validity of a context should be tested before it is used. */ readonly valid: boolean; readonly message?: string; static readonly UserKind: string; /** * Contexts should be created using the static factory method {@link Context.fromLDContext}. * @param kind The kind of the context. * * The factory methods are static functions within the class because they access private * implementation details, so they cannot be free functions. */ private constructor(); private static _contextForError; private static _getValueFromContext; private _contextForKind; private static _fromMultiKindContext; private static _fromSingleKindContext; private static _fromLegacyUser; /** * Attempt to create a {@link Context} from an {@link LDContext}. * @param context The input context to create a Context from. * @returns a {@link Context}, if the context was not valid, then the returned contexts `valid` * property will be false. */ static fromLDContext(context: LDContext): Context; /** * Creates a {@link LDContext} from a {@link Context}. * @param context to be converted * @returns an {@link LDContext} if input was valid, otherwise undefined */ static toLDContext(context: Context): LDContext | undefined; /** * Attempt to get a value for the given context kind using the given reference. * @param reference The reference to the value to get. * @param kind The kind of the context to get the value for. * @returns a value or `undefined` if one is not found. */ valueForKind(reference: AttributeReference, kind?: string): any | undefined; /** * Attempt to get a key for the specified kind. * @param kind The kind to get a key for. * @returns The key for the specified kind, or undefined. */ key(kind?: string): string | undefined; /** * True if this is a multi-kind context. */ get isMultiKind(): boolean; /** * Get the canonical key for this context. */ get canonicalKey(): string; /** * Get the kinds of this context. */ get kinds(): string[]; /** * Get the kinds, and their keys, for this context. */ get kindsAndKeys(): Record<string, string>; /** * Get the attribute references. * * @param kind */ privateAttributes(kind: string): AttributeReference[]; /** * Get the underlying context objects from this context. * * This method is intended to be used in event generation. * * The returned objects should not be modified. */ getContexts(): [string, LDContextCommon][]; get legacy(): boolean; /** * Get the serialized canonical JSON for this context. This is not filtered for use in events. * * This method will cache the result. * * @returns The serialized canonical JSON or undefined if it cannot be serialized. */ canonicalUnfilteredJson(): string | undefined; } //# sourceMappingURL=Context.d.ts.map