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/KnexCrudOptions.ts
2020-04-17 11:03:15 +02:00

89 lines
2.8 KiB
TypeScript

import { Request, Response } from 'express';
type Authorization = {
(req: Request, res: Response, next: Function);
(req: Request, res: Response): Promise<boolean>;
}
type AuthRunner = {
(req: Request, res: Response): Promise<boolean>;
}
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<string>;
private get_auth_runner (
auth?: Authorization
): AuthRunner {
if (typeof auth === 'undefined')
return (): Promise<boolean> => new Promise ((r) => r (true));
return (): Promise<boolean> => 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<boolean> => {
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<boolean> => {
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<boolean> => {
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<boolean> => {
const res = (await general (req, res)) && (await specific (req, res));
return res;
};
}
public set delete_authorization (value: Authorization): void{
this._delete_authorization = value;
}
}