From bc50897afc3f07e1f1aa8f26d4fe970fd839fb7a Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Thu, 9 Apr 2020 12:29:30 +0200 Subject: [PATCH] fix compile errors --- lib/classes/Handler.ts | 30 ++++++++++++------------ lib/classes/Status.ts | 47 -------------------------------------- lib/classes/Transaction.ts | 17 ++++++++------ lib/index.ts | 1 - yarn.lock | 6 ++--- 5 files changed, 28 insertions(+), 73 deletions(-) delete mode 100644 lib/classes/Status.ts diff --git a/lib/classes/Handler.ts b/lib/classes/Handler.ts index 3c1323a..6d7d580 100644 --- a/lib/classes/Handler.ts +++ b/lib/classes/Handler.ts @@ -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 = []; + private _handlers: Array = []; private _method_handlers: Record = {}; - 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 { 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 { 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; } diff --git a/lib/classes/Status.ts b/lib/classes/Status.ts deleted file mode 100644 index 9cbf6a3..0000000 --- a/lib/classes/Status.ts +++ /dev/null @@ -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; - } -} diff --git a/lib/classes/Transaction.ts b/lib/classes/Transaction.ts index 4702c5e..3701780 100644 --- a/lib/classes/Transaction.ts +++ b/lib/classes/Transaction.ts @@ -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); } } diff --git a/lib/index.ts b/lib/index.ts index 1bee950..c31662e 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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'; diff --git a/yarn.lock b/yarn.lock index af5e29c..2d76a90 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"