UNPKG

rjweb-server

Version:

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

155 lines (154 loc) 5.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.currentVersion = void 0; const HttpRequestContext_1 = __importDefault(require("./request/HttpRequestContext")); const WsOpenContext_1 = __importDefault(require("./request/WsOpenContext")); const WsMessageContext_1 = __importDefault(require("./request/WsMessageContext")); const WsCloseContext_1 = __importDefault(require("./request/WsCloseContext")); exports.currentVersion = 9; class Dummy { } class Middleware { /** * Build a new Middleware * @example * ``` * import { Middleware } from "rjweb-server" * * const middleware = new Middleware<{ greeting: string }>('Say Hi', '1.0.0') * .httpRequest((config, server, context, ctr, end) => { * if (ctr.url.path.includes('hello')) end(ctr.print(config.greeting)) * }) * .export() * ``` * @since 9.0.0 */ constructor(name, version) { this.name = name; this.version = version; this.data = { classContexts: { HttpRequest: () => HttpRequestContext_1.default, WsOpen: () => WsOpenContext_1.default, WsMessage: () => WsMessageContext_1.default, WsClose: () => WsCloseContext_1.default }, callbacks: {}, finishCallbacks: {} }; } /** * Callback that runs when the middleware is loaded (server starting, can be multiple times!) * @since 9.0.0 */ load(callback) { this.data.callbacks.load = callback; return this; } /** * Callback that runs when any HTTP Request is made * @since 9.0.0 */ httpRequest(callback) { this.data.callbacks.httpRequest = callback; return this; } /** * Callback that runs when any HTTP Request finishes * @since 9.0.0 */ httpRequestFinish(callback) { this.data.finishCallbacks.httpRequest = callback; return this; } /** * Modify the HttpRequestContext Class to add new properties or methods for the user * @since 9.0.0 */ httpRequestContext(callback) { this.data.classContexts.HttpRequest = callback; return this; } /** * Callback that runs when any WebSocket Connection is opened * @since 9.0.0 */ wsOpen(callback) { this.data.callbacks.wsOpen = callback; return this; } /** * Callback that runs when any WebSocket Connection is opened and finishes running all events * @since 9.0.0 */ wsOpenFinish(callback) { this.data.finishCallbacks.wsOpen = callback; return this; } /** * Modify the WsOpenContext Class to add new properties or methods for the user * @since 9.0.0 */ wsOpenContext(callback) { this.data.classContexts.WsOpen = callback; return this; } /** * Callback that runs when any WebSocket Message is recieved * @since 9.0.0 */ wsMessage(callback) { this.data.callbacks.wsMessage = callback; return this; } /** * Callback that runs when any WebSocket Message is recieved and finishes running all events * @since 9.0.0 */ wsMessageFinish(callback) { this.data.finishCallbacks.wsMessage = callback; return this; } /** * Modify the WsMessageContext Class to add new properties or methods for the user * @since 9.0.0 */ wsMessageContext(callback) { this.data.classContexts.WsMessage = callback; return this; } /** * Callback that runs when any WebSocket Connection is closed * @since 9.0.0 */ wsClose(callback) { this.data.callbacks.wsClose = callback; return this; } /** * Callback that runs when any WebSocket Connection is closed and finishes running all events * @since 9.0.0 */ wsCloseFinish(callback) { this.data.finishCallbacks.wsClose = callback; return this; } /** * Modify the WsCloseContext Class to add new properties or methods for the user * @since 9.0.0 */ wsCloseContext(callback) { this.data.classContexts.WsClose = callback; return this; } /** * Export the Middleware for use in the Server * @since 9.0.0 */ export() { const self = this; return { use(config) { return { callbacks: self.data.callbacks, classContexts: self.data.classContexts, finishCallbacks: self.data.finishCallbacks, config, rjwebVersion: exports.currentVersion, infos: { name: self.name, version: self.version } }; } }; } } exports.default = Middleware;