smart-saga-pattern
Version:
A library implementing the Saga pattern for microservice architecture
64 lines (63 loc) • 1.9 kB
TypeScript
import { Saga } from '../core/Saga';
import { SagaParticipant } from './SagaParticipant';
import { SagaContext, SagaOptions, StateStore } from '../types';
/**
* Options for a StructuredSagaCoordinator
*/
export interface StructuredSagaCoordinatorOptions extends SagaOptions {
name: string;
subscribeTopic: string;
publishTopic: string;
}
/**
* Coordinates a choreography-based Saga with structured context
* The context is structured as:
* {
* global: { ... },
* steps: {
* "Step Name": {
* input: { ... },
* output: { ... },
* compensation: { ... }
* }
* }
* }
* @template T Type of the input data
* @template R Type of the result data
*/
export declare class StructuredSagaCoordinator<T = Record<string, any>, R = Record<string, any>> extends Saga<T & R> {
private participant;
private stateStore?;
private completionPromise?;
private completionResolve?;
private completionReject?;
/**
* Creates a new StructuredSagaCoordinator
* @param participant Saga participant for communication
* @param options Options for the coordinator
* @param stateStore Optional state store for persistence
*/
constructor(participant: SagaParticipant, options: StructuredSagaCoordinatorOptions, stateStore?: StateStore<T & R>);
/**
* Executes the Saga
* @param initialData Initial data for the Saga
*/
execute(initialData?: Partial<T>): Promise<SagaContext<T & R>>;
/**
* Structures the initial data into the format:
* {
* global: initialData,
* steps: {}
* }
* @param initialData Initial data
*/
private structureInitialData;
/**
* Compensates the Saga (rolls back)
*/
compensate(): Promise<SagaContext<T & R>>;
/**
* Sets up event handlers for the coordinator
*/
private setupEventHandlers;
}