pipio
Version:
Chain functions together and handle a complex workflow
40 lines • 1.41 kB
TypeScript
export type SyncPipioHandler<T, U> = (req: T) => U;
export type AsyncPipioHandler<T, U> = (req: T) => Promise<U>;
export type PipioHandler<T, U> = SyncPipioHandler<T, U> | AsyncPipioHandler<T, U>;
export type BuildParams = {
/**
* Error handler
* @param {Error|unknown} err Thrown error object
* @returns The pipeline output or throw error
*/
onError?: (err: Error | any) => any;
};
export declare class Pipio<U> {
protected readonly handlers: [
...PipioHandler<any, any>[],
PipioHandler<any, U>
];
constructor(handlers: [
...PipioHandler<any, any>[],
PipioHandler<any, U>
]);
/**
* Add a new middleware to the pipeline
* @param fn Middleware function
* @returns {Pipio} A new instance of the pipio with the handler setup
*/
use<TNextResponse>(fn: PipioHandler<Awaited<U>, Awaited<TNextResponse>>): Pipio<TNextResponse>;
/**
* Build the pipeline and create a callable function
* @param params Build-time parameters
* @returns The wrapper function of the pipeline
*/
build(params?: BuildParams): (...args: any[]) => Promise<any>;
}
/**
* pipio create factory
* @param fn Main middleware
* @returns {Pipio} A new instance of pipio with the first handler configured
*/
export declare const pipio: <U>(fn: PipioHandler<any[], U>) => Pipio<U>;
//# sourceMappingURL=pipio.d.ts.map