UNPKG

rjweb-server

Version:

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

117 lines (116 loc) 4.26 kB
import WebSocket from "../../types/webSocket"; import HTTP from "../../types/http"; import { HTTPMethods, MiddlewareInitted, RoutedValidation } from "../../types/internal"; import RouteWS from "./ws"; import RouteHTTP from "./http"; import RouteDefaultHeaders from "./defaultHeaders"; export default class RouteFile<GlobContext extends Record<any, any>, Middlewares extends MiddlewareInitted[]> { private routes; private webSockets; private headers; private parsedHeaders; private validations; private externals; private hasCalledGet; /** * Create a new Route File * @example * ``` * // routes/say.js * module.exports = new controller.routeFile((file) => file * .http('GET', '/say/<text>', (http) => http * .onRequest((ctr) => { * ctr.print(ctr.params.get('text')) * }) * ) * ) * * // index.js * const controller = new Server({ }) * * controller.path('/', (path) => path * .loadCJS('./routes') * ) * ``` * @since 6.0.0 */ constructor( /** The Code to handle the File */ code: (file: RouteFile<GlobContext, Middlewares>) => RouteFile<GlobContext, Middlewares>); /** * Add a Validation * @example * ``` * // The /api route will automatically check for correct credentials * // Obviously still putting the prefix (in this case / from the RoutePath in front) * const controller = new Server({ }) * * module.exports = new controller.routeFile((file) => file * .validate(async(ctr) => { * if (!ctr.headers.has('Authorization')) return end(ctr.status(401).print('Unauthorized')) * if (ctr.headers.get('Authorization') !== 'key123 or db request ig') return end(ctr.status(401).print('Unauthorized')) * }) * .redirect('/pics', 'https://google.com/search?q=devil') * ) * ``` * @since 6.0.2 */ validate<Context extends Record<any, any> = {}, Body = unknown>( /** The Function to Validate the Request */ code: RoutedValidation<GlobContext & Context, Body>): this; /** * Add Default Headers * @example * ``` * module.exports = new controller.routeFile((file) => file * .defaultHeaders((dH) => dH * .add('X-Api-Version', '1.0.0') * ) * ) * ``` * @since 6.0.1 */ defaultHeaders( /** The Code to handle the Headers */ code: (path: RouteDefaultHeaders) => RouteDefaultHeaders): this; /** * Add a HTTP Route * @example * ``` * module.exports = new controller.routeFile((file) => file * .http('GET', '/hello', (ws) => ws * .onRequest(async(ctr) => { * ctr.print('Hello bro!') * }) * ) * ) * ``` * @since 6.0.0 */ http<Context extends Record<any, any> = {}, Body = unknown, Path extends string = '/'>( /** The Request Method */ method: HTTPMethods, /** The Path on which this will be available */ path: Path | RegExp, /** The Callback to handle the Endpoint */ callback: (path: RouteHTTP<GlobContext, Context, Body, Middlewares, Path>) => RouteHTTP<GlobContext, Context, Body, Middlewares, Path>): this; /** * Add a Websocket Route * @example * ``` * module.exports = new controller.routeFile((file) => file * .ws('/uptime', (ws) => ws * .onConnect(async(ctr) => { * console.log('Connected to ws!') * }) * .onMessage((ctr) => { * console.log('Received message', ctr.message) * }) * .onClose((ctr) => { * console.log('Disconnected from ws!') * }) * ) * ) * ``` * @since 5.4.0 */ ws<Context extends Record<any, any> = {}, Message = unknown, Path extends string = '/'>( /** The Path on which this will be available */ path: Path | RegExp, /** The Callback to handle the Endpoint */ callback: (path: RouteWS<GlobContext, Context, Message, Middlewares, Path>) => RouteWS<GlobContext, Context, Message, Middlewares, Path>): this; /** * Internal Method for Generating Router Object * @since 6.0.0 */ getData(prefix: string): Promise<{ routes: HTTP[]; webSockets: WebSocket[]; }>; }