UNPKG

node-rest-server

Version:
88 lines (82 loc) 4.01 kB
import { Request as Request$1, Response, NextFunction, Locals } from 'express'; export { Request as ExpressRequest, Response } from 'express'; import { Server, IncomingMessage, RequestListener } from 'node:http'; import { ServerOptions } from 'node:https'; import { Socket } from 'node:net'; import { Duplex } from 'node:stream'; import { CorsOptions } from 'cors'; interface BaseRequest { pathParams: Request$1['params']; queryParams: Request$1['query']; headers: Request$1['headers']; } interface FilterData { filter: Locals; } declare function ExpressMiddlewareFunc(request: Request$1, response: Response, next: NextFunction): void; type HttpServerInstance = Server | undefined; interface LoggerConfiguration { enable: boolean; name?: string; level?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'; /** * @deprecated The "debug" property is deprecated. Use "level" instead. */ debug?: boolean; file?: string; } declare function DatabaseConnectionFunc(requestData: HttpRequest): Promise<unknown>; declare function FilterFunc(requestData: HttpRequest): Promise<unknown>; declare function HeaderFunc(requestData: HttpRequest): Record<string, string>; interface ControllerOptions { getDatabaseConnection?: typeof DatabaseConnectionFunc; } interface ServerConfiguration extends ControllerOptions { basePath?: string; port?: number; headers?: Record<string, string> | typeof HeaderFunc; delay?: number; logger?: boolean | LoggerConfiguration; filter?: typeof FilterFunc; cors?: CorsOptions; https?: ServerOptions; middlewares?: Array<typeof ExpressMiddlewareFunc>; } interface RestServer { close: (forced?: boolean) => Promise<Error | undefined>; addListener(event: string, listener: (...args: unknown[]) => void): HttpServerInstance; addListener(event: 'close' | 'listening', listener: () => void): HttpServerInstance; addListener(event: 'connect' | 'upgrade', listener: (req: InstanceType<typeof IncomingMessage>, socket: Duplex, head: Buffer) => void): HttpServerInstance; addListener(event: 'checkContinue' | 'checkExpectation' | 'request', listener: RequestListener): HttpServerInstance; addListener(event: 'connection', listener: (socket: Socket) => void): HttpServerInstance; addListener(event: 'dropRequest', listener: (req: InstanceType<typeof IncomingMessage>, socket: Duplex) => void): HttpServerInstance; addListener(event: 'clientError', listener: (err: Error, socket: Duplex) => void): HttpServerInstance; addListener(event: 'error', listener: (err: Error) => void): HttpServerInstance; } type RouteMethod = 'all' | 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head'; interface Request extends BaseRequest { url: string; body: unknown; getHeader: (name: string) => string | undefined; method: Lowercase<RouteMethod> | Uppercase<RouteMethod>; } interface HttpRequest extends Request, Partial<FilterData> { } interface ControllerResponse { status?: number; payload?: unknown; headers?: Record<string, string>; [key: string]: unknown; } declare function ControllerFunc(requestData: HttpRequest, controllerOptions: ControllerOptions): ControllerResponse | Promise<ControllerResponse>; interface RouteConfigItem { method: Lowercase<RouteMethod> | Uppercase<RouteMethod>; status?: number; headers?: Record<string, string>; middlewares?: Array<typeof ExpressMiddlewareFunc>; controller: typeof ControllerFunc; } type RouteConfiguration = Record<string, RouteConfigItem | Array<RouteConfigItem>>; declare function NodeRestServer(routeConfig: RouteConfiguration, serverConfig?: ServerConfiguration): RestServer; export { ControllerFunc, DatabaseConnectionFunc, FilterFunc, HeaderFunc, NodeRestServer, NodeRestServer as default }; export type { ControllerOptions, ControllerResponse, HttpRequest, LoggerConfiguration, RestServer, RouteConfigItem, RouteConfiguration, RouteMethod, ServerConfiguration };