UNPKG

smart-saga-pattern

Version:

A library implementing the Saga pattern for microservice architecture

167 lines (166 loc) 4.46 kB
/** * Represents the status of a Saga */ export declare enum SagaStatus { STARTED = "STARTED", EXECUTING = "EXECUTING", COMPENSATING = "COMPENSATING", COMPLETED = "COMPLETED", FAILED = "FAILED", COMPENSATED = "COMPENSATED", COMPENSATION_FAILED = "COMPENSATION_FAILED" } /** * Represents the status of a Transaction */ export declare enum TransactionStatus { PENDING = "PENDING", EXECUTING = "EXECUTING", COMPLETED = "COMPLETED", FAILED = "FAILED" } /** * Represents a message in the Saga system */ export interface SagaMessage { id: string; type: string; payload: any; sagaId: string; timestamp: number; } /** * Represents an event in the Saga system */ export interface SagaEvent extends SagaMessage { source: string; } /** * Represents a command in the Saga system */ export interface SagaCommand extends SagaMessage { destination: string; } /** * Represents a reply in the Saga system */ export interface SagaReply extends SagaMessage { success: boolean; error?: string; } /** * Represents the context of a Saga * @template T Type of the data in the context */ export interface SagaContext<T = Record<string, any>> { sagaId: string; data: T; status: SagaStatus; startTime: number; endTime?: number; error?: string; } /** * Represents the options for a Saga */ export interface SagaOptions { id?: string; timeout?: number; retryCount?: number; retryDelay?: number; } /** * Metadata for a transaction step */ export interface TransactionMetadata { /** * Unique identifier for the transaction */ id: string; /** * Name of the transaction */ name: string; /** * Start time of the transaction */ startTime?: number; /** * End time of the transaction */ endTime?: number; /** * Whether the transaction requires compensation in case of failure */ requiresCompensation: boolean; } /** * Represents a function that can be used as a transaction step * @template TInput Type of the input data required by the transaction * @template TOutput Type of the output data produced by the transaction */ export type TransactionFunction<TInput = any, TOutput = any> = (context: TInput, metadata: TransactionMetadata) => Promise<TOutput>; /** * Metadata for a compensation step */ export interface CompensationMetadata { /** * Unique identifier for the compensation */ id: string; /** * Name of the compensation */ name: string; /** * Start time of the compensation */ startTime?: number; /** * End time of the compensation */ endTime?: number; /** * ID of the transaction being compensated */ transactionId: string; /** * Name of the transaction being compensated */ transactionName: string; } /** * Represents a function that can be used as a compensation step * @template TInput Type of the input data required for compensation * @template TTransactionOutput Type of the output data produced by the transaction being compensated */ export type CompensationFunction<TInput = any, TTransactionOutput = any> = (context: TInput, transactionOutput: TTransactionOutput | undefined, metadata: CompensationMetadata) => Promise<void>; /** * Represents a message broker adapter */ export interface MessageBrokerAdapter { publish(topic: string, message: SagaMessage): Promise<void>; subscribe(topic: string, handler: (message: SagaMessage) => Promise<void>): Promise<void>; unsubscribe(topic: string): Promise<void>; connect(): Promise<void>; disconnect(): Promise<void>; } /** * Represents a state store for Saga state * @template T Type of the data in the context */ export interface StateStore<T = Record<string, any>> { save(sagaId: string, context: SagaContext<T>): Promise<void>; load(sagaId: string): Promise<SagaContext<T> | null>; update(sagaId: string, context: Partial<SagaContext<T>>): Promise<void>; delete(sagaId: string): Promise<void>; } /** * Represents a logger for the Saga system */ export interface SagaLogger { debug(message: string, meta?: Record<string, any>): void; info(message: string, meta?: Record<string, any>): void; warn(message: string, meta?: Record<string, any>): void; error(message: string, error?: Error, meta?: Record<string, any>): void; }