UNPKG

@micro.ts/core

Version:

Microservice framework with Typescript

168 lines (167 loc) 7.02 kB
import { IBroker } from '../brokers/IBroker'; import { BaseRouteDefinition, ServerOptions } from './types'; import { ParamDescription } from '../decorators/types/ParamMetadataTypes'; export declare class BaseServer { private options; constructor(options: ServerOptions); private get logger(); private static get controllersMetadata(); /** * Execute a single middleware and return its result * @param middleware Middleware function or IMiddleware instance to execute * @param def Route definition on which the middleware is being called from * @param action The action object, with the state of the action just before this middleware is being executed * @param controller The controller instance * @param broker The broker instance */ private static executeMiddleware; /** * Execute the error handler and return its result as a boolean * @param handler Error handler function or IErrorHandler instance * @param error Error object * @param action Action object up to the state before the error * @param def Route definition where the error was thrown * @param controller The controller instance * @param broker The broker instance */ private static executeErrorHandler; /** * If an error was thrown, the error object will go through all the error handlers sequentially until on of the handlers returns true * @param handlers List of handlers to execute * @param error Error object * @param action Action state on the moment the error was thrown * @param def Route definition where the error was thrown * @param controllerInstance The controller instance * @param broker The broker instance */ private static handleError; private executeWithTimeout; /** * Execute the request (passing through all the middlewares) and handle errors if thrown any * @param def Route definition * @param action Action from the broker * @param broker Broker instance */ private executeRequest; /** * Checks if the action for the handler handler needs to pass through authorizationChecker function, * And executes the the authorizationChecker function, with the corresponding arguments * @param action Action object at the time of invocation * @param methodMetadata Metadata for the handler to check if the request should get filtered by authorization checker */ private checkAuthorization; /** * Group an array of middleware options , with the before flag, into to groups, before middlewares and after middlewares * @param middlewares List of middleware options */ private groupMiddlewares; /** * Get all middlewares for a specific handler, * The sorting of middlewares in this method determines the sequence of the middleware executions * Before the handler is executed middlewares are executed on this order: * 1. App before middlewares, * 2. Controller before middlewares, * 3. Handler before middlewares * After the handler is executed after middlewares are executed in this order * 1. Handler after middlewares * 2. Controller's after middlewares * 3. App's after middlewares * @param methodMetadata */ private getMiddlewares; /** * Build error handlers for the route, using first the method error handlers, then controller error handlers, and app-level error handlers * @param methodMetadata Metadata for the handler */ private getErrorHandlers; /** * Handle the before middlewares execution, handler execution, and after middlewares execution * @param def Route definition * @param action The action object from the broker * @param broker The broker instance * @param controllerInstance The controller instance (needs to be passed into the middlewares) * @param methodControllerMetadata Handler metadata */ private handleRequest; /** * Build the arguments list for the handler * @param action Action object * @param metadata * @param broker */ private buildParams; /** * Execute currentUserChecker function, to get the user from a request, and inject it if required int the handlers arguments * @param action Action object with the request * @param broker Broker instance */ private getUser; private transform; /** * Validate a single method argument * @param value Value of the argument * @param required If true and value is empty value it throws bad request * @param validate If true, and the validate function throws it throws bad request * @param name Key of the value in case is a single key option * @param type Type of parameter to use in validation * @param isObject if the value is a key-value object * @param notEmpty if the value should not be empty */ private validateParam; /** * Switches through all the cases of param types and maps the correct information * @param action Action object after all before middlewares executed * @param metadata Metadata for the handler * @param broker Broker instance */ private buildSingleParam; /** * Adds route to its corresponding brokers * @param def Route definition * @param brokers List of brokers enabled for this handler * @param params List of parameters required for this handler (to be used when generating API specifications) */ private addRoute; /** * Registers all routes to the brokers * Initializes all brokers */ start(): Promise<void>; private _serverInfo; get serverInfo(): Map<IBroker, { route: string; def: BaseRouteDefinition; params: ParamDescription[]; }[]>; /** * Build route for a single handler * @param methodName Name of the method * @param desc Method metadata * @param basePath Base path of the app * @param controllerPath Path of the controller * @param ctor Controller constructor function * @param isJson Is JsonController * @param brokers List of brokers enabled for the controller * @param routes Routes to append to the result * @param controllerName Name of the controller */ private buildSingleMethodRoute; /** * Build routes for a single controller * @param controllerMetadata Controller metadata * @param basePath Base path of the app * @param routes All the routes of the controller * @param brokers All the brokers filtered for this controller */ private buildSingleControllerRoute; /** * Build all the routes for all the app's controllers * @param controllers Metadata for all registered controllers */ private buildAllControllers; /** * Gets all the controllers metadata and build all the routes * Displays route table on the screen */ buildRoutes(): Promise<void>; }