import { Entry, CommandMessage, ActionMessage } from './messages'; import DataSource from './data-source'; export declare type DispatchDelegate = (message: TMessage) => Promise; export declare enum MiddlewareHooks { Connect = "connect", Disconnect = "disconnect", Command = "command", Respond = "respond", Write = "write", Process = "process" } /** * Context for an execution chain shared between middlewares. */ export declare type DispatchContext = { /** * Stack of middlewares to be executed when delegating to `next()`. * Middlewares can be removed from the stack to prevent them from executing. */ stack: Middleware[]; /** * A bag of properties shared between middlewares. Use this to pass messages to subsequent * middlewares in the execution chain. */ properties: { [key: string]: any; }; }; export default interface Middleware { name?: string; /** * Hook executed when the `DataSource` is connected. */ connect?: (next: DispatchDelegate, dataSource: DataSource, context: DispatchContext) => (data?: any) => void | Promise; /** * Hook executed when the `DataSource` is disconnected. */ disconnect?: (next: DispatchDelegate, dataSource: DataSource, context: DispatchContext) => (data?: any) => void | Promise; /** * Hook executed when the `DataSource` dispatched a command. */ command?: (next: DispatchDelegate, dataSource: DataSource, context: DispatchContext) => (message: CommandMessage) => void | Promise; /** * Hook executed when the `DataSource` responds with an action. */ respond?: (next: DispatchDelegate, dataSource: DataSource, context: DispatchContext) => (message: ActionMessage) => void | Promise; /** * Hook executed when the `DataSource` is written to. */ write?: (next: DispatchDelegate, dataSource: DataSource, context: DispatchContext) => (entries: Entry[]) => Entry[] | Promise; /** * Hook executed when the `DataSource` processes its stream of entries. */ process?: (next: DispatchDelegate, dataSource: DataSource, context: DispatchContext) => (entries: Entry[]) => Entry[] | Promise; } export declare function callMiddleware(dataSource: DataSource, method: string, data: any): Promise; export interface AddMiddlewareOptions { before?: string; after?: string; } export declare function addMiddleware(dataSource: DataSource, middleware: Middleware, options: AddMiddlewareOptions): void; //# sourceMappingURL=middleware.d.ts.map