This repository has been archived on 2020-08-13. You can view files and clone it, but cannot push or open issues or pull requests.
requestor/lib/classes/Handler.ts

42 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-04-09 09:14:54 +02:00
import { Request, Response } from '@types/express';
import Transaction from './Transaction';
2020-04-08 20:46:48 +02:00
export default abstract class Handler {
private _handlers: array<Function> = [];
private _method_handlers: Record<string, Function> = {};
protected register_handler (f: Function): void {
this._handlers.push (f);
}
protected register_handler (method: string, f: Function): void {
const m = method.toUpperCase ();
if (typeof this._method_handlers[m] !== 'undefined')
throw new Error (`Handler for ${m} already registered`);
this._method_handlers[m] = f;
}
private async run_method_handler (method: string, t: Transaction): void {
const m = method.toUpperCase ();
if (this._method_handlers[m] !== 'undefined')
await this._method_handlers[m] (t);
}
public async run_http_handler (req: Request, res: Response): void {
const t = new Transaction (req, res);
for (const handler of this._handlers) {
// eslint-disable-next-line no-await-in-loop
if (await handler (t) === false) {
if (!t.status.has_status)
t.status.error ();
t.finalize ();
return;
}
}
await this.run_method_handler ('ALL', t);
await this.run_method_handler (t.req.method, t);
}
public abstract get path(): string;
}