try to refact

This commit is contained in:
srgooglo 2021-07-27 15:02:16 +02:00
parent fa14a25a22
commit e1cff3b583
3 changed files with 47 additions and 26 deletions

View File

@ -5,4 +5,7 @@ const random = require("corenode/dist/libs/random")
new cloudlink.Server({ new cloudlink.Server({
autoInit: true, autoInit: true,
id: runtime.args.id ?? random.generateName(), id: runtime.args.id ?? random.generateName(),
})
new cloudlink.Client.createInterface("http://localhost:3010").then((client) => {
}) })

View File

@ -28,7 +28,7 @@ export class RequestAdaptor {
if (typeof this.payload[1] === "object") { if (typeof this.payload[1] === "object") {
payloads.query = this.payload[1] payloads.query = this.payload[1]
} }
}else if (typeof this.payload === "object"){ } else if (typeof this.payload === "object") {
payloads = { payloads = {
...payloads, ...payloads,
...this.payload ...this.payload
@ -94,7 +94,7 @@ function generateDispatcher(bridge, method, route, getContext) {
if (opt.parseData) { if (opt.parseData) {
obj = req.data obj = req.data
}else { } else {
obj = req obj = req
} }
@ -103,12 +103,7 @@ function generateDispatcher(bridge, method, route, getContext) {
} }
async function createInterface(address, getContext) { async function createInterface(address, getContext) {
let objects = { let objects = {}
get: Object(),
post: Object(),
put: Object(),
delete: Object()
}
const bridge = new Bridge({ const bridge = new Bridge({
origin: address origin: address
@ -116,12 +111,15 @@ async function createInterface(address, getContext) {
await bridge.connect() await bridge.connect()
const routes = bridge.map.routes ?? [] const map = bridge.map
const methods = bridge.map.methods ?? {}
if (Array.isArray(routes)) { Object.keys(map).forEach((method) => {
routes.forEach((route) => { method = method.toLowerCase()
const method = methods[route].toLowerCase() if (typeof objects[method] !== "object") {
objects[method] = Object()
}
Object.keys(map[method]).forEach((route) => {
const tree = route.split("/") const tree = route.split("/")
const hasTree = tree.length >= 1 const hasTree = tree.length >= 1
let nameKey = route let nameKey = route
@ -143,7 +141,9 @@ async function createInterface(address, getContext) {
objects[method][nameKey] = generateDispatcher(bridge, method, route, getContext) objects[method][nameKey] = generateDispatcher(bridge, method, route, getContext)
}) })
}
})
return objects return objects
} }

View File

@ -94,10 +94,14 @@ class Server {
if (typeof endpoint.controller === "function") { if (typeof endpoint.controller === "function") {
endpoint.controller = new classes.Controller(endpoint.route, endpoint.controller) endpoint.controller = new classes.Controller(endpoint.route, endpoint.controller)
} }
endpoint.method = endpoint.method.toLowerCase() 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) this.routes.push(endpoint.route)
const routeModel = [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({ this.registerEndpoint({
method: "get", method: "get",
route: "/map", route: "/map",
controller: (req, res) => { controller: (req, res) => {
const methods = {} const map = {}
this.routes.forEach((route) => { Object.keys(this.endpoints).forEach((method) => {
const endpoint = this.endpoints[route] ?? {} if (typeof map[method] !== "object") {
map[method] = Object()
if (typeof endpoint.method === "string") {
methods[route] = endpoint.method
} }
})
res.json({ Object.keys(this.endpoints[method]).forEach((route) => {
routes: this.routes, map[method] = route
methods: methods })
}) })
res.json(map)
} }
}) })
} }