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

View File

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

View File

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