UNPKG

@reflet/express

Version:

Well-defined and well-typed express decorators

227 lines (226 loc) 6.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractRoutes = exports.Route = exports.Delete = exports.Patch = exports.Put = exports.Post = exports.Get = void 0; const metadata_map_1 = require("./metadata-map"); const METAKEY_ROUTE = Symbol('route'); /** * Routes HTTP `GET` requests. * @param path - path for which the decorated class method is invoked. * @see https://expressjs.com/en/4x/api.html#app.get.method * @example * ```ts * class Foo { * @Get('/things/:id') * get(req: Request, res: Response, next: NextFunction) {} * } * ``` * ------ * @public */ function Get(path = '') { return Route('get', path); } exports.Get = Get; /** * Routes HTTP `POST` requests. * @param path - path for which the decorated class method is invoked. * @see https://expressjs.com/en/4x/api.html#app.post.method * @example * ```ts * class Foo { * @Post('/things') * create(req: Request, res: Response, next: NextFunction) {} * } * ``` * ------ * @public */ function Post(path = '') { return Route('post', path); } exports.Post = Post; /** * Routes HTTP `PUT` requests. * @param path - path for which the decorated class method is invoked. * @see https://expressjs.com/en/4x/api.html#app.put.method * @example * ```ts * class Foo { * @Put('/things/:id') * replace(req: Request, res: Response, next: NextFunction) {} * } * ``` * ------ * @public */ function Put(path = '') { return Route('put', path); } exports.Put = Put; /** * Routes HTTP `PATCH` requests. * @param path - path for which the decorated class method is invoked. * @see https://expressjs.com/en/4x/api.html#app.METHOD * @example * ```ts * class Foo { * @Patch('/things/:id') * update(req: Request, res: Response, next: NextFunction) {} * } * ``` * ------ * @public */ function Patch(path = '') { return Route('patch', path); } exports.Patch = Patch; /** * Routes HTTP `DELETE` requests. * @param path - path for which the decorated class method is invoked. * @see https://expressjs.com/en/4x/api.html#app.delete.method * @example * ```ts * class Foo { * @Delete('/things/:id') * remove(req: Request, res: Response, next: NextFunction) {} * } * ``` * ------ * @public */ function Delete(path = '') { return Route('delete', path); } exports.Delete = Delete; /** * Routes an HTTP request. * @param method - HTTP method of the request, in lowercase. * @param path - path for which the decorated class method is invoked. * @see https://expressjs.com/en/4x/api.html#app.METHOD * @public */ function Route(method, path) { return (target, key, descriptor) => { // Attach routes to class instead of methods to extract and traverse all of them at once const routes = (0, metadata_map_1.getOwnMetadata)(METAKEY_ROUTE, target) || []; if (Array.isArray(method)) { for (const methodd of method) { routes.push({ path, method: methodd.toLowerCase(), key }); } } else { routes.push({ path, method: method.toLowerCase(), key }); } (0, metadata_map_1.defineMetadata)(METAKEY_ROUTE, routes, target); }; } exports.Route = Route; /* istanbul ignore next */ // tslint:disable: no-shadowed-variable (function (Route) { function Get(path = '') { return Route('get', path); } Route.Get = Get; function Post(path = '') { return Route('post', path); } Route.Post = Post; function Put(path = '') { return Route('put', path); } Route.Put = Put; function Patch(path = '') { return Route('patch', path); } Route.Patch = Patch; function Delete(path = '') { return Route('delete', path); } Route.Delete = Delete; function Head(path = '') { return Route('head', path); } Route.Head = Head; function Options(path = '') { return Route('options', path); } Route.Options = Options; function Trace(path = '') { return Route('trace', path); } Route.Trace = Trace; function Notify(path = '') { return Route('notify', path); } Route.Notify = Notify; function Subscribe(path = '') { return Route('subscribe', path); } Route.Subscribe = Subscribe; function Unsubscribe(path = '') { return Route('unsubscribe', path); } Route.Unsubscribe = Unsubscribe; function Purge(path = '') { return Route('purge', path); } Route.Purge = Purge; function Checkout(path = '') { return Route('checkout', path); } Route.Checkout = Checkout; function Move(path = '') { return Route('move', path); } Route.Move = Move; function Copy(path = '') { return Route('copy', path); } Route.Copy = Copy; function Merge(path = '') { return Route('merge', path); } Route.Merge = Merge; function Report(path = '') { return Route('report', path); } Route.Report = Report; function MSearch(path = '') { return Route('m-search', path); } Route.MSearch = MSearch; function Mkactivity(path = '') { return Route('mkactivity', path); } Route.Mkactivity = Mkactivity; function Mkcol(path = '') { return Route('mkcol', path); } Route.Mkcol = Mkcol; function Search(path = '') { return Route('search', path); } Route.Search = Search; function Lock(path = '') { return Route('lock', path); } Route.Lock = Lock; function Unlock(path = '') { return Route('unlock', path); } Route.Unlock = Unlock; function All(path = '') { return Route('all', path); } Route.All = All; })(Route = exports.Route || (exports.Route = {})); /** * Retrieve routes of a class. * @internal */ function extractRoutes(target) { return (0, metadata_map_1.getOwnMetadata)(METAKEY_ROUTE, target.prototype) || []; } exports.extractRoutes = extractRoutes;