fix compile errors
This commit is contained in:
parent
64ec1b5db5
commit
bc50897afc
@ -1,34 +1,34 @@
|
||||
import { Request, Response } from '@types/express';
|
||||
import { Request, Response } from 'express';
|
||||
import { http } from '@scode/consts';
|
||||
import Transaction from './Transaction';
|
||||
|
||||
export default abstract class Handler {
|
||||
private _handlers: array<Function> = [];
|
||||
private _handlers: Array<Function> = [];
|
||||
private _method_handlers: Record<string, Function> = {};
|
||||
|
||||
protected register_handler (f: Function): void {
|
||||
this._handlers.push (f);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
protected register_handler (method: string, f: Function): void {
|
||||
private async run_method_handler (method: string, t: Transaction): Promise<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 {
|
||||
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.status.has_status)
|
||||
t.status.error ();
|
||||
if (!t.has_status)
|
||||
t.status = http.status_internal_server_error;
|
||||
t.finalize ();
|
||||
return;
|
||||
}
|
||||
|
@ -1,47 +0,0 @@
|
||||
import {http} from '@scode/consts';
|
||||
|
||||
export default class Status {
|
||||
private _status = -1;
|
||||
|
||||
public get status (): number {
|
||||
if (this._status === -1)
|
||||
throw new Error ('status undefined');
|
||||
return this._status;
|
||||
}
|
||||
|
||||
public set status (value: number) {
|
||||
this._status = value;
|
||||
}
|
||||
|
||||
public get has_status (): boolean {
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,30 +1,33 @@
|
||||
import { Request, Response } from 'express';
|
||||
import Status from './Status';
|
||||
import { http } from '@scode/consts';
|
||||
import Session from './Session';
|
||||
|
||||
export default class Transaction {
|
||||
/* private */
|
||||
private _req: Request;
|
||||
private _res: Response;
|
||||
private _status: Status;
|
||||
|
||||
/* public */
|
||||
public status = -1;
|
||||
public session?: Session;
|
||||
|
||||
public get req (): Request { return this._req; }
|
||||
public get res (): Response { return this._res; }
|
||||
public get status (): Status { return this._status; }
|
||||
public session?: Session;
|
||||
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;
|
||||
this._status = new Status;
|
||||
}
|
||||
|
||||
/* methods */
|
||||
public finalize (data?: any): void {
|
||||
if (this._status.has_status)
|
||||
this._res.status (this.status.status);
|
||||
if (this.has_status)
|
||||
this._res.status (this.status);
|
||||
this._res.end (data);
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,5 @@ export default function load_handlers (
|
||||
}
|
||||
|
||||
export * from './classes/Session';
|
||||
export * from './classes/Status';
|
||||
export * from './classes/Transaction';
|
||||
export * from './classes/Handler';
|
||||
|
@ -258,9 +258,9 @@
|
||||
fastq "^1.6.0"
|
||||
|
||||
"@scode/consts@^1.0.15":
|
||||
version "1.0.20"
|
||||
resolved "https://npm.scode.ovh/@scode%2fconsts/-/consts-1.0.20.tgz#9bb0c2a975638a3d2c8507b7af8b6f0088b7c435"
|
||||
integrity sha512-4EIs8bxixaovtwuv2I3RfDD2AVXa29z8xwGyXOCHUa/AeGoWpjtMXRBnOxE6PcfPNlDzxNQpyBMZrqEGZnwefA==
|
||||
version "1.1.2"
|
||||
resolved "https://npm.scode.ovh/@scode%2fconsts/-/consts-1.1.2.tgz#16114df6169120e9a8ecb10dccf85c46ed017cf8"
|
||||
integrity sha512-MTRhiUOiFHzYRg2KN1P+9G4BM+oI7JtdH05gAlhOP4VCt2wMdXExaS6SGAJmT9e8j14qu2uZBqQEX4ABdddrRg==
|
||||
|
||||
"@scode/eslint-config-es6@^1.0.1":
|
||||
version "1.0.16"
|
||||
|
Reference in New Issue
Block a user