complete handler structure

This commit is contained in:
Timo Hocker 2020-04-08 20:46:48 +02:00
parent 715f64408d
commit 89a861a2dd
6 changed files with 1272 additions and 72 deletions

41
lib/classes/Handler.ts Normal file
View File

@ -0,0 +1,41 @@
import { Request, Response } from '@types/express/index.d.ts';
import Transaction from './Transaction.ts';
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;
}

View File

@ -17,7 +17,31 @@ export default class Status {
return this._status !== -1;
}
/*
* Status setters
*/
public ok (): void {
this._status = http.status_ok;
}
public ok_no_content (): void {
this._status = http.status_ok_no_content;
}
public bad_request (): void {
this._status = http.status_bad_request;
}
public unauthorized (): void {
this._status = http.status_unauthorized;
}
public forbidden (): void {
this._status = http.status_forbidden;
}
public not_found (): void {
this._status = http.status_not_found;
}
}

View File

@ -1,38 +0,0 @@
interface AllHandler {
async handle_all_request(req: Request, res: Response): void;
}
interface DeleteHandler {
async handle_delete_request(req: Request, res: Response): void;
}
interface GetHandler {
async handle_get_request(req: Request, res: Response): void;
}
interface HeadHandler {
async handle_head_request(req: Request, res: Response): void;
}
interface PostHandler {
async handle_post_request(req: Request, res: Response): void;
}
interface PutHandler {
async handle_put_request(req: Request, res: Response): void;
}
interface TraceHandler {
async handle_trace_request(req: Request, res: Response): void;
}
/* eslint-disable @typescript-eslint/naming-convention */
export default {
AllHandler,
DeleteHandler,
GetHandler,
HeadHandler,
PostHandler,
PutHandler,
TraceHandler
};

View File

@ -1 +1,21 @@
export * from './handler_interfaces.ts';
import { App } from '@types/express/index.d.ts';
import Handler from './classes/Handler.ts';
/**
* register an array of handlers to an express app
*
* @param {App} app express app
* @param {Array<Handler>} handlers handlers to register
*/
export default function load_handlers (
app: App,
handlers: array<Handler>
): void {
for (const h of handlers)
app.use (h.path, h.run_http_handler);
}
export * from './classes/Session.ts';
export * from './classes/Status.ts';
export * from './classes/Transaction.ts';
export * from './classes/Handler.ts';

View File

@ -22,9 +22,10 @@
"node": ">=10.0.0"
},
"devDependencies": {
"@scode/eslint-config-ts": "^1.0.11",
"@scode/eslint-config-ts": "^1.0.12",
"@stryker-mutator/core": "^3.1.0",
"@stryker-mutator/javascript-mutator": "^3.1.0",
"ava": "^3.6.0",
"eslint": "^6.8.0",
"nyc": "^15.0.1",
"typescript": "^3.8.3"

1216
yarn.lock

File diff suppressed because it is too large Load Diff