/** * Ductape Resilience Types * * Type definitions for healthchecks, quotas, and fallbacks. * Aligned with backend types in productsBuilder.types.ts */ import { StepEventTypes, IStepInput, IActionRequest, ProviderStatus as BackendProviderStatus } from '../../types'; export { IStepInput }; export { BackendProviderStatus as ProviderStatus }; /** * Type of probe for healthchecks */ export declare enum ProbeType { APP = "app", DATABASE = "database", FEATURE = "feature", GRAPH = "graph", EVENTS = "events", /** @deprecated Use EVENTS. */ MESSAGE_BROKER = "message_broker", STORAGE = "storage", CACHE = "cache", VECTOR = "vector", NOTIFICATION = "notification" } /** * Type of resilience provider event */ export type ResilienceEventType = StepEventTypes.ACTION; export { StepEventTypes }; /** * Healthcheck probe configuration (for code-first API) */ export interface IHealthcheckProbe { type: ProbeType; app?: string; database?: string; feature?: string; /** @deprecated Use feature */ workflow?: string; graph?: string; events?: string; /** @deprecated Use events. */ messageBroker?: string; /** @deprecated Use events. */ message_broker?: string; storage?: string; cache?: string; vector?: string; notification?: string; channels?: Array<'sms' | 'email' | 'push'>; event?: string; input?: Record; } /** * Healthcheck environment configuration * Matches backend CheckEnvStatus interface */ export interface IHealthcheckEnvConfig { slug: string; status?: string; lastAvailable?: string; lastChecked?: string; lastLatency?: string; averageLatency?: string; input?: any; } /** * Webhook configuration for failure notifications */ export interface IHealthcheckWebhook { url: string; method?: 'GET' | 'POST' | 'PUT' | 'PATCH'; headers?: Record; body?: Record; } /** * Notification channel configuration */ export interface INotificationChannel { push?: { recipients?: string[]; }; email?: { recipients: string[]; }; sms?: { recipients: string[]; }; callback?: { data?: Record; }; } /** * Notification configuration for healthcheck failures */ export interface IHealthcheckNotification { notification: string; message: string; channels: INotificationChannel; } /** * Event emission configuration */ export interface IHealthcheckEmit { event: string; data?: Record; } /** * On-failure configuration for healthchecks */ export interface IHealthcheckOnFailure { notifications?: IHealthcheckNotification[]; webhooks?: IHealthcheckWebhook[]; emit?: IHealthcheckEmit[]; } /** * Healthcheck definition schema * Compatible with backend IProductAppHealth */ export interface IHealthcheckSchema { _id?: string; tag: string; name?: string; description?: string; app?: string; event?: string; probe?: IHealthcheckProbe; interval: number; retries: number; onFailure?: IHealthcheckOnFailure; envs: IHealthcheckEnvConfig[]; } /** * Healthcheck status response */ export type HealthStatus = 'healthy' | 'degraded' | 'unhealthy' | 'unknown' | 'never_run' | 'stale'; export interface IHealthcheckStatus { status: HealthStatus; checkedAt?: string; validUntil?: string; nextCheckAt?: string; latencyMs?: number; attempts?: number; consecutiveFailures?: number; revision?: string; failureCode?: string; lastAvailable?: string; lastChecked?: string; lastLatency?: number; averageLatency?: number; } /** * Healthcheck run result */ export interface IHealthcheckRunResult { status: string; latency: number; checkedAt: string; error?: string; } /** * Quota provider option configuration * Compatible with backend IQuotaOptions (extends IFallbackOptions) * Note: Features are deprecated - use workflows instead */ export interface IQuotaOption { quota: number; uses?: number; provider?: string; app?: string; type: StepEventTypes.ACTION; event: string; input: IActionRequest | Record; output: Record; retries: number; healthcheck?: string; provider_status?: BackendProviderStatus; last_available?: boolean; last_checked?: Date; check_interval?: number; } /** * Quota definition schema * Compatible with backend IProductQuota */ export interface IQuotaSchema { _id?: string; name?: string; description?: string; tag: string; input: Record; total_quota?: number; total_init?: number; options: IQuotaOption[]; } /** * Quota run options */ export interface IQuotaRunOptions { product: string; env: string; tag: string; input: Record; /** Session token in format: session_tag:jwt_token */ session?: string; cache?: string; } /** * Quota dispatch options */ export interface IQuotaDispatchOptions extends IQuotaRunOptions { schedule?: { cron?: string; delay?: number; at?: Date; }; } /** * Fallback provider option configuration * Compatible with backend IFallbackOptions * Note: Features are deprecated - use workflows instead */ export interface IFallbackOption { provider?: string; app?: string; type: StepEventTypes.ACTION; event: string; input: IActionRequest | Record; output: Record; retries: number; healthcheck?: string; provider_status?: BackendProviderStatus; last_available?: boolean; last_checked?: Date; check_interval?: number; } /** * Fallback definition schema * Compatible with backend IProductFallback */ export interface IFallbackSchema { _id?: string; name?: string; description?: string; tag: string; input: Record; options: IFallbackOption[]; } /** * Fallback run options */ export interface IFallbackRunOptions { product: string; env: string; tag: string; input: Record; /** Session token in format: session_tag:jwt_token */ session?: string; cache?: string; } /** * Fallback dispatch options */ export interface IFallbackDispatchOptions extends IFallbackRunOptions { schedule?: { cron?: string; delay?: number; at?: Date; }; } /** * Probe builder interface for healthchecks */ export interface IProbeBuilder { app(tag: string): IAppProbeBuilder; database(tag: string): IDatabaseProbeBuilder; feature(tag: string): IFeatureProbeBuilder; graph(tag: string): IGraphProbeBuilder; events(tag: string): IMessageBrokerProbeBuilder; /** @deprecated Use events. */ messageBroker(tag: string): IMessageBrokerProbeBuilder; storage(tag: string): IStorageProbeBuilder; cache(tag: string): ICacheProbeBuilder; vector(tag: string): IVectorProbeBuilder; notification(tag: string): INotificationProbeBuilder; } export interface IAppProbeBuilder { action(event: string): IAppProbeActionBuilder; } export interface IAppProbeActionBuilder { input(data: Record): void; } export interface IDatabaseProbeBuilder { action(event: string): IDatabaseProbeActionBuilder; } export interface IDatabaseProbeActionBuilder { input(data: Record): void; } export interface IFeatureProbeBuilder { input(data: Record): void; } export interface IGraphProbeBuilder { action(event: string): IGraphProbeActionBuilder; } export interface IGraphProbeActionBuilder { input(data: Record): void; } export interface IMessageBrokerProbeBuilder { action(event: string): IMessageBrokerProbeActionBuilder; } export interface IMessageBrokerProbeActionBuilder { input(data: Record): void; } export interface IStorageProbeBuilder { action(event: string): IStorageProbeActionBuilder; } export interface IStorageProbeActionBuilder { input(data: Record): void; } export interface ICacheProbeBuilder { action(event: string): ICacheProbeActionBuilder; } export interface ICacheProbeActionBuilder { input(data: Record): void; } export interface IVectorProbeBuilder { action(event: string): IVectorProbeActionBuilder; } export interface IVectorProbeActionBuilder { input(data: Record): void; } export interface INotificationProbeBuilder { channels(channels: Array<'sms' | 'email' | 'push'>): INotificationProbeBuilder; action(event: string): INotificationProbeActionBuilder; } export interface INotificationProbeActionBuilder { input(data: Record): void; } /** * Failure notification builder interface */ export interface IFailureNotificationBuilder { notification(tag: string): INotificationMessageBuilder; webhook(config: IHealthcheckWebhook): IFailureNotificationBuilder; emit(config: IHealthcheckEmit): IFailureNotificationBuilder; } export interface INotificationMessageBuilder { message(event: string): INotificationChannelBuilder; } export interface INotificationChannelBuilder extends IFailureNotificationBuilder { push(config?: { recipients?: string[]; }): INotificationChannelBuilder; email(config: { recipients: string[]; }): INotificationChannelBuilder; sms(config: { recipients: string[]; }): INotificationChannelBuilder; callback(config?: { data?: Record; }): INotificationChannelBuilder; } /** * Healthcheck context interface */ export interface IHealthcheckContext { probe(): IProbeBuilder; interval(ms: number): void; retries(count: number): void; env(slug: string, options?: Partial): void; onFailure(): IFailureNotificationBuilder; auth(field: string): string; token(key: string): string; variable(app: string, key: string): string; constant(app: string, key: string): string; } /** * Provider builder interface for quotas and fallbacks */ export interface IProviderBuilder> { weight(value: number): IProviderBuilder; healthcheck(tag: string): IProviderBuilder; app(tag: string): IAppProviderBuilder; database(tag: string): IDatabaseProviderBuilder; feature(tag: string): IFeatureProviderBuilder; notification(tag: string): INotificationProviderBuilder; mapInput(mapper: (input: TInput) => Record): IProviderBuilder; mapOutput(mapper: (response: unknown) => Record): IProviderBuilder; retries(count: number): IProviderBuilder; checkInterval(ms: number): IProviderBuilder; } export interface IAppProviderBuilder extends IProviderBuilder { action(event: string): IProviderBuilder; } export interface IDatabaseProviderBuilder extends IProviderBuilder { action(event: string): IProviderBuilder; } export interface IFeatureProviderBuilder extends IProviderBuilder { input(data: Record): IProviderBuilder; } export interface INotificationProviderBuilder extends IProviderBuilder { event(event: string): IProviderBuilder; } /** * Quota context interface */ export interface IQuotaContext> { readonly input: TInput; provider(name: string): IProviderBuilder; auth(field: string): string; token(key: string): string; variable(app: string, key: string): string; constant(app: string, key: string): string; } /** * Fallback provider builder interface */ export interface IFallbackProviderBuilder> extends IProviderBuilder { } /** * Fallback context interface */ export interface IFallbackContext> { readonly input: TInput; primary(name: string): IFallbackProviderBuilder; fallback(name: string): IFallbackProviderBuilder; auth(field: string): string; token(key: string): string; variable(app: string, key: string): string; constant(app: string, key: string): string; } /** * Healthcheck define options */ export interface IHealthcheckDefineOptions { product?: string; tag: string; name?: string; description?: string; handler: (ctx: IHealthcheckContext) => Promise; } /** * Quota define options */ export interface IQuotaDefineOptions> { product?: string; tag: string; name?: string; description?: string; input: Record; handler: (ctx: IQuotaContext) => Promise; } /** * Fallback define options */ export interface IFallbackDefineOptions> { product?: string; tag: string; name?: string; description?: string; input: Record; handler: (ctx: IFallbackContext) => Promise; } /** * Resilience service configuration */ export interface IResilienceServiceConfig { workspace_id: string; public_key: string; user_id: string; token: string; env_type: string; base_url?: string; /** Redis client for caching healthcheck results */ redis_client?: any; /** BullMQ queues for job scheduling */ queues?: { health?: any; jobs?: any; }; default_product?: string; default_env?: string; } /** * Defined healthcheck with compile method */ export interface IDefinedHealthcheck { tag: string; name?: string; description?: string; compile(): IHealthcheckSchema; registration?: { status: 'created' | 'unchanged' | 'updated' | 'conflict'; tag: string; revision: string; operationId: string; }; } /** * Defined quota with compile method */ export interface IDefinedQuota { tag: string; name?: string; description?: string; input: Record; compile(): IQuotaSchema; } /** * Defined fallback with compile method */ export interface IDefinedFallback { tag: string; name?: string; description?: string; input: Record; compile(): IFallbackSchema; } /** * Combined resilience definition options */ export interface IResilienceDefineOptions { tag: string; name?: string; description?: string; handler: (ctx: IResilienceContext) => Promise; } /** * Combined resilience context */ export interface IResilienceContext { healthcheck(tag: string): IHealthcheckBuilder; quota(tag: string, options: { input: Record; }): IQuotaBuilder; fallback(tag: string, options: { input: Record; }): IFallbackBuilder; } /** * Healthcheck builder in resilience context */ export interface IHealthcheckBuilder { probe(): IProbeBuilder; interval(ms: number): IHealthcheckBuilder; retries(count: number): IHealthcheckBuilder; env(slug: string, options?: Partial): IHealthcheckBuilder; onFailure(): IFailureNotificationBuilder; } /** * Quota builder in resilience context */ export interface IQuotaBuilder { provider(name: string): IProviderBuilder; } /** * Fallback builder in resilience context */ export interface IFallbackBuilder { primary(name: string): IFallbackProviderBuilder; fallback(name: string): IFallbackProviderBuilder; }