import type { FeatureDefinition, FeatureOverrides, FeatureOverridesFn, IdType, TypedFeatureKey } from "./types"; import { ClientOptions, Context, ContextWithTracking, HttpClient, Logger, TrackOptions, TypedFeatures } from "./types"; /** * The SDK client. * * @remarks * This is the main class for interacting with Bucket. * It is used to evaluate feature flags, update user and company contexts, and track events. * * @example * ```ts * // set the BUCKET_SECRET_KEY environment variable or pass the secret key to the constructor * const client = new BucketClient(); * * // evaluate a feature flag * const isFeatureEnabled = client.getFeature("feature-flag-key", { * user: { id: "user-id" }, * company: { id: "company-id" }, * }); * ``` **/ export declare class BucketClient { private _config; httpClient: HttpClient; private featuresCache; private batchBuffer; private rateLimiter; /** * Gets the logger associated with the client. */ readonly logger: Logger; private initializationFinished; private _initialize; /** * Creates a new SDK client. * See README for configuration options. * * @param options - The options for the client or an existing client to clone. * @param options.secretKey - The secret key to use for the client. * @param options.apiBaseUrl - The base URL to send requests to (optional). * @param options.logger - The logger to use for logging (optional). * @param options.httpClient - The HTTP client to use for sending requests (optional). * @param options.logLevel - The log level to use for logging (optional). * @param options.offline - Whether to run in offline mode (optional). * @param options.fallbackFeatures - The fallback features to use if the feature is not found (optional). * @param options.batchOptions - The options for the batch buffer (optional). * @param options.featureOverrides - The feature overrides to use for the client (optional). * @param options.configFile - The path to the config file (optional). * @param options.featuresFetchRetries - Number of retries for fetching features (optional, defaults to 3). * @param options.fetchTimeoutMs - Timeout for fetching features (optional, defaults to 10000ms). * @param options.cacheStrategy - The cache strategy to use for the client (optional, defaults to "periodically-update"). * * @throws An error if the options are invalid. **/ constructor(options?: ClientOptions); /** * Sets the feature overrides. * * @param overrides - The feature overrides. * * @remarks * The feature overrides are used to override the feature definitions. * This is useful for testing or development. * * @example * ```ts * client.featureOverrides = { * "feature-1": true, * "feature-2": false, * }; * ``` **/ set featureOverrides(overrides: FeatureOverridesFn | FeatureOverrides); /** * Clears the feature overrides. * * @remarks * This is useful for testing or development. * * @example * ```ts * afterAll(() => { * client.clearFeatureOverrides(); * }); * ``` **/ clearFeatureOverrides(): void; /** * Returns a new BoundBucketClient with the user/company/otherContext * set to be used in subsequent calls. * For example, for evaluating feature targeting or tracking events. * * @param context - The context to bind the client to. * @param context.enableTracking - Whether to enable tracking for the context. * @param context.user - The user context. * @param context.company - The company context. * @param context.other - The other context. * * @returns A new client bound with the arguments given. * * @throws An error if the user/company is given but their ID is not a string. * * @remarks * The `updateUser` / `updateCompany` methods will automatically be called when * the user/company is set respectively. **/ bindClient({ enableTracking, ...context }: ContextWithTracking): BoundBucketClient; /** * Updates the associated user in Bucket. * * @param userId - The userId of the user to update. * @param options - The options for the user. * @param options.attributes - The additional attributes of the user (optional). * @param options.meta - The meta context associated with tracking (optional). * * @throws An error if the company is not set or the options are invalid. * @remarks * The company must be set using `withCompany` before calling this method. * If the user is set, the company will be associated with the user. **/ updateUser(userId: IdType, options?: TrackOptions): Promise; /** * Updates the associated company in Bucket. * * @param companyId - The companyId of the company to update. * @param options - The options for the company. * @param options.attributes - The additional attributes of the company (optional). * @param options.meta - The meta context associated with tracking (optional). * @param options.userId - The userId of the user to associate with the company (optional). * * @throws An error if the company is not set or the options are invalid. * @remarks * The company must be set using `withCompany` before calling this method. * If the user is set, the company will be associated with the user. **/ updateCompany(companyId: IdType, options?: TrackOptions & { userId?: IdType; }): Promise; /** * Tracks an event in Bucket. * @param options.companyId - Optional company ID for the event (optional). * * @throws An error if the user is not set or the event is invalid or the options are invalid. * @remarks * If the company is set, the event will be associated with the company. **/ track(userId: IdType, event: string, options?: TrackOptions & { companyId?: IdType; }): Promise; /** * Initializes the client by caching the features definitions. * * @remarks * Call this method before calling `getFeatures` to ensure the feature definitions are cached. * The client will ignore subsequent calls to this method. **/ initialize(): Promise; /** * Flushes and completes any in-flight fetches in the feature cache. * * @remarks * It is recommended to call this method when the application is shutting down to ensure all events are sent * before the process exits. * * This method is automatically called when the process exits if `batchOptions.flushOnExit` is `true` in the options (default). */ flush(): Promise; /** * Gets the feature definitions, including all config values. * To evaluate which features are enabled for a given user/company, use `getFeatures`. * * @returns The features definitions. */ getFeatureDefinitions(): FeatureDefinition[]; /** * Gets the evaluated features for the current context which includes the user, company, and custom context. * * @param options - The options for the context. * @param options.enableTracking - Whether to enable tracking for the context. * @param options.meta - The meta context associated with the context. * @param options.user - The user context. * @param options.company - The company context. * @param options.other - The other context. * * @returns The evaluated features. * * @remarks * Call `initialize` before calling this method to ensure the feature definitions are cached, no features will be returned otherwise. **/ getFeatures({ enableTracking, ...context }: ContextWithTracking): TypedFeatures; /** * Gets the evaluated feature for the current context which includes the user, company, and custom context. * Using the `isEnabled` property sends a `check` event to Bucket. * * @param key - The key of the feature to get. * @returns The evaluated feature. * * @remarks * Call `initialize` before calling this method to ensure the feature definitions are cached, no features will be returned otherwise. **/ getFeature({ enableTracking, ...context }: ContextWithTracking, key: TKey): TypedFeatures[TKey]; /** * Gets evaluated features with the usage of remote context. * This method triggers a network request every time it's called. * * @param userId - The userId of the user to get the features for. * @param companyId - The companyId of the company to get the features for. * @param additionalContext - The additional context to get the features for. * * @returns evaluated features */ getFeaturesRemote(userId?: IdType, companyId?: IdType, additionalContext?: Context): Promise; /** * Gets evaluated feature with the usage of remote context. * This method triggers a network request every time it's called. * * @param key - The key of the feature to get. * @param userId - The userId of the user to get the feature for. * @param companyId - The companyId of the company to get the feature for. * @param additionalContext - The additional context to get the feature for. * * @returns evaluated feature */ getFeatureRemote(key: TKey, userId?: IdType, companyId?: IdType, additionalContext?: Context): Promise; private buildUrl; /** * Sends a POST request to the specified path. * * @param path - The path to send the request to. * @param body - The body of the request. * * @returns A boolean indicating if the request was successful. * * @throws An error if the path or body is invalid. **/ private post; /** * Sends a GET request to the specified path. * * @param path - The path to send the request to. * @param retries - Optional number of retries for the request. * * @returns The response from the server. * @throws An error if the path is invalid. **/ private get; /** * Sends a batch of events to the Bucket API. * * @param events - The events to send. * * @throws An error if the send fails. **/ private sendBulkEvents; /** * Sends a feature event to the Bucket API. * * Feature events are used to track the evaluation of feature targeting rules. * "check" events are sent when a feature's `isEnabled` property is checked. * "evaluate" events are sent when a feature's targeting rules are matched against * the current context. * * @param event - The event to send. * @param event.action - The action to send. * @param event.key - The key of the feature to send. * @param event.targetingVersion - The targeting version of the feature to send. * @param event.evalResult - The evaluation result of the feature to send. * @param event.evalContext - The evaluation context of the feature to send. * @param event.evalRuleResults - The evaluation rule results of the feature to send. * @param event.evalMissingFields - The evaluation missing fields of the feature to send. * * @throws An error if the event is invalid. * * @remarks * This method is rate-limited to prevent too many events from being sent. **/ private sendFeatureEvent; /** * Updates the context in Bucket (if needed). * This method should be used before requesting feature flags or binding a client. * * @param options - The options for the context. * @param options.enableTracking - Whether to enable tracking for the context. * @param options.meta - The meta context associated with the context. * @param options.user - The user context. * @param options.company - The company context. * @param options.other - The other context. */ private syncContext; /** * Warns if a feature has targeting rules that require context fields that are missing. * * @param context - The context. * @param feature - The feature to check. */ private _warnMissingFeatureContextFields; private _getFeatures; private _wrapRawFeature; private _getFeaturesRemote; } /** * A client bound with a specific user, company, and other context. */ export declare class BoundBucketClient { private readonly _client; private readonly _options; /** * (Internal) Creates a new BoundBucketClient. Use `bindClient` to create a new client bound with a specific context. * * @param client - The `BucketClient` to use. * @param options - The options for the client. * @param options.enableTracking - Whether to enable tracking for the client. * * @internal */ constructor(client: BucketClient, { enableTracking, ...context }: ContextWithTracking); /** * Gets the "other" context associated with the client. * * @returns The "other" context or `undefined` if it is not set. **/ get otherContext(): Record | undefined; /** * Gets the user associated with the client. * * @returns The user or `undefined` if it is not set. **/ get user(): { [k: string]: any; id: string | number | undefined; name?: string | undefined; email?: string | undefined; avatar?: string | undefined; } | undefined; /** * Gets the company associated with the client. * * @returns The company or `undefined` if it is not set. **/ get company(): { [k: string]: any; id: string | number | undefined; name?: string | undefined; avatar?: string | undefined; } | undefined; /** * Get features for the user/company/other context bound to this client. * Meant for use in serialization of features for transferring to the client-side/browser. * * @returns Features for the given user/company and whether each one is enabled or not */ getFeatures(): TypedFeatures; /** * Get a specific feature for the user/company/other context bound to this client. * Using the `isEnabled` property sends a `check` event to Bucket. * * @param key - The key of the feature to get. * * @returns Features for the given user/company and whether each one is enabled or not */ getFeature(key: TKey): TypedFeatures[TKey]; /** * Get remotely evaluated feature for the user/company/other context bound to this client. * * @returns Features for the given user/company and whether each one is enabled or not */ getFeaturesRemote(): Promise>>; /** * Get remotely evaluated feature for the user/company/other context bound to this client. * * @param key - The key of the feature to get. * * @returns Feature for the given user/company and key and whether it's enabled or not */ getFeatureRemote(key: string): Promise>; /** * Track an event in Bucket. * * @param event - The event to track. * @param options - The options for the event. * @param options.attributes - The attributes of the event (optional). * @param options.meta - The meta context associated with tracking (optional). * @param options.companyId - Optional company ID for the event (optional). * * @throws An error if the event is invalid or the options are invalid. */ track(event: string, options?: TrackOptions & { companyId?: string; }): Promise; /** * Create a new client bound with the additional context. * Note: This performs a shallow merge for user/company/other individually. * * @param context - The context to bind the client to. * @param context.user - The user to bind the client to. * @param context.company - The company to bind the client to. * @param context.other - The other context to bind the client to. * @param context.enableTracking - Whether to enable tracking for the client. * @param context.meta - The meta context to bind the client to. * * @returns new client bound with the additional context */ bindClient({ user, company, other, enableTracking, meta, }: ContextWithTracking): BoundBucketClient; /** * Flushes the batch buffer. */ flush(): Promise; } //# sourceMappingURL=client.d.ts.map