added support for endpoint function transformation

This commit is contained in:
srgooglo 2023-01-26 18:18:34 +01:00
parent 28a99afae4
commit 132d3fd009

View File

@ -43,7 +43,7 @@ module.exports = class Controller {
const methodEndpoints = Object.entries(endpointsByMethod) const methodEndpoints = Object.entries(endpointsByMethod)
for (let [endpointKey, endpoint] of methodEndpoints) { for (let [endpointKey, endpoint] of methodEndpoints) {
// check if endpoint is a class or an object, if it's a object, create a new class from it extending Endpoint // Handle endpoint transformation as an object
if (typeof endpoint === "object") { if (typeof endpoint === "object") {
const objEndpoint = endpoint const objEndpoint = endpoint
@ -62,6 +62,21 @@ module.exports = class Controller {
} }
} }
// Handle endpoint transformation as a function
if (typeof endpoint === "function") {
const endpointFn = endpoint
endpoint = class extends Endpoint {
static method = httpMethodKey
static route = endpointKey
constructor(args) {
super(args)
this.fn = endpointFn
}
}
}
// check if endpoint is a class // check if endpoint is a class
if (typeof endpoint !== "function") { if (typeof endpoint !== "function") {
throw new Error(`Invalid endpoint. Expected class or object, got ${typeof endpoint}`) throw new Error(`Invalid endpoint. Expected class or object, got ${typeof endpoint}`)