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 => {
done(TaskState.IDLE, result);
}).catch((err: any) => {
}).catch(err => {
Logger.tag('jobs').error("Job %s failed: %s", this.name, err);
done(TaskState.FAILED, null);
});

View file

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

View file

@ -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 {

View file

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

View file

@ -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<T extends Record<S, any>, 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<S>(
};
}
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');
}

View file

@ -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 {