Latest
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124// Copyright 2020 Liam Tan. All rights reserved. MIT license.
import { HttpMethod, ControllerMetadata } from "./types.ts";import { getControllerMeta, defaultMetadata, setControllerMeta } from "./metadata.ts";/** * Responsible for producing function decorators for all given HttpMethods. * Uses a curried function to return the function decorator. */export const defineRouteDecorator = (path: string = "/", requestMethod: HttpMethod): MethodDecorator => ( target: any, propertyKey: string | Symbol): void => { const meta: ControllerMetadata = getControllerMeta(target) ?? defaultMetadata();
meta.routes.set(propertyKey, { requestMethod, path, methodName: propertyKey, });
setControllerMeta(target, meta);};
/** * Method decorator function for mapping Get requests. * * `path` will be the routed path with the prefix of the * parent controller prefix, E.g. * * ```ts * @Controller('api') * class DefaultController { * @Get('/:id') * public controllerAction(): any {} * } * ``` * * The above action `controllerAction` will then be mapped * to `GET` requests that match pattern `api/:id` */export function Get(path?: string): MethodDecorator { return defineRouteDecorator(path, HttpMethod.GET);}/** * Method decorator function for mapping Put requests. * * `path` will be the routed path with the prefix of the * parent controller prefix, E.g. * * ```ts * @Controller('api') * class DefaultController { * @Put('/:id') * public controllerAction(): any {} * } * ``` * * The above action `controllerAction` will then be mapped * to `PUT` requests that match pattern `api/:id` */export function Put(path?: string): MethodDecorator { return defineRouteDecorator(path, HttpMethod.PUT);}/** * Method decorator function for mapping Post requests. * * `path` will be the routed path with the prefix of the * parent controller prefix, E.g. * * ```ts * @Controller('api') * class DefaultController { * @Post('/:id') * public controllerAction(): any {} * } * ``` * * The above action `controllerAction` will then be mapped * to `POST` requests that match pattern `api/:id` */export function Post(path?: string): MethodDecorator { return defineRouteDecorator(path, HttpMethod.POST);}/** * Method decorator function for mapping Patch requests. * * `path` will be the routed path with the prefix of the * parent controller prefix, E.g. * * ```ts * @Controller('api') * class DefaultController { * @Patch('/:id') * public controllerAction(): any {} * } * ``` * * The above action `controllerAction` will then be mapped * to `PATCH` requests that match pattern `api/:id` */export function Patch(path?: string): MethodDecorator { return defineRouteDecorator(path, HttpMethod.PATCH);}/** * Method decorator function for mapping Delete requests. * * `path` will be the routed path with the prefix of the * parent controller prefix, E.g. * * ```ts * @Controller('api') * class DefaultController { * @Delete('/:id') * public controllerAction(): any {} * } * ``` * * The above action `controllerAction` will then be mapped * to `DELETE` requests that match pattern `api/:id` */export function Delete(path?: string): MethodDecorator { return defineRouteDecorator(path, HttpMethod.DELETE);}