UNPKG

dino-express

Version:

DinO enabled REST framework based on express

129 lines (128 loc) 5.38 kB
import { ApplicationContext, Component, EnvironmentConfiguration, Monitor } from 'dino-core'; import { Application, Router } from 'express'; import { InfoObject } from 'express-openapi-validate/dist/OpenApiDocument'; import { OpenAPI } from 'openapi-types'; import { ErrorHandler } from './error.handler'; import { EventProducerInterface } from './events/EventProducerInterface'; import { DescriptorProvider } from './openapi/descriptor.provider'; import { PolicyFactory } from './policies/PolicyFactory'; import { RuntimeContext } from './RuntimeContext'; import { MiddlewareErrorHandler, MiddlewareHandler, Middlewares, OrderedMiddlewares, Policy, ResponseValidator } from './Types'; type Deps = { applicationContext: ApplicationContext; environment: EnvironmentConfiguration; }; /** * Configure express routing based on swagger configuration. * * @typedef RoutingConfigurer * @public */ export declare class RoutingConfigurer extends Component { private server; private applicationStateMonitor; protected readonly environment: EnvironmentConfiguration; protected readonly applicationContext: ApplicationContext; protected readonly runtimeContext: RuntimeContext; protected readonly policyFactory: PolicyFactory; protected eventProducer: EventProducerInterface; constructor({ applicationContext, environment }: Deps); postConstruct(): Promise<void>; preDestroy(): void; private configureApiPath; private configureApiMethod; protected startServer(app: Application, router: Router): void; protected resolveDescriptorProvider(): DescriptorProvider; protected setupMonitoringEndpointsIfRequired(router: Router): void; /** * Allows to define top level monitors. The returned objects contains two arrays * before and after middlewares, the before middlewares are added at the very start * of the middleware chain while the after are added at the very end of the middleware * chain, both in the order that are defined. * * @returns {Object} * ```{ * before: [], * after: [] * }``` */ protected defineTopLevelMonitorsIfRequired(): OrderedMiddlewares; /** * Allows to add a monitor * @param {Monitor} monitor */ addMonitor(monitor: Monitor): RoutingConfigurer; /** * Hook method allow to customize the express instance. by default it add the json middleware only. * This method can be overwritten in order to provide more tailored configuration of express instance. * * @param express the express instance * @protected */ protected extend(_express: Application): void; /** * Hook allows to provide an express error handler, by default a JSON error handler is used. * @protected */ protected errorHandler(): MiddlewareErrorHandler; /** * Allows users to provide custom middlewares the middlewares will be inserted on the * chain as provided by the user but between the built-in API validation and route * handler and error handler. * @param {any} api the swagger API definition * @param {any} components the components OpenAPI definition * @returns a collection of middlewares split between before and after request handler * * @protected */ protected middlewares(api: OpenAPI.Operation, components: OpenAPI.Document): OrderedMiddlewares; /** * Hook allow to define a custom request handler middleware. If no middleware is * returned the default will be used. * @param {any} api the OpenAPI definition * @param {function} responseValidator the validator for the response expected by the OpenAPI definition. * @returns {Function} * * @protected */ protected requestHandler(path: string, api: OpenAPI.Operation<{}>, responseValidator: ResponseValidator): MiddlewareHandler; /** * Configures application observability events. * @returns the event producer */ protected configureApplicationObservability(apiInfo: InfoObject): EventProducerInterface; protected getCorsMiddlewareIfConfigured(api: OpenAPI.Operation): MiddlewareHandler; /** * Get the policies defined for this route * @param {Array<String>} policies the policies defined for this route * @returns {Array<AbstractPolicy>} the policies * * @private */ protected getPolicies(policies?: Policy[]): Middlewares; /** * Get the cache middleware configured as requested for the current route * @param {any} cache the cache configuration * @returns {Function} the cache middleware. * * @private */ protected getCache(api: any, _components: any): MiddlewareHandler; /** * Hook method that allows to define a global error handle, by default the * register global error handler will log the error and send an error event. * * @see {@link GlobalErrorHandler} for more info * @returns the error handler to register */ protected getGlobalErrorHandler(): ErrorHandler; /** * Define a function that discriminate what responses should be cached. In this case only responses * with 200 response code from GET requests. * @returns {Function} the cache toggle function * * @private */ private cacheOnlySuccessResponsesForGet; } export {};