JsonRPC/templates/ts_base.ts

37 lines
884 B
TypeScript
Raw Normal View History

2022-01-02 22:02:47 +00:00
export class VerificationError extends Error {
constructor(
public readonly type?: string,
public readonly field?: string,
public readonly value?: any
) {
2022-04-13 18:58:53 +00:00
super(
"Parameter verification failed! " +
(type ? "Expected " + type + "! " : "") +
(field ? "At: " + field + "! " : "")
);
2022-01-02 22:02:47 +00:00
}
}
2022-04-13 18:58:53 +00:00
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) {
2022-01-03 17:11:53 +00:00
data = Number(data);
2022-04-13 18:58:53 +00:00
if (Number.isNaN(data))
throw new VerificationError("float", undefined, data);
2022-01-02 22:02:47 +00:00
return data;
}
2022-01-03 17:11:53 +00:00
export function apply_string(data: any) {
return String(data);
2022-01-02 22:02:47 +00:00
}
2022-01-03 17:11:53 +00:00
export function apply_boolean(data: any) {
return Boolean(data);
2022-01-02 22:02:47 +00:00
}
2022-01-03 17:11:53 +00:00
export function apply_void(data: any) {}