UNPKG

@creamapi/cream

Version:

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

59 lines (58 loc) 2.18 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.ROUTES_METADATA_KEY = exports.Route = void 0; /** * @internal * This class represents the route associated with a ExpressCall. * it is used to map endpoints to Express Functions that will * map parameters to the target method and then call the method. */ class Route { route; method; methodName; httpMethod; middlewares; /** * * @param route The route of the ExpressCall decorated method. It is relative to the base path of the ExpressController * @param method The function that will handle the request and then call the class method with the correct bounded "this" * @param methodName the original method name * @param httpMethod the http method associated with the ExpressCall decorated module * @param middlewares the stack of middleware that should be called before param method */ constructor(route, method, methodName, httpMethod, middlewares = []) { this.route = route; this.method = method; this.methodName = methodName; this.httpMethod = httpMethod; this.middlewares = middlewares; } addMiddleware(middleware) { this.middlewares.push(middleware); } getMiddlewareMethods() { let expressEndpoints = []; for (let middleware of this.middlewares) { expressEndpoints.push(middleware.handle.bind(middleware)); } return expressEndpoints; } } exports.Route = Route; exports.ROUTES_METADATA_KEY = Symbol('express:routes');