ESLint: Auto reformat and fixing some warnings / errors.
This commit is contained in:
parent
5237db38e0
commit
91690509d3
50 changed files with 2141 additions and 1493 deletions
server/resources
|
@ -2,11 +2,11 @@ import Constraints from "../shared/validation/constraints";
|
|||
import ErrorTypes from "../utils/errorTypes";
|
||||
import * as MonitoringService from "../services/monitoringService";
|
||||
import * as NodeService from "../services/nodeService";
|
||||
import {normalizeMac, normalizeString} from "../shared/utils/strings";
|
||||
import {forConstraint, forConstraints} from "../shared/validation/validator";
|
||||
import { normalizeMac, normalizeString } from "../shared/utils/strings";
|
||||
import { forConstraint, forConstraints } from "../shared/validation/validator";
|
||||
import * as Resources from "../utils/resources";
|
||||
import {handleJSONWithData} from "../utils/resources";
|
||||
import {Request, Response} from "express";
|
||||
import { handleJSONWithData } from "../utils/resources";
|
||||
import { Request, Response } from "express";
|
||||
import {
|
||||
CreateOrUpdateNode,
|
||||
DomainSpecificNodeResponse,
|
||||
|
@ -24,18 +24,26 @@ import {
|
|||
toDomainSpecificNodeResponse,
|
||||
Token,
|
||||
toNodeResponse,
|
||||
toNodeTokenResponse
|
||||
toNodeTokenResponse,
|
||||
} from "../types";
|
||||
|
||||
const nodeFields = ['hostname', 'key', 'email', 'nickname', 'mac', 'coords', 'monitoring'];
|
||||
const nodeFields = [
|
||||
"hostname",
|
||||
"key",
|
||||
"email",
|
||||
"nickname",
|
||||
"mac",
|
||||
"coords",
|
||||
"monitoring",
|
||||
];
|
||||
|
||||
function getNormalizedNodeData(reqData: JSONObject): CreateOrUpdateNode {
|
||||
const node: { [key: string]: any } = {};
|
||||
const node: { [key: string]: unknown } = {};
|
||||
for (const field of nodeFields) {
|
||||
let value: JSONValue | undefined = reqData[field];
|
||||
if (isString(value)) {
|
||||
value = normalizeString(value);
|
||||
if (field === 'mac') {
|
||||
if (field === "mac") {
|
||||
value = normalizeMac(value as MAC);
|
||||
}
|
||||
}
|
||||
|
@ -49,7 +57,7 @@ function getNormalizedNodeData(reqData: JSONObject): CreateOrUpdateNode {
|
|||
return node;
|
||||
}
|
||||
|
||||
throw {data: "Invalid node data.", type: ErrorTypes.badRequest};
|
||||
throw { data: "Invalid node data.", type: ErrorTypes.badRequest };
|
||||
}
|
||||
|
||||
const isValidNode = forConstraints(Constraints.node, false);
|
||||
|
@ -57,77 +65,82 @@ const isValidToken = forConstraint(Constraints.token, false);
|
|||
|
||||
function getValidatedToken(data: JSONObject): Token {
|
||||
if (!isToken(data.token)) {
|
||||
throw {data: 'Missing token.', type: ErrorTypes.badRequest};
|
||||
throw { data: "Missing token.", type: ErrorTypes.badRequest };
|
||||
}
|
||||
const token = normalizeString(data.token);
|
||||
if (!isValidToken(token)) {
|
||||
throw {data: 'Invalid token.', type: ErrorTypes.badRequest};
|
||||
throw { data: "Invalid token.", type: ErrorTypes.badRequest };
|
||||
}
|
||||
return token as Token;
|
||||
}
|
||||
|
||||
export const create = handleJSONWithData<NodeTokenResponse>(async data => {
|
||||
export const create = handleJSONWithData<NodeTokenResponse>(async (data) => {
|
||||
const baseNode = getNormalizedNodeData(data);
|
||||
if (!isValidNode(baseNode)) {
|
||||
throw {data: 'Invalid node data.', type: ErrorTypes.badRequest};
|
||||
throw { data: "Invalid node data.", type: ErrorTypes.badRequest };
|
||||
}
|
||||
|
||||
const node = await NodeService.createNode(baseNode);
|
||||
return toNodeTokenResponse(node);
|
||||
});
|
||||
|
||||
export const update = handleJSONWithData<NodeTokenResponse>(async data => {
|
||||
export const update = handleJSONWithData<NodeTokenResponse>(async (data) => {
|
||||
const validatedToken: Token = getValidatedToken(data);
|
||||
const baseNode = getNormalizedNodeData(data);
|
||||
if (!isValidNode(baseNode)) {
|
||||
throw {data: 'Invalid node data.', type: ErrorTypes.badRequest};
|
||||
throw { data: "Invalid node data.", type: ErrorTypes.badRequest };
|
||||
}
|
||||
|
||||
const node = await NodeService.updateNode(validatedToken, baseNode);
|
||||
return toNodeTokenResponse(node);
|
||||
});
|
||||
|
||||
export const remove = handleJSONWithData<void>(async data => {
|
||||
export const remove = handleJSONWithData<void>(async (data) => {
|
||||
const validatedToken = getValidatedToken(data);
|
||||
await NodeService.deleteNode(validatedToken);
|
||||
});
|
||||
|
||||
export const get = handleJSONWithData<NodeResponse>(async data => {
|
||||
export const get = handleJSONWithData<NodeResponse>(async (data) => {
|
||||
const validatedToken: Token = getValidatedToken(data);
|
||||
const node = await NodeService.getNodeDataByToken(validatedToken);
|
||||
return toNodeResponse(node);
|
||||
});
|
||||
|
||||
async function doGetAll(req: Request): Promise<{ total: number; pageNodes: any }> {
|
||||
const restParams = await Resources.getValidRestParams('list', 'node', req);
|
||||
async function doGetAll(
|
||||
req: Request
|
||||
): Promise<{ total: number; pageNodes: any }> {
|
||||
const restParams = await Resources.getValidRestParams("list", "node", req);
|
||||
|
||||
const nodes = await NodeService.getAllNodes();
|
||||
|
||||
const realNodes = nodes.filter(node =>
|
||||
// We ignore nodes without tokens as those are only manually added ones like gateways.
|
||||
!!node.token // FIXME: As node.token may not be undefined or null here, handle this when loading!
|
||||
const realNodes = nodes.filter(
|
||||
(node) =>
|
||||
// We ignore nodes without tokens as those are only manually added ones like gateways.
|
||||
!!node.token // FIXME: As node.token may not be undefined or null here, handle this when loading!
|
||||
);
|
||||
|
||||
const macs: MAC[] = realNodes.map(node => node.mac);
|
||||
const macs: MAC[] = realNodes.map((node) => node.mac);
|
||||
const nodeStateByMac = await MonitoringService.getByMacs(macs);
|
||||
|
||||
const domainSpecificNodes: DomainSpecificNodeResponse[] = realNodes.map(node => {
|
||||
const nodeState: NodeStateData = nodeStateByMac[node.mac] || {};
|
||||
return toDomainSpecificNodeResponse(node, nodeState);
|
||||
});
|
||||
const domainSpecificNodes: DomainSpecificNodeResponse[] = realNodes.map(
|
||||
(node) => {
|
||||
const nodeState: NodeStateData = nodeStateByMac[node.mac] || {};
|
||||
return toDomainSpecificNodeResponse(node, nodeState);
|
||||
}
|
||||
);
|
||||
|
||||
const filteredNodes = Resources.filter<DomainSpecificNodeResponse>(
|
||||
domainSpecificNodes,
|
||||
[
|
||||
'hostname',
|
||||
'nickname',
|
||||
'email',
|
||||
'token',
|
||||
'mac',
|
||||
'site',
|
||||
'domain',
|
||||
'key',
|
||||
'onlineState'
|
||||
"hostname",
|
||||
"nickname",
|
||||
"email",
|
||||
"token",
|
||||
"mac",
|
||||
"site",
|
||||
"domain",
|
||||
"key",
|
||||
"onlineState",
|
||||
],
|
||||
restParams
|
||||
);
|
||||
|
@ -141,13 +154,13 @@ async function doGetAll(req: Request): Promise<{ total: number; pageNodes: any }
|
|||
);
|
||||
const pageNodes = Resources.getPageEntities(sortedNodes, restParams);
|
||||
|
||||
return {total, pageNodes};
|
||||
return { total, pageNodes };
|
||||
}
|
||||
|
||||
export function getAll(req: Request, res: Response): void {
|
||||
doGetAll(req)
|
||||
.then((result: { total: number, pageNodes: any[] }) => {
|
||||
res.set('X-Total-Count', result.total.toString(10));
|
||||
.then((result: { total: number; pageNodes: any[] }) => {
|
||||
res.set("X-Total-Count", result.total.toString(10));
|
||||
return Resources.success(res, result.pageNodes);
|
||||
})
|
||||
.catch((err: any) => Resources.error(res, err));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue