From a1f27a9f648c4d26b67d47da84f2041b75f43ef4 Mon Sep 17 00:00:00 2001 From: baldo Date: Thu, 21 Jul 2022 18:49:49 +0200 Subject: [PATCH] Fixed compile errors. --- server/resources/monitoringResource.ts | 19 +++++++++++-------- server/types/shared.ts | 1 + 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/server/resources/monitoringResource.ts b/server/resources/monitoringResource.ts index b86a983..ec54a3f 100644 --- a/server/resources/monitoringResource.ts +++ b/server/resources/monitoringResource.ts @@ -8,7 +8,7 @@ import {handleJSONWithData} from "../utils/resources"; import {normalizeString} from "../utils/strings"; import {forConstraint} from "../validation/validator"; import {Request, Response} from "express"; -import {MonitoringResponse, MonitoringToken, toMonitoringResponse} from "../types"; +import {isMonitoringToken, JSONObject, MonitoringResponse, MonitoringToken, toMonitoringResponse} from "../types"; const isValidToken = forConstraint(CONSTRAINTS.token, false); @@ -33,23 +33,26 @@ export function getAll(req: Request, res: Response): void { .catch(err => Resources.error(res, err)); } -export const confirm = handleJSONWithData(async data => { +function getValidatedToken(data: JSONObject): MonitoringToken { + if (!isMonitoringToken(data.token)) { + throw {data: 'Missing token.', type: ErrorTypes.badRequest}; + } const token = normalizeString(data.token); if (!isValidToken(token)) { throw {data: 'Invalid token.', type: ErrorTypes.badRequest}; } - const validatedToken: MonitoringToken = token as MonitoringToken; + return token as MonitoringToken; +} + +export const confirm = handleJSONWithData(async data => { + const validatedToken = getValidatedToken(data); const node = await MonitoringService.confirm(validatedToken); return toMonitoringResponse(node); }); export const disable = handleJSONWithData(async data => { - const token = normalizeString(data.token); - if (!isValidToken(token)) { - throw {data: 'Invalid token.', type: ErrorTypes.badRequest}; - } - const validatedToken: MonitoringToken = token as MonitoringToken; + const validatedToken: MonitoringToken = getValidatedToken(data); const node = await MonitoringService.disable(validatedToken); return toMonitoringResponse(node); diff --git a/server/types/shared.ts b/server/types/shared.ts index 7edbbd3..39d1d34 100644 --- a/server/types/shared.ts +++ b/server/types/shared.ts @@ -337,6 +337,7 @@ export function toUnixTimestampSeconds(ms: UnixTimestampMilliseconds): UnixTimes } export type MonitoringToken = string & { readonly __tag: unique symbol }; +export const isMonitoringToken = isString; export enum MonitoringState { ACTIVE = "active",