This commit is contained in:
Timo Hocker 2020-04-17 11:27:56 +02:00
parent 3b9e3d416a
commit 3716e80674
5 changed files with 37 additions and 33 deletions

View File

@ -1,3 +1,5 @@
import { Request, Response } from 'express';
export interface CrudHandler {
public create(req: Request, res: Response): Promise<void>;
public read(req: Request, res: Response): Promise<void>;

View File

@ -100,12 +100,11 @@ export class KnexCrudHandler extends HttpHandler implements CrudHandler {
}
const json = await this.knex (this.table)
.select ([
.where ({ id: req.headers.id })
.select (
'id',
...this.columns
])
.where ({ id: req.headers.id });
);
res.status (json.length > 0 ? http.status_ok : http.status_not_found)
.json (json[0]);
}
@ -133,7 +132,7 @@ export class KnexCrudHandler extends HttpHandler implements CrudHandler {
.update (db_data);
res.status (http.status_ok)
.end (inserted[0]);
.end ();
}
public async delete (req: Request, res: Response): Promise<void> {

View File

@ -1,8 +1,8 @@
import { Request, Response } from 'express';
type Authorization = {
(req: Request, res: Response, next: Function);
(req: Request, res: Response): Promise<boolean>;
(req: Request, res: Response, next: Function): unknown;
}
type AuthRunner = {
@ -23,66 +23,66 @@ export class KnexCrudOptions {
): AuthRunner {
if (typeof auth === 'undefined')
return (): Promise<boolean> => new Promise ((r) => r (true));
return (): Promise<boolean> => new Promise ((resolve) => {
return (req, res): Promise<boolean> => new Promise ((resolve) => {
const result = auth (req, res, resolve);
if (typeof result !== 'undefined')
resolve (result);
resolve (result as boolean);
});
}
public set general_authorization (value: Authorization): void{
public set general_authorization (value: Authorization) {
this._general_authorization = value;
}
public get create_authorization (): AuthRunner {
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, res): Promise<boolean> => {
const res = (await general (req, res)) && (await specific (req, res));
return res;
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): void{
public set create_authorization (value: Authorization) {
this._create_authorization = value;
}
public get read_authorization (): AuthRunner {
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, res): Promise<boolean> => {
const res = (await general (req, res)) && (await specific (req, res));
return res;
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): void{
public set read_authorization (value: Authorization) {
this._read_authorization = value;
}
public get update_authorization (): AuthRunner {
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, res): Promise<boolean> => {
const res = (await general (req, res)) && (await specific (req, res));
return res;
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): void{
public set update_authorization (value: Authorization) {
this._update_authorization = value;
}
public get delete_authorization (): AuthRunner {
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, res): Promise<boolean> => {
const res = (await general (req, res)) && (await specific (req, res));
return res;
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): void{
public set delete_authorization (value: Authorization) {
this._delete_authorization = value;
}
}

View File

@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/naming-convention */
interface KnexQuery {
insert(data: object): Promise;
update(data: object): Promise;
select(...columns: Array<string>): Promise;
delete(): Promise;
insert(data: object): Promise<Array<unknown>>;
update(data: object): Promise<unknown>;
select(...columns: Array<string>): Promise<Array<object>>;
delete(): Promise<unknown>;
where(filter: object): KnexQuery;
whereNot(filter: object): KnexQuery;
whereIn(filter: object): KnexQuery;

3
lib/index.ts Normal file
View File

@ -0,0 +1,3 @@
export { HttpHandler } from './HttpHandler';
export { CrudHandler } from './CrudHandler';
export { KnexCrudHandler } from './KnexCrudHandler';