UNPKG

flagsmith-nodejs

Version:

Flagsmith lets you manage features flags and remote config across web, mobile and server side applications. Deliver true Continuous Integration. Get builds out faster. Control who has access to new features.

133 lines (132 loc) 5.86 kB
import { Dispatcher } from 'undici-types'; import { BaseOfflineHandler } from './offline_handlers.js'; import { DefaultFlag, Flags } from './models.js'; import { EnvironmentDataPollingManager } from './polling_manager.js'; import { SegmentModel, EnvironmentModel, IdentityModel } from '../flagsmith-engine/index.js'; import { FlagsmithConfig, FlagsmithTraitValue, TraitConfig } from './types.js'; export { AnalyticsProcessor, AnalyticsProcessorOptions } from './analytics.js'; export { FlagsmithAPIError, FlagsmithClientError } from './errors.js'; export { BaseFlag, DefaultFlag, Flags } from './models.js'; export { EnvironmentDataPollingManager } from './polling_manager.js'; export { FlagsmithCache, FlagsmithConfig } from './types.js'; /** * A client for evaluating Flagsmith feature flags. * * Flags are evaluated remotely by the Flagsmith API over HTTP by default. * To evaluate flags locally, create the client using {@link FlagsmithConfig.enableLocalEvaluation} and a server-side SDK key. * * @example * import { Flagsmith, Flags, DefaultFlag } from 'flagsmith-nodejs' * * const flagsmith = new Flagsmith({ * environmentKey: 'your_sdk_key', * defaultFlagHandler: (flagKey: string) => { new DefaultFlag(...) }, * }); * * // Fetch the current environment flags * const environmentFlags: Flags = flagsmith.getEnvironmentFlags() * const isFooEnabled: boolean = environmentFlags.isFeatureEnabled('foo') * * // Evaluate flags for any identity * const identityFlags: Flags = flagsmith.getIdentityFlags('my_user_123', {'vip': true}) * const bannerVariation: string = identityFlags.getFeatureValue('banner_flag') * * @see FlagsmithConfig */ export declare class Flagsmith { environmentKey?: string; apiUrl?: string; analyticsUrl?: string; customHeaders?: { [key: string]: any; }; agent?: Dispatcher; requestTimeoutMs?: number; enableLocalEvaluation?: boolean; environmentRefreshIntervalSeconds: number; retries?: number; enableAnalytics: boolean; defaultFlagHandler?: (featureName: string) => DefaultFlag; environmentFlagsUrl?: string; identitiesUrl?: string; environmentUrl?: string; environmentDataPollingManager?: EnvironmentDataPollingManager; private environment?; offlineMode: boolean; offlineHandler?: BaseOfflineHandler; identitiesWithOverridesByIdentifier?: Map<string, IdentityModel>; private cache?; private onEnvironmentChange; private analyticsProcessor?; private logger; private customFetch; private readonly requestRetryDelayMilliseconds; /** * Creates a new {@link Flagsmith} client. * * If using local evaluation, the environment will be fetched lazily when needed by any method. Polling the * environment for updates will start after {@link environmentRefreshIntervalSeconds} once the client is created. * @param data The {@link FlagsmithConfig} options for this client. */ constructor(data: FlagsmithConfig); /** * Get all the default for flags for the current environment. * * @returns Flags object holding all the flags for the current environment. */ getEnvironmentFlags(): Promise<Flags>; /** * Get all the flags for the current environment for a given identity. Will also upsert all traits to the Flagsmith API for future evaluations. Providing a trait with a value of None will remove the trait from the identity if it exists. * * @param {string} identifier a unique identifier for the identity in the current environment, e.g. email address, username, uuid * @param {{[key:string]:any | TraitConfig}} traits? a dictionary of traits to add / update on the identity in Flagsmith, e.g. {"num_orders": 10} or {age: {value: 30, transient: true}} * @returns Flags object holding all the flags for the given identity. */ getIdentityFlags(identifier: string, traits?: { [key: string]: FlagsmithTraitValue | TraitConfig; }, transient?: boolean): Promise<Flags>; /** * Get the segments for the current environment for a given identity. Will also upsert all traits to the Flagsmith API for future evaluations. Providing a trait with a value of None will remove the trait from the identity if it exists. * * @param {string} identifier a unique identifier for the identity in the current environment, e.g. email address, username, uuid * @param {{[key:string]:any}} traits? a dictionary of traits to add / update on the identity in Flagsmith, e.g. {"num_orders": 10} * @returns Segments that the given identity belongs to. */ getIdentitySegments(identifier: string, traits?: { [key: string]: any; }): Promise<SegmentModel[]>; private fetchEnvironment; /** * Fetch the latest environment state from the Flagsmith API to use for local flag evaluation. * * If the environment is currently being fetched, calling this method will not cause additional fetches. */ updateEnvironment(): Promise<void>; close(): Promise<void>; private getJSONResponse; /** * This promise ensures that the environment is retrieved before attempting to locally evaluate. */ private environmentPromise?; /** * Returns the current environment, fetching it from the API if needed. * * Calling this method concurrently while the environment is being fetched will not cause additional requests. */ getEnvironment(): Promise<EnvironmentModel>; private getEnvironmentFromApi; private getEnvironmentFlagsFromDocument; private getIdentityFlagsFromDocument; private getEnvironmentFlagsFromApi; private getIdentityFlagsFromApi; private getIdentityModel; } export default Flagsmith;