import { Hookified } from "hookified"; //#region src/types.d.ts /** * Custom attributes that can contain any key-value pairs. */ type CustomAttributes = Record; /** * User information for evaluation context. */ type ToggleUser = { /** * Unique identifier for the user. */ id: string; /** * User's email address. */ email?: string; /** * User's display name. */ name?: string; /** * Custom attributes specific to the user. */ customAttributes?: CustomAttributes; }; /** * Evaluation context for Hyphen feature toggle evaluation. * * @example * ```typescript * const context: ToggleContext = { * targetingKey: "user-123", * ipAddress: "203.0.113.42", * customAttributes: { * subscriptionLevel: "premium", * region: "us-east", * }, * user: { * id: "user-123", * email: "john.doe@example.com", * name: "John Doe", * customAttributes: { * role: "admin", * }, * }, * }; * ``` */ type ToggleContext = { /** * Primary key used for targeting and bucketing. */ targetingKey?: string; /** * IP address of the user making the request. */ ipAddress?: string; /** * Custom attributes for evaluation context. */ customAttributes?: CustomAttributes; /** * User information for evaluation. */ user?: ToggleUser; }; type GetOptions = { context?: ToggleContext; cache?: boolean; }; declare enum ToggleEvents { Error = "error" } //#endregion //#region src/index.d.ts type ToggleOptions = { /** * public api key */ publicApiKey?: string; /** * The default context to use when once is not passed */ defaultContext?: ToggleContext; /** * Horizon Endpoint Urls to use for Toggle. This will place these urls to be * load balanced. If endpoints fail it will attempt to use the default horizon * endpoint service. * @see {@link https://hyphen.ai/horizon} for more information */ horizonUrls?: string[]; applicationId?: string; environment?: string; defaultTargetKey?: string; }; declare class Toggle extends Hookified { private _publicApiKey?; private _organizationId; private _applicationId; private _environment; private _horizonUrls; private _defaultContext; private _defaultTargetingKey; constructor(options?: ToggleOptions); /** * Gets the public API key used for authentication. * * @returns The current public API key or undefined if not set */ get publicApiKey(): string | undefined; /** * Sets the public API key used for authentication. * * @param value - The public API key string or undefined to clear * @throws {Error} If the key doesn't start with "public_" */ set publicApiKey(value: string | undefined); /** * Gets the default context used for toggle evaluations. * * @returns The current default ToggleContext */ get defaultContext(): ToggleContext | undefined; /** * Sets the default context used for toggle evaluations. * * @param value - The ToggleContext to use as default */ set defaultContext(value: ToggleContext); /** * Gets the organization ID extracted from the public API key. * * @returns The organization ID string or undefined if not available */ get organizationId(): string | undefined; /** * Gets the Horizon endpoint URLs used for load balancing. * * These URLs are used to distribute requests across multiple Horizon endpoints. * If endpoints fail, the system will attempt to use the default horizon endpoint service. * * @returns Array of Horizon endpoint URLs * @see {@link https://hyphen.ai/horizon} for more information */ get horizonUrls(): string[]; /** * Sets the Horizon endpoint URLs for load balancing. * * Configures multiple Horizon endpoints that will be used for load balancing. * When endpoints fail, the system will fall back to the default horizon endpoint service. * * @param value - Array of Horizon endpoint URLs or empty array to clear * @see {@link https://hyphen.ai/horizon} for more information * * @example * ```typescript * const toggle = new Toggle(); * toggle.horizonUrls = [ * 'https://org1.toggle.hyphen.cloud', * 'https://org2.toggle.hyphen.cloud' * ]; * ``` */ set horizonUrls(value: string[]); /** * Gets the application ID used for toggle context. * * @returns The current application ID or undefined if not set */ get applicationId(): string | undefined; /** * Sets the application ID used for toggle context. * * @param value - The application ID string or undefined to clear */ set applicationId(value: string | undefined); /** * Gets the environment used for toggle context. * * @returns The current environment (defaults to 'development') */ get environment(): string | undefined; /** * Sets the environment used for toggle context. * * @param value - The environment string or undefined to clear */ set environment(value: string | undefined); /** * Gets the default targeting key used for toggle evaluations. * * @returns The current default targeting key or undefined if not set */ get defaultTargetingKey(): string; /** * Sets the default targeting key used for toggle evaluations. * * @param value - The targeting key string or undefined to clear */ set defaultTargetingKey(value: string); get(toggleKey: string, defaultValue: T, options?: GetOptions): Promise; /** * Retrieves a boolean toggle value. * * This is a convenience method that wraps the generic get() method with boolean type safety. * * @param toggleKey - The key of the toggle to retrieve * @param defaultValue - The boolean value to return if the toggle is not found or an error occurs * @param options - Optional configuration including context for toggle evaluation * @returns Promise resolving to the boolean toggle value or defaultValue * * @example * ```typescript * const toggle = new Toggle({ publicApiKey: 'public_key', applicationId: 'app-id' }); * const isFeatureEnabled = await toggle.getBoolean('feature-flag', false); * console.log(isFeatureEnabled); // true or false * ``` */ getBoolean(toggleKey: string, defaultValue: boolean, options?: GetOptions): Promise; /** * Retrieves a string toggle value. * * This is a convenience method that wraps the generic get() method with string type safety. * * @param toggleKey - The key of the toggle to retrieve * @param defaultValue - The string value to return if the toggle is not found or an error occurs * @param options - Optional configuration including context for toggle evaluation * @returns Promise resolving to the string toggle value or defaultValue * * @example * ```typescript * const toggle = new Toggle({ publicApiKey: 'public_key', applicationId: 'app-id' }); * const message = await toggle.getString('welcome-message', 'Hello World'); * console.log(message); // 'Welcome to our app!' or 'Hello World' * ``` */ getString(toggleKey: string, defaultValue: string, options?: GetOptions): Promise; /** * Retrieves an object toggle value. * * This is a convenience method that wraps the generic get() method with object type safety. * Note that the toggle service may return JSON as a string, which should be parsed if needed. * * @template T - The expected object type * @param toggleKey - The key of the toggle to retrieve * @param defaultValue - The object value to return if the toggle is not found or an error occurs * @param options - Optional configuration including context for toggle evaluation * @returns Promise resolving to the object toggle value or defaultValue * * @example * ```typescript * const toggle = new Toggle({ publicApiKey: 'public_key', applicationId: 'app-id' }); * const config = await toggle.getObject('app-config', { theme: 'light' }); * console.log(config); // { theme: 'dark', features: ['a', 'b'] } or { theme: 'light' } * ``` */ getObject(toggleKey: string, defaultValue: T, options?: GetOptions): Promise; /** * Retrieves a number toggle value. * * This is a convenience method that wraps the generic get() method with number type safety. * * @param toggleKey - The key of the toggle to retrieve * @param defaultValue - The number value to return if the toggle is not found or an error occurs * @param options - Optional configuration including context for toggle evaluation * @returns Promise resolving to the number toggle value or defaultValue * * @example * ```typescript * const toggle = new Toggle({ publicApiKey: 'public_key', applicationId: 'app-id' }); * const maxRetries = await toggle.getNumber('max-retries', 3); * console.log(maxRetries); // 5 or 3 * ``` */ getNumber(toggleKey: string, defaultValue: number, options?: GetOptions): Promise; /** * Makes an HTTP POST request to the specified URL with automatic authentication. * * This method uses browser-compatible fetch and automatically includes the * public API key in the x-api-key header if available. It supports load * balancing across multiple horizon URLs with fallback behavior. * * @template T - The expected response type * @param path - The API path to request (e.g., '/api/toggles') * @param payload - The JSON payload to send in the request body * @param options - Optional fetch configuration * @returns Promise resolving to the parsed JSON response * @throws {Error} If no horizon URLs are configured or all requests fail * * @example * ```typescript * const toggle = new Toggle({ * publicApiKey: 'public_your-key-here', * horizonUrls: ['https://api.hyphen.cloud'] * }); * * interface ToggleResponse { * enabled: boolean; * value: string; * } * * const result = await toggle.fetch('/api/toggle/feature-flag', { * context: { targetingKey: 'user-123' } * }); * console.log(result.enabled); // true/false * ``` */ fetch(path: string, payload?: unknown, options?: RequestInit): Promise; /** * Validates and sets the public API key. This is used internally * * @param key - The public API key string or undefined to clear * @throws {Error} If the key doesn't start with "public_" */ setPublicKey(key: string | undefined): void; /** * Extracts the organization ID from a public API key. * * The public key format is: `public_` * The base64 data contains: `orgId:secretData` * Only alphanumeric characters, underscores, and hyphens are considered valid in org IDs. * * @param publicKey - The public API key to extract the organization ID from * @returns The organization ID if valid and extractable, undefined otherwise * * @example * ```typescript * const toggle = new Toggle(); * const orgId = toggle.getOrgIdFromPublicKey('public_dGVzdC1vcmc6c2VjcmV0'); * console.log(orgId); // 'test-org' * ``` */ getOrgIdFromPublicKey(publicKey: string): string | undefined; /** * Builds the default Horizon API URL for the given public key. * * If a valid organization ID can be extracted from the public key, returns an * organization-specific URL. Otherwise, returns the default fallback URL. * * @param publicKey - The public API key to build the URL for * @returns Organization-specific URL or default fallback URL * * @example * ```typescript * const toggle = new Toggle(); * * // With valid org ID * const orgUrl = toggle.buildDefaultHorizonUrl('public_dGVzdC1vcmc6c2VjcmV0'); * console.log(orgUrl); // 'https://test-org.toggle.hyphen.cloud' * * // With invalid key * const defaultUrl = toggle.buildDefaultHorizonUrl('invalid-key'); * console.log(defaultUrl); // 'https://toggle.hyphen.cloud' * ``` */ getDefaultHorizonUrl(publicKey?: string): string; /** * Will get the urls. If you pass in the public key it will provide two urls. * @param publicKey * @returns */ getDefaultHorizonUrls(publicKey?: string): string[]; /** * Generates a unique targeting key based on available context. * * @returns A targeting key in the format: `[app]-[env]-[random]` or simplified versions */ generateTargetKey(): string; /** * Extracts targeting key from a toggle context with fallback logic. * * @param context - The toggle context to extract targeting key from * @returns The targeting key string */ private getTargetingKey; } //#endregion export { Toggle, type ToggleContext, ToggleEvents, ToggleOptions }; //# sourceMappingURL=index.d.ts.map