From d783bb634f32844933fd31e42ebf7be7f8f77c46 Mon Sep 17 00:00:00 2001 From: baldo Date: Thu, 28 Jul 2022 13:49:22 +0200 Subject: [PATCH] Get rid of some anys. --- server/jobs/scheduler.ts | 2 +- server/resources/monitoringResource.ts | 1 + server/resources/taskResource.ts | 24 ++++----- server/types/index.ts | 15 +++--- server/utils/resources.ts | 17 ++++--- server/validation/validator.ts | 70 ++++++++------------------ 6 files changed, 53 insertions(+), 76 deletions(-) diff --git a/server/jobs/scheduler.ts b/server/jobs/scheduler.ts index a66c0a1..0fafb5c 100644 --- a/server/jobs/scheduler.ts +++ b/server/jobs/scheduler.ts @@ -85,7 +85,7 @@ export class Task { this.job.run().then(result => { done(TaskState.IDLE, result); - }).catch((err: any) => { + }).catch(err => { Logger.tag('jobs').error("Job %s failed: %s", this.name, err); done(TaskState.FAILED, null); }); diff --git a/server/resources/monitoringResource.ts b/server/resources/monitoringResource.ts index 4b44c0c..1a78bb3 100644 --- a/server/resources/monitoringResource.ts +++ b/server/resources/monitoringResource.ts @@ -10,6 +10,7 @@ import {isMonitoringToken, JSONObject, MonitoringResponse, MonitoringToken, toMo const isValidToken = forConstraint(CONSTRAINTS.token, false); +// FIXME: Get rid of any async function doGetAll(req: Request): Promise<{ total: number, result: any }> { const restParams = await Resources.getValidRestParams('list', null, req); const {monitoringStates, total} = await MonitoringService.getAll(restParams); diff --git a/server/resources/taskResource.ts b/server/resources/taskResource.ts index 6020d05..8f2e939 100644 --- a/server/resources/taskResource.ts +++ b/server/resources/taskResource.ts @@ -10,18 +10,18 @@ import {isString, isTaskSortField} from "../types"; const isValidId = forConstraint(CONSTRAINTS.id, false); -interface TaskResponse { - id: number, - name: string, - description: string, - schedule: string, - runningSince: number | null, - lastRunStarted: number | null, - lastRunDuration: number | null, - state: string, - result: string | null, - message: string | null, - enabled: boolean, +type TaskResponse = { + id: number; + name: string; + description: string; + schedule: string; + runningSince: number | null; + lastRunStarted: number | null; + lastRunDuration: number | null; + state: string; + result: string | null; + message: string | null; + enabled: boolean; } function toTaskResponse(task: Task): TaskResponse { diff --git a/server/types/index.ts b/server/types/index.ts index 04b39d8..5324418 100644 --- a/server/types/index.ts +++ b/server/types/index.ts @@ -111,12 +111,11 @@ export enum MailType { export const isMailType = toIsEnum(MailType); -export interface Mail { - id: MailId, - email: MailType, - sender: EmailAddress, - recipient: EmailAddress, - data: MailData, - failures: number, +export type Mail = { + id: MailId; + email: MailType; + sender: EmailAddress; + recipient: EmailAddress; + data: MailData; + failures: number; } - diff --git a/server/utils/resources.ts b/server/utils/resources.ts index 3d459c5..89dbd95 100644 --- a/server/utils/resources.ts +++ b/server/utils/resources.ts @@ -9,8 +9,12 @@ import { EnumTypeGuard, EnumValue, type GenericSortField, - isJSONObject, isNumber, isString, isUndefined, + isJSONObject, + isNumber, + isString, + isUndefined, JSONObject, + JSONValue, SortDirection, TypeGuard } from "../types"; @@ -36,7 +40,9 @@ export type OrderByClause = { query: string, params: any[] }; export type LimitOffsetClause = { query: string, params: any[] }; export type FilterClause = { query: string, params: any[] }; -function respond(res: Response, httpCode: number, data: any, type: string): void { +function respond(res: Response, httpCode: number, data: string, type: "html"): void; +function respond(res: Response, httpCode: number, data: JSONValue, type: "json"): void; +function respond(res: Response, httpCode: number, data: JSONValue, type: "html" | "json"): void { switch (type) { case 'html': res.writeHead(httpCode, {'Content-Type': 'text/html'}); @@ -249,8 +255,7 @@ export function sort, S extends string>(entities: T[], let order = 0; if (as < bs) { order = -1; - } - else if (bs > as) { + } else if (bs > as) { order = 1; } @@ -293,7 +298,7 @@ export function filterClause( }; } -export function success(res: Response, data: any) { +export function success(res: Response, data: JSONValue) { respond(res, 200, data, 'json'); } @@ -301,7 +306,7 @@ export function successHtml(res: Response, html: string) { respond(res, 200, html, 'html'); } -export function error(res: Response, err: { data: any, type: { code: number } }) { +export function error(res: Response, err: { data: JSONValue, type: { code: number } }) { respond(res, err.type.code, err.data, 'json'); } diff --git a/server/validation/validator.ts b/server/validation/validator.ts index 523ac14..a771acf 100644 --- a/server/validation/validator.ts +++ b/server/validation/validator.ts @@ -1,6 +1,6 @@ import {parseInteger} from "../utils/strings"; import Logger from "../logger"; -import {isArray, isBoolean, isNumber, isObject, isRegExp, isString, isUndefined} from "../types"; +import {isBoolean, isNumber, isObject, isOptional, isRegExp, isString, toIsArray} from "../types"; export interface Constraint { type: string, @@ -20,52 +20,24 @@ export interface Constraint { export type Constraints = { [key: string]: Constraint }; export type Values = { [key: string]: any }; -export function isConstraint(val: any): val is Constraint { - if (!isObject(val)) { +export function isConstraint(arg: unknown): arg is Constraint { + if (!isObject(arg)) { return false; } - const constraint = val as { [key: string]: any }; - - if (!("type" in constraint) || !isString(constraint.type)) { - return false; - } - - if ("optional" in constraint - && !isUndefined(constraint.optional) - && !isBoolean(constraint.optional)) { - return false; - } - - if ("allowed" in constraint - && !isUndefined(constraint.allowed) - && !isArray(constraint.allowed, isString)) { - return false; - } - - if ("min" in constraint - && !isUndefined(constraint.min) - && !isNumber(constraint.min)) { - return false; - } - - if ("max" in constraint - && !isUndefined(constraint.max) - && !isNumber(constraint.max)) { - return false; - } - - // noinspection RedundantIfStatementJS - if ("regex" in constraint - && !isUndefined(constraint.regex) - && !isRegExp(constraint.regex)) { - return false; - } - - return true; + const constraint = arg as Constraint; + return ( + isString(constraint.type) && + // default?: any + isOptional(constraint.optional, isBoolean) && + isOptional(constraint.allowed, toIsArray(isString)) && + isOptional(constraint.min, isNumber) && + isOptional(constraint.max, isNumber) && + isOptional(constraint.regex, isRegExp) + ); } -export function isConstraints(constraints: any): constraints is Constraints { +export function isConstraints(constraints: unknown): constraints is Constraints { if (!isObject(constraints)) { return false; } @@ -75,11 +47,11 @@ export function isConstraints(constraints: any): constraints is Constraints { // TODO: sanitize input for further processing as specified by constraints (correct types, trimming, etc.) -function isValidBoolean(value: any): boolean { +function isValidBoolean(value: unknown): boolean { return isBoolean(value) || value === 'true' || value === 'false'; } -function isValidNumber(constraint: Constraint, value: any): boolean { +function isValidNumber(constraint: Constraint, value: unknown): boolean { if (isString(value)) { value = parseInteger(value); } @@ -104,7 +76,7 @@ function isValidNumber(constraint: Constraint, value: any): boolean { return true; } -function isValidEnum(constraint: Constraint, value: any): boolean { +function isValidEnum(constraint: Constraint, value: unknown): boolean { if (!isString(value)) { return false; } @@ -113,7 +85,7 @@ function isValidEnum(constraint: Constraint, value: any): boolean { return allowed.indexOf(value) >= 0; } -function isValidString(constraint: Constraint, value: any): boolean { +function isValidString(constraint: Constraint, value: unknown): boolean { if (!constraint.regex) { throw new Error("String constraints must have regex set: " + constraint); } @@ -126,7 +98,7 @@ function isValidString(constraint: Constraint, value: any): boolean { return (trimmed === '' && constraint.optional) || constraint.regex.test(trimmed); } -function isValid(constraint: Constraint, acceptUndefined: boolean, value: any): boolean { +function isValid(constraint: Constraint, acceptUndefined: boolean, value: unknown): boolean { if (value === undefined) { return acceptUndefined || constraint.optional === true; } @@ -167,8 +139,8 @@ function areValid(constraints: Constraints, acceptUndefined: boolean, values: Va return true; } -export function forConstraint(constraint: Constraint, acceptUndefined: boolean): (value: any) => boolean { - return ((value: any): boolean => isValid(constraint, acceptUndefined, value)); +export function forConstraint(constraint: Constraint, acceptUndefined: boolean): (value: unknown) => boolean { + return ((value: unknown): boolean => isValid(constraint, acceptUndefined, value)); } export function forConstraints(constraints: Constraints, acceptUndefined: boolean): (values: Values) => boolean {