refactor endpoint registration method

This commit is contained in:
srgooglo 2021-07-09 13:02:13 +02:00
parent 09fd76e554
commit 9ecceef97d

View File

@ -200,38 +200,36 @@ class Server {
} }
try { try {
let { method, route, controller, fn } = endpoint
// check if controller is an already a controller // check if controller is an already a controller
if (typeof controller === "string") { if (typeof endpoint.controller === "string") {
// check if the controller is already loaded, else try to fetch // check if the controller is already loaded, else try to fetch
if (typeof this.controllers[controller] !== "undefined") { if (typeof this.controllers[endpoint.controller] !== "undefined") {
controller = this.controllers[controller] endpoint.controller = this.controllers[endpoint.controller]
} else { } else {
controller = fetchController(controller) endpoint.controller = fetchController(endpoint.controller)
} }
} }
// check if the controller is an default function and transform it into an controller // check if the controller is an default function and transform it into an controller
if (typeof controller === "function") { if (typeof endpoint.controller === "function") {
controller = { endpoint.controller = {
default: controller default: endpoint.controller
} }
} }
// fullfill undefined // fulfill undefined
if (typeof method === "undefined") { if (typeof endpoint.method === "undefined") {
method = "GET" endpoint.method = "GET"
} }
if (typeof fn === "undefined") { if (typeof endpoint.fn === "undefined") {
fn = "default" endpoint.fn = "default"
} }
// convert with class
endpoint.controller = new classes.Controller(endpoint.route, endpoint.controller[endpoint.fn])
// append to server // append to server
this.registerEndpoint({ this.registerEndpoint(endpoint)
...endpoint,
controller: new classes.Controller(route, controller[fn])
})
} catch (error) { } catch (error) {
runtime.logger.dump(error) runtime.logger.dump(error)
console.error(error) console.error(error)