UNPKG

@creamapi/cream

Version:

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

57 lines (56 loc) 2.73 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.MiddlewareParameterProp = void 0; exports.MiddlewareData = MiddlewareData; const ParameterProp_1 = require("../ExpressAdapter/ParameterProp"); const ExpressAdapters_1 = require("../ExpressAdapter/ExpressAdapters"); /** * @internal * This class is used to define parameter prop for middleware data * This is used to map parameters to the data in a middleware collection */ class MiddlewareParameterProp extends ParameterProp_1.ParameterProp { collection; /** * @param index the index of the parameter in the parameter call array * @param name The name of the parameter in the data collection * @param collection The collection that the data should be retrieved from * * @remarks The data is accessed like collection[name] */ constructor(index, name, collection) { super(index, name); this.collection = collection; } } exports.MiddlewareParameterProp = MiddlewareParameterProp; /** * This decorator factory is used to declare that a parameter of a method should be filled <br> * from the collection `collectionName`. The parameter will be filled either with the data or <br> * undefined if the data is not found in the collection or the collection is not found in the map * @param collectionName The collection the data should be retrieved from. The default collection name is 'default' * @param dataName the name of the field in the collection. To retrieve the entire collection the string "*" is used. * @returns the decorator that will effectively decorate the method */ function MiddlewareData(collectionName = 'default', dataName = '*') { return function (target, propertyKey, parameterIndex) { let existingRequiredParameters = Reflect.getOwnMetadata(ExpressAdapters_1.MIDDLEWARE_METADATA_KEY, target, propertyKey) || []; existingRequiredParameters.push(new MiddlewareParameterProp(parameterIndex, dataName, collectionName)); Reflect.defineMetadata(ExpressAdapters_1.MIDDLEWARE_METADATA_KEY, existingRequiredParameters, target, propertyKey); }; }