From 3716e80674766a1e17768e6cacd2638bfaec96a0 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Fri, 17 Apr 2020 11:27:56 +0200 Subject: [PATCH] fixes --- lib/CrudHandler.ts | 2 ++ lib/KnexCrudHandler.ts | 9 ++++---- lib/KnexCrudOptions.ts | 48 +++++++++++++++++++++--------------------- lib/KnexInterface.ts | 8 +++---- lib/index.ts | 3 +++ 5 files changed, 37 insertions(+), 33 deletions(-) create mode 100644 lib/index.ts diff --git a/lib/CrudHandler.ts b/lib/CrudHandler.ts index 70cb09c..6b3ba33 100644 --- a/lib/CrudHandler.ts +++ b/lib/CrudHandler.ts @@ -1,3 +1,5 @@ +import { Request, Response } from 'express'; + export interface CrudHandler { public create(req: Request, res: Response): Promise; public read(req: Request, res: Response): Promise; diff --git a/lib/KnexCrudHandler.ts b/lib/KnexCrudHandler.ts index 89585fb..13f1761 100644 --- a/lib/KnexCrudHandler.ts +++ b/lib/KnexCrudHandler.ts @@ -100,12 +100,11 @@ export class KnexCrudHandler extends HttpHandler implements CrudHandler { } const json = await this.knex (this.table) - .select ([ + .where ({ id: req.headers.id }) + .select ( 'id', ...this.columns - ]) - .where ({ id: req.headers.id }); - + ); res.status (json.length > 0 ? http.status_ok : http.status_not_found) .json (json[0]); } @@ -133,7 +132,7 @@ export class KnexCrudHandler extends HttpHandler implements CrudHandler { .update (db_data); res.status (http.status_ok) - .end (inserted[0]); + .end (); } public async delete (req: Request, res: Response): Promise { diff --git a/lib/KnexCrudOptions.ts b/lib/KnexCrudOptions.ts index 905905a..2f02a88 100644 --- a/lib/KnexCrudOptions.ts +++ b/lib/KnexCrudOptions.ts @@ -1,8 +1,8 @@ import { Request, Response } from 'express'; type Authorization = { - (req: Request, res: Response, next: Function); (req: Request, res: Response): Promise; + (req: Request, res: Response, next: Function): unknown; } type AuthRunner = { @@ -23,66 +23,66 @@ export class KnexCrudOptions { ): AuthRunner { if (typeof auth === 'undefined') return (): Promise => new Promise ((r) => r (true)); - return (): Promise => new Promise ((resolve) => { + return (req, res): Promise => new Promise ((resolve) => { const result = auth (req, res, resolve); if (typeof result !== 'undefined') - resolve (result); + resolve (result as boolean); }); } - public set general_authorization (value: Authorization): void{ + public set general_authorization (value: Authorization) { this._general_authorization = value; } - public get create_authorization (): AuthRunner { + public get create_authorization (): Authorization { const general = this.get_auth_runner (this._general_authorization); const specific = this.get_auth_runner (this._create_authorization); - return async (req, res): Promise => { - const res = (await general (req, res)) && (await specific (req, res)); - return res; + return async (req: Request, res: Response): Promise => { + const result = (await general (req, res)) && (await specific (req, res)); + return result; }; } - public set create_authorization (value: Authorization): void{ + public set create_authorization (value: Authorization) { this._create_authorization = value; } - public get read_authorization (): AuthRunner { + public get read_authorization (): Authorization { const general = this.get_auth_runner (this._general_authorization); const specific = this.get_auth_runner (this._read_authorization); - return async (req, res): Promise => { - const res = (await general (req, res)) && (await specific (req, res)); - return res; + return async (req: Request, res: Response): Promise => { + const result = (await general (req, res)) && (await specific (req, res)); + return result; }; } - public set read_authorization (value: Authorization): void{ + public set read_authorization (value: Authorization) { this._read_authorization = value; } - public get update_authorization (): AuthRunner { + public get update_authorization (): Authorization { const general = this.get_auth_runner (this._general_authorization); const specific = this.get_auth_runner (this._update_authorization); - return async (req, res): Promise => { - const res = (await general (req, res)) && (await specific (req, res)); - return res; + return async (req: Request, res: Response): Promise => { + const result = (await general (req, res)) && (await specific (req, res)); + return result; }; } - public set update_authorization (value: Authorization): void{ + public set update_authorization (value: Authorization) { this._update_authorization = value; } - public get delete_authorization (): AuthRunner { + public get delete_authorization (): Authorization { const general = this.get_auth_runner (this._general_authorization); const specific = this.get_auth_runner (this._delete_authorization); - return async (req, res): Promise => { - const res = (await general (req, res)) && (await specific (req, res)); - return res; + return async (req: Request, res: Response): Promise => { + const result = (await general (req, res)) && (await specific (req, res)); + return result; }; } - public set delete_authorization (value: Authorization): void{ + public set delete_authorization (value: Authorization) { this._delete_authorization = value; } } diff --git a/lib/KnexInterface.ts b/lib/KnexInterface.ts index 8b072d8..8ad894e 100644 --- a/lib/KnexInterface.ts +++ b/lib/KnexInterface.ts @@ -1,10 +1,10 @@ /* eslint-disable @typescript-eslint/naming-convention */ interface KnexQuery { - insert(data: object): Promise; - update(data: object): Promise; - select(...columns: Array): Promise; - delete(): Promise; + insert(data: object): Promise>; + update(data: object): Promise; + select(...columns: Array): Promise>; + delete(): Promise; where(filter: object): KnexQuery; whereNot(filter: object): KnexQuery; whereIn(filter: object): KnexQuery; diff --git a/lib/index.ts b/lib/index.ts new file mode 100644 index 0000000..63ab5bb --- /dev/null +++ b/lib/index.ts @@ -0,0 +1,3 @@ +export { HttpHandler } from './HttpHandler'; +export { CrudHandler } from './CrudHandler'; +export { KnexCrudHandler } from './KnexCrudHandler';