rjweb-server
Version:
Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS
148 lines (147 loc) • 4.63 kB
JavaScript
import HttpRequestContext from "./request/HttpRequestContext";
import WsOpenContext from "./request/WsOpenContext";
import WsMessageContext from "./request/WsMessageContext";
import WsCloseContext from "./request/WsCloseContext";
export const currentVersion = 9;
class Dummy {
}
export default 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,
WsOpen: () => WsOpenContext,
WsMessage: () => WsMessageContext,
WsClose: () => WsCloseContext
}, 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: currentVersion,
infos: {
name: self.name,
version: self.version
}
};
}
};
}
}