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

89 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-04-16 11:09:04 +02:00
import { Request, Response } from 'express';
2020-04-17 11:03:15 +02:00
type Authorization = {
2020-04-17 08:31:14 +02:00
(req: Request, res: Response): Promise<boolean>;
2020-04-17 11:27:56 +02:00
(req: Request, res: Response, next: Function): unknown;
2020-04-16 11:09:04 +02:00
}
2020-04-17 11:03:15 +02:00
type AuthRunner = {
2020-04-17 08:31:14 +02:00
(req: Request, res: Response): Promise<boolean>;
2020-04-16 11:09:04 +02:00
}
export class KnexCrudOptions {
2020-04-17 11:03:15 +02:00
private _general_authorization?: Authorization;
private _create_authorization?: Authorization;
private _read_authorization?: Authorization;
private _update_authorization?: Authorization;
private _delete_authorization?: Authorization;
2020-04-16 11:09:04 +02:00
public optional_columns?: Array<string>;
2020-04-17 11:03:15 +02:00
private get_auth_runner (
auth?: Authorization
): AuthRunner {
if (typeof auth === 'undefined')
return (): Promise<boolean> => new Promise ((r) => r (true));
2020-04-17 11:27:56 +02:00
return (req, res): Promise<boolean> => new Promise ((resolve) => {
2020-04-17 11:03:15 +02:00
const result = auth (req, res, resolve);
if (typeof result !== 'undefined')
2020-04-17 11:27:56 +02:00
resolve (result as boolean);
2020-04-17 11:03:15 +02:00
});
}
2020-04-17 11:27:56 +02:00
public set general_authorization (value: Authorization) {
2020-04-17 11:03:15 +02:00
this._general_authorization = value;
}
2020-04-17 11:27:56 +02:00
public get create_authorization (): Authorization {
2020-04-17 11:03:15 +02:00
const general = this.get_auth_runner (this._general_authorization);
const specific = this.get_auth_runner (this._create_authorization);
2020-04-17 11:27:56 +02:00
return async (req: Request, res: Response): Promise<boolean> => {
const result = (await general (req, res)) && (await specific (req, res));
return result;
2020-04-17 11:03:15 +02:00
};
}
2020-04-17 11:27:56 +02:00
public set create_authorization (value: Authorization) {
2020-04-17 11:03:15 +02:00
this._create_authorization = value;
}
2020-04-17 11:27:56 +02:00
public get read_authorization (): Authorization {
2020-04-17 11:03:15 +02:00
const general = this.get_auth_runner (this._general_authorization);
const specific = this.get_auth_runner (this._read_authorization);
2020-04-17 11:27:56 +02:00
return async (req: Request, res: Response): Promise<boolean> => {
const result = (await general (req, res)) && (await specific (req, res));
return result;
2020-04-17 11:03:15 +02:00
};
}
2020-04-17 11:27:56 +02:00
public set read_authorization (value: Authorization) {
2020-04-17 11:03:15 +02:00
this._read_authorization = value;
}
2020-04-17 11:27:56 +02:00
public get update_authorization (): Authorization {
2020-04-17 11:03:15 +02:00
const general = this.get_auth_runner (this._general_authorization);
const specific = this.get_auth_runner (this._update_authorization);
2020-04-17 11:27:56 +02:00
return async (req: Request, res: Response): Promise<boolean> => {
const result = (await general (req, res)) && (await specific (req, res));
return result;
2020-04-17 11:03:15 +02:00
};
}
2020-04-17 11:27:56 +02:00
public set update_authorization (value: Authorization) {
2020-04-17 11:03:15 +02:00
this._update_authorization = value;
}
2020-04-17 11:27:56 +02:00
public get delete_authorization (): Authorization {
2020-04-17 11:03:15 +02:00
const general = this.get_auth_runner (this._general_authorization);
const specific = this.get_auth_runner (this._delete_authorization);
2020-04-17 11:27:56 +02:00
return async (req: Request, res: Response): Promise<boolean> => {
const result = (await general (req, res)) && (await specific (req, res));
return result;
2020-04-17 11:03:15 +02:00
};
}
2020-04-17 11:27:56 +02:00
public set delete_authorization (value: Authorization) {
2020-04-17 11:03:15 +02:00
this._delete_authorization = value;
}
2020-04-16 11:09:04 +02:00
}