This repository has been archived on 2020-08-13. You can view files and clone it, but cannot push or open issues or pull requests.
requestor/lib/KnexCrudOptionsReader.ts
2020-04-22 22:11:19 +02:00

67 lines
2.4 KiB
TypeScript

import { Request, Response } from 'express';
import { KnexCrudOptions, Authorization } from './KnexCrudOptions';
type AuthRunner = {
(req: Request, res: Response): Promise<boolean>;
}
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<boolean> => new Promise ((r) => r (true));
return (req, res): Promise<boolean> => new Promise ((resolve) => {
const result = auth (req, res, resolve);
if (typeof result !== 'undefined')
resolve (result as boolean);
});
}
public get optional_columns (): Array<string> | 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<boolean> => {
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<boolean> => {
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<boolean> => {
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<boolean> => {
const result = (await general (req, res)) && (await specific (req, res));
return result;
};
}
}