diff --git a/client/demo/db.json b/client/demo/db.json index 90ee806..adb839a 100644 --- a/client/demo/db.json +++ b/client/demo/db.json @@ -51,11 +51,15 @@ ], "restart": {}, "reset": {}, - "config": ["$return", "pki:\n ca: /config/ca.crt\n cert: /config/cosmos.crt\n key: /config/cosmos.key\n blocklist: []\nstatic_host_map:\n 192.168.201.1:\n - vpn.domain.com:4242\nlighthouse:\n am_lighthouse: true\n interval: 60\n hosts: []\nlisten:\n host: 0.0.0.0\n port: 4242\npunchy:\n punch: true\n respond: true\nrelay:\n am_relay: true\n use_relays: true\n relays: []\ntun:\n disabled: false\n dev: nebula1\n drop_local_broadcast: false\n drop_multicast: false\n tx_queue: 500\n mtu: 1300\n routes: []\n unsafe_routes: []\nlogging:\n level: info\n format: text\nfirewall:\n outbound_action: drop\n inbound_action: drop\n conntrack:\n tcp_timeout: 12m\n udp_timeout: 3m\n default_timeout: 10m\n outbound:\n - port: any\n proto: any\n host: any\n inbound:\n - port: any\n proto: any\n host: any\n"], - "logs": ["$return", "Some logs..."] + "config": { + "$return": "pki:\n ca: /config/ca.crt\n cert: /config/cosmos.crt\n key: /config/cosmos.key\n blocklist: []\nstatic_host_map:\n 192.168.201.1:\n - vpn.domain.com:4242\nlighthouse:\n am_lighthouse: true\n interval: 60\n hosts: []\nlisten:\n host: 0.0.0.0\n port: 4242\npunchy:\n punch: true\n respond: true\nrelay:\n am_relay: true\n use_relays: true\n relays: []\ntun:\n disabled: false\n dev: nebula1\n drop_local_broadcast: false\n drop_multicast: false\n tx_queue: 500\n mtu: 1300\n routes: []\n unsafe_routes: []\nlogging:\n level: info\n format: text\nfirewall:\n outbound_action: drop\n inbound_action: drop\n conntrack:\n tcp_timeout: 12m\n udp_timeout: 3m\n default_timeout: 10m\n outbound:\n - port: any\n proto: any\n host: any\n inbound:\n - port: any\n proto: any\n host: any\n" + }, + "logs": { + "$return": "Some logs..." + } }, "servapps": { - "list": [ + "$return": [ { "Id": "a03ea9a3408b2831198f2192c628090193e9d5e8e4c4515f2c94d1cc26f6a7f5", "Names": [ diff --git a/client/demo/proxy.ts b/client/demo/proxy.ts index 8169939..343aa91 100644 --- a/client/demo/proxy.ts +++ b/client/demo/proxy.ts @@ -1,7 +1,7 @@ import { Request, Response } from "express" import { resolve } from "path" import { readFileSync } from "fs" -import { stringify } from "querystring" +import querystring from "querystring" import jsonServer from "json-server" import traverse from "traverse" @@ -14,8 +14,6 @@ const definedDB: Record = JSON.parse(readFileSync(resolve(__dirname const autoDB = genDB(definedDB) const dbPaths = Object.keys(autoDB) -console.log(dbPaths) - // TODO: Reference types do not work at the ["$return", data] level // A possible solution is to replace filter with map and custom behavior for get, post and put // defining them to interact with the parent object @@ -43,8 +41,8 @@ const router = jsonServer.router(autoDB, { router.render = (req: Request, res: Response) => { if (typeof res.locals.data["_comment"] === "string" && res.locals.data["_comment"].toLowerCase().includes("todo")) return res.sendStatus(404) - else if (Array.isArray(res.locals.data.list)) - res.locals.data = res.locals.data.list; + else if ("$return" in res.locals.data) + res.locals.data = res.locals.data["$return"]; else if (res.locals.data[0] === "$return") res.locals.data = res.locals.data[1] @@ -57,6 +55,10 @@ router.render = (req: Request, res: Response) => { break } + const query = querystring.parse(req.url) + + if (query["_inline"] && Array.isArray(res.locals.result) && res.locals.result.length === 1) + res.locals.result = res.locals.result.at(0) return res.jsonp({ data: res.locals.result, status: "ok" @@ -68,7 +70,7 @@ proxyServer.use(middlewares) proxyServer.use("/api", rewriter) proxyServer.use("/api", (req, res, next) => { - const query = stringify(req.query as Record) + const query = querystring.stringify(req.query as Record) const dbKey = req.path .replace("/", "") .replace(/\//g, ".") @@ -91,9 +93,11 @@ function genDB(initialDB: JSONObject, skipRoot: boolean = true): Record, node) { if (skipRoot && this.isRoot) return acc - const currentPath = this.path.join(".") + const currentPath = this.path.join(".").replace(/\.\$return/g, "") - acc[currentPath] = isObject(node) ? node : ["$return", node] + acc[currentPath] = isObject(node) ? node : { + $return: node + } if (Array.isArray(node)) { node @@ -107,7 +111,7 @@ function genDB(initialDB: JSONObject, skipRoot: boolean = true): Record !!value && typeof value === "string" && !value.includes(".")) .forEach(value => Object.keys(childNodeDB).forEach(childNodePath => acc[`${currentPath}.${value}${childNodePath && "." || "" - }${childNodePath}`.replace(/\//g, ".")] = childNodeDB[childNodePath] + }${childNodePath}`.replace(/\//g, ".")] = childNodeDB[childNodePath] )) ) } diff --git a/client/demo/routes.json b/client/demo/routes.json index 6929477..85d4100 100644 --- a/client/demo/routes.json +++ b/client/demo/routes.json @@ -1,10 +1,10 @@ { "/servapps/:container/logs": "/servapps/demo/logs", - "/volume/:name": "/volumes/Volumes?Name=:name", - "/network/:name": "/networks?Name=:name", "/servapps/:id/secure/:res": "/servapps/demo/secure", "/servapps/:id/manage/:action": "/servapps/demo/manage", - "/servapps/:id/networks": "/test/:id/NetworkSettings", + "/volume/:name": "/volumes/Volumes/:name", + "/network/:name": "/networks/:name", + "/servapps/:id/networks": "/servapps/:id/NetworkSettings", "/test/:id": "/servapps?Id=:id", "/servapps/:containerId/network/:networkId": "/servapps/:containerId/networks" } \ No newline at end of file