handler templates only
This commit is contained in:
6
lib/CrudHandler.ts
Normal file
6
lib/CrudHandler.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export interface CrudHandler {
|
||||
public create(req: Request, res: Response): Promise<void>;
|
||||
public read(req: Request, res: Response): Promise<void>;
|
||||
public update(req: Request, res: Response): Promise<void>;
|
||||
public delete(req: Request, res: Response): Promise<void>;
|
||||
}
|
19
lib/KnexCrudHandler.ts
Normal file
19
lib/KnexCrudHandler.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { CrudHandler } from './CrudHandler';
|
||||
|
||||
export class KnexCrudHandler implements CrudHandler {
|
||||
private _table: string;
|
||||
private _columns: Array<string>;
|
||||
private _options: Record<string, unknown>;
|
||||
|
||||
public get table (): string { return this._table; }
|
||||
|
||||
public constructor (
|
||||
table: string,
|
||||
columns: Array<string>,
|
||||
options: Record<string, unknown> = {}
|
||||
) {
|
||||
this.table = table;
|
||||
this.columns = columns;
|
||||
this.options = options;
|
||||
}
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { http } from '@scode/consts';
|
||||
import Transaction from './Transaction';
|
||||
|
||||
export default abstract class Handler {
|
||||
private _handlers: Array<Function> = [];
|
||||
private _method_handlers: Record<string, Function> = {};
|
||||
|
||||
protected register_handler (f: Function, method?: string): void {
|
||||
if (typeof method === 'undefined') {
|
||||
this._handlers.push (f);
|
||||
}
|
||||
else {
|
||||
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
|
||||
): Promise<void> {
|
||||
const m = method.toUpperCase ();
|
||||
if (typeof this._method_handlers[m] !== 'undefined')
|
||||
await this._method_handlers[m] (t);
|
||||
}
|
||||
|
||||
public async run_http_handler (req: Request, res: Response): Promise<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.has_status)
|
||||
t.status = http.status_internal_server_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;
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
export default class Session {
|
||||
public user_id?: number;
|
||||
public user_name?: string;
|
||||
public permissions?: Array<string>;
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { http } from '@scode/consts';
|
||||
import Session from './Session';
|
||||
|
||||
export default class Transaction {
|
||||
/* private */
|
||||
private _req: Request;
|
||||
private _res: Response;
|
||||
|
||||
/* public */
|
||||
public status = -1;
|
||||
public session?: Session;
|
||||
|
||||
public get req (): Request { return this._req; }
|
||||
public get res (): Response { return this._res; }
|
||||
public get has_status (): boolean {
|
||||
return Object.values (http.status_codes)
|
||||
.includes (this.status);
|
||||
}
|
||||
|
||||
/* constructor */
|
||||
public constructor (req: Request, res: Response) {
|
||||
this._req = req;
|
||||
this._res = res;
|
||||
}
|
||||
|
||||
/* methods */
|
||||
public finalize (data?: any): void {
|
||||
if (this.has_status)
|
||||
this._res.status (this.status);
|
||||
this._res.end (data);
|
||||
}
|
||||
}
|
20
lib/index.ts
20
lib/index.ts
@ -1,20 +0,0 @@
|
||||
import { Application } from 'express';
|
||||
import Handler from './classes/Handler';
|
||||
import Session from './classes/Session';
|
||||
import Transaction from './classes/Transaction';
|
||||
|
||||
/**
|
||||
* register an array of handlers to an express app
|
||||
*
|
||||
* @param {Application} app express app
|
||||
* @param {Array<Handler>} handlers handlers to register
|
||||
*/
|
||||
export default function load_handlers (
|
||||
app: Application,
|
||||
handlers: Array<Handler>
|
||||
): void {
|
||||
for (const h of handlers)
|
||||
app.use (h.path, h.run_http_handler);
|
||||
}
|
||||
|
||||
export { Handler, Session, Transaction, load_handlers };
|
Reference in New Issue
Block a user