From 228c204255bc8d3a15a2d7456698332505920696 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Thu, 16 Apr 2020 11:09:04 +0200 Subject: [PATCH] imp --- lib/KnexCrudHandler.ts | 33 +++++++++++++++++++++++++++++++-- lib/KnexCrudOptions.ts | 25 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 lib/KnexCrudOptions.ts diff --git a/lib/KnexCrudHandler.ts b/lib/KnexCrudHandler.ts index 249b20f..d40b1e8 100644 --- a/lib/KnexCrudHandler.ts +++ b/lib/KnexCrudHandler.ts @@ -1,17 +1,46 @@ +import { Request, Response } from 'express'; +import { KnexCrudOptions } from './KnexCrudOptions'; import { CrudHandler } from './CrudHandler'; export class KnexCrudHandler implements CrudHandler { protected table: string; protected columns: Array; - protected options: Record; + protected options: KnexCrudOptions; public constructor ( table: string, columns: Array, - options: Record = {} + options: KnexCrudOptions = {} ) { this.table = table; this.columns = columns; this.options = options; } + + private call_auth (auth?: Function, req: Request, res: Response): boolean { + if (typeof auth === 'undefined') + return true; + + const promise = new Promise ((resolve) => { + const result = auth (req, res, resolve); + }); + } + + public async create (req: Request, res: Response): Promise { + if (typeof req.body === 'undefined') + throw new Error ('request body is undefined. is body-parser running?'); + const data = JSON.parse (req.body); + } + + public async read (req: Request, res: Response): Promise { + + } + + public async update (req: Request, res: Response): Promise { + + } + + public async delete (req: Request, res: Response): Promise { + + } } diff --git a/lib/KnexCrudOptions.ts b/lib/KnexCrudOptions.ts new file mode 100644 index 0000000..886aa61 --- /dev/null +++ b/lib/KnexCrudOptions.ts @@ -0,0 +1,25 @@ +import { Request, Response } from 'express'; + +type Authentication = { + (req: Request, res: Response, next: Function); + (req: Request, res: Response): boolean; +} + +type Authorization = { + (req: Request, res: Response, next: Function); + (req: Request, res: Response): boolean; +} + +export class KnexCrudOptions { + public create_authentication?: Authentication; + public read_authentication?: Authentication; + public update_authentication?: Authentication; + public delete_authentication?: Authentication; + + public create_authorization?: Authorization; + public read_authorization?: Authorization; + public update_authorization?: Authorization; + public delete_authorization?: Authorization; + + public optional_columns?: Array; +}