smart-saga-pattern
Version:
A library implementing the Saga pattern for microservice architecture
52 lines (51 loc) • 1.39 kB
TypeScript
import { SagaContext, SagaOptions, SagaStatus } from "../types";
/**
* Base class for all Sagas
* @template T Type of the data in the context
*/
export declare abstract class Saga<T = Record<string, any>> {
protected id: string;
protected context: SagaContext<T>;
protected options: SagaOptions;
/**
* Creates a new Saga instance
* @param options Options for the Saga
*/
constructor(options?: SagaOptions, initialData?: T);
/**
* Gets the ID of the Saga
*/
getId(): string;
/**
* Gets the context of the Saga
*/
getContext(): SagaContext<T>;
/**
* Updates the context data of the Saga
* @param data Data to update in the context
*/
updateContext(data: Partial<T>): void;
/**
* Sets the status of the Saga
* @param status New status
* @param error Optional error message
*/
setStatus(status: SagaStatus, error?: string): void;
/**
* Checks if the Saga is completed
*/
isCompleted(): boolean;
/**
* Checks if the Saga has failed
*/
isFailed(): boolean;
/**
* Executes the Saga
* @param initialData Initial data for the Saga
*/
abstract execute(initialData?: Partial<T>): Promise<SagaContext<T>>;
/**
* Compensates the Saga (rolls back)
*/
abstract compensate(): Promise<SagaContext<T>>;
}