@creamapi/cream
Version:
Concise REST API Maker - An extension library for express to create REST APIs faster
90 lines (89 loc) • 3.55 kB
JavaScript
;
/*
* 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.ExpressModule = void 0;
const express_1 = require("express");
const ExpressAdapters_1 = require("./ExpressAdapters");
/**
* This class is just a way to allow to explicitly declare
* that the class inheriting ExpressModule is a controller.
* This is because {@link ExpressController} decorates only
* **ExpressModules** and defines some fields that are used by
* the ExpressApplication to handle requests and define endpoints.
* Also it gives an interface for accessing the express application
* handling the class easily (by using ExpressModule.app)
*/
class ExpressModule {
middlewareList;
#router_accessor_storage;
/**
* This allows to access the express router that will handle
* the requests. This router will be registered to the
* {@link ExpressApplication} that will be defined in {@link ExpressModule.app}
*/
get router() { return this.#router_accessor_storage; }
set router(value) { this.#router_accessor_storage = value; }
#baseUrl_accessor_storage;
/**
* This is the basepoint to which the controller is bound to
*/
get baseUrl() { return this.#baseUrl_accessor_storage; }
set baseUrl(value) { this.#baseUrl_accessor_storage = value; }
#className_accessor_storage;
/**
* This is used to keep the information about the class
* that will inherit the ExpressModule and that will be
* decorated by {@link ExpressController}
*/
get className() { return this.#className_accessor_storage; }
set className(value) { this.#className_accessor_storage = value; }
/**
* The {@link ExpressApplication} the controller is registered to
*/
_app;
/**
* @param middlewareList The list of controller-wise middlewares associated
* with the controller. The method-associated middlewares will not appear in this list
*/
constructor(middlewareList = []) {
this.middlewareList = middlewareList;
this.router = (0, express_1.Router)();
this.baseUrl = '/';
this.className = '';
}
set app(v) {
this._app = v;
}
get app() {
return this._app;
}
prepareTransaction() {
let error = new Error();
let oldLimit = Error.stackTraceLimit;
Error.stackTraceLimit = 2;
Error.captureStackTrace(error);
Error.stackTraceLimit = oldLimit;
let line = error.stack.split('\n')[2];
let callerName = line.split('.')[1].split(' ')[0];
let transactionManager = Reflect.getMetadata(ExpressAdapters_1.TRANSACTION_MANAGER_METADATA_KEY, this, callerName);
if (transactionManager == undefined) {
throw new Error('Cannot prepare a transaction in a method that is not an ExpressMethod (or any of Get, Put, Post, Delete).');
}
return transactionManager;
}
}
exports.ExpressModule = ExpressModule;