Extracted some types to shared module.

This commit is contained in:
baldo 2022-02-22 15:39:39 +01:00
parent 7671bfd4d3
commit 7ef13bc28c
4 changed files with 47 additions and 12 deletions

View file

@ -1,7 +1,5 @@
import {ArrayField, Field, RawJsonField} from "sparkson" import {ArrayField, Field, RawJsonField} from "sparkson"
export type Version = string;
// TODO: Replace string types by more specific types like URL, Password, etc. // TODO: Replace string types by more specific types like URL, Password, etc.
export class LoggingConfig { export class LoggingConfig {

View file

@ -1,5 +1,6 @@
export * from "./config"; export * from "./config";
export * from "./logger"; export * from "./logger";
export * from "./shared";
// TODO: Token type. // TODO: Token type.
export type Token = string; export type Token = string;
@ -48,16 +49,6 @@ export type NodeSecrets = {
monitoringToken?: MonitoringToken, monitoringToken?: MonitoringToken,
}; };
export type NodeStatistics = {
registered: number,
withVPN: number,
withCoords: number,
monitoring: {
active: number,
pending: number
}
};
export type MailId = string; export type MailId = string;
export type MailData = any; export type MailData = any;
export type MailType = string; export type MailType = string;

45
server/types/shared.ts Normal file
View file

@ -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);
}

View file

@ -11,6 +11,7 @@ stdenv.mkDerivation rec {
nasm nasm
nodejs-16_x nodejs-16_x
rsync rsync
sass
sqlite sqlite
yarn yarn
zlib zlib