UNPKG

@creamapi/cream

Version:

Concise REST API Maker - An extension library for express to create REST APIs faster

56 lines (55 loc) 2.31 kB
"use strict"; /* * Copyright 2024 Raul Radu * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.UseMiddlewaresForController = UseMiddlewaresForController; exports.UseMiddleware = UseMiddleware; require("reflect-metadata"); const Route_1 = require("../HttpUtils/Route"); /** * This decorator is used to create a stack of middlewares for a controller. <br> * This stack of middlewares is applied to all ExpressCalls in the controller * @param middlewares the middlewares that should be called before calling the endpoint * @returns a class that extends the target class and will initialize the middlewares */ function UseMiddlewaresForController(middlewares) { return function (target) { return class extends target { constructor(...args) { super(middlewares); } }; }; } /** * This decorator is used to push to the middleware stack a new middleware for the <br> * decorated ExpressCall. Middlewares pushed with this decorator will be executed with <br> * a top-down approach (like a stack) * @param middleware the middleware that should be pushed to the stack * @returns the actual decorator */ function UseMiddleware(middleware) { return function (target, propertyName, descriptor) { let methodRouters = Reflect.getOwnMetadata(Route_1.ROUTES_METADATA_KEY, target) || []; let route = methodRouters.find((value) => value.methodName == propertyName); if (route == undefined) { throw Error('Method ' + propertyName + ' is not a valid ExpressCall'); } route.addMiddleware(middleware); Reflect.defineMetadata(Route_1.ROUTES_METADATA_KEY, methodRouters, target); return descriptor; }; }