import { Evaluation } from "./evaluation"; import { BooleanEvaluationResponse, EvaluationRequest, VariantEvaluationResponse } from "./evaluation/models"; import { Flags } from "./flags"; // Re-export all evaluation models export { EvaluationRequest, VariantEvaluationResponse, BooleanEvaluationResponse, BatchEvaluationRequest, BatchEvaluationResponse, EvaluationResponse, EvaluationResponseType, EvaluationReason, ErrorEvaluationReason } from "./evaluation/models"; // Re-export all flag models export { Flag, FlagType, Variant, ListFlagsRequest, ListFlagsResponse } from "./flags/models"; interface FliptClientOptions { url?: string; authenticationStrategy?: AuthenticationStrategy; timeout?: number; headers?: Record; } export interface AuthenticationStrategy { authenticate(): Map; } export class ClientTokenAuthentication implements AuthenticationStrategy { private clientToken: string; public constructor(clientToken: string) { this.clientToken = clientToken; } public authenticate(): Map { return new Map([["Authorization", `Bearer ${this.clientToken}`]]); } } export class JWTAuthentication implements AuthenticationStrategy { private jwtToken: string; public constructor(jwtToken: string) { this.jwtToken = jwtToken; } public authenticate(): Map { return new Map([["Authorization", `JWT ${this.jwtToken}`]]); } } const defaultURL = "http://localhost:8080"; const defaultFliptClientOptions: FliptClientOptions = { url: defaultURL }; export class FliptClient { public evaluation: Evaluation; public flags: Flags; public constructor(options?: FliptClientOptions) { const clientOptions = { ...defaultFliptClientOptions }; if (options?.url !== undefined) { clientOptions.url = options.url; } if (options?.timeout !== undefined) { clientOptions.timeout = options.timeout; } if (options?.authenticationStrategy !== undefined) { clientOptions.authenticationStrategy = options.authenticationStrategy; } if (options?.headers !== undefined) { clientOptions.headers = options.headers; } this.evaluation = new Evaluation( clientOptions.url || defaultURL, clientOptions.timeout, clientOptions.authenticationStrategy, clientOptions.headers ); this.flags = new Flags( clientOptions.url || defaultURL, clientOptions.timeout, clientOptions.authenticationStrategy, clientOptions.headers ); } } export class FliptMetrics { evaluationClient: Evaluation; datadogRum: any; constructor(evaluationClient: Evaluation, datadogRum: any) { this.evaluationClient = evaluationClient; this.datadogRum = datadogRum; } public async boolean( request: EvaluationRequest ): Promise { const response = await this.evaluationClient.boolean(request); this.datadogRum.addFeatureFlagEvaluation( `${request.namespaceKey}/${request.flagKey}`, response.enabled ); return response; } public async variant( request: EvaluationRequest ): Promise { const response = await this.evaluationClient.variant(request); this.datadogRum.addFeatureFlagEvaluation( `${request.namespaceKey}/${request.flagKey}`, response.variantKey ); return response; } }