UNPKG

@t3ned/channel

Version:

Ergonomic, chaining-based Typescript framework for quick API development for Fastify

164 lines 5.62 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Application = void 0; const cors_1 = __importDefault(require("@fastify/cors")); const utils_1 = require("../../utils"); const ApplicationLoader_1 = require("./ApplicationLoader"); const fastify_1 = require("fastify"); const constants_1 = require("../../constants"); const errors_1 = require("../errors"); const path_1 = require("path"); class Application { /** * @param options The application options */ constructor(options = {}) { /** * The fastify instance for the application to use */ Object.defineProperty(this, "instance", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The application loader for loading routes */ Object.defineProperty(this, "loader", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The path to the directory routes are located */ Object.defineProperty(this, "routeDirPath", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The prefix for a route path */ Object.defineProperty(this, "routePathPrefix", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The prefix to use in file names to ignore in route paths */ Object.defineProperty(this, "routeFileIgnorePrefix", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The default route version prefix */ Object.defineProperty(this, "routeDefaultVersionPrefix", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The default route version number */ Object.defineProperty(this, "routeDefaultVersionNumber", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The validation error mapper */ Object.defineProperty(this, "validationErrorMapper", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * The logger to use */ Object.defineProperty(this, "logger", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** * Whether to enable debug logs */ Object.defineProperty(this, "debug", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.instance = options.instance ?? (0, fastify_1.fastify)(); this.instance.register(cors_1.default, options.cors); this.loader = options.loader ?? new ApplicationLoader_1.ApplicationLoader(this); this.routeDirPath = options.routeDirPath ? (0, path_1.join)(...(0, utils_1.arrayify)(options.routeDirPath)) : null; this.routePathPrefix = options.routePathPrefix; this.routeFileIgnorePrefix = options.routeFileIgnorePrefix ?? "_"; this.routeDefaultVersionPrefix = options.routeDefaultVersionPrefix ?? null; this.routeDefaultVersionNumber = options.routeDefaultVersionNumber ?? null; this.debug = options.debug ?? false; this.validationErrorMapper = options.validationErrorMapper ?? ((error) => error.format()); this.logger = options.logger ?? { debug: console.debug, info: console.info, error: console.error, }; if (options.useDefaultErrorHandler ?? true) { this.instance.setErrorHandler(async (error, _req, reply) => { const apiError = (0, utils_1.convertErrorToApiError)(error); const apiErrorResponse = { ...apiError.toJSON(), trace: this.debug ? apiError.trace : undefined, }; this.logger.error(apiError); return reply.status(apiError.status).send(apiErrorResponse); }); } if (options.useDefaultNotFoundHandler ?? true) { this.instance.setNotFoundHandler(async (req) => { throw new errors_1.ApiError() .setCode(0) .setStatus(constants_1.HttpStatus.NotFound) .setMessage(`Route \`${req.url}\` not found`); }); } } /** * The default route version */ get routeDefaultVersion() { return `${this.routeDefaultVersionPrefix ?? ""}${this.routeDefaultVersionNumber ?? ""}`; } /** * Listen for connections * @param host The host to bind * @param port The port to bind * * @returns The application */ async listen(host, port) { await this.loader.loadRoutes(); await this.instance.listen({ port, host }); return this; } } exports.Application = Application; //# sourceMappingURL=Application.js.map