UNPKG

@zerooneit/expressive-tea

Version:
198 lines (197 loc) 9.06 kB
import { Router } from 'express'; import { ExpressiveTeaHandlerOptions } from '../libs/interfaces'; import { ExpressMiddlewareHandler, MethodDecorator } from '../libs/types'; /** * @module Decorators/Router */ /** * Route Controller Decorator assign a router point into the module mount-point placeholder and is used just to create * a bucket to contains all the endpoints defined by method of the Controller. If mount-point is not defined automati- * cally this must considerate mounted on root, but is mandatory define the Controller with this decorator in order * to allow Expressive Tea Setting up the Controller as part of a Module. * * @decorator {ClassDecorator} Route - Assign a route to controller endpoints. * @summary Generate a Placeholder endpoint root for controller routes. * @param {string} mountpoint Register the url part to mount the Controller. * @example * {REPLACE-AT}Route('/) * class Example {} */ export declare function Route(mountpoint?: string): <T extends new (...args: any[]) => {}>(Route: T) => { new (...args: any[]): { readonly router: Router; readonly mountpoint: string; __mount(parent: Router): any; __registerHandler(options: ExpressiveTeaHandlerOptions): ExpressMiddlewareHandler; }; } & T; /** * Define a GET Endpoint response over the controller router and can define in which route should be responds and * by default this is responding to everything on the controller root path. * @decorator {MethodDecorator} Get - Create an GET Response over the designed route. * @summary Define a GET Controller Endpoint on Controller. * @param {string} route URL Part to mount create a handler. * @example * {REPLACE-AT}Route('/') * class GenericController { * {REPLACE-AT}Get() // This Response to all GET Requests for controller route. * methodError() {} * * {REPLACE-AT}Get('/data') // This Response to "/data" GET Requests for controller route. * methodData() {} * } * */ export declare function Get(route?: string): (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void; /** * Define a POST Endpoint response over the controller router and can define in which route should be responds and * by default this is responding to everything on the controller root path. * @decorator {MethodDecorator} POST - Create an POST Response over the designed route. * @summary Define a POST Controller Endpoint on Controller. * @param {string} route URL Part to mount create a handler. * @example * {REPLACE-AT}Route('/') * class GenericController { * {REPLACE-AT}Post() // This Response to all GET Requests for controller route. * methodError() {} * * {REPLACE-AT}Post('/data') // This Response to "/data" GET Requests for controller route. * methodData() {} * } * */ export declare function Post(route?: string): (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void; /** * Define a PUT Endpoint response over the controller router and can define in which route should be responds and * by default this is responding to everything on the controller root path. * @decorator {MethodDecorator} Put - Create an PUT Response over the designed route. * @summary Define a PUT Controller Endpoint on Controller. * @param {string} route URL Part to mount create a handler. * @example * {REPLACE-AT}Route('/') * class GenericController { * {REPLACE-AT}Put() // This Response to all PUT Requests for controller route. * methodError() {} * * {REPLACE-AT}Put('/data') // This Response to "/data" PUT Requests for controller route. * methodData() {} * } * */ export declare function Put(route?: string): (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void; /** * Define a PATCH Endpoint response over the controller router and can define in which route should be responds and * by default this is responding to everything on the controller root path. * @decorator {MethodDecorator} Patch - Create an PATCH Response over the designed route. * @summary Define a PATCH Controller Endpoint on Controller. * @param {string} route URL Part to mount create a handler. * @example * {REPLACE-AT}Route('/') * class GenericController { * {REPLACE-AT}Patch() // This Response to all PATCH Requests for controller route. * methodError() {} * * {REPLACE-AT}Patch('/data') // This Response to "/data" PATCH Requests for controller route. * methodData() {} * } * */ export declare function Patch(route?: string): (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void; /** * Define a DELETE Endpoint response over the controller router and can define in which route should be responds and * by default this is responding to everything on the controller root path. * @decorator {MethodDecorator} Delete - Create an DELETE Response over the designed route. * @summary Define a DELETE Controller Endpoint on Controller. * @param {string} route URL Part to mount create a handler. * @example * {REPLACE-AT}Route('/') * class GenericController { * {REPLACE-AT}Delete() // This Response to all DELETE Requests for controller route. * methodError() {} * * {REPLACE-AT}Delete('/data') // This Response to "/data" DELETE Requests for controller route. * methodData() {} * } * */ export declare function Delete(route?: string): (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void; /** * Define a Route path parameter transformation method as mentioned on Express this is just call by controller endpoints * route paths and it helps to define logic on each route path parameters. Example, you can require transform userId * parameter and added to request object. * * The method to response this must follow the Express callback notation (Request, Response, Next, paramValue) which * also must be use Next method to allow continue the process flow from Express. * @decorator {MethodDecorator} Param - Create a transformation middleware for router parameters. * @summary Define a Parameter transformation on router path.. * @param {string} route URL Part to mount create a handler. * @example * {REPLACE-AT}Route('/') * class GenericController { * {REPLACE-AT}Param('userId') // This Response to all GET Requests for controller route. * async methodError(req, res, next, param, id) { * const user = await User.find(id) * try { * if(!user) throw new Error('User not Found'); * * req.user = user; * next(); * } catch(e) { * next(e); * } * } * * {REPLACE-AT}Get('/:userId') // This Response to "/:userId" GET Requests for controller route. * methodData(req, res) { * res.json(req.user); * } * } * */ export declare function Param(route?: string): (target: object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => void; /** * Middleware Decorator is following the middleware functionality inheritance from Express framework itself and follow * the same rules, that execute any code before response the request and can change the current request or response * object, and requires use next callback to pass control to next middleware. * * This define the middlewares at Controller and Controller methods level, for application levels please see the * Plug Decorator which can be useful to declare at that level.. * @decorator {MethodDecorator|ClassDecorator} Middleware - Attach a middleware definition on the root route or endpoint * routes. * @summary Assign a middleware to the Controller or Method route path flow. * @example * {REPLACE-AT}Middleware((req, res, next) { * req.controllerName = 'Example'; * next(); * }) * class ExampleController { * {REPLACE-AT}Get('/someResponse') * {REPLACE-AT}Middleware((req, res, next) { * req.methodName = 'someResponse'; * next(); * }) * someRespone(req, res) { * res.send(`${req.controllerName}:${req.methodName}`); * } * } * * @param {Function} middleware Register a middleware over router. */ export declare function Middleware(middleware: any): ClassDecorator & MethodDecorator; /** * Will generate a GET route to respond rendering a view template setting up before; the controller method <b>MUST</b> * return an object in order to fulfilled the local variables into the view. * @decorator {MethodDecorator} View - Create a Vew Response over the designed route. * @summary Define a View response endpoint on Controller. * @param {string} viewName - View render layout name defined by configuration. * @param {string} route - URL Part to mount create a handler. * @return {object} Controller must return and object as local variables for the view. * @example * {REPLACE-AT}Route('/') * class GenericController { * {REPLACE-AT}View('login', '/view') // This Response to all GET Requests for controller route. * methodError() {} * } * */ export declare function View(viewName: string, route?: string): MethodDecorator;