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

@ -6,3 +6,6 @@ new cloudlink.Server({
autoInit: true,
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") {
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
}

View File

@ -97,7 +97,11 @@ class Server {
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]
@ -174,23 +178,37 @@ class Server {
})
this.registerEndpoint({
method: "get",
route: "/map",
method: "PUT",
route: "/session",
controller: (req, res) => {
const methods = {}
this.routes.forEach((route) => {
const endpoint = this.endpoints[route] ?? {}
if (typeof endpoint.method === "string") {
methods[route] = endpoint.method
res.send("bruh")
}
})
this.registerEndpoint({
method: "DELETE",
route: "/session",
controller: (req, res) => {
res.send("deleted bruh")
}
})
res.json({
routes: this.routes,
methods: methods
this.registerEndpoint({
method: "get",
route: "/map",
controller: (req, res) => {
const map = {}
Object.keys(this.endpoints).forEach((method) => {
if (typeof map[method] !== "object") {
map[method] = Object()
}
Object.keys(this.endpoints[method]).forEach((route) => {
map[method] = route
})
})
res.json(map)
}
})
}