@bool-ts/core 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/__test/controller.d.ts +11 -0
- package/__test/controller.js +59 -0
- package/__test/controller.ts +62 -0
- package/__test/index.d.ts +1 -0
- package/__test/index.js +7 -0
- package/__test/index.ts +11 -0
- package/__test/interfaces.d.ts +6 -0
- package/__test/interfaces.js +0 -0
- package/__test/interfaces.ts +7 -0
- package/__test/module.d.ts +3 -0
- package/__test/module.js +24 -0
- package/__test/module.ts +17 -0
- package/__test/repository.d.ts +8 -0
- package/__test/repository.js +20 -0
- package/__test/repository.ts +16 -0
- package/__test/router.d.ts +0 -0
- package/__test/router.js +1 -0
- package/__test/router.ts +0 -0
- package/__test/service.d.ts +9 -0
- package/__test/service.js +29 -0
- package/__test/service.ts +20 -0
- package/bun.lockb +0 -0
- package/dist/decorators/controller.d.ts +3 -0
- package/dist/decorators/controller.js +8 -0
- package/dist/decorators/http.d.ts +51 -0
- package/dist/decorators/http.js +122 -0
- package/dist/decorators/index.d.ts +5 -0
- package/dist/decorators/index.js +5 -0
- package/dist/decorators/inject.d.ts +3 -0
- package/dist/decorators/inject.js +9 -0
- package/dist/decorators/injectable.d.ts +3 -0
- package/dist/decorators/injectable.js +6 -0
- package/dist/decorators/module.d.ts +7 -0
- package/dist/decorators/module.js +6 -0
- package/dist/hooks/factory.d.ts +8 -0
- package/dist/hooks/factory.js +119 -0
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.js +2 -0
- package/dist/hooks/injector.d.ts +6 -0
- package/dist/hooks/injector.js +23 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/interfaces/index.d.ts +2 -0
- package/dist/interfaces/index.js +1 -0
- package/package.json +33 -0
- package/src/decorators/controller.ts +18 -0
- package/src/decorators/http.ts +185 -0
- package/src/decorators/index.ts +5 -0
- package/src/decorators/inject.ts +19 -0
- package/src/decorators/injectable.ts +12 -0
- package/src/decorators/module.ts +19 -0
- package/src/hooks/factory.ts +151 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/injector.ts +39 -0
- package/src/index.ts +5 -0
- package/src/interfaces/index.ts +3 -0
- package/test.http +28 -0
- package/tsconfig.json +106 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import "colors";
|
|
3
|
+
import * as Qs from "qs";
|
|
4
|
+
import * as ResponseTime from "response-time";
|
|
5
|
+
import { controllerKey, controllerRoutesKey, moduleKey } from "../decorators";
|
|
6
|
+
import { default as ExpressApp, Router, json, urlencoded } from "express";
|
|
7
|
+
import { Injector } from "./injector";
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* @param target
|
|
11
|
+
* @param router
|
|
12
|
+
*/
|
|
13
|
+
const controllerCreator = (target, router = Router()) => {
|
|
14
|
+
if (!Reflect.getOwnMetadataKeys(target).includes(controllerKey)) {
|
|
15
|
+
throw Error(`${target.name} is not a controller.`);
|
|
16
|
+
}
|
|
17
|
+
const controller = Injector.get(target);
|
|
18
|
+
if (!controller) {
|
|
19
|
+
throw Error("Can not initialize controller.");
|
|
20
|
+
}
|
|
21
|
+
const controllerMetadata = Reflect.getOwnMetadata(controllerKey, target) || "/";
|
|
22
|
+
const routesMetadata = (Reflect.getOwnMetadata(controllerRoutesKey, target) || []);
|
|
23
|
+
const innerRouter = router.route(controllerMetadata);
|
|
24
|
+
routesMetadata.forEach(route => {
|
|
25
|
+
if (typeof route.descriptor.value !== "function") {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
switch (route.httpMethod) {
|
|
29
|
+
case "GET":
|
|
30
|
+
return innerRouter.get(route.descriptor.value.bind(controller));
|
|
31
|
+
case "POST":
|
|
32
|
+
return innerRouter.post(route.descriptor.value.bind(controller));
|
|
33
|
+
case "PUT":
|
|
34
|
+
return innerRouter.put(route.descriptor.value.bind(controller));
|
|
35
|
+
case "PATCH":
|
|
36
|
+
return innerRouter.patch(route.descriptor.value.bind(controller));
|
|
37
|
+
case "DELETE":
|
|
38
|
+
return innerRouter.delete(route.descriptor.value.bind(controller));
|
|
39
|
+
case "OPTIONS":
|
|
40
|
+
return innerRouter.options(route.descriptor.value.bind(controller));
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return router;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
*
|
|
47
|
+
* @param target
|
|
48
|
+
*/
|
|
49
|
+
export const BoolFactory = (target) => {
|
|
50
|
+
if (!Reflect.getOwnMetadataKeys(target).includes(moduleKey)) {
|
|
51
|
+
throw Error(`${target.name} is not a module.`);
|
|
52
|
+
}
|
|
53
|
+
const metadata = Reflect.getOwnMetadata(moduleKey, target);
|
|
54
|
+
const routers = !metadata?.controllers ? [] : metadata.controllers.map(controllerConstructor => controllerCreator(controllerConstructor));
|
|
55
|
+
const app = ExpressApp();
|
|
56
|
+
const configs = Object.freeze({
|
|
57
|
+
allowLogsMethods: [
|
|
58
|
+
"GET",
|
|
59
|
+
"POST",
|
|
60
|
+
"PUT",
|
|
61
|
+
"PATCH",
|
|
62
|
+
"DELETE"
|
|
63
|
+
]
|
|
64
|
+
});
|
|
65
|
+
app.set("etag", "strong");
|
|
66
|
+
app.set("query parser", (query) => Qs.parse(query, {
|
|
67
|
+
["depth"]: 10,
|
|
68
|
+
["arrayLimit"]: 50
|
|
69
|
+
}));
|
|
70
|
+
app.use(urlencoded({
|
|
71
|
+
extended: true,
|
|
72
|
+
inflate: true,
|
|
73
|
+
limit: "1mb",
|
|
74
|
+
parameterLimit: 20,
|
|
75
|
+
type: "application/x-www-form-urlencoded",
|
|
76
|
+
verify: undefined
|
|
77
|
+
}), json({
|
|
78
|
+
inflate: true,
|
|
79
|
+
limit: "5mb",
|
|
80
|
+
reviver: undefined,
|
|
81
|
+
strict: true,
|
|
82
|
+
type: "application/json",
|
|
83
|
+
verify: undefined
|
|
84
|
+
}),
|
|
85
|
+
// Headers parser
|
|
86
|
+
(req, res, next) => {
|
|
87
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
88
|
+
req.headers[key] = typeof value !== "string" ? value : decodeURI(value);
|
|
89
|
+
}
|
|
90
|
+
next();
|
|
91
|
+
},
|
|
92
|
+
// Body parser
|
|
93
|
+
(req, res, next) => {
|
|
94
|
+
if (typeof req.body !== "object" || !req.body) {
|
|
95
|
+
req.body = Object.freeze({});
|
|
96
|
+
}
|
|
97
|
+
next();
|
|
98
|
+
},
|
|
99
|
+
// Error catcher
|
|
100
|
+
(err, req, res, next) => {
|
|
101
|
+
console.error(err);
|
|
102
|
+
next();
|
|
103
|
+
},
|
|
104
|
+
// Response time log
|
|
105
|
+
ResponseTime.default((req, res, time) => {
|
|
106
|
+
const requestMethod = req.method.toUpperCase();
|
|
107
|
+
if (!configs.allowLogsMethods.includes(requestMethod)) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const convertedMethod = `${requestMethod.yellow}`.bgBlue;
|
|
111
|
+
const convertedPID = `${process.pid}`.yellow;
|
|
112
|
+
const convertedReqIp = `${req.headers["x-forwarded-for"] || req.headers["x-real-ip"] || req.ip || "<Unknown>"}`.yellow;
|
|
113
|
+
const convertedTime = `${Math.round((time + Number.EPSILON) * 10 ** 2) / 10 ** 2}ms`.yellow;
|
|
114
|
+
console.info(`PID: ${convertedPID} - Method: ${convertedMethod} - IP: ${convertedReqIp} - ${req.originalUrl.blue} - Time: ${convertedTime}`);
|
|
115
|
+
}));
|
|
116
|
+
app.use(routers);
|
|
117
|
+
return app;
|
|
118
|
+
};
|
|
119
|
+
export default BoolFactory;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
export const Injector = new class {
|
|
3
|
+
_mapper = new Map();
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param constructor
|
|
7
|
+
*/
|
|
8
|
+
get(target) {
|
|
9
|
+
if (this._mapper.has(target)) {
|
|
10
|
+
return this._mapper.get(target);
|
|
11
|
+
}
|
|
12
|
+
if (!Reflect.getOwnMetadataKeys(target).includes("__bool:injectable__")) {
|
|
13
|
+
throw Error("Missing dependency declaration, please check @Injectable() used on dependency(ies).");
|
|
14
|
+
}
|
|
15
|
+
// Initialize dependencies injection
|
|
16
|
+
const dependencies = Reflect.getOwnMetadata("design:paramtypes", target) || [];
|
|
17
|
+
const injections = dependencies.map(dependency => Injector.get(dependency));
|
|
18
|
+
const instance = new target(...injections);
|
|
19
|
+
this._mapper.set(target, instance);
|
|
20
|
+
return instance;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
export default Injector;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default {};
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bool-ts/core",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"build": "tsc"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/BoolTS/core.git"
|
|
13
|
+
},
|
|
14
|
+
"author": "Trần Đức Tâm (Neo)",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/BoolTS/core/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/BoolTS/core#readme",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"colors": "^1.4.0",
|
|
22
|
+
"express": ">=5.0.0-beta.1",
|
|
23
|
+
"qs": "^6.12.1",
|
|
24
|
+
"reflect-metadata": "^0.2.2",
|
|
25
|
+
"response-time": "^2.3.2"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/express": "^4.17.21",
|
|
29
|
+
"@types/node-statsd": "^0.1.6",
|
|
30
|
+
"@types/response-time": "^2.3.8",
|
|
31
|
+
"typescript": "^5.4.5"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { injectableKey } from "./injectable";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export const controllerKey = "__bool:controller__";
|
|
5
|
+
|
|
6
|
+
export const Controller = (
|
|
7
|
+
prefix: string
|
|
8
|
+
) => <T extends Object>(
|
|
9
|
+
target: T,
|
|
10
|
+
context?: ClassDecoratorContext
|
|
11
|
+
) => {
|
|
12
|
+
Reflect.defineMetadata(controllerKey, !prefix.startsWith("/") ? `/${prefix}` : prefix, target);
|
|
13
|
+
Reflect.defineMetadata(injectableKey, undefined, target);
|
|
14
|
+
|
|
15
|
+
return target;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default Controller;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
export interface IControllerRoute {
|
|
2
|
+
path: string;
|
|
3
|
+
httpMethod: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS";
|
|
4
|
+
methodName: string;
|
|
5
|
+
descriptor: PropertyDescriptor;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export const controllerRoutesKey = "__bool:controller.routes__";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @param path
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
export const Get = (
|
|
17
|
+
path = "/"
|
|
18
|
+
) => (
|
|
19
|
+
target: Object,
|
|
20
|
+
methodName: string,
|
|
21
|
+
descriptor: PropertyDescriptor
|
|
22
|
+
) => {
|
|
23
|
+
if (typeof descriptor.value !== "function") {
|
|
24
|
+
throw Error("Get decorator only use for method.");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
Reflect.defineMetadata(controllerRoutesKey, [
|
|
28
|
+
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
29
|
+
{
|
|
30
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
31
|
+
httpMethod: "GET",
|
|
32
|
+
methodName: methodName,
|
|
33
|
+
descriptor: descriptor
|
|
34
|
+
}
|
|
35
|
+
], target.constructor);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
*
|
|
41
|
+
* @param path
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
export const Post = (
|
|
45
|
+
path = "/"
|
|
46
|
+
) => (
|
|
47
|
+
target: Object,
|
|
48
|
+
methodName: string,
|
|
49
|
+
descriptor: PropertyDescriptor
|
|
50
|
+
) => {
|
|
51
|
+
if (typeof descriptor.value !== "function") {
|
|
52
|
+
throw Error("Post decorator only use for method.");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
Reflect.defineMetadata(controllerRoutesKey, [
|
|
56
|
+
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
57
|
+
{
|
|
58
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
59
|
+
httpMethod: "POST",
|
|
60
|
+
methodName: methodName,
|
|
61
|
+
descriptor: descriptor
|
|
62
|
+
}
|
|
63
|
+
], target.constructor);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
*
|
|
69
|
+
* @param path
|
|
70
|
+
* @returns
|
|
71
|
+
*/
|
|
72
|
+
export const Put = (
|
|
73
|
+
path = "/"
|
|
74
|
+
) => (
|
|
75
|
+
target: Object,
|
|
76
|
+
methodName: string,
|
|
77
|
+
descriptor: PropertyDescriptor
|
|
78
|
+
) => {
|
|
79
|
+
if (typeof descriptor.value !== "function") {
|
|
80
|
+
throw Error("Put decorator only use for method.");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
Reflect.defineMetadata(controllerRoutesKey, [
|
|
84
|
+
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
85
|
+
{
|
|
86
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
87
|
+
httpMethod: "PUT",
|
|
88
|
+
methodName: methodName,
|
|
89
|
+
descriptor: descriptor
|
|
90
|
+
}
|
|
91
|
+
], target.constructor);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
*
|
|
97
|
+
* @param path
|
|
98
|
+
* @returns
|
|
99
|
+
*/
|
|
100
|
+
export const Patch = (
|
|
101
|
+
path = "/"
|
|
102
|
+
) => (
|
|
103
|
+
target: Object,
|
|
104
|
+
methodName: string,
|
|
105
|
+
descriptor: PropertyDescriptor
|
|
106
|
+
) => {
|
|
107
|
+
if (typeof descriptor.value !== "function") {
|
|
108
|
+
throw Error("Patch decorator only use for method.");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
Reflect.defineMetadata(controllerRoutesKey, [
|
|
112
|
+
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
113
|
+
{
|
|
114
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
115
|
+
httpMethod: "PATCH",
|
|
116
|
+
methodName: methodName,
|
|
117
|
+
descriptor: descriptor
|
|
118
|
+
}
|
|
119
|
+
], target.constructor);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
*
|
|
125
|
+
* @param path
|
|
126
|
+
* @returns
|
|
127
|
+
*/
|
|
128
|
+
export const Delete = (
|
|
129
|
+
path = "/"
|
|
130
|
+
) => (
|
|
131
|
+
target: Object,
|
|
132
|
+
methodName: string,
|
|
133
|
+
descriptor: PropertyDescriptor
|
|
134
|
+
) => {
|
|
135
|
+
if (typeof descriptor.value !== "function") {
|
|
136
|
+
throw Error("Delete decorator only use for method.");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
Reflect.defineMetadata(controllerRoutesKey, [
|
|
140
|
+
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
141
|
+
{
|
|
142
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
143
|
+
httpMethod: "DELETE",
|
|
144
|
+
methodName: methodName,
|
|
145
|
+
descriptor: descriptor
|
|
146
|
+
}
|
|
147
|
+
], target.constructor);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
*
|
|
153
|
+
* @param path
|
|
154
|
+
* @returns
|
|
155
|
+
*/
|
|
156
|
+
export const Options = (
|
|
157
|
+
path = "/"
|
|
158
|
+
) => (
|
|
159
|
+
target: Object,
|
|
160
|
+
methodName: string,
|
|
161
|
+
descriptor: PropertyDescriptor
|
|
162
|
+
) => {
|
|
163
|
+
if (typeof descriptor.value !== "function") {
|
|
164
|
+
throw Error("Options decorator only use for method.");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
Reflect.defineMetadata(controllerRoutesKey, [
|
|
168
|
+
...Reflect.getOwnMetadata(controllerRoutesKey, target.constructor) || [],
|
|
169
|
+
{
|
|
170
|
+
path: !path.startsWith("/") ? `/${path}` : path,
|
|
171
|
+
httpMethod: "OPTIONS",
|
|
172
|
+
methodName: methodName,
|
|
173
|
+
descriptor: descriptor
|
|
174
|
+
}
|
|
175
|
+
], target.constructor);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export default {
|
|
179
|
+
Get,
|
|
180
|
+
Post,
|
|
181
|
+
Put,
|
|
182
|
+
Patch,
|
|
183
|
+
Delete
|
|
184
|
+
};
|
|
185
|
+
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { Controller, controllerKey } from "./controller";
|
|
2
|
+
export { Inject, injectKey } from "./inject";
|
|
3
|
+
export { Injectable, injectableKey } from "./injectable";
|
|
4
|
+
export { Module, moduleKey, type TModuleOptions } from "./module";
|
|
5
|
+
export { Get, Post, Put, Patch, Delete, Options, controllerRoutesKey, type IControllerRoute } from "./http";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const injectKey = "design:paramtypes";
|
|
2
|
+
|
|
3
|
+
export const Inject = <T>(
|
|
4
|
+
constructor: new (...args: any[]) => T
|
|
5
|
+
) => {
|
|
6
|
+
return (
|
|
7
|
+
target: Object,
|
|
8
|
+
parameterName: string | Symbol | undefined,
|
|
9
|
+
parameterIndex: number
|
|
10
|
+
) => {
|
|
11
|
+
const designParameterTypes: any[] = Reflect.getMetadata(injectKey, target.constructor) || [];
|
|
12
|
+
|
|
13
|
+
designParameterTypes[parameterIndex] = constructor;
|
|
14
|
+
|
|
15
|
+
Reflect.defineMetadata(injectKey, designParameterTypes, target.constructor);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default Inject;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const injectableKey = "__bool:injectable__";
|
|
2
|
+
|
|
3
|
+
export const Injectable = () => <T extends Object>(
|
|
4
|
+
target: T,
|
|
5
|
+
context?: ClassDecoratorContext
|
|
6
|
+
) => {
|
|
7
|
+
Reflect.defineMetadata(injectableKey, undefined, target.constructor);
|
|
8
|
+
|
|
9
|
+
return target;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default Injectable;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type TModuleOptions = Partial<{
|
|
2
|
+
controllers: Array<new (...args: any[]) => unknown>;
|
|
3
|
+
dependencies: Array<new (...args: any[]) => unknown>;
|
|
4
|
+
}> | undefined;
|
|
5
|
+
|
|
6
|
+
export const moduleKey = "__bool:module__";
|
|
7
|
+
|
|
8
|
+
export const Module = (
|
|
9
|
+
args?: TModuleOptions
|
|
10
|
+
) => <T extends { new(...args: any[]): {} }>(
|
|
11
|
+
target: T,
|
|
12
|
+
context?: ClassDecoratorContext
|
|
13
|
+
) => {
|
|
14
|
+
Reflect.defineMetadata(moduleKey, args, target);
|
|
15
|
+
|
|
16
|
+
return target;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default Module;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import "colors";
|
|
3
|
+
|
|
4
|
+
import * as Qs from "qs";
|
|
5
|
+
import * as ResponseTime from "response-time";
|
|
6
|
+
|
|
7
|
+
import { type IControllerRoute, type TModuleOptions, controllerKey, controllerRoutesKey, moduleKey } from "../decorators";
|
|
8
|
+
import { default as ExpressApp, Router, json, urlencoded, Request, Response, NextFunction, Errback } from "express";
|
|
9
|
+
import { Injector } from "./injector";
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param target
|
|
15
|
+
* @param router
|
|
16
|
+
*/
|
|
17
|
+
const controllerCreator = (
|
|
18
|
+
target: new (...args: any[]) => unknown,
|
|
19
|
+
router = Router()
|
|
20
|
+
) => {
|
|
21
|
+
if (!Reflect.getOwnMetadataKeys(target).includes(controllerKey)) {
|
|
22
|
+
throw Error(`${target.name} is not a controller.`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const controller = Injector.get(target);
|
|
26
|
+
|
|
27
|
+
if (!controller) {
|
|
28
|
+
throw Error("Can not initialize controller.");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const controllerMetadata = Reflect.getOwnMetadata(controllerKey, target) || "/";
|
|
32
|
+
const routesMetadata = (Reflect.getOwnMetadata(controllerRoutesKey, target) || []) as Array<IControllerRoute>;
|
|
33
|
+
const innerRouter = router.route(controllerMetadata);
|
|
34
|
+
|
|
35
|
+
routesMetadata.forEach(route => {
|
|
36
|
+
if (typeof route.descriptor.value !== "function") {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
switch (route.httpMethod) {
|
|
41
|
+
case "GET":
|
|
42
|
+
return innerRouter.get(route.descriptor.value.bind(controller));
|
|
43
|
+
case "POST":
|
|
44
|
+
return innerRouter.post(route.descriptor.value.bind(controller));
|
|
45
|
+
case "PUT":
|
|
46
|
+
return innerRouter.put(route.descriptor.value.bind(controller));
|
|
47
|
+
case "PATCH":
|
|
48
|
+
return innerRouter.patch(route.descriptor.value.bind(controller));
|
|
49
|
+
case "DELETE":
|
|
50
|
+
return innerRouter.delete(route.descriptor.value.bind(controller));
|
|
51
|
+
case "OPTIONS":
|
|
52
|
+
return innerRouter.options(route.descriptor.value.bind(controller));
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return router;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
*
|
|
62
|
+
* @param target
|
|
63
|
+
*/
|
|
64
|
+
export const BoolFactory = (
|
|
65
|
+
target: new (...args: any[]) => unknown
|
|
66
|
+
) => {
|
|
67
|
+
if (!Reflect.getOwnMetadataKeys(target).includes(moduleKey)) {
|
|
68
|
+
throw Error(`${target.name} is not a module.`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const metadata = Reflect.getOwnMetadata(moduleKey, target) as TModuleOptions;
|
|
72
|
+
const routers = !metadata?.controllers ? [] : metadata.controllers.map(controllerConstructor => controllerCreator(controllerConstructor));
|
|
73
|
+
const app = ExpressApp();
|
|
74
|
+
const configs = Object.freeze({
|
|
75
|
+
allowLogsMethods: [
|
|
76
|
+
"GET",
|
|
77
|
+
"POST",
|
|
78
|
+
"PUT",
|
|
79
|
+
"PATCH",
|
|
80
|
+
"DELETE"
|
|
81
|
+
]
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
app.set("etag", "strong");
|
|
85
|
+
app.set("query parser", (query: string) => Qs.parse(query, {
|
|
86
|
+
["depth"]: 10,
|
|
87
|
+
["arrayLimit"]: 50
|
|
88
|
+
}));
|
|
89
|
+
|
|
90
|
+
app.use(
|
|
91
|
+
urlencoded({
|
|
92
|
+
extended: true,
|
|
93
|
+
inflate: true,
|
|
94
|
+
limit: "1mb",
|
|
95
|
+
parameterLimit: 20,
|
|
96
|
+
type: "application/x-www-form-urlencoded",
|
|
97
|
+
verify: undefined
|
|
98
|
+
}),
|
|
99
|
+
json({
|
|
100
|
+
inflate: true,
|
|
101
|
+
limit: "5mb",
|
|
102
|
+
reviver: undefined,
|
|
103
|
+
strict: true,
|
|
104
|
+
type: "application/json",
|
|
105
|
+
verify: undefined
|
|
106
|
+
}),
|
|
107
|
+
// Headers parser
|
|
108
|
+
(req: Request, res: Response, next: NextFunction) => {
|
|
109
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
110
|
+
req.headers[key] = typeof value !== "string" ? value : decodeURI(value)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
next();
|
|
114
|
+
},
|
|
115
|
+
// Body parser
|
|
116
|
+
(req: Request, res: Response, next: NextFunction) => {
|
|
117
|
+
if (typeof req.body !== "object" || !req.body) {
|
|
118
|
+
req.body = Object.freeze({});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
next();
|
|
122
|
+
},
|
|
123
|
+
// Error catcher
|
|
124
|
+
(err: Errback, req: Request, res: Response, next: NextFunction) => {
|
|
125
|
+
console.error(err);
|
|
126
|
+
|
|
127
|
+
next();
|
|
128
|
+
},
|
|
129
|
+
// Response time log
|
|
130
|
+
ResponseTime.default((req: Request, res: Response, time: number) => {
|
|
131
|
+
const requestMethod = req.method.toUpperCase();
|
|
132
|
+
|
|
133
|
+
if (!configs.allowLogsMethods.includes(requestMethod)) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const convertedMethod = `${requestMethod.yellow}`.bgBlue;
|
|
138
|
+
const convertedPID = `${process.pid}`.yellow;
|
|
139
|
+
const convertedReqIp = `${req.headers["x-forwarded-for"] || req.headers["x-real-ip"] || req.ip || "<Unknown>"}`.yellow;
|
|
140
|
+
const convertedTime = `${Math.round((time + Number.EPSILON) * 10 ** 2) / 10 ** 2}ms`.yellow;
|
|
141
|
+
|
|
142
|
+
console.info(`PID: ${convertedPID} - Method: ${convertedMethod} - IP: ${convertedReqIp} - ${req.originalUrl.blue} - Time: ${convertedTime}`);
|
|
143
|
+
})
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
app.use(routers);
|
|
147
|
+
|
|
148
|
+
return app;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export default BoolFactory;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
interface IInjector {
|
|
5
|
+
get<T>(target: new (...args: any[]) => T): T
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const Injector: IInjector = new class {
|
|
9
|
+
|
|
10
|
+
private readonly _mapper: Map<Function, any> = new Map();
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param constructor
|
|
15
|
+
*/
|
|
16
|
+
get<T>(
|
|
17
|
+
target: new (...args: any[]) => T
|
|
18
|
+
) {
|
|
19
|
+
if (this._mapper.has(target)) {
|
|
20
|
+
return this._mapper.get(target) as T;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (!Reflect.getOwnMetadataKeys(target).includes("__bool:injectable__")) {
|
|
24
|
+
throw Error("Missing dependency declaration, please check @Injectable() used on dependency(ies).");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Initialize dependencies injection
|
|
28
|
+
const dependencies: any[] = Reflect.getOwnMetadata("design:paramtypes", target) || [];
|
|
29
|
+
const injections: any[] = dependencies.map(dependency => Injector.get(dependency));
|
|
30
|
+
const instance = new target(...injections);
|
|
31
|
+
|
|
32
|
+
this._mapper.set(target, instance);
|
|
33
|
+
|
|
34
|
+
return instance;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export default Injector;
|
package/src/index.ts
ADDED