From 7ef13bc28c93a28ff5bff881efa4eb3a3eb82c40 Mon Sep 17 00:00:00 2001 From: baldo Date: Tue, 22 Feb 2022 15:39:39 +0100 Subject: [PATCH] Extracted some types to shared module. --- server/types/config.ts | 2 -- server/types/index.ts | 11 +---------- server/types/shared.ts | 45 ++++++++++++++++++++++++++++++++++++++++++ shell.nix | 1 + 4 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 server/types/shared.ts diff --git a/server/types/config.ts b/server/types/config.ts index acebb9c..f369ccb 100644 --- a/server/types/config.ts +++ b/server/types/config.ts @@ -1,7 +1,5 @@ import {ArrayField, Field, RawJsonField} from "sparkson" -export type Version = string; - // TODO: Replace string types by more specific types like URL, Password, etc. export class LoggingConfig { diff --git a/server/types/index.ts b/server/types/index.ts index 8b27f9d..bc0e85e 100644 --- a/server/types/index.ts +++ b/server/types/index.ts @@ -1,5 +1,6 @@ export * from "./config"; export * from "./logger"; +export * from "./shared"; // TODO: Token type. export type Token = string; @@ -48,16 +49,6 @@ export type NodeSecrets = { monitoringToken?: MonitoringToken, }; -export type NodeStatistics = { - registered: number, - withVPN: number, - withCoords: number, - monitoring: { - active: number, - pending: number - } -}; - export type MailId = string; export type MailData = any; export type MailType = string; diff --git a/server/types/shared.ts b/server/types/shared.ts new file mode 100644 index 0000000..4d143bd --- /dev/null +++ b/server/types/shared.ts @@ -0,0 +1,45 @@ +// Types shared with the client. + +export function isObject(arg: unknown): arg is object { + return arg !== null && typeof arg === "object"; +} + +export type Version = string; + +export function isVersion(arg: unknown): arg is Version { + // Should be good enough for now. + return typeof arg === "string"; +} + +export type NodeStatistics = { + registered: number; + withVPN: number; + withCoords: number; + monitoring: { + active: number; + pending: number; + }; +}; + +export function isNodeStatistics(arg: unknown): arg is NodeStatistics { + if (!isObject(arg)) { + return false; + } + const stats = arg as NodeStatistics; + return ( + typeof stats.registered === "number" && + typeof stats.withVPN === "number" && + typeof stats.withCoords === "number" && + typeof stats.monitoring === "object" && + typeof stats.monitoring.active === "number" && + typeof stats.monitoring.pending === "number" + ); +} + +export interface Statistics { + nodes: NodeStatistics; +} + +export function isStatistics(arg: unknown): arg is Statistics { + return isObject(arg) && isNodeStatistics((arg as Statistics).nodes); +} diff --git a/shell.nix b/shell.nix index 6934ce6..96db906 100644 --- a/shell.nix +++ b/shell.nix @@ -11,6 +11,7 @@ stdenv.mkDerivation rec { nasm nodejs-16_x rsync + sass sqlite yarn zlib