import { Dispatcher } from 'undici-types'; import { BaseOfflineHandler } from './offline_handlers.js'; import { DefaultFlag, Flag, Flags } from './models.js'; import { EnvironmentDataPollingManager } from './polling_manager.js'; import { SegmentModel, EnvironmentModel, IdentityModel } from '../flagsmith-engine/index.js'; import { FlagsmithConfig, FlagsmithTraitValue, FlagsmithValue, TraitConfig } from './types.js'; export { AnalyticsProcessor, AnalyticsProcessorOptions } from './analytics.js'; export { FlagsmithAPIError, FlagsmithClientError } from './errors.js'; export { DEFAULT_EVENTS_API_URL, EventProcessor, EventProcessorOptions, FLAG_EXPOSURE_EVENT, FlagsmithEvent } from './events.js'; export { BaseFlag, DefaultFlag, ExperimentMetadata, Flag, 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; private cache?; private onEnvironmentChange; private analyticsProcessor?; private eventProcessor?; 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; /** * 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; /** * Get a single flag for a given identity, recording a {@link FLAG_EXPOSURE_EVENT} if that * identity is enrolled in a running experiment on the feature. * * Flags are fetched exactly as {@link getIdentityFlags} fetches them, so traits are upserted and * the identity cache is used when one is configured. * * @param featureName the name of the feature to evaluate. * @param identifier a unique identifier for the identity in the current environment. * @param traits? a dictionary of traits to add / update on the identity in Flagsmith. * @returns the {@link Flag} for the given feature. If it was not found, the result of * {@link FlagsmithConfig.defaultFlagHandler}, or a disabled flag with `isDefault` set if there is * no handler. * @throws if {@link FlagsmithConfig.enableEvents} is not set. */ getExperimentFlag(featureName: string, identifier: string, traits?: { [key: string]: FlagsmithTraitValue | TraitConfig; }): Promise; /** * Record a custom event, e.g. a conversion to reconcile with experiment exposures. * * @param event the event name. Names starting with `$` are reserved for Flagsmith. * @param opts? the identity, value, traits and metadata to record alongside the event. * @throws if {@link FlagsmithConfig.enableEvents} is not set, or if `event` starts with `$`. */ trackEvent(event: string, opts?: { identifier?: string; value?: FlagsmithValue; traits?: { [key: string]: FlagsmithTraitValue | TraitConfig; }; metadata?: Record; }): void; /** * Record a {@link FLAG_EXPOSURE_EVENT} for a feature. * * {@link getExperimentFlag} records exposures on its own; use this method to record one for a * flag that was evaluated elsewhere. * * @param featureName the name of the feature the identity was exposed to. * @param opts the identity, value, traits and metadata to record alongside the exposure. * @throws if {@link FlagsmithConfig.enableEvents} is not set. */ trackExposureEvent(featureName: string, opts: { identifier: string; value?: FlagsmithValue; traits?: { [key: string]: FlagsmithTraitValue | TraitConfig; }; metadata?: Record; }): void; /** * Send all buffered events to the Flagsmith events API now. * * Resolves once every event tracked before the call has been posted, dropped, or kept for the * next flush because the events API is unavailable. Resolves immediately * if {@link FlagsmithConfig.enableEvents} is not set. */ flushEvents(): Promise; /** * 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; 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; /** * Stop polling the environment and send any buffered events. Events tracked afterwards are * ignored. * * @param timeoutMs? how long to wait for the buffered events to be sent. Once it has passed, * the events still unsent are dropped and this resolves. Waits for them to be sent or dropped * if not set. */ close(timeoutMs?: number): Promise; 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; private getEnvironmentFromApi; private getEnvironmentFlagsFromDocument; private getIdentityFlagsFromDocument; private getEnvironmentFlagsFromApi; private getIdentityFlagsFromApi; private getIdentityModel; } export default Flagsmith;