fixes
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { http } from '@scode/consts';
|
||||
import { try_parse_json } from '@scode/utilities';
|
||||
import { KnexCrudOptions } from './KnexCrudOptions';
|
||||
import { CrudHandler } from './CrudHandler';
|
||||
|
||||
@ -17,19 +19,44 @@ export class KnexCrudHandler implements CrudHandler {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
private call_auth (auth?: Function, req: Request, res: Response): boolean {
|
||||
private call_auth (
|
||||
auth?: Function,
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<boolean> {
|
||||
if (typeof auth === 'undefined')
|
||||
return true;
|
||||
|
||||
const promise = new Promise ((resolve) => {
|
||||
const result = auth (req, res, resolve);
|
||||
if (typeof result !== 'undefined')
|
||||
resolve (result);
|
||||
});
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
protected validate_body (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<object> | object {
|
||||
if (typeof req.body === 'undefined') {
|
||||
res.status (http.status_bad_request);
|
||||
res.end ();
|
||||
return null;
|
||||
}
|
||||
return try_parse_json (req.body);
|
||||
}
|
||||
|
||||
public async create (req: Request, res: Response): Promise<void> {
|
||||
if (typeof req.body === 'undefined')
|
||||
throw new Error ('request body is undefined. is body-parser running?');
|
||||
const data = JSON.parse (req.body);
|
||||
if (!await this.call_auth (this.options.create_authentication, req, res))
|
||||
return;
|
||||
if (!await this.call_auth (this.options.create_authorization, req, res))
|
||||
return;
|
||||
|
||||
const body_data = await this.validate_body (req, res);
|
||||
if (body_data === null)
|
||||
return;
|
||||
}
|
||||
|
||||
public async read (req: Request, res: Response): Promise<void> {
|
||||
|
@ -2,12 +2,12 @@ import { Request, Response } from 'express';
|
||||
|
||||
type Authentication = {
|
||||
(req: Request, res: Response, next: Function);
|
||||
(req: Request, res: Response): boolean;
|
||||
(req: Request, res: Response): Promise<boolean>;
|
||||
}
|
||||
|
||||
type Authorization = {
|
||||
(req: Request, res: Response, next: Function);
|
||||
(req: Request, res: Response): boolean;
|
||||
(req: Request, res: Response): Promise<boolean>;
|
||||
}
|
||||
|
||||
export class KnexCrudOptions {
|
||||
|
Reference in New Issue
Block a user