added ComplexController class

This commit is contained in:
srgooglo 2022-02-18 13:03:26 +01:00
parent 9aa6b617e8
commit 97b20ca4d6

View File

@ -0,0 +1,52 @@
const { EventEmitter } = require("events")
export default class ComplexController {
constructor(params) {
this.params = { ...params }
this.internalEvents = new EventEmitter()
}
getEndpoints() {
let endpoints = []
global.VALID_HTTP_METHODS.forEach((httpMethod) => {
if (typeof this[httpMethod] === "object") {
const controllerMethods = Object.keys(this[httpMethod])
controllerMethods.forEach((methodKey) => {
const fn = this[httpMethod][methodKey]
let endpoint = {
method: httpMethod,
route: methodKey,
middlewares: [],
fn: fn,
}
if (typeof fn === "object") {
endpoint.middlewares = fn.middlewares
endpoint.fn = fn.fn
}
endpoint.fn = this.createHandler(endpoint.fn)
endpoints.push(endpoint)
})
}
})
return endpoints
}
createHandler = (fn) => {
return (...args) => new Promise(async (resolve, reject) => {
try {
const result = await fn(...args)
return resolve(result)
} catch (error) {
return reject(error)
}
})
}
}