@nodeswork/sbase
Version:
Basic REST api foundation from Nodeswork.
84 lines (83 loc) • 3.73 kB
TypeScript
import 'reflect-metadata';
import * as Router from 'koa-router';
import { IHandlerOptions } from './declarations';
import { OverrideRule } from './overrides';
import { ParamsOptions } from './params';
export declare function Config(options: Router.IRouterOptions): (cls: any) => void;
export declare function Handler(options?: IHandlerOptions): PropertyDecorator;
export declare function Get(path: string, options?: IHandlerOptions): PropertyDecorator;
export declare function Post(path: string, options?: IHandlerOptions): PropertyDecorator;
export declare function Put(path: string, options?: IHandlerOptions): PropertyDecorator;
export declare function Delete(path: string, options?: IHandlerOptions): PropertyDecorator;
export declare function Middleware(middlewares: Router.IMiddleware | Router.IMiddleware[]): ClassDecorator & PropertyDecorator;
/**
* 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, elseClause?: Router.IMiddleware): ClassDecorator & PropertyDecorator;
/**
* 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): ClassDecorator & PropertyDecorator;
/**
* 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;
/**
* A Tee middleware that helps execute a side function.
* Usage:
*
* @Tee((ctx: Router.IRouterContext) => console.log(ctx.path))
*
* @param fn - The side function to execute.
* @param opts.post - specifies run tee after returned.
*/
export declare function Tee(fn: (ctx: Router.IRouterContext, next: () => any) => void | Promise<void>, opts?: TeeOptions): ClassDecorator & PropertyDecorator;
export interface TeeOptions {
post?: boolean;
}
/**
* Generate an overrides middleware to help build mongoose queries.
*
* 1. Fetch query params to override query:
* @Overrides('request.query.status->query.status')
* @Overrides('request.body.status->query.status')
*
* 2. Set object to override query:
* @Overrides(['constValue', 'query.status'])
*
* 3. Extract object from ctx:
* @Overrides([(ctx) =>
* moment(ctx.request.query.date).startOf('month').toDate(),
* 'query.date',
* ])
*/
export declare const Overrides: (...rules: OverrideRule[]) => ClassDecorator & PropertyDecorator;
/**
* Clear existing overrides.
*
* @ClearOverrides()
*/
export declare const ClearOverrides: () => ClassDecorator & PropertyDecorator;
export declare const Params: (options: ParamsOptions) => ClassDecorator & PropertyDecorator;