added endpoint reachability

This commit is contained in:
srgooglo 2022-05-05 10:40:16 +02:00
parent 5ceb68a577
commit bc8c4ecfc4

View File

@ -214,11 +214,17 @@ class Server {
...this.endpointsMap[endpoint.method], ...this.endpointsMap[endpoint.method],
[endpoint.route]: { [endpoint.route]: {
route: endpoint.route, route: endpoint.route,
} enabled: endpoint.enabled ?? true,
},
} }
this.httpInterface[endpoint.method](endpoint.route, ...middlewares, async (req, res) => { this.httpInterface[endpoint.method](endpoint.route, ...middlewares, async (req, res) => {
try { try {
// check if endpoint is disabled
if (!this.endpointsMap[endpoint.method][endpoint.route].enabled) {
throw new Error("Endpoint is disabled!")
}
return await endpoint.fn(req, res) return await endpoint.fn(req, res)
} catch (error) { } catch (error) {
if (typeof this.params.onRouteError === "function") { if (typeof this.params.onRouteError === "function") {
@ -300,6 +306,18 @@ class Server {
this.httpInterface.close() this.httpInterface.close()
this.wsInterface.io.close() this.wsInterface.io.close()
} }
toogleEndpointReachability = (method, route, enabled) => {
if (typeof this.endpointsMap[method] !== "object") {
throw new Error(`Cannot toogle endpoint, method [${method}] not set!`)
}
if (typeof this.endpointsMap[method][route] !== "object") {
throw new Error(`Cannot toogle endpoint [${route}], is not registered!`)
}
this.endpointsMap[method][route].enabled = enabled ?? !this.endpointsMap[method][route].enabled
}
} }
module.exports = Server module.exports = Server