import { Request, Response } from 'express'; type Authorization = { (req: Request, res: Response): Promise; (req: Request, res: Response, next: Function): unknown; } type AuthRunner = { (req: Request, res: Response): Promise; } export class KnexCrudOptions { private _general_authorization?: Authorization; private _create_authorization?: Authorization; private _read_authorization?: Authorization; private _update_authorization?: Authorization; private _delete_authorization?: Authorization; public optional_columns?: Array; private get_auth_runner ( auth?: Authorization ): AuthRunner { if (typeof auth === 'undefined') return (): Promise => new Promise ((r) => r (true)); return (req, res): Promise => new Promise ((resolve) => { const result = auth (req, res, resolve); if (typeof result !== 'undefined') resolve (result as boolean); }); } public set general_authorization (value: Authorization) { this._general_authorization = value; } 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: Request, res: Response): Promise => { const result = (await general (req, res)) && (await specific (req, res)); return result; }; } public set create_authorization (value: Authorization) { this._create_authorization = value; } 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: Request, res: Response): Promise => { const result = (await general (req, res)) && (await specific (req, res)); return result; }; } public set read_authorization (value: Authorization) { this._read_authorization = value; } 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: Request, res: Response): Promise => { const result = (await general (req, res)) && (await specific (req, res)); return result; }; } public set update_authorization (value: Authorization) { this._update_authorization = value; } 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: Request, res: Response): Promise => { const result = (await general (req, res)) && (await specific (req, res)); return result; }; } public set delete_authorization (value: Authorization) { this._delete_authorization = value; } }