UNPKG

@ark7/router

Version:
53 lines (52 loc) 3.08 kB
import * as Router from 'koa-router'; /** * A IF middleware that helps a branch checking. Usage: * * @If(predictor, ifClause) * - execute ifClause when predictor returns true. * - execute next middleware when predictor returns false. * * @If(predictor, ifClause, elseClause) * - execute ifClause when predictor returns true. * - execute elseClause when predictor returns false. * * @param predictor - Predict which clause to execute. * @param ifClause - Execute when predictor returns true. * @param elseClause - Execute when predictor returns false. By default, execute * else clause. */ export declare function If(predictor: (ctx: Router.IRouterContext) => boolean | Promise<boolean>, ifClause: Router.IMiddleware | Router.IMiddleware[], elseClause?: Router.IMiddleware | Router.IMiddleware[]): ClassDecorator & PropertyDecorator & MethodDecorator; export declare function _if(predictor: (ctx: Router.IRouterContext) => boolean | Promise<boolean>, ifClause: Router.IMiddleware | Router.IMiddleware[], elseClause?: Router.IMiddleware | Router.IMiddleware[]): (ctx: Router.IRouterContext, next: () => any) => Promise<void>; /** * A WHEN middleware that helps a branch checking, next middleware will always * be executed regardless what predictor returns. * * @param predictor - Predict when the when clause will be executed. * @param whenClause - Execute when predictor returns true. */ export declare function When(predictor: (ctx: Router.IRouterContext) => boolean | Promise<boolean>, whenClause: Router.IMiddleware | Router.IMiddleware[]): ClassDecorator & PropertyDecorator & MethodDecorator; export declare function _when(predictor: (ctx: Router.IRouterContext) => boolean | Promise<boolean>, whenClause: Router.IMiddleware | Router.IMiddleware[]): (ctx: Router.IRouterContext, next: () => any) => Promise<void>; /** * A CHECK middleware that helps determines if to execute next middleware. * Usage: * * @Check((ctx: Router.IRouterContext) => ctx.isAuthenticated()) * * @param predictor - Returns true to execute next middleware. */ export declare function Check(predictor: (ctx: Router.IRouterContext) => boolean | Promise<boolean>): ClassDecorator & PropertyDecorator & MethodDecorator; export declare function _check(predictor: (ctx: Router.IRouterContext) => boolean | Promise<boolean>): (ctx: Router.IRouterContext, next: () => any) => Promise<void>; /** * A Tee middleware that helps execute a side function. * Usage: * * @Tee((ctx) => console.log(ctx.path)) * * @param fn - The side function to execute. * @param opts.post - specifies run tee after returned. */ export declare function Tee<T extends Router.IRouterContext>(fn: (ctx: T, next: () => any) => void | Promise<void>, opts?: TeeOptions): ClassDecorator & PropertyDecorator & MethodDecorator; export declare function TeePost<T extends Router.IRouterContext>(fn: (ctx: T, next: () => any) => void | Promise<void>): ClassDecorator & PropertyDecorator & MethodDecorator; export interface TeeOptions { post?: boolean; }