-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.ts
More file actions
178 lines (145 loc) · 5.54 KB
/
application.ts
File metadata and controls
178 lines (145 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import express, { Application as ExApplication, Handler, NextFunction, RequestHandler } from 'express';
import { controllers } from './src/controllers/index.controller';
import { MetadataKeys } from './src/utils/metadata.keys';
import { IRouter } from './src/utils/decorators/handlers.decorator';
import asyncWrap from './src/utils/asyncwrapper';
import errorHandler from './src/errors/error.handler';
import { MissingFieldError } from './src/errors/app.error';
import initDB from './src/configs/database';
class Application {
private readonly _instance: ExApplication;
get instance(): ExApplication {
initDB.connect()
return this._instance;
}
constructor() {
this._instance = express();
this._instance.use(express.json());
this.registerRouters();
errorHandler(this._instance);
}
async configureDb() {
/**
* Configure mongoose
**/
await initDB.connect();
}
bodyStringValidators(keys: string[]): RequestHandler {
return function (req: any, res: any, next: NextFunction) {
if(!keys){
next()
}
if (keys && keys.length !== 0 && !req.body) {
throw new MissingFieldError("", "Invalid request")
}
for (let key of keys) {
if (!req.body[key]) {
throw new MissingFieldError(key, "is string required")
} else if (typeof req.body[key] !== 'string') {
throw new MissingFieldError(key, "is string required")
}
}
next();
};
}
bodyArrayValidators(keys: string[]): RequestHandler {
return function (req: any, res: any, next: NextFunction) {
if(!keys){
next()
}
if (keys && keys.length !== 0 && !req.body) {
throw new MissingFieldError("", "Invalid request")
}
for (let key of keys) {
if (!req.body[key]) {
throw new MissingFieldError(key, "is array required")
} else if (typeof req.body[key] !== 'object') {
throw new MissingFieldError(key, "is array required")
}
}
next();
};
}
bodyEmailValidators(keys: string[] = []): RequestHandler {
return function (req: any, res: any, next: NextFunction) {
if(!keys){
next()
} if (keys && keys.length !== 0 && !req.body) {
throw new MissingFieldError("", "Invalid request")
}
function validateEmail(email: string) {
var re = /\S+@\S+\.\S+/;
return re.test(email);
}
for (let key of keys) {
if (!req.body[key]) {
throw new MissingFieldError(key, "is required")
} else if (!validateEmail(req.body[key])) {
throw new MissingFieldError(key, "is required")
}
}
next();
};
}
bodyNumberValidators(keys: string[] = []): RequestHandler {
return function (req: any, res: any, next: NextFunction) {
if(!keys){
next()
}
if (keys && keys.length !== 0 && !req.body) {
throw new MissingFieldError("", "Invalid request")
}
for (let key of keys) {
if (!req.body[key]) {
throw new MissingFieldError(key, "is number required")
} else if (typeof req.body[key] !== 'number') {
throw new MissingFieldError(key, "is number required")
}
}
next();
};
}
private registerRouters() {
this._instance.get('/', (req, res) => {
res.json({ message: 'Hello World!' });
});
const info: Array<{ api: string, handler: string }> = [];
controllers.forEach((controllerClass) => {
const controllerInstance: { [handleName: string]: Handler } = new controllerClass() as any;
const basePath: string = Reflect.getMetadata(MetadataKeys.BASE_PATH, controllerClass);
const routers: IRouter[] = Reflect.getMetadata(MetadataKeys.ROUTERS, controllerClass);
const exRouter = express.Router();
routers.forEach(({ method, path, handlerName }) => {
const requiredBodyProps = Reflect.getMetadata(MetadataKeys.VALIDATION, controllerInstance, handlerName) || [];
let validator: any = []
if (requiredBodyProps.length > 0) {
const isStringRequiredList = Reflect.getMetadata(MetadataKeys.VALIDATION_IS_STRING, new requiredBodyProps[0]())
const isNumberRequiredList = Reflect.getMetadata(MetadataKeys.VALIDATION_IS_NUMBER, new requiredBodyProps[0]())
const isEMAILRequiredList = Reflect.getMetadata(MetadataKeys.VALIDATION_IS_EMAIL, new requiredBodyProps[0]())
const isArrayRequiredList = Reflect.getMetadata(MetadataKeys.VALIDATION_IS_ARRAY, new requiredBodyProps[0]())
if(isStringRequiredList){
validator.push(this.bodyStringValidators(isStringRequiredList))
}
if(isNumberRequiredList){
validator.push(this.bodyNumberValidators(isNumberRequiredList))
}
if(isEMAILRequiredList){
validator.push(this.bodyEmailValidators(isEMAILRequiredList))
}
if(isArrayRequiredList){
validator.push(this.bodyArrayValidators(isArrayRequiredList))
}
}
const middleware: any[] = Reflect.getMetadata(MetadataKeys.MIDDLEWARE, controllerInstance, handlerName) || [];
exRouter[method](path, ...middleware, validator, asyncWrap(controllerInstance[String(handlerName)].bind(controllerInstance)));
info.push({
api: `${method.toLocaleUpperCase()} ${basePath + path}`,
handler: `${controllerClass.name}.${String(handlerName)}`,
});
});
this._instance.use(basePath, exRouter);
});
console.table(info);
}
}
export default new Application();