UNPKG

@piggly/fastify-chassis

Version:

An ESM/CommonJS toolkit to help you to do common operations in your back-end applications with Fastify and NodeJS.

192 lines 6.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractServer = void 0; const tslib_1 = require("tslib"); const ddd_toolkit_1 = require("@piggly/ddd-toolkit"); const debug_1 = tslib_1.__importDefault(require("debug")); const services_1 = require("../../services/index.js"); const HttpServer_1 = require("../HttpServer.js"); /** * @file The API server. * @copyright Piggly Lab 2023 */ class AbstractServer { /** * The Fastify application. * * @protected * @memberof ApiServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ _app; /** * The options. * * @protected * @memberof ApiServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ _options; /** * Create a new API server. * * @param options The options. * @public * @constructor * @memberof ApiServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ constructor(options, app) { this._options = options; this._app = app; } /** * Bootstrap the API server. * * This method will prepare the environment, the logger, * the plugins and the routes for fastify. * * After that, it will return a new HttpServer instance. * This instance will be used to start the server. * * @public * @async * @memberof ApiServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ async bootstrap() { if (this._options.hooks.beforeInit) { await this._options.hooks.beforeInit(this._app, this._options.env); } await this.init(); if (this._options.hooks.afterInit) { await this._options.hooks.afterInit(this._app, this._options.env); } return new HttpServer_1.HttpServer(this); } /** * Get the Fastify application. * * @public * @memberof ApiServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ getApp() { return this._app; } /** * Get the global environment. * * @public * @memberof ApiServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ getEnv() { return this._options.env; } /** * Initialize the API server. * * @protected * @memberof ApiServer * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ async init() { if (this._options.logger) { (0, debug_1.default)('http:server')('Registering logger'); ddd_toolkit_1.LoggerService.register(this._options.logger); } (0, debug_1.default)('http:server')('Registering environment service'); services_1.EnvironmentService.register(new services_1.EnvironmentService(this._options.env)); // Plugins (0, debug_1.default)('http:server')('Applying plugins'); await this._options.plugins.apply(this._app, this._options.env); // Routes (0, debug_1.default)('http:server')('Applying routes'); await this._options.routes.apply(this._app, this._options.env); // Not found routes (0, debug_1.default)('http:server')('Setting not found handler'); this._app.setNotFoundHandler((request, reply) => { reply .status(404) .send(this._options.errors.notFound.toJSON(['extra'])); }); // Any error (0, debug_1.default)('http:server')('Setting error handler'); this._app.setErrorHandler((error, request, reply) => { (0, debug_1.default)('http:server')('Handling error', error); if (error instanceof ddd_toolkit_1.DomainError) { const _error = error.toJSON(['extra']); if (this._options.env.debug) { console.error('DomainError', _error); } ddd_toolkit_1.LoggerService.softResolve().debug('UNCAUGHT_DOMAIN_ERROR', error); return reply.status(error.status).send(_error); } if (error instanceof ddd_toolkit_1.RuntimeError) { const _error = error.toJSON(['extra']); if (this._options.env.debug) { console.error('RuntimeError', _error); } ddd_toolkit_1.LoggerService.softResolve().debug('UNCAUGHT_RUNTIME_ERROR', error); return reply.status(error.status).send(_error); } ddd_toolkit_1.LoggerService.softResolve().error('UNCAUGHT_ERROR', error); if (this._options.errors.handler) { this._options.errors.handler(error); } return reply .status(500) .send(this._options.errors.unknown.toJSON(['extra'])); }); } /** * Get the default logger configuration. * * @public * @static * @memberof AbstractServer * @since 3.0.0 * @author Caique Araujo <caique@piggly.com.br> */ static defaultLogger(env, root_path, debug) { const logger = { file: `${root_path}/logs/server.log`, level: debug ? 'debug' : 'info', }; if (env === 'production') { return logger; } return { ...logger, serializers: { req: (req) => ({ hostname: req.hostname, method: req.method, url: req.url, }), res: (res) => ({ statusCode: res.statusCode, }), }, transport: { options: { colorize: true, ignore: 'pid', messageFormat: '{msg} [id={reqId} method={req.method} url={req.url} statusCode={res.statusCode} responseTime={responseTime}ms hostname={req.hostname}]', translateTime: true, }, target: 'pino-pretty', }, }; } } exports.AbstractServer = AbstractServer; //# sourceMappingURL=AbstractServer.js.map