UNPKG

smart-saga-pattern

Version:

A library implementing the Saga pattern for microservice architecture

67 lines (66 loc) 2.27 kB
import { Transaction } from "../core/Transaction"; import { Compensation } from "../core/Compensation"; import { CompensationFunction, TransactionFunction } from "../types"; /** * Represents a step in a Saga definition * @template TInput Type of the input data required by the step * @template TOutput Type of the output data produced by the step */ export interface SagaStep<TInput = any, TOutput = any> { /** * The transaction to execute for this step */ transaction: Transaction<TInput, TOutput>; /** * The compensation to execute if the saga fails after this step */ compensation?: Compensation<TInput, TOutput>; /** * Whether this step requires compensation in case of failure * @default true */ requiresCompensation?: boolean; } /** * Builder for creating Saga definitions * @template T Type of the input data * @template R Type of the result data (combined from all steps) */ export declare class SagaDefinitionBuilder<T = any, R = any> { private steps; /** * Adds a step to the Saga definition * @param name Step name * @param transaction Transaction function * @param compensation Optional compensation function * @param requiresCompensation Whether this step requires compensation in case of failure */ step<TStepOutput = any>(name: string, transaction: TransactionFunction<T, TStepOutput>, compensation?: CompensationFunction<T, TStepOutput>, requiresCompensation?: boolean): SagaDefinitionBuilder<T, R>; /** * Builds the Saga definition */ build(): SagaDefinition<T, R>; } /** * Represents a Saga definition * @template T Type of the input data * @template R Type of the result data (combined from all steps) */ export declare class SagaDefinition<T = any, R = any> { private steps; /** * Creates a new Saga definition * @param steps Steps in the Saga */ constructor(steps: SagaStep[]); /** * Gets all steps in the Saga definition */ getSteps(): SagaStep[]; /** * Creates a new builder for a Saga definition * @template T Type of the input data * @template R Type of the result data */ static builder<T = any, R = any>(): SagaDefinitionBuilder<T, R>; }