Get rid of some anys.

This commit is contained in:
baldo 2022-07-28 13:49:22 +02:00
parent 5592892f0d
commit d783bb634f
6 changed files with 53 additions and 76 deletions

View file

@ -85,7 +85,7 @@ export class Task {
this.job.run().then(result => { this.job.run().then(result => {
done(TaskState.IDLE, result); done(TaskState.IDLE, result);
}).catch((err: any) => { }).catch(err => {
Logger.tag('jobs').error("Job %s failed: %s", this.name, err); Logger.tag('jobs').error("Job %s failed: %s", this.name, err);
done(TaskState.FAILED, null); done(TaskState.FAILED, null);
}); });

View file

@ -10,6 +10,7 @@ import {isMonitoringToken, JSONObject, MonitoringResponse, MonitoringToken, toMo
const isValidToken = forConstraint(CONSTRAINTS.token, false); const isValidToken = forConstraint(CONSTRAINTS.token, false);
// FIXME: Get rid of any
async function doGetAll(req: Request): Promise<{ total: number, result: any }> { async function doGetAll(req: Request): Promise<{ total: number, result: any }> {
const restParams = await Resources.getValidRestParams('list', null, req); const restParams = await Resources.getValidRestParams('list', null, req);
const {monitoringStates, total} = await MonitoringService.getAll(restParams); const {monitoringStates, total} = await MonitoringService.getAll(restParams);

View file

@ -10,18 +10,18 @@ import {isString, isTaskSortField} from "../types";
const isValidId = forConstraint(CONSTRAINTS.id, false); const isValidId = forConstraint(CONSTRAINTS.id, false);
interface TaskResponse { type TaskResponse = {
id: number, id: number;
name: string, name: string;
description: string, description: string;
schedule: string, schedule: string;
runningSince: number | null, runningSince: number | null;
lastRunStarted: number | null, lastRunStarted: number | null;
lastRunDuration: number | null, lastRunDuration: number | null;
state: string, state: string;
result: string | null, result: string | null;
message: string | null, message: string | null;
enabled: boolean, enabled: boolean;
} }
function toTaskResponse(task: Task): TaskResponse { function toTaskResponse(task: Task): TaskResponse {

View file

@ -111,12 +111,11 @@ export enum MailType {
export const isMailType = toIsEnum(MailType); export const isMailType = toIsEnum(MailType);
export interface Mail { export type Mail = {
id: MailId, id: MailId;
email: MailType, email: MailType;
sender: EmailAddress, sender: EmailAddress;
recipient: EmailAddress, recipient: EmailAddress;
data: MailData, data: MailData;
failures: number, failures: number;
} }

View file

@ -9,8 +9,12 @@ import {
EnumTypeGuard, EnumTypeGuard,
EnumValue, EnumValue,
type GenericSortField, type GenericSortField,
isJSONObject, isNumber, isString, isUndefined, isJSONObject,
isNumber,
isString,
isUndefined,
JSONObject, JSONObject,
JSONValue,
SortDirection, SortDirection,
TypeGuard TypeGuard
} from "../types"; } from "../types";
@ -36,7 +40,9 @@ export type OrderByClause = { query: string, params: any[] };
export type LimitOffsetClause = { query: string, params: any[] }; export type LimitOffsetClause = { query: string, params: any[] };
export type FilterClause = { 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) { switch (type) {
case 'html': case 'html':
res.writeHead(httpCode, {'Content-Type': 'text/html'}); res.writeHead(httpCode, {'Content-Type': 'text/html'});
@ -249,8 +255,7 @@ export function sort<T extends Record<S, any>, S extends string>(entities: T[],
let order = 0; let order = 0;
if (as < bs) { if (as < bs) {
order = -1; order = -1;
} } else if (bs > as) {
else if (bs > as) {
order = 1; order = 1;
} }
@ -293,7 +298,7 @@ export function filterClause<S>(
}; };
} }
export function success(res: Response, data: any) { export function success(res: Response, data: JSONValue) {
respond(res, 200, data, 'json'); respond(res, 200, data, 'json');
} }
@ -301,7 +306,7 @@ export function successHtml(res: Response, html: string) {
respond(res, 200, html, 'html'); 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'); respond(res, err.type.code, err.data, 'json');
} }

View file

@ -1,6 +1,6 @@
import {parseInteger} from "../utils/strings"; import {parseInteger} from "../utils/strings";
import Logger from "../logger"; 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 { export interface Constraint {
type: string, type: string,
@ -20,52 +20,24 @@ export interface Constraint {
export type Constraints = { [key: string]: Constraint }; export type Constraints = { [key: string]: Constraint };
export type Values = { [key: string]: any }; export type Values = { [key: string]: any };
export function isConstraint(val: any): val is Constraint { export function isConstraint(arg: unknown): arg is Constraint {
if (!isObject(val)) { if (!isObject(arg)) {
return false; return false;
} }
const constraint = val as { [key: string]: any }; const constraint = arg as Constraint;
return (
if (!("type" in constraint) || !isString(constraint.type)) { isString(constraint.type) &&
return false; // default?: any
isOptional(constraint.optional, isBoolean) &&
isOptional(constraint.allowed, toIsArray(isString)) &&
isOptional(constraint.min, isNumber) &&
isOptional(constraint.max, isNumber) &&
isOptional(constraint.regex, isRegExp)
);
} }
if ("optional" in constraint export function isConstraints(constraints: unknown): constraints is Constraints {
&& !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;
}
export function isConstraints(constraints: any): constraints is Constraints {
if (!isObject(constraints)) { if (!isObject(constraints)) {
return false; 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.) // 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'; return isBoolean(value) || value === 'true' || value === 'false';
} }
function isValidNumber(constraint: Constraint, value: any): boolean { function isValidNumber(constraint: Constraint, value: unknown): boolean {
if (isString(value)) { if (isString(value)) {
value = parseInteger(value); value = parseInteger(value);
} }
@ -104,7 +76,7 @@ function isValidNumber(constraint: Constraint, value: any): boolean {
return true; return true;
} }
function isValidEnum(constraint: Constraint, value: any): boolean { function isValidEnum(constraint: Constraint, value: unknown): boolean {
if (!isString(value)) { if (!isString(value)) {
return false; return false;
} }
@ -113,7 +85,7 @@ function isValidEnum(constraint: Constraint, value: any): boolean {
return allowed.indexOf(value) >= 0; return allowed.indexOf(value) >= 0;
} }
function isValidString(constraint: Constraint, value: any): boolean { function isValidString(constraint: Constraint, value: unknown): boolean {
if (!constraint.regex) { if (!constraint.regex) {
throw new Error("String constraints must have regex set: " + constraint); 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); 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) { if (value === undefined) {
return acceptUndefined || constraint.optional === true; return acceptUndefined || constraint.optional === true;
} }
@ -167,8 +139,8 @@ function areValid(constraints: Constraints, acceptUndefined: boolean, values: Va
return true; return true;
} }
export function forConstraint(constraint: Constraint, acceptUndefined: boolean): (value: any) => boolean { export function forConstraint(constraint: Constraint, acceptUndefined: boolean): (value: unknown) => boolean {
return ((value: any): boolean => isValid(constraint, acceptUndefined, value)); return ((value: unknown): boolean => isValid(constraint, acceptUndefined, value));
} }
export function forConstraints(constraints: Constraints, acceptUndefined: boolean): (values: Values) => boolean { export function forConstraints(constraints: Constraints, acceptUndefined: boolean): (values: Values) => boolean {