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';
|
import Transaction from './Transaction';
|
||||||
|
|
||||||
export default abstract class Handler {
|
export default abstract class Handler {
|
||||||
private _handlers: array<Function> = [];
|
private _handlers: Array<Function> = [];
|
||||||
private _method_handlers: Record<string, Function> = {};
|
private _method_handlers: Record<string, Function> = {};
|
||||||
|
|
||||||
protected register_handler (f: Function): void {
|
protected register_handler (f: Function, method?: string): void {
|
||||||
this._handlers.push (f);
|
if (typeof method === 'undefined') { this._handlers.push (f); }
|
||||||
}
|
else {
|
||||||
|
|
||||||
protected register_handler (method: string, f: Function): void {
|
|
||||||
const m = method.toUpperCase ();
|
const m = method.toUpperCase ();
|
||||||
if (typeof this._method_handlers[m] !== 'undefined')
|
if (typeof this._method_handlers[m] !== 'undefined')
|
||||||
throw new Error (`Handler for ${m} already registered`);
|
throw new Error (`Handler for ${m} already registered`);
|
||||||
this._method_handlers[m] = f;
|
this._method_handlers[m] = f;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async run_method_handler (method: string, t: Transaction): void {
|
private async run_method_handler (method: string, t: Transaction): Promise<void> {
|
||||||
const m = method.toUpperCase ();
|
const m = method.toUpperCase ();
|
||||||
if (this._method_handlers[m] !== 'undefined')
|
if (typeof this._method_handlers[m] !== 'undefined')
|
||||||
await this._method_handlers[m] (t);
|
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);
|
const t = new Transaction (req, res);
|
||||||
for (const handler of this._handlers) {
|
for (const handler of this._handlers) {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
if (await handler (t) === false) {
|
if (await handler (t) === false) {
|
||||||
if (!t.status.has_status)
|
if (!t.has_status)
|
||||||
t.status.error ();
|
t.status = http.status_internal_server_error;
|
||||||
t.finalize ();
|
t.finalize ();
|
||||||
return;
|
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 { Request, Response } from 'express';
|
||||||
import Status from './Status';
|
import { http } from '@scode/consts';
|
||||||
import Session from './Session';
|
import Session from './Session';
|
||||||
|
|
||||||
export default class Transaction {
|
export default class Transaction {
|
||||||
/* private */
|
/* private */
|
||||||
private _req: Request;
|
private _req: Request;
|
||||||
private _res: Response;
|
private _res: Response;
|
||||||
private _status: Status;
|
|
||||||
|
|
||||||
/* public */
|
/* public */
|
||||||
|
public status = -1;
|
||||||
|
public session?: Session;
|
||||||
|
|
||||||
public get req (): Request { return this._req; }
|
public get req (): Request { return this._req; }
|
||||||
public get res (): Response { return this._res; }
|
public get res (): Response { return this._res; }
|
||||||
public get status (): Status { return this._status; }
|
public get has_status (): boolean {
|
||||||
public session?: Session;
|
return Object.values (http.status_codes)
|
||||||
|
.includes (this.status);
|
||||||
|
}
|
||||||
|
|
||||||
/* constructor */
|
/* constructor */
|
||||||
public constructor (req: Request, res: Response) {
|
public constructor (req: Request, res: Response) {
|
||||||
this._req = req;
|
this._req = req;
|
||||||
this._res = res;
|
this._res = res;
|
||||||
this._status = new Status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* methods */
|
/* methods */
|
||||||
public finalize (data?: any): void {
|
public finalize (data?: any): void {
|
||||||
if (this._status.has_status)
|
if (this.has_status)
|
||||||
this._res.status (this.status.status);
|
this._res.status (this.status);
|
||||||
this._res.end (data);
|
this._res.end (data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,5 @@ export default function load_handlers (
|
|||||||
}
|
}
|
||||||
|
|
||||||
export * from './classes/Session';
|
export * from './classes/Session';
|
||||||
export * from './classes/Status';
|
|
||||||
export * from './classes/Transaction';
|
export * from './classes/Transaction';
|
||||||
export * from './classes/Handler';
|
export * from './classes/Handler';
|
||||||
|
@ -258,9 +258,9 @@
|
|||||||
fastq "^1.6.0"
|
fastq "^1.6.0"
|
||||||
|
|
||||||
"@scode/consts@^1.0.15":
|
"@scode/consts@^1.0.15":
|
||||||
version "1.0.20"
|
version "1.1.2"
|
||||||
resolved "https://npm.scode.ovh/@scode%2fconsts/-/consts-1.0.20.tgz#9bb0c2a975638a3d2c8507b7af8b6f0088b7c435"
|
resolved "https://npm.scode.ovh/@scode%2fconsts/-/consts-1.1.2.tgz#16114df6169120e9a8ecb10dccf85c46ed017cf8"
|
||||||
integrity sha512-4EIs8bxixaovtwuv2I3RfDD2AVXa29z8xwGyXOCHUa/AeGoWpjtMXRBnOxE6PcfPNlDzxNQpyBMZrqEGZnwefA==
|
integrity sha512-MTRhiUOiFHzYRg2KN1P+9G4BM+oI7JtdH05gAlhOP4VCt2wMdXExaS6SGAJmT9e8j14qu2uZBqQEX4ABdddrRg==
|
||||||
|
|
||||||
"@scode/eslint-config-es6@^1.0.1":
|
"@scode/eslint-config-es6@^1.0.1":
|
||||||
version "1.0.16"
|
version "1.0.16"
|
||||||
|
Reference in New Issue
Block a user