More typesafe NodeSortField definition.
This commit is contained in:
parent
91690509d3
commit
66fb4e5004
|
@ -18,6 +18,7 @@ import { forConstraint } from "../shared/validation/validator";
|
||||||
import {
|
import {
|
||||||
Domain,
|
Domain,
|
||||||
DurationSeconds,
|
DurationSeconds,
|
||||||
|
filterUndefinedFromJSON,
|
||||||
Hostname,
|
Hostname,
|
||||||
isBoolean,
|
isBoolean,
|
||||||
isDomain,
|
isDomain,
|
||||||
|
@ -438,7 +439,7 @@ async function sendMonitoringMailsBatched(
|
||||||
node.nickname + " <" + node.email + ">",
|
node.nickname + " <" + node.email + ">",
|
||||||
mailType,
|
mailType,
|
||||||
{
|
{
|
||||||
node: node,
|
node: filterUndefinedFromJSON(node),
|
||||||
lastSeen: nodeState.last_seen,
|
lastSeen: nodeState.last_seen,
|
||||||
disableUrl: monitoringDisableUrl(monitoringToken),
|
disableUrl: monitoringDisableUrl(monitoringToken),
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import {
|
||||||
CreateOrUpdateNode,
|
CreateOrUpdateNode,
|
||||||
EmailAddress,
|
EmailAddress,
|
||||||
FastdKey,
|
FastdKey,
|
||||||
|
filterUndefinedFromJSON,
|
||||||
Hostname,
|
Hostname,
|
||||||
isFastdKey,
|
isFastdKey,
|
||||||
isHostname,
|
isHostname,
|
||||||
|
@ -504,7 +505,7 @@ async function sendMonitoringConfirmationMail(
|
||||||
node.nickname + " <" + node.email + ">",
|
node.nickname + " <" + node.email + ">",
|
||||||
MailType.MONITORING_CONFIRMATION,
|
MailType.MONITORING_CONFIRMATION,
|
||||||
{
|
{
|
||||||
node: node,
|
node: filterUndefinedFromJSON(node),
|
||||||
confirmUrl: confirmUrl,
|
confirmUrl: confirmUrl,
|
||||||
disableUrl: disableUrl,
|
disableUrl: disableUrl,
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,19 @@ export function parseJSON(str: string): JSONValue {
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function filterUndefinedFromJSON(obj: {
|
||||||
|
[key: string]: JSONValue | undefined;
|
||||||
|
}): JSONObject {
|
||||||
|
const result: JSONObject = {};
|
||||||
|
for (const [key, value] of Object.entries(obj)) {
|
||||||
|
if (value !== undefined) {
|
||||||
|
result[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
export type JSONValue =
|
export type JSONValue =
|
||||||
| null
|
| null
|
||||||
| string
|
| string
|
||||||
|
@ -403,8 +416,8 @@ export type BaseNode = {
|
||||||
nickname: Nickname;
|
nickname: Nickname;
|
||||||
email: EmailAddress;
|
email: EmailAddress;
|
||||||
hostname: Hostname;
|
hostname: Hostname;
|
||||||
coords?: Coordinates;
|
coords: Coordinates | undefined;
|
||||||
key?: FastdKey;
|
key: FastdKey | undefined;
|
||||||
mac: MAC;
|
mac: MAC;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -506,11 +519,10 @@ export const isDomain = toIsNewtype(isString, "" as Domain);
|
||||||
/**
|
/**
|
||||||
* Represents a node in the context of a Freifunk site and domain.
|
* Represents a node in the context of a Freifunk site and domain.
|
||||||
*/
|
*/
|
||||||
export type DomainSpecificNodeResponse = Record<NodeSortField, any> &
|
export type DomainSpecificNodeResponse = NodeResponse & {
|
||||||
NodeResponse & {
|
site: Site | undefined;
|
||||||
site?: Site;
|
domain: Domain | undefined;
|
||||||
domain?: Domain;
|
onlineState: OnlineState | undefined;
|
||||||
onlineState?: OnlineState;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export function isDomainSpecificNodeResponse(
|
export function isDomainSpecificNodeResponse(
|
||||||
|
@ -549,7 +561,8 @@ export function isMonitoringResponse(arg: unknown): arg is MonitoringResponse {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum NodeSortField {
|
// noinspection JSUnusedGlobalSymbols
|
||||||
|
enum NodeSortFieldEnum {
|
||||||
HOSTNAME = "hostname",
|
HOSTNAME = "hostname",
|
||||||
NICKNAME = "nickname",
|
NICKNAME = "nickname",
|
||||||
EMAIL = "email",
|
EMAIL = "email",
|
||||||
|
@ -563,7 +576,17 @@ export enum NodeSortField {
|
||||||
MONITORING_STATE = "monitoringState",
|
MONITORING_STATE = "monitoringState",
|
||||||
}
|
}
|
||||||
|
|
||||||
export const isNodeSortField = toIsEnum(NodeSortField);
|
export type NodeSortField = keyof Pick<
|
||||||
|
DomainSpecificNodeResponse,
|
||||||
|
NodeSortFieldEnum
|
||||||
|
>;
|
||||||
|
|
||||||
|
export function isNodeSortField(arg: unknown): arg is NodeSortField {
|
||||||
|
if (!isString(arg)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return Object.values(NodeSortFieldEnum).includes(arg as NodeSortField);
|
||||||
|
}
|
||||||
|
|
||||||
export type NodesFilter = {
|
export type NodesFilter = {
|
||||||
hasKey?: boolean;
|
hasKey?: boolean;
|
||||||
|
|
|
@ -286,12 +286,15 @@ export function filter<E>(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sort<T extends Record<S, unknown>, S extends string>(
|
export function sort<
|
||||||
entities: T[],
|
Type extends { [Key in SortField]: unknown },
|
||||||
isSortField: TypeGuard<S>,
|
SortField extends string
|
||||||
|
>(
|
||||||
|
entities: Type[],
|
||||||
|
isSortField: TypeGuard<SortField>,
|
||||||
restParams: RestParams
|
restParams: RestParams
|
||||||
): T[] {
|
): Type[] {
|
||||||
const sortField: S | undefined = isSortField(restParams._sortField)
|
const sortField: SortField | undefined = isSortField(restParams._sortField)
|
||||||
? restParams._sortField
|
? restParams._sortField
|
||||||
: undefined;
|
: undefined;
|
||||||
if (!sortField) {
|
if (!sortField) {
|
||||||
|
|
Loading…
Reference in a new issue