UNPKG

@micro.ts/core

Version:

Microservice framework with Typescript

193 lines (192 loc) 4.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OptionsBuilder = void 0; const Logger_1 = require("./Logger"); class OptionsBuilder { constructor(config, container) { this.config = config; this.container = container; this.beforeStartHooks = []; this.afterStartHooks = []; this.options = { brokers: [], controllers: [], beforeMiddlewares: [], afterMiddlewares: [], errorHandlers: [], }; } addBeforeStartHook(hook) { this.beforeStartHooks.push(hook); return this; } addAfterStartHook(hook) { this.afterStartHooks.push(hook); return this; } /** * Return the built options */ get serverOptions() { return this.options; } /** * Enable or disable developer mode * Returns the full error stack (Not yet implemented) * @param val */ setDevMode(val) { this.options.dev = val; return this; } /** * Base path for all the endpoints * @param val */ setBasePath(val) { this.options.basePath = val; return this; } /** * Enable or disable request logging * @param val */ setLogRequests(val) { this.options.logRequests = val; return this; } /* * Listen on startup when a route is registered! * */ onRoute(fn) { this.options.onRouteListeners = this.options.onRouteListeners || []; this.options.onRouteListeners.push(fn); return this; } /** * Enable or disable server errors logging * @param val */ setLogErrors(val) { this.options.logErrors = val; return this; } /** * Add a prebuilt broker * @param broker */ addBroker(broker) { if (this.options.brokers) { this.options.brokers.push(broker); } return this; } /** * Add a list of controllers to the server * @param controllers */ addControllers(...controllers) { this.options.controllers.push(...controllers); return this; } /** * Add middlewares that get executed before any request handling, on all requests * @param middlewares */ addBeforeMiddlewares(...middlewares) { if (this.options.beforeMiddlewares) { this.options.beforeMiddlewares.push(...middlewares); } return this; } /** * Add middlewares that get executed after all successfully handled requests * @param middlewares */ addAfterMiddlewares(...middlewares) { if (this.options.afterMiddlewares) { this.options.afterMiddlewares.push(...middlewares); } return this; } /** * Add global error handlers * @param handlers */ addErrorHandlers(...handlers) { if (this.options.errorHandlers) { this.options.errorHandlers.push(...handlers); } return this; } /** * Add authorization function to be called on @Authorized routes * @param handler */ useAuthorization(handler) { this.options.authorizationChecker = handler; return this; } /** * Add authorization error function to be called if @Authorized routes are not authorized * @param handler */ setAuthorizationError(handler) { this.options.getNotAuthorizedError = handler; return this; } /** * Add user returning function for all the requests * @param checker */ useAuthentication(checker) { this.options.currentUserChecker = checker; return this; } /** * Set validate function * @param func */ setValidateFunction(func) { this.options.validateFunction = func; return this; } /** * Set logger in container * @param logger */ setLogger(logger) { this.container.set(Logger_1.LoggerKey, logger); return this; } setTimeout(timeout) { if (timeout > 0) { this.options.timeout = timeout; } return this; } /** * Set broker connection error handler * @param handler */ setConnectionErrorHandler(handler) { this.options.onBrokerConnnectionError = handler; return this; } /* * If true it will fill the SpecBuilder singleton with the routes * */ setGenerateSwagger(value) { this.options.generateSwagger = value; return this; } addPlugin(plugin) { plugin(this); return this; } useTypeTransformer(fn) { this.options.transformerFunction = fn; return this; } } exports.OptionsBuilder = OptionsBuilder;