import { Request, Response } from 'express'; import { KnexCrudOptions, Authorization } from './KnexCrudOptions'; type AuthRunner = { (req: Request, res: Response): Promise; } export class KnexCrudOptionsReader { private options: KnexCrudOptions; public constructor (options: KnexCrudOptions) { this.options = options; } 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 get optional_columns (): Array | undefined { return this.options.optional_columns; } public get create_authorization (): Authorization { const general = this.get_auth_runner (this.options.general_authorization); const specific = this.get_auth_runner (this.options.create_authorization); return async (req: Request, res: Response): Promise => { const result = (await general (req, res)) && (await specific (req, res)); return result; }; } public get read_authorization (): Authorization { const general = this.get_auth_runner (this.options.general_authorization); const specific = this.get_auth_runner (this.options.read_authorization); return async (req: Request, res: Response): Promise => { const result = (await general (req, res)) && (await specific (req, res)); return result; }; } public get update_authorization (): Authorization { const general = this.get_auth_runner (this.options.general_authorization); const specific = this.get_auth_runner (this.options.update_authorization); return async (req: Request, res: Response): Promise => { const result = (await general (req, res)) && (await specific (req, res)); return result; }; } public get delete_authorization (): Authorization { const general = this.get_auth_runner (this.options.general_authorization); const specific = this.get_auth_runner (this.options.delete_authorization); return async (req: Request, res: Response): Promise => { const result = (await general (req, res)) && (await specific (req, res)); return result; }; } }