import { Request, Response } from 'express'; type Authorization = { (req: Request, res: Response, next: Function); (req: Request, res: Response): Promise; } 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 (): Promise => new Promise ((resolve) => { const result = auth (req, res, resolve); if (typeof result !== 'undefined') resolve (result); }); } public set general_authorization (value: Authorization): void{ this._general_authorization = value; } public get create_authorization (): AuthRunner { 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; }; } public set create_authorization (value: Authorization): void{ this._create_authorization = value; } public get read_authorization (): AuthRunner { 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; }; } public set read_authorization (value: Authorization): void{ this._read_authorization = value; } public get update_authorization (): AuthRunner { 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; }; } public set update_authorization (value: Authorization): void{ this._update_authorization = value; } public get delete_authorization (): AuthRunner { 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; }; } public set delete_authorization (value: Authorization): void{ this._delete_authorization = value; } }