change http controller
This commit is contained in:
parent
fa645dde92
commit
f746039ee5
4 changed files with 123 additions and 21 deletions
|
@ -3,7 +3,7 @@ import { Result } from "../helper/result";
|
|||
import { Router, Request, Response } from "express";
|
||||
import { IRouteModel, Routes } from "../interfaces/router";
|
||||
|
||||
type Method =
|
||||
export type Method =
|
||||
| "all"
|
||||
| "get"
|
||||
| "post"
|
||||
|
@ -15,16 +15,33 @@ type Method =
|
|||
|
||||
export type ResponseBase = Promise<Result<any, any>>;
|
||||
|
||||
// TODO(IDONTSUDO): rewrite the router for the strategy
|
||||
export type CallbackStrategyWithEmpty = () => ResponseBase;
|
||||
export type CallbackStrategyWithValidationModel<T> = (a: T) => ResponseBase;
|
||||
export type CallbackStrategyWithIdQuery = (id: string) => ResponseBase;
|
||||
export type CallBackStrategyWithQueryPage = (page: string) => ResponseBase;
|
||||
export type CallbackStrategyWithFileUpload = (file: File) => ResponseBase;
|
||||
export abstract class CallbackStrategyWithEmpty {
|
||||
abstract call(): ResponseBase;
|
||||
}
|
||||
export abstract class CallbackStrategyWithValidationModel<T> {
|
||||
abstract call(a: T): ResponseBase;
|
||||
}
|
||||
export abstract class CallbackStrategyWithIdQuery {
|
||||
abstract call(id: string): ResponseBase;
|
||||
}
|
||||
export abstract class CallBackStrategyWithQueryPage {
|
||||
abstract call(page: string): ResponseBase;
|
||||
}
|
||||
|
||||
export abstract class CallbackStrategyWithFileUpload {
|
||||
abstract checkingFileExpression: RegExp;
|
||||
abstract call(file: File): ResponseBase;
|
||||
}
|
||||
|
||||
interface ISubSetFeatureRouter<T> {
|
||||
method: Method;
|
||||
fn: CallbackStrategyWithValidationModel<T>;
|
||||
subUrl: string;
|
||||
fn:
|
||||
| CallbackStrategyWithValidationModel<T>
|
||||
| CallbackStrategyWithEmpty
|
||||
| CallbackStrategyWithIdQuery
|
||||
| CallBackStrategyWithQueryPage
|
||||
| CallbackStrategyWithFileUpload;
|
||||
}
|
||||
|
||||
abstract class ICoreHttpController {
|
||||
|
@ -36,7 +53,7 @@ abstract class ICoreHttpController {
|
|||
export class CoreHttpController<V> implements ICoreHttpController {
|
||||
mainURL: string;
|
||||
validationModel: any;
|
||||
subRoutes: ISubSetFeatureRouter<any>[] = [];
|
||||
subRoutes: ISubSetFeatureRouter<V>[] = [];
|
||||
|
||||
routes = {
|
||||
POST: null,
|
||||
|
@ -51,13 +68,70 @@ export class CoreHttpController<V> implements ICoreHttpController {
|
|||
this.mainURL = "/" + routerModel.url;
|
||||
this.validationModel = routerModel.validationModel;
|
||||
}
|
||||
|
||||
async responseHelper(res: Response, fn: ResponseBase) {
|
||||
(await fn).fold(
|
||||
(ok) => {
|
||||
res.json(ok);
|
||||
return;
|
||||
},
|
||||
(err) => {
|
||||
res.status(400).json(err);
|
||||
return;
|
||||
}
|
||||
);
|
||||
}
|
||||
call(): Routes {
|
||||
if (this.subRoutes.isNotEmpty()) {
|
||||
this.subRoutes.map((el) => {
|
||||
console.log(this.router[el.method]);
|
||||
this.router[el.method](
|
||||
this.mainURL + "/" + el.subUrl,
|
||||
async (req, res) => {
|
||||
if (el.fn instanceof CallbackStrategyWithValidationModel) {
|
||||
// TODO(IDONTSUDO):
|
||||
throw Error("needs to be implimed");
|
||||
}
|
||||
if (el.fn instanceof CallbackStrategyWithIdQuery) {
|
||||
throw Error("needs to be implimed");
|
||||
}
|
||||
if (el.fn instanceof CallBackStrategyWithQueryPage) {
|
||||
throw Error("needs to be implimed");
|
||||
}
|
||||
if (el.fn instanceof CallbackStrategyWithEmpty) {
|
||||
await this.responseHelper(res, el.fn.call());
|
||||
return;
|
||||
}
|
||||
|
||||
if (el.fn instanceof CallbackStrategyWithFileUpload) {
|
||||
if (req["files"] === undefined) {
|
||||
res.status(400).json("need files to form-data request");
|
||||
return;
|
||||
}
|
||||
if (req["files"]["file"] === undefined) {
|
||||
res.status(400).json("need file to form data request");
|
||||
return;
|
||||
}
|
||||
if (el.fn instanceof CallbackStrategyWithFileUpload) {
|
||||
if (
|
||||
!el.fn.checkingFileExpression.test(
|
||||
req["files"]["file"]["name"]
|
||||
)
|
||||
) {
|
||||
res
|
||||
.status(400)
|
||||
.json(
|
||||
"a file with this extension is expected: " +
|
||||
String(el.fn.checkingFileExpression)
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
await this.responseHelper(res, el.fn.call(req["files"]["file"]));
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
if (this.routes["POST"] != null) {
|
||||
this.router.post(
|
||||
this.mainURL,
|
||||
|
@ -101,7 +175,7 @@ export class CoreHttpController<V> implements ICoreHttpController {
|
|||
usecase: CallbackStrategyWithValidationModel<T>
|
||||
) {
|
||||
let payload = null;
|
||||
|
||||
const useCase = usecase as any;
|
||||
if (req["model"] != undefined) {
|
||||
payload = req.body as T;
|
||||
}
|
||||
|
@ -113,7 +187,7 @@ export class CoreHttpController<V> implements ICoreHttpController {
|
|||
if (req.query.id !== undefined) {
|
||||
payload = String(req.query.id);
|
||||
}
|
||||
(await usecase(payload)).fold(
|
||||
(await useCase(payload)).fold(
|
||||
(ok) => {
|
||||
res.json(ok);
|
||||
return;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue