handle method fixments

This commit is contained in:
srgooglo 2021-10-18 09:24:21 +02:00
parent d9d7252762
commit f2dd71ecf6

View File

@ -26,6 +26,11 @@ const helpers = process.runtime.helpers ?? require('@corenode/helpers')
//* set globals //* set globals
global.SERVER_VERSION = helpers.getVersion() global.SERVER_VERSION = helpers.getVersion()
const MethodsFix = {
"delete": "del",
}
const ValidMethods = ["get", "post", "put", "patch", "del", "trace", "head"]
class Server { class Server {
constructor(params, endpoints, middlewares) { constructor(params, endpoints, middlewares) {
this.params = params ?? {} this.params = params ?? {}
@ -95,9 +100,18 @@ class Server {
return false return false
} }
// fix data // check and fix method
controller.method = controller.method?.toLowerCase() ?? "get" controller.method = controller.method?.toLowerCase() ?? "get"
if (MethodsFix[controller.method]) {
controller.method = MethodsFix[controller.method]
}
// validate method
if (!ValidMethods.includes(controller.method)){
throw new Error(`Invalid endpoint method: ${controller.method}`)
}
// fulfill an undefined fn // fulfill an undefined fn
if (typeof controller.fn === "undefined") { if (typeof controller.fn === "undefined") {
controller.fn = (req, res, next) => { controller.fn = (req, res, next) => {