JsonRPC/templates/ts_base.ts

37 lines
884 B
TypeScript

export class VerificationError extends Error {
constructor(
public readonly type?: string,
public readonly field?: string,
public readonly value?: any
) {
super(
"Parameter verification failed! " +
(type ? "Expected " + type + "! " : "") +
(field ? "At: " + field + "! " : "")
);
}
}
export function apply_int(data: any) {
data = Math.floor(Number(data));
if (Number.isNaN(data)) throw new VerificationError("int", undefined, data);
return data;
}
export function apply_float(data: any) {
data = Number(data);
if (Number.isNaN(data))
throw new VerificationError("float", undefined, data);
return data;
}
export function apply_string(data: any) {
return String(data);
}
export function apply_boolean(data: any) {
return Boolean(data);
}
export function apply_void(data: any) {}