/** * @author Olsi Rrjolli * In these class are all decorators defined that we will use for the Controllers */ import { httpMethod, controllerMetaKey } from '../constants/constants'; import { IController, IModel } from '../types/index'; import { generateResourceMetaKey } from '../utils/reflectMetadataHelpers'; /** * HTTP Controller Marker * @param resourcePath * The path to the resource for default */ /* tslint:disable:variable-name */ export const Controller = (resourcePath: string = '') => { return (target: any) => { Reflect.defineMetadata(controllerMetaKey.RESOURCE_KEY, resourcePath, target); const resource = Reflect.getOwnMetadata(controllerMetaKey.RESOURCE_KEY, target); const metaDataKeys = Reflect.getOwnMetadata(controllerMetaKey.METHODS_KEY, target); metaDataKeys.forEach((metaData) => { Reflect.defineMetadata(generateResourceMetaKey(metaData.http, resource, metaData.path) , metaData.method, target); }); }; }; /** * HTTP POST Marker * @param path * The path to the resource for default */ export const POST = (path: string = '') => { return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor) => { const ownMethods = Reflect.getOwnMetadata(controllerMetaKey.METHODS_KEY, target.constructor); ownMethods.push({http: httpMethod.POST, path: path, method: propertyKey}); Reflect.defineMetadata(controllerMetaKey.METHODS_KEY, ownMethods, target.constructor); }; }; /** * HTTP GET Marker, Decorate the method with the GET Decorator * @param path * The path to the resource for default */ export const GET = (path: string = '') => { return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor) => { const ownMethods = Reflect.getOwnMetadata(controllerMetaKey.METHODS_KEY, target.constructor) || []; ownMethods.push({http: httpMethod.GET, path: path, method: propertyKey}); Reflect.defineMetadata(controllerMetaKey.METHODS_KEY, ownMethods, target.constructor); }; }; /** * HTTP DELETE Marker * @param path * The path to the resource for default */ export const DELETE = (path: string = '') => { return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor) => { const ownMethods = Reflect.getOwnMetadata(controllerMetaKey.METHODS_KEY, target.constructor); ownMethods.push({http: httpMethod.DELETE, path: path, method: propertyKey}); Reflect.defineMetadata(controllerMetaKey.METHODS_KEY, ownMethods, target.constructor); }; }; /** * HTTP PUT Marker * @param path * The path to the resource for default */ export const PUT = (path: string = '') => { return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor) => { const ownMethods = Reflect.getOwnMetadata(controllerMetaKey.METHODS_KEY, target.constructor); ownMethods.push({http: httpMethod.PUT, path: path, method: propertyKey}); Reflect.defineMetadata(controllerMetaKey.METHODS_KEY, ownMethods, target.constructor); }; }; /** * PathParam Setter/Marker * @param pathParam * The path parameter from the Request */ export const PathParam = (pathParam: string) => { return function(target: IController, propertyKey: string, parameterIndex: number){ const ownDecorators = Reflect.getOwnMetadata(propertyKey, target, propertyKey) || []; ownDecorators.push({paramName: pathParam, index: parameterIndex}); Reflect.defineMetadata(propertyKey, ownDecorators, target, propertyKey); }; }; /** * Query Parameter Setter/Marker * @param queryParam * The query parameter from the Request */ export const QueryParam = (queryParam: string) => { return function(target: IController, propertyKey: string, parameterIndex: number){ const ownDecorators = Reflect.getOwnMetadata(propertyKey, target, propertyKey) || []; ownDecorators.push({queryName: queryParam, index: parameterIndex}); Reflect.defineMetadata(propertyKey, ownDecorators, target, propertyKey); }; }; /** * Mapper Decorator * @param type * Decorate the Controller with the Model, which should use for the Mapper */ export const Model = (model: T) => { return (target: IModel) => { Reflect.defineMetadata(controllerMetaKey.MODEL_KEY, model, target); }; };