mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 10:34:17 +00:00
added ComplexController
class
This commit is contained in:
parent
9aa6b617e8
commit
97b20ca4d6
52
src/classes/ComplexController/index.js
Normal file
52
src/classes/ComplexController/index.js
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user