mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 10:34:17 +00:00
update Controller
generation
This commit is contained in:
parent
51a4b17835
commit
c3debd31b4
@ -1,13 +1,14 @@
|
|||||||
const { EventEmitter } = require("events")
|
const { EventEmitter } = require("events")
|
||||||
|
const Endpoint = require("../endpoint")
|
||||||
|
|
||||||
class Controller {
|
module.exports = class Controller {
|
||||||
constructor(params) {
|
constructor(params) {
|
||||||
this.params = { ...params }
|
this.params = { ...params }
|
||||||
|
|
||||||
this.internalEvents = new EventEmitter()
|
this.internalEvents = new EventEmitter()
|
||||||
}
|
}
|
||||||
|
|
||||||
getWSEndpoints() {
|
__get_ws_endpoints() {
|
||||||
if (typeof this.channels !== "object") {
|
if (typeof this.channels !== "object") {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
@ -24,55 +25,96 @@ class Controller {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
getEndpoints() {
|
__get_http_endpoints() {
|
||||||
let endpoints = []
|
let endpoints = []
|
||||||
|
|
||||||
global.VALID_HTTP_METHODS.forEach((httpMethod) => {
|
global.VALID_HTTP_METHODS.forEach((httpMethodKey) => {
|
||||||
if (typeof this[httpMethod] === "object") {
|
const endpointsByMethod = this.httpEndpoints[httpMethodKey]
|
||||||
const fixedMethod = global.FIXED_HTTP_METHODS[httpMethod]
|
|
||||||
const controllerMethods = Object.keys(this[httpMethod])
|
|
||||||
|
|
||||||
controllerMethods.forEach((methodKey) => {
|
if (typeof endpointsByMethod !== "object") {
|
||||||
const fn = this[httpMethod][methodKey]
|
return
|
||||||
|
}
|
||||||
|
|
||||||
let endpoint = {
|
const fixedMethod = global.FIXED_HTTP_METHODS[httpMethodKey]
|
||||||
method: fixedMethod ?? httpMethod,
|
const methodEndpoints = Object.entries(endpointsByMethod)
|
||||||
route: methodKey,
|
|
||||||
middlewares: [],
|
for (let [endpointKey, endpoint] of methodEndpoints) {
|
||||||
fn: fn,
|
// check if endpoint is a class or an object, if it's a object, create a new class from it extending Endpoint
|
||||||
|
if (typeof endpoint === "object") {
|
||||||
|
const objEndpoint = endpoint
|
||||||
|
|
||||||
|
endpoint = class extends Endpoint {
|
||||||
|
static method = httpMethodKey
|
||||||
|
static route = objEndpoint.route ?? endpointKey
|
||||||
|
static enabled = objEndpoint.enabled
|
||||||
|
static middlewares = objEndpoint.middlewares
|
||||||
|
|
||||||
|
constructor(args) {
|
||||||
|
super(args)
|
||||||
|
this.fn = objEndpoint.fn
|
||||||
|
this.onCatch = objEndpoint.onCatch
|
||||||
|
this.customHandler = objEndpoint.customHandler
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof this.constructor.useRoute === "string") {
|
// check if endpoint is a class
|
||||||
endpoint.route = this.constructor.useRoute + endpoint.route
|
if (typeof endpoint !== "function") {
|
||||||
}
|
throw new Error(`Invalid endpoint. Expected class or object, got ${typeof endpoint}`)
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof fn === "object") {
|
// check if controller has a static useRoute property
|
||||||
endpoint.middlewares = fn.middlewares
|
if (typeof this.constructor.useRoute === "string") {
|
||||||
endpoint.fn = fn.fn
|
endpoint.route = `${this.constructor.useRoute}${endpoint.route}`
|
||||||
endpoint.enabled = fn.enabled
|
endpoint.route = endpoint.route.replace(/\/\//g, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
endpoint.fn = this.createHandler(endpoint.fn)
|
const endpointInstance = new endpoint()
|
||||||
|
|
||||||
endpoints.push(endpoint)
|
const functionHandler = this.__create_default_fn_handler({
|
||||||
|
fn: endpointInstance.fn,
|
||||||
|
onCatch: endpointInstance.onCatch,
|
||||||
|
customHandler: endpointInstance.customHandler,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const endpointGenerationObject = {
|
||||||
|
method: fixedMethod ?? httpMethodKey,
|
||||||
|
route: endpoint.route,
|
||||||
|
middlewares: endpoint.middlewares,
|
||||||
|
enabled: endpoint.enabled,
|
||||||
|
fn: functionHandler,
|
||||||
|
}
|
||||||
|
|
||||||
|
endpoints.push(endpointGenerationObject)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return endpoints
|
return endpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
createHandler = (fn) => {
|
__create_default_fn_handler = ({
|
||||||
|
fn,
|
||||||
|
onCatch,
|
||||||
|
customHandler,
|
||||||
|
}) => {
|
||||||
|
if (typeof customHandler === "function") {
|
||||||
|
return customHandler
|
||||||
|
}
|
||||||
|
|
||||||
return (...args) => new Promise(async (resolve, reject) => {
|
return (...args) => new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
const result = await fn(...args)
|
const result = await fn(...args)
|
||||||
|
|
||||||
return resolve(result)
|
return resolve(result)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.internalEvents.emit("requestError", error)
|
this.internalEvents.emit("request:error", error)
|
||||||
|
|
||||||
|
if (typeof onCatch === "function") {
|
||||||
|
return onCatch(error, ...args)
|
||||||
|
}
|
||||||
|
|
||||||
return reject(error)
|
return reject(error)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Controller
|
|
Loading…
x
Reference in New Issue
Block a user