import { Async, Constructor } from "@bonbons/contracts"; import * as c from "@bonbons/contracts/dist/src/private-api"; /** * Base BONBONS Pipe * --- * * you should always extends this Class * * contains input params and request/response context support * * @description * @author Big Mogician * @export * @abstract * @class PipeMiddleware * @implements {IPipe} * @template T */ export abstract class PipeMiddleware implements c.IPipe { public readonly params!: T; constructor() { } public readonly context!: c.IBonbonsContext; abstract process(): Async; protected break(): c.PipeProcessResult { return { breakOut: true }; } } /** * Bonbons Pipe Factory Generator * --- * use this generator to create factory and params bundle. */ export const PipeFactory = { /** * Create a generic pipe * ----- * Create a bundle with pipe which input params is a typed-object. * @description * @author Big Mogician * @template T * @param {Constructor>} type * @returns */ generic(type: Constructor>) { return createGenericPipeFactory(type); }, /** * Create a array pipe * ----- * Create a bundle with pipe which input params is an array. * @description * @author Big Mogician * @template T * @param {Constructor>} type * @returns */ fromArray(type: Constructor>) { return createArrayPipeFactory(type); }, /** * * Create a common pipe * ----- * Create a bundle with pipe which input params is an object. * @description * @author Big Mogician * @template T * @param {Constructor>} type * @returns */ fromMap(type: Constructor>) { return createPipeFactory(type); } }; function createGenericPipeFactory(target: Constructor>): (params: T) => c.IPipeBundle { return resolvePipe(target); } function createArrayPipeFactory(target: Constructor>) { return resolvePipe(target); } function createPipeFactory(target: Constructor>): (params: T) => c.IPipeBundle { return resolvePipe(target); } function resolvePipe(target: Constructor>): (params: T) => c.IPipeBundle; function resolvePipe(target: Constructor>): (params: T) => c.IPipeBundle; function resolvePipe(target: Constructor>) { return (params) => ({ params, target }); }