import { NextFunction, Request, RequestHandler, Response, Router } from "express"; import { RouteError, RouteErrorI } from "./RouteError"; import { BodyParameter, ParameterType, RouteParameter } from "./RequestParameters"; import { VersionedMiddleware } from 'version-router-express'; /** * response options * @public */ export declare type SuccessResponse = 'message' | 'Array' | 'object' | 'file'; export declare type RouteHandler = (req: Request, res: Response, next: NextFunction, errorOrMessage: string | Error | RouteError, statusCode: number, log?: boolean, redirect?: string | false, options?: { [key: string]: any; }) => {}; /** * Configuration Object for SuperRoute instance * @public */ export interface RouteSettings { /** * route path */ path: string; /** * Http verb */ verb: ExpressHttpVerb; /** * response content type - used to set headers */ responseContentType?: string; /** * response body type - used for automated testing */ responseReturnType?: SuccessResponse; /** * response format used for documentation */ responseFormat?: string; /** * An array of Request Handlers hat will be mounted on the route */ middleware?: Array | RequestHandler | RequestHandlerWithPromise; /** * An Array of VersionedMiddleware instances - see {@link https://www.npmjs.com/package/version-router-express} * route can accept either */ versionedMiddleware?: Array; /** * Route name */ name?: string; /** * Route description */ description?: string; /** * Additional comments for documentation */ comments?: string; /** * When true, will mount the authentication function as middleware before other routes */ authenticate?: boolean; /** * A RoutePermissions object for use with the package's standard access control function */ permissions?: RoutePermissions; /** * An Array of BodyParameter instances, defining required and/or optional parameters for request body * as well as input validation tests */ bodyParams?: Array; /** * An Array of RouteParameter instances, defining the types and validation rules for route parameters */ routeParams?: Array; /** * Optional redirect when error is handled by the route */ redirectOnError?: string; /** * When set to true, will return an ascii output of the requested route's information when called with the OPTIONS http method. * * Example: curl --request OPTIONS https://localhost:8080/path/to/my/route */ showHelp?: boolean | ((self: SuperRoute) => boolean); /** * Options that will be passed to the global error handler */ errorHandlerOptions?: ErrorHandlerOptions; } export interface ErrorHandlerOptions { log?: boolean; redirectOnError?: string; [key: string]: any; } /** * Base Class for an express route super route * Middleware order: * 1. Authentication function * 2. Access control * 3. Route Parameters validation * 4. Route Parameters validation * 5. Route Specific middleware defined in the middleware or versioned middleware arrays * @class * @public */ export declare abstract class SuperRoute implements RouteSettings { path: string; verb: ExpressHttpVerb; responseContentType?: string; responseFormat?: string; middleware: Array | RequestHandler | RequestHandlerWithPromise; versionedMiddleware: Array; name: string; description: string; authenticate: boolean; permissions: RoutePermissions; bodyParams: Array; routeParams: Array; redirectOnError: string; showHelp?: boolean | ((self: SuperRoute) => boolean); errorHandlerOptions: ErrorHandlerOptions; $$authenticationFunction: RequestHandler | RequestHandlerWithPromise; $$accessControlFunction: AccessControlFunction; $$errorHandler: srErrorHandlerFunction; private settings; private static defaultSettings; /** * @param settings */ constructor(settings: RouteSettings); /** * mounts the route on a router instance or express app * @param router * @public */ mount(router: Router): void; /** * checks if the route or body param matches the specified type * @param value * @param type * @hidden */ static matchesType(value: any, type: ParameterType): boolean; /** * checks if the user has the permissions defined in the permissions object and according to the defined hierarchy * For use with an access control function. * @param userPermissions * @param permissions * @param hierarchy */ static checkPermissions(userPermissions: Array | string, permissions: RoutePermissions, hierarchy: Array): boolean; /** * a default error handler to mount as the last middleware of the app * @param err * @param req * @param res * @param next */ static DefaultErrorHandler(err: RouteError | RouteErrorI, req: Request, res: Response, next: NextFunction): void; static HandleError(route: any): RouteHandler; /** * Middleware that handles body parameters validation * It runs the following tests in this order, for all defined parameters, and can return more than one error: * 1. Checks the presence of the property in the body * 2. Matches the type of the property's value * 3. Runs additional tests if such were defined (e.g. RegEx match test for a string) * @param req * @param res * @param next * @hidden */ protected validateBody(req: Request, res: Response, next: NextFunction): void; /** * Middleware that handles route parameters validation * It runs the following tests in this order, for all defined parameters, and can return more than one error: * 1. Checks the presence of the property in the route * 2. Runs additional tests if such were defined (e.g. RegEx match test for a string) * @param req * @param res * @param next * @hidden */ protected validateRouteParams(req: Request, res: Response, next: NextFunction): void; /** * Renders markdown output containing the description of the route and sends back * @param req * @param res * @param next * @hidden */ protected help(req: Request, res: Response, next: NextFunction): Promise; /** * Generates markdown documentation for hte route */ toMarkdown(): string; /** * generates tables * @protected * @hidden */ protected generateTables(): { bodyParamsTable: string; routeParamsTable: string; permissionsTable: string; }; /** * generates a route error * @param message - error message * @param statusCode - http status code for response * @param redirect - optional redirect * @param logError * @hidden */ Error(message: string, statusCode?: number, redirect?: string | false, logError?: boolean): RouteError; /** * handles errors in the routes logic. * errors can be channeled to a custom error handler by defining $$errorHandler * by default, a RouteError object will be created and passed to next(err) * @param req * @param res * @param next * @param errorOrMessage - error message * @param statusCode - html response status code * @param respondWith - optional custom error message to send as response * @param log - log the error to the console if true * @param redirect - redirect url * @param options - options object to pass to a custom error handler * @hidden */ handleError(req: Request, res: Response, next: NextFunction, errorOrMessage: string | Error | RouteError, statusCode?: number, respondWith?: string, log?: boolean, redirect?: string | false, options?: { [key: string]: any; }): void; /** * handles errors in the routes logic. * errors can be channeled to a custom error handler by defining $$errorHandler * by default, a RouteError object containing route and request data will be created and passed to next(err) * @param middlewareArgs - req, res, next from the express middleware function * @param errorOrMessage - error message * @param statusCode - html response status code * @param respondWith - optional custom error message to send as response * @param log - log the error to the console if true * @param redirect - redirect url * @param options - options object to pass to a custom error handler */ handle(middlewareArgs: IArguments, errorOrMessage: string | Error | RouteError, statusCode?: number, respondWith?: string, log?: boolean, redirect?: string | false, options?: { [key: string]: any; }): void; } /** * Access Control configuration for a route instance or extending class * equalOrGreaterThan - requester must have a permission level that is equal or greater * than the given string as defined by the hierarchy array. * specific - requester must have all the given permissions * merge - when set to 'and' requester must satisfy both the specific an hierarchical rules. * * Example: * * { * equalOrGreaterThan: 'admin', * specific: ['specialPermission', 'awesomeDude'], * merge: 'and' * } * will only grant access to admins that also have the specialPermission and awesomeDude permisions */ export interface RoutePermissions { equalOrGreaterThan?: string; specific?: Array; merge?: 'and' | 'or'; } export interface RequestHandlerWithPromise extends RequestHandler { (req: Request, res: Response, next: NextFunction): Promise; } /** * A function that returns a request handler */ export interface RequestHandlerFactoryFunction { (...args: Array): RequestHandler | RequestHandlerWithPromise; } /** * Access control function for SuperRoute settings * When mounted, it will be called with the route's permissions and should return a RequestHandler */ export interface AccessControlFunction extends RequestHandlerFactoryFunction { (permissions: RoutePermissions): RequestHandler | RequestHandlerWithPromise; } /** * A function that replaces the default error handler of the SuperRoute class */ export interface srErrorHandlerFunction { (error: RouteError, req: Request, res: Response, next: NextFunction, options: { [key: string]: any; }): void; } /** * Available http verbs for super-route settings object */ export declare type ExpressHttpVerb = 'get' | 'post' | 'put' | 'head' | 'delete' | 'options' | 'trace' | 'copy' | 'lock' | 'mkcol' | 'move' | 'purge' | 'propfind' | 'proppatch' | 'unlock' | 'report' | 'mkactivity' | 'checkout' | 'merge' | 'm-search' | 'notify' | 'subscribe' | 'unsubscribe' | 'patch' | 'search' | 'connect'; export declare const AvailableVerbs: Array; export declare class Templates { static routeInfoMarkdown: string; } //# sourceMappingURL=SuperRoute.d.ts.map