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
Timo Hocker 3716e80674 fixes
2020-04-17 11:27:56 +02:00

89 lines
2.9 KiB
TypeScript

import { Request, Response } from 'express';
type Authorization = {
(req: Request, res: Response): Promise<boolean>;
(req: Request, res: Response, next: Function): unknown;
}
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 (req, res): Promise<boolean> => 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<boolean> => {
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<boolean> => {
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<boolean> => {
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<boolean> => {
const result = (await general (req, res)) && (await specific (req, res));
return result;
};
}
public set delete_authorization (value: Authorization) {
this._delete_authorization = value;
}
}