feat: es6+mods

This commit is contained in:
Andres 2023-10-06 22:11:40 +02:00
parent 773b64ea30
commit 19c92ed244
19 changed files with 179 additions and 245 deletions

View file

@ -1,7 +1,7 @@
const express = require("express");
import express from "express";
const router = express.Router();
const auth = require("../services/auth");
import { authorize } from "../services/auth.js";
router.get("/login", async function (req, res) {
if (process.env.ZU_DISABLE_AUTH === "true") {
@ -13,7 +13,7 @@ router.get("/login", async function (req, res) {
router.post("/login", async function (req, res) {
if (req.body.username && req.body.password) {
auth.authorize(req.body.username, req.body.password, function (err, user) {
authorize(req.body.username, req.body.password, function (err, user) {
if (user) {
res.send({ token: user["token"] });
} else {
@ -27,4 +27,4 @@ router.post("/login", async function (req, res) {
}
});
module.exports = router;
export default router;

View file

@ -1,13 +1,13 @@
const express = require("express");
import express from "express";
const router = express.Router();
const auth = require("../services/auth");
const api = require("../utils/controller-api");
import { isAuthorized } from "../services/auth.js";
import { api } from "../utils/controller-api.js";
router.get("/status", auth.isAuthorized, async function (req, res) {
router.get("/status", isAuthorized, async function (req, res) {
api.get("status").then(function (controllerRes) {
res.send(controllerRes.data);
});
});
module.exports = router;
export default router;

View file

@ -1,20 +1,24 @@
const express = require("express");
import express from "express";
const router = express.Router({ mergeParams: true });
const auth = require("../services/auth");
const member = require("../services/member");
import { isAuthorized } from "../services/auth.js";
import {
deleteMemberAdditionalData,
getMembersData,
updateMemberAdditionalData,
} from "../services/member.js";
const api = require("../utils/controller-api");
import { api } from "../utils/controller-api.js";
// get all members
router.get("/", auth.isAuthorized, async function (req, res) {
router.get("/", isAuthorized, async function (req, res) {
// @ts-ignore
const nwid = req.params.nwid;
api
.get("controller/network/" + nwid + "/member")
.then(async function (controllerRes) {
const mids = Object.keys(controllerRes.data);
const data = await member.getMembersData(nwid, mids);
const data = await getMembersData(nwid, mids);
res.send(data);
})
.catch(function (err) {
@ -23,11 +27,11 @@ router.get("/", auth.isAuthorized, async function (req, res) {
});
// get member
router.get("/:mid", auth.isAuthorized, async function (req, res) {
router.get("/:mid", isAuthorized, async function (req, res) {
// @ts-ignore
const nwid = req.params.nwid;
const mid = req.params.mid;
const data = await member.getMembersData(nwid, [mid]);
const data = await getMembersData(nwid, [mid]);
if (data[0]) {
res.send(data[0]);
} else {
@ -36,33 +40,33 @@ router.get("/:mid", auth.isAuthorized, async function (req, res) {
});
// update member
router.post("/:mid", auth.isAuthorized, async function (req, res) {
router.post("/:mid", isAuthorized, async function (req, res) {
// @ts-ignore
const nwid = req.params.nwid;
const mid = req.params.mid;
member.updateMemberAdditionalData(nwid, mid, req.body);
updateMemberAdditionalData(nwid, mid, req.body);
if (req.body.config) {
api
.post("controller/network/" + nwid + "/member/" + mid, req.body.config)
.then(async function () {
const data = await member.getMembersData(nwid, [mid]);
const data = await getMembersData(nwid, [mid]);
res.send(data[0]);
})
.catch(function (err) {
res.status(500).send({ error: err.message });
});
} else {
const data = await member.getMembersData(nwid, [mid]);
const data = await getMembersData(nwid, [mid]);
res.send(data[0]);
}
});
// delete member
router.delete("/:mid", auth.isAuthorized, async function (req, res) {
router.delete("/:mid", isAuthorized, async function (req, res) {
// @ts-ignore
const nwid = req.params.nwid;
const mid = req.params.mid;
member.deleteMemberAdditionalData(nwid, mid);
deleteMemberAdditionalData(nwid, mid);
api
.delete("controller/network/" + nwid + "/member/" + mid)
.then(function () {})
@ -86,4 +90,4 @@ router.delete("/:mid", auth.isAuthorized, async function (req, res) {
});
});
module.exports = router;
export default router;

View file

@ -1,12 +1,17 @@
const express = require("express");
import express from "express";
const router = express.Router();
const auth = require("../services/auth");
const network = require("../services/network");
import { isAuthorized } from "../services/auth.js";
import {
getNetworksData,
createNetworkAdditionalData,
updateNetworkAdditionalData,
deleteNetworkAdditionalData,
} from "../services/network.js";
const api = require("../utils/controller-api");
const constants = require("../utils/constants");
const getZTAddress = require("../utils/zt-address");
import { api } from "../utils/controller-api.js";
import { defaultRules } from "../utils/constants.js";
import { getZTAddress } from "../utils/zt-address.js";
let ZT_ADDRESS = null;
getZTAddress().then(function (address) {
@ -14,18 +19,18 @@ getZTAddress().then(function (address) {
});
// get all networks
router.get("/", auth.isAuthorized, async function (req, res) {
router.get("/", isAuthorized, async function (req, res) {
api.get("controller/network").then(async function (controllerRes) {
const nwids = controllerRes.data;
const data = await network.getNetworksData(nwids);
const data = await getNetworksData(nwids);
res.send(data);
});
});
// get network
router.get("/:nwid", auth.isAuthorized, async function (req, res) {
router.get("/:nwid", isAuthorized, async function (req, res) {
const nwid = req.params.nwid;
const data = await network.getNetworksData([nwid]);
const data = await getNetworksData([nwid]);
if (data[0]) {
res.send(data[0]);
} else {
@ -34,49 +39,49 @@ router.get("/:nwid", auth.isAuthorized, async function (req, res) {
});
// create new network
router.post("/", auth.isAuthorized, async function (req, res) {
router.post("/", isAuthorized, async function (req, res) {
let reqData = req.body;
if (reqData.config) {
const config = reqData.config;
delete reqData.config;
reqData = config;
reqData.rules = JSON.parse(constants.defaultRules);
reqData.rules = JSON.parse(defaultRules);
} else {
res.status(400).send({ error: "Bad request" });
}
api
.post("controller/network/" + ZT_ADDRESS + "______", reqData)
.then(async function (controllerRes) {
await network.createNetworkAdditionalData(controllerRes.data.id);
const data = await network.getNetworksData([controllerRes.data.id]);
await createNetworkAdditionalData(controllerRes.data.id);
const data = await getNetworksData([controllerRes.data.id]);
res.send(data[0]);
});
});
// update network
router.post("/:nwid", auth.isAuthorized, async function (req, res) {
router.post("/:nwid", isAuthorized, async function (req, res) {
const nwid = req.params.nwid;
network.updateNetworkAdditionalData(nwid, req.body);
updateNetworkAdditionalData(nwid, req.body);
if (req.body.config) {
api
.post("controller/network/" + nwid, req.body.config)
.then(async function () {
const data = await network.getNetworksData([nwid]);
const data = await getNetworksData([nwid]);
res.send(data[0]);
})
.catch(function (err) {
res.status(500).send({ error: err.message });
});
} else {
const data = await network.getNetworksData([nwid]);
const data = await getNetworksData([nwid]);
res.send(data[0]);
}
});
// delete network
router.delete("/:nwid", auth.isAuthorized, async function (req, res) {
router.delete("/:nwid", isAuthorized, async function (req, res) {
const nwid = req.params.nwid;
network.deleteNetworkAdditionalData(nwid);
deleteNetworkAdditionalData(nwid);
api
.delete("controller/network/" + nwid)
.then(function (controllerRes) {
@ -87,4 +92,4 @@ router.delete("/:nwid", auth.isAuthorized, async function (req, res) {
});
});
module.exports = router;
export default router;