From 8acaba3911bc1be42c58860a8b8f0e29d92f9ad1 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Mon, 31 Jul 2023 19:23:22 +0000 Subject: [PATCH] fix linting errors, group more routes, use mergeParams in router init --- src/main.js | 2 -- src/routes/auth.js | 2 +- src/routes/cluster.js | 19 +++++-------------- src/routes/cluster/disk.js | 15 ++++++++------- src/routes/cluster/net.js | 9 +++++---- src/routes/cluster/pci.js | 12 ++++++------ src/routes/proxmox.js | 2 +- src/routes/sync.js | 2 +- src/routes/user.js | 3 ++- 9 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/main.js b/src/main.js index d6509b1..7bdb7af 100644 --- a/src/main.js +++ b/src/main.js @@ -24,8 +24,6 @@ app.use(morgan("combined")); // legacy defs const requestPVE = global.pve.requestPVE; const handleResponse = global.pve.handleResponse; -const getDiskInfo = global.pve.getDiskInfo; -const getDeviceInfo = global.pve.getDeviceInfo; const getNodeAvailDevices = global.pve.getNodeAvailDevices; const checkAuth = global.utils.checkAuth; const approveResources = global.utils.approveResources; diff --git a/src/routes/auth.js b/src/routes/auth.js index 7a06103..72fcbc8 100644 --- a/src/routes/auth.js +++ b/src/routes/auth.js @@ -1,5 +1,5 @@ import { Router } from "express"; -export const router = Router(); +export const router = Router({ mergeParams: true }); ; const domain = global.db.domain; const checkAuth = global.utils.checkAuth; diff --git a/src/routes/cluster.js b/src/routes/cluster.js index eeae7f1..b379774 100644 --- a/src/routes/cluster.js +++ b/src/routes/cluster.js @@ -1,5 +1,5 @@ import { Router } from "express"; -export const router = Router(); +export const router = Router({ mergeParams: true }); const nodeRegexP = "[\\w-]+"; const typeRegexP = "qemu|lxc"; @@ -8,22 +8,13 @@ const vmidRegexP = "\\d+"; const basePath = `/:node(${nodeRegexP})/:type(${typeRegexP})/:vmid(${vmidRegexP})`; import("./cluster/disk.js").then((module) => { - router.use(`${basePath}/disk`, (req, res, next) => { - req.routeparams = Object.assign({}, req.routeparams, req.params); - next(); - }, module.router); + router.use(`${basePath}/disk`, module.router); }); import("./cluster/net.js").then((module) => { - router.use(`${basePath}/net`, (req, res, next) => { - req.routeparams = Object.assign({}, req.routeparams, req.params); - next(); - }, module.router); + router.use(`${basePath}/net`, module.router); }); import("./cluster/pci.js").then((module) => { - router.use(`${basePath}/pci`,(req, res, next) => { - req.routeparams = Object.assign({}, req.routeparams, req.params); - next(); - }, module.router); -}); \ No newline at end of file + router.use(`${basePath}/pci`, module.router); +}); diff --git a/src/routes/cluster/disk.js b/src/routes/cluster/disk.js index a54c238..43bb51e 100644 --- a/src/routes/cluster/disk.js +++ b/src/routes/cluster/disk.js @@ -1,6 +1,7 @@ import { Router } from "express"; -export const router = Router(); +export const router = Router({ mergeParams: true }); +const db = global.db; const requestPVE = global.pve.requestPVE; const handleResponse = global.pve.handleResponse; const getDiskInfo = global.pve.getDiskInfo; @@ -21,7 +22,7 @@ const pveAPIToken = global.db.pveAPIToken; * - 500: {error: string} * - 500: PVE Task Object */ -router.post(`/:disk/detach`, async (req, res) => { +router.post("/:disk/detach", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, @@ -69,7 +70,7 @@ router.post(`/:disk/detach`, async (req, res) => { * - 500: {error: string} * - 500: PVE Task Object */ -router.post(`/:disk/attach`, async (req, res) => { +router.post("/:disk/attach", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, @@ -125,7 +126,7 @@ router.post(`/:disk/attach`, async (req, res) => { * - 500: {request: Object, error: string} * - 500: PVE Task Object */ -router.post(`/:disk/resize`, async (req, res) => { +router.post("/:disk/resize", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, @@ -179,7 +180,7 @@ router.post(`/:disk/resize`, async (req, res) => { * - 500: {request: Object, error: string} * - 500: PVE Task Object */ -router.post(`/:disk/move`, async (req, res) => { +router.post("/:disk/move", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, @@ -243,7 +244,7 @@ router.post(`/:disk/move`, async (req, res) => { * - 500: {error: string} * - 500: PVE Task Object */ -router.delete(`/:disk/delete`, async (req, res) => { +router.delete("/:disk/delete", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, @@ -295,7 +296,7 @@ router.delete(`/:disk/delete`, async (req, res) => { * - 500: {request: Object, error: string} * - 500: PVE Task Object */ -router.post(`/:disk/create`, async (req, res) => { +router.post("/:disk/create", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, diff --git a/src/routes/cluster/net.js b/src/routes/cluster/net.js index 0dc6384..708011e 100644 --- a/src/routes/cluster/net.js +++ b/src/routes/cluster/net.js @@ -1,6 +1,7 @@ import { Router } from "express"; -export const router = Router(); +export const router = Router({ mergeParams: true }); ; +const db = global.db; const requestPVE = global.pve.requestPVE; const handleResponse = global.pve.handleResponse; const checkAuth = global.utils.checkAuth; @@ -23,7 +24,7 @@ const pveAPIToken = global.db.pveAPIToken; * - 500: {request: Object, error: string} * - 500: PVE Task Object */ -router.post(`/:netid/create`, async (req, res) => { +router.post("/:netid/create", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, @@ -92,7 +93,7 @@ router.post(`/:netid/create`, async (req, res) => { * - 500: {request: Object, error: string} * - 500: PVE Task Object */ -router.post(`/:netid/modify`, async (req, res) => { +router.post("/:netid/modify", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, @@ -149,7 +150,7 @@ router.post(`/:netid/modify`, async (req, res) => { * - 500: {error: string} * - 500: PVE Task Object */ -router.delete(`/:netid/delete`, async (req, res) => { +router.delete("/:netid/delete", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, diff --git a/src/routes/cluster/pci.js b/src/routes/cluster/pci.js index 50af381..8468bc4 100644 --- a/src/routes/cluster/pci.js +++ b/src/routes/cluster/pci.js @@ -1,13 +1,13 @@ import { Router } from "express"; -export const router = Router(); +export const router = Router({ mergeParams: true }); ; +const db = global.db; const requestPVE = global.pve.requestPVE; const handleResponse = global.pve.handleResponse; const getDeviceInfo = global.pve.getDeviceInfo; const getNodeAvailDevices = global.pve.getNodeAvailDevices; const checkAuth = global.utils.checkAuth; const approveResources = global.utils.approveResources; -const getUserResources = global.utils.getUserResources; const pveAPIToken = global.db.pveAPIToken; /** @@ -22,7 +22,7 @@ const pveAPIToken = global.db.pveAPIToken; * - 401: {auth: false, path: string} * - 500: {error: string} */ -router.get(`/:hostpci`, async (req, res) => { +router.get("/:hostpci", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, @@ -70,7 +70,7 @@ router.get(`/:hostpci`, async (req, res) => { * - 500: {request: Object, error: string} * - 500: PVE Task Object */ -router.post(`/:hostpci/modify`, async (req, res) => { +router.post("/:hostpci/modify", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, @@ -154,7 +154,7 @@ router.post(`/:hostpci/modify`, async (req, res) => { * - 500: {request: Object, error: string} * - 500: PVE Task Object */ -router.post(`/create`, async (req, res) => { +router.post("/create", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, @@ -233,7 +233,7 @@ router.post(`/create`, async (req, res) => { * - 500: {request: Object, error: string} * - 500: PVE Task Object */ -router.delete(`/:hostpci/delete`, async (req, res) => { +router.delete("/:hostpci/delete", async (req, res) => { req.params = Object.assign({}, req.routeparams, req.params); const params = { node: req.params.node, diff --git a/src/routes/proxmox.js b/src/routes/proxmox.js index 2d12922..6437ff9 100644 --- a/src/routes/proxmox.js +++ b/src/routes/proxmox.js @@ -1,5 +1,5 @@ import { Router } from "express"; -export const router = Router(); +export const router = Router({ mergeParams: true }); ; const requestPVE = global.pve.requestPVE; diff --git a/src/routes/sync.js b/src/routes/sync.js index 0f6f875..8c43198 100644 --- a/src/routes/sync.js +++ b/src/routes/sync.js @@ -2,7 +2,7 @@ import { WebSocketServer } from "ws"; import * as cookie from "cookie"; import { Router } from "express"; -export const router = Router(); +export const router = Router({ mergeParams: true }); ; const requestPVE = global.pve.requestPVE; const checkAuth = global.utils.checkAuth; diff --git a/src/routes/user.js b/src/routes/user.js index ca26c67..329253f 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -1,6 +1,7 @@ import { Router } from "express"; -export const router = Router(); +export const router = Router({ mergeParams: true }); ; +const db = global.db; const requestPVE = global.pve.requestPVE; const checkAuth = global.utils.checkAuth; const getUserResources = global.utils.getUserResources;