UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

74 lines (73 loc) 2.41 kB
import { LoadPath, MiddlewareInitted } from "../../types/internal"; import SafeServerEventEmitter from "../safeEventEmitter"; import HTTP from "../../types/http"; import Websocket from "../../types/webSocket"; import Static from "../../types/static"; import RoutePath from "./path"; import RouteContentTypes from "./contentTypes"; import RouteDefaultHeaders from "./defaultHeaders"; export default class RouteIndex<GlobContext extends Record<any, any>, Middlewares extends MiddlewareInitted[] = []> extends SafeServerEventEmitter<GlobContext, Middlewares> { protected middlewares: MiddlewareInitted[]; private externals; /** * List of Routes */ constructor(); /** * Add a new Block of Routes with a Prefix * @example * ``` * const controller = new Server({ }) * * controller.path('/', (path) => path * .http('GET', '/cool', (http) => http * .onRequest((ctr) => { * ctr.print('cool!') * }) * ) * .path('/api', (path) => path * .http('GET', '/', (http) => http * .onRequest((ctr) => { * ctr.print('Welcome to the API!') * }) * ) * ) * ) * ``` * @since 5.0.0 */ path<Path extends string>( /** The Path Prefix */ prefix: Path, /** The Code to handle the Prefix */ router: ((path: RoutePath<GlobContext, Middlewares, Path>) => RoutePath<GlobContext, Middlewares, Path>) | RoutePath<any, any>): this; /** * Add Content Type Mapping * @example * ``` * controller.contentTypes((cT) => cT * .add('.jayson', 'application/json') * ) * ``` * @since 5.3.0 */ contentTypes( /** The Code to handle the Headers */ code: (path: RouteContentTypes) => RouteContentTypes): this; /** * Add Default Headers * @example * ``` * controller.defaultHeaders((dH) => dH * .add('X-Api-Version', '1.0.0') * ) * ``` * @since 5.3.0 */ defaultHeaders( /** The Code to handle the Headers */ code: (path: RouteDefaultHeaders) => RouteDefaultHeaders): this; /** * Internal Method for Generating Routes Object * @since 6.0.0 */ getData(): Promise<{ routes: HTTP[]; webSockets: Websocket[]; statics: Static[]; loadPaths: LoadPath[]; contentTypes: {}; defaultHeaders: {}; }>; }