reest
Version:
A library inspired by NestJS's elegance, specifically designed for efficient serverless API development on AWS Lambda. It streamlines the creation of microservices with automated Swagger documentation and enhanced decorator-based middleware support, makin
117 lines • 4.34 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.App = void 0;
require("reflect-metadata");
const express_1 = __importDefault(require("express"));
const class_validator_jsonschema_1 = require("class-validator-jsonschema");
const storage_js_1 = require("class-transformer/cjs/storage.js");
const utils_1 = require("../utils");
const partials_1 = require("../partials");
class App {
app;
//Define Controllers and Global Middlewares
Controllers = [];
Middlewares = [];
//Define Interceptors
Interceptors;
//Define App Options
openapiOptions = {};
//private multerOptions: any = {};
serverless = false;
port;
//Option getters and setters
setPort(port) {
this.port = port || 3000;
}
getPort() {
return this.port;
}
isServerless() {
return this.serverless;
}
setOpenapiOptions(options) {
this.openapiOptions = options;
}
joinDefinitions(definitions) {
this.openapiOptions.definitions = {
...this.openapiOptions.definitions,
...definitions,
};
}
joinPaths(paths) {
this.openapiOptions.paths = {
...this.openapiOptions.paths,
...paths,
};
}
getOpenapiOptions() {
return this.openapiOptions;
}
//Get the express application instance
getApplication() {
return this.app;
}
//Define Schemas for Swagger
schemas = (0, class_validator_jsonschema_1.validationMetadatasToSchemas)({
classTransformerMetadataStorage: storage_js_1.defaultMetadataStorage,
});
constructor(app = (0, express_1.default)()) {
this.app = app;
//Set options before the construction (App Level Decorators)
//Set the port from the metadata
this.setPort(Reflect.getMetadata("port", this.constructor));
this.setOpenapiOptions(Reflect.getMetadata("openapi-options", this.constructor));
//Set the definitions for the openapi settings
this.joinDefinitions(this.schemas);
this.joinDefinitions({
File: {
type: "string",
format: "binary",
},
});
//Collect Global Interceptors
this.Interceptors = Reflect.getMetadata("GlobalInterceptors", this.constructor);
//Initialize the global interceptors
this.Interceptors?.sort((a, b) => -1).forEach((interceptor) => {
this.app.use((req, _, next) => {
const req_interceptor = new interceptor();
req_interceptor.intercept(req);
req.meta = {
...req.meta,
[interceptor.name]: req_interceptor.data,
};
next();
});
});
//Initialize Global Parser Middlewares
this.app.use(express_1.default.json());
this.app.use(express_1.default.urlencoded({ extended: true }));
//Collect the controllers and global middlewares
this.Controllers = Reflect.getMetadata("Controllers", this.constructor);
this.Middlewares = Reflect.getMetadata("Middlewares", this.constructor);
//Initialize controllers
this.Controllers?.forEach((controller) => {
controller = new controller(this.constructor);
this.joinPaths(controller.openapiPaths);
app.use(controller.prefix, controller.router);
});
//Initialize Middlewares
this.Middlewares?.forEach((middleware) => {
//this.app.use(middleware);
});
//Initialize Openapi Documentation
this.getOpenapiOptions().openapi &&
(0, partials_1.initializeOpenapi)(this.app, this.getOpenapiOptions());
//Start the server if not in serverless environment
!(this.isServerless() && process.env.NODE_ENV !== "local") &&
this.app.listen(this.getPort(), () => {
(0, utils_1.Log)(`Server is running on PORT:${this.port}`);
console.log(`-------------------------------------------------------------`);
});
}
}
exports.App = App;
//# sourceMappingURL=App.js.map