diff --git a/bin/server.js b/bin/server.js index 4b87a39..e8f839f 100644 --- a/bin/server.js +++ b/bin/server.js @@ -5,4 +5,7 @@ const random = require("corenode/dist/libs/random") new cloudlink.Server({ autoInit: true, id: runtime.args.id ?? random.generateName(), +}) + +new cloudlink.Client.createInterface("http://localhost:3010").then((client) => { }) \ No newline at end of file diff --git a/src/client/index.js b/src/client/index.js index 777a0ff..750de25 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -28,7 +28,7 @@ export class RequestAdaptor { if (typeof this.payload[1] === "object") { payloads.query = this.payload[1] } - }else if (typeof this.payload === "object"){ + } else if (typeof this.payload === "object") { payloads = { ...payloads, ...this.payload @@ -94,7 +94,7 @@ function generateDispatcher(bridge, method, route, getContext) { if (opt.parseData) { obj = req.data - }else { + } else { obj = req } @@ -103,12 +103,7 @@ function generateDispatcher(bridge, method, route, getContext) { } async function createInterface(address, getContext) { - let objects = { - get: Object(), - post: Object(), - put: Object(), - delete: Object() - } + let objects = {} const bridge = new Bridge({ origin: address @@ -116,12 +111,15 @@ async function createInterface(address, getContext) { await bridge.connect() - const routes = bridge.map.routes ?? [] - const methods = bridge.map.methods ?? {} + const map = bridge.map - if (Array.isArray(routes)) { - routes.forEach((route) => { - const method = methods[route].toLowerCase() + Object.keys(map).forEach((method) => { + method = method.toLowerCase() + if (typeof objects[method] !== "object") { + objects[method] = Object() + } + + Object.keys(map[method]).forEach((route) => { const tree = route.split("/") const hasTree = tree.length >= 1 let nameKey = route @@ -143,7 +141,9 @@ async function createInterface(address, getContext) { objects[method][nameKey] = generateDispatcher(bridge, method, route, getContext) }) - } + + }) + return objects } diff --git a/src/server/index.js b/src/server/index.js index acb0016..64fcda2 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -94,10 +94,14 @@ class Server { if (typeof endpoint.controller === "function") { endpoint.controller = new classes.Controller(endpoint.route, endpoint.controller) } - + endpoint.method = endpoint.method.toLowerCase() - this.endpoints[endpoint.route] = endpoint + if (typeof this.endpoints[endpoint.method] !== "object") { + this.endpoints[endpoint.method] = Object() + } + + this.endpoints[endpoint.method][endpoint.route] = endpoint this.routes.push(endpoint.route) const routeModel = [endpoint.route] @@ -173,24 +177,38 @@ class Server { } }) + this.registerEndpoint({ + method: "PUT", + route: "/session", + controller: (req, res) => { + res.send("bruh") + } + }) + this.registerEndpoint({ + method: "DELETE", + route: "/session", + controller: (req, res) => { + res.send("deleted bruh") + } + }) + this.registerEndpoint({ method: "get", route: "/map", controller: (req, res) => { - const methods = {} + const map = {} - this.routes.forEach((route) => { - const endpoint = this.endpoints[route] ?? {} - - if (typeof endpoint.method === "string") { - methods[route] = endpoint.method + Object.keys(this.endpoints).forEach((method) => { + if (typeof map[method] !== "object") { + map[method] = Object() } - }) - res.json({ - routes: this.routes, - methods: methods + Object.keys(this.endpoints[method]).forEach((route) => { + map[method] = route + }) }) + + res.json(map) } }) }