c2-http 1.0.107 → 1.0.110
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.
|
@@ -1,11 +1,24 @@
|
|
|
1
|
-
import { Router } from "express";
|
|
2
|
-
import {
|
|
1
|
+
import { RequestHandler, Router } from "express";
|
|
2
|
+
import { NormalizePathEntry } from "express-prom-bundle";
|
|
3
3
|
import { IControllerOptions } from "./IControllerOptions";
|
|
4
|
+
export interface ISingleRouteParam {
|
|
5
|
+
method: "get" | "post" | "patch" | "delete";
|
|
6
|
+
uri: string;
|
|
7
|
+
middlewareAuthorization?: RequestHandler;
|
|
8
|
+
middlewares?: RequestHandler[];
|
|
9
|
+
handlerFn: RequestHandler;
|
|
10
|
+
}
|
|
4
11
|
export declare abstract class Controller {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
12
|
+
router: Router;
|
|
13
|
+
normalizedPaths: NormalizePathEntry[];
|
|
14
|
+
uri: string;
|
|
15
|
+
basePath: string;
|
|
16
|
+
relativePath: string;
|
|
17
|
+
prefixRole: string[];
|
|
18
|
+
roles: string[];
|
|
19
|
+
byPass: boolean;
|
|
20
|
+
constructor(options: IControllerOptions);
|
|
21
|
+
registerRoute(options: ISingleRouteParam): void;
|
|
9
22
|
}
|
|
10
23
|
export declare function HttpDispatchHandling(target: any, methodName: string, descriptor: PropertyDescriptor): PropertyDescriptor;
|
|
11
24
|
export declare function HttpDispatchDownload(target: any, methodName: string, descriptor: PropertyDescriptor): PropertyDescriptor;
|
package/dist/model/Controller.js
CHANGED
|
@@ -4,28 +4,48 @@ exports.OpenApi = exports.HttpDispatchDownload = exports.HttpDispatchHandling =
|
|
|
4
4
|
const express_1 = require("express");
|
|
5
5
|
const http_status_1 = require("http-status");
|
|
6
6
|
class Controller {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
router;
|
|
8
|
+
normalizedPaths;
|
|
9
|
+
uri;
|
|
10
|
+
basePath;
|
|
11
|
+
relativePath;
|
|
12
|
+
prefixRole;
|
|
13
|
+
roles;
|
|
14
|
+
byPass;
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.roles = options.roles ?? [];
|
|
17
|
+
this.byPass = options.byPass ?? false;
|
|
18
|
+
this.normalizedPaths = [];
|
|
19
|
+
this.router = (0, express_1.Router)();
|
|
20
|
+
this.basePath = options.basePath ?? "";
|
|
21
|
+
this.relativePath = options.relativePath;
|
|
22
|
+
if (!this.relativePath?.startsWith("/")) {
|
|
13
23
|
throw new Error("the 'relativePatch' must start with a slash '/'");
|
|
14
24
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this.options.roles = [];
|
|
20
|
-
}
|
|
21
|
-
if (!this.options?.prefixRole) {
|
|
22
|
-
let defaultPrefixRole = this.options.relativePath;
|
|
25
|
+
this.uri = `${this.basePath}${this.relativePath}`;
|
|
26
|
+
this.prefixRole = options.prefixRole;
|
|
27
|
+
if (!this?.prefixRole) {
|
|
28
|
+
let defaultPrefixRole = this.relativePath;
|
|
23
29
|
if (defaultPrefixRole.startsWith("/")) {
|
|
24
30
|
defaultPrefixRole = defaultPrefixRole.replace("/", "");
|
|
25
31
|
}
|
|
26
|
-
this.
|
|
32
|
+
this.prefixRole = [defaultPrefixRole.replaceAll("/", "-")];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
registerRoute(options) {
|
|
36
|
+
const { method, uri, middlewareAuthorization, middlewares, handlerFn } = options;
|
|
37
|
+
let middlewaresAux = middlewareAuthorization ? [middlewareAuthorization] : [];
|
|
38
|
+
if (middlewares) {
|
|
39
|
+
middlewaresAux = [...middlewaresAux, ...middlewares];
|
|
40
|
+
}
|
|
41
|
+
// Registra rota normalmente
|
|
42
|
+
this.router[method](uri, ...middlewaresAux, handlerFn);
|
|
43
|
+
if (uri.includes(":")) {
|
|
44
|
+
// Ex: /users/:id -> regex /users/[^/]+
|
|
45
|
+
const regex = new RegExp(`^${uri.replace(/:[^/]+/g, "[^/]+")}$`);
|
|
46
|
+
// Armazena no router para posterior coleta de métricas
|
|
47
|
+
this.normalizedPaths.push([regex, uri]);
|
|
27
48
|
}
|
|
28
|
-
this.options.uri = `${this.options.basePath}${this.options.relativePath}`;
|
|
29
49
|
}
|
|
30
50
|
}
|
|
31
51
|
exports.Controller = Controller;
|
|
@@ -4,13 +4,11 @@ import { ICrudControllerOptions } from "./ICrudControllerOptions";
|
|
|
4
4
|
export declare abstract class CrudController extends Controller {
|
|
5
5
|
constructor(options: ICrudControllerOptions);
|
|
6
6
|
abstract search(request: Request, response: Response): Promise<[number, any]>;
|
|
7
|
-
abstract deepSearch(request: Request, response: Response): Promise<[number, any]>;
|
|
8
7
|
abstract getById(request: Request, response: Response): Promise<[number, any]>;
|
|
9
8
|
abstract create(request: Request, response: Response): Promise<[number, any]>;
|
|
10
9
|
abstract update(request: Request, response: Response): Promise<[number, any]>;
|
|
11
10
|
abstract delete(request: Request, response: Response): Promise<[number, any]>;
|
|
12
11
|
searchRunner(request: Request, response: Response): Promise<[number, any]>;
|
|
13
|
-
deepSearchRunner(request: Request, response: Response): Promise<[number, any]>;
|
|
14
12
|
getByIdRunner(request: Request, response: Response): Promise<[number, any]>;
|
|
15
13
|
createRunner(request: Request, response: Response): Promise<[number, any]>;
|
|
16
14
|
updateRunner(request: Request, response: Response): Promise<[number, any]>;
|
|
@@ -8,8 +8,6 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.CrudController = void 0;
|
|
10
10
|
const Controller_1 = require("./Controller");
|
|
11
|
-
const ControllerRoleMiddleware_1 = require("./ControllerRoleMiddleware");
|
|
12
|
-
const ControllerRoute_1 = require("./ControllerRoute");
|
|
13
11
|
class CrudController extends Controller_1.Controller {
|
|
14
12
|
constructor(options) {
|
|
15
13
|
super(options);
|
|
@@ -18,97 +16,69 @@ class CrudController extends Controller_1.Controller {
|
|
|
18
16
|
this.createRunner = this.createRunner.bind(this);
|
|
19
17
|
this.updateRunner = this.updateRunner.bind(this);
|
|
20
18
|
this.deleteRunner = this.deleteRunner.bind(this);
|
|
21
|
-
this.deepSearchRunner = this.deepSearchRunner.bind(this);
|
|
22
19
|
if (options.byPass === true || options.middlewareCheckAuthorization === undefined) {
|
|
23
|
-
this.
|
|
24
|
-
router: this.routers,
|
|
20
|
+
this.registerRoute({
|
|
25
21
|
method: "get",
|
|
26
|
-
uri: `${this.
|
|
27
|
-
|
|
28
|
-
})
|
|
29
|
-
this.
|
|
30
|
-
router: this.routers,
|
|
22
|
+
uri: `${this.uri}/:id`,
|
|
23
|
+
handlerFn: this.getByIdRunner
|
|
24
|
+
});
|
|
25
|
+
this.registerRoute({
|
|
31
26
|
method: "get",
|
|
32
|
-
uri: `${this.
|
|
33
|
-
|
|
34
|
-
})
|
|
35
|
-
this.
|
|
36
|
-
router: this.routers,
|
|
27
|
+
uri: `${this.uri}`,
|
|
28
|
+
handlerFn: this.searchRunner
|
|
29
|
+
});
|
|
30
|
+
this.registerRoute({
|
|
37
31
|
method: "patch",
|
|
38
|
-
uri: `${this.
|
|
39
|
-
|
|
40
|
-
})
|
|
41
|
-
this.
|
|
42
|
-
router: this.routers,
|
|
32
|
+
uri: `${this.uri}/:id`,
|
|
33
|
+
handlerFn: this.updateRunner
|
|
34
|
+
});
|
|
35
|
+
this.registerRoute({
|
|
43
36
|
method: "post",
|
|
44
|
-
uri: `${this.
|
|
45
|
-
|
|
46
|
-
})
|
|
47
|
-
this.
|
|
48
|
-
router: this.routers,
|
|
49
|
-
method: "patch",
|
|
50
|
-
uri: `${this.options.uri}/:id`,
|
|
51
|
-
controllerFn: this.updateRunner
|
|
52
|
-
}));
|
|
53
|
-
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
54
|
-
router: this.routers,
|
|
37
|
+
uri: `${this.uri}`,
|
|
38
|
+
handlerFn: this.createRunner
|
|
39
|
+
});
|
|
40
|
+
this.registerRoute({
|
|
55
41
|
method: "delete",
|
|
56
|
-
uri: `${this.
|
|
57
|
-
|
|
58
|
-
})
|
|
42
|
+
uri: `${this.uri}/:id`,
|
|
43
|
+
handlerFn: this.deleteRunner
|
|
44
|
+
});
|
|
59
45
|
}
|
|
60
46
|
else {
|
|
61
|
-
this.
|
|
62
|
-
router: this.routers,
|
|
63
|
-
method: "get",
|
|
64
|
-
uri: `${this.options.uri}/:id`,
|
|
65
|
-
// MiddlewareCheckRolesFlow.middleware(this.options.prefixRole.map((pr: string) => `${pr}:read`)),
|
|
66
|
-
middlewareCheckAuthorization: new ControllerRoleMiddleware_1.ControllerRoleMiddleware(this.options.prefixRole.map((pr) => `${pr}:read`), options.middlewareCheckAuthorization),
|
|
67
|
-
controllerFn: this.getByIdRunner
|
|
68
|
-
}));
|
|
69
|
-
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
70
|
-
router: this.routers,
|
|
71
|
-
method: "get",
|
|
72
|
-
uri: `${this.options.uri}`,
|
|
73
|
-
middlewareCheckAuthorization: new ControllerRoleMiddleware_1.ControllerRoleMiddleware(this.options.prefixRole.map((pr) => `${pr}:read`), options.middlewareCheckAuthorization),
|
|
74
|
-
controllerFn: this.searchRunner
|
|
75
|
-
}));
|
|
76
|
-
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
77
|
-
router: this.routers,
|
|
78
|
-
method: "patch",
|
|
79
|
-
uri: `${this.options.uri}`,
|
|
80
|
-
middlewareCheckAuthorization: new ControllerRoleMiddleware_1.ControllerRoleMiddleware(this.options.prefixRole.map((pr) => `${pr}:read`), options.middlewareCheckAuthorization),
|
|
81
|
-
controllerFn: this.deepSearchRunner
|
|
82
|
-
}));
|
|
83
|
-
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
84
|
-
router: this.routers,
|
|
47
|
+
this.registerRoute({
|
|
85
48
|
method: "post",
|
|
86
|
-
uri: `${this.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
})
|
|
90
|
-
this.
|
|
91
|
-
router: this.routers,
|
|
49
|
+
uri: `${this.uri}`,
|
|
50
|
+
handlerFn: this.createRunner,
|
|
51
|
+
middlewareAuthorization: () => options.middlewareCheckAuthorization(this.prefixRole.map((pr) => `${pr}:create`))
|
|
52
|
+
});
|
|
53
|
+
this.registerRoute({
|
|
92
54
|
method: "patch",
|
|
93
|
-
uri: `${this.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
})
|
|
97
|
-
this.
|
|
98
|
-
|
|
55
|
+
uri: `${this.uri}/:id`,
|
|
56
|
+
handlerFn: this.updateRunner,
|
|
57
|
+
middlewareAuthorization: () => options.middlewareCheckAuthorization(this.prefixRole.map((pr) => `${pr}:edit`))
|
|
58
|
+
});
|
|
59
|
+
this.registerRoute({
|
|
60
|
+
method: "get",
|
|
61
|
+
uri: `${this.uri}/:id`,
|
|
62
|
+
handlerFn: this.getByIdRunner,
|
|
63
|
+
middlewareAuthorization: () => options.middlewareCheckAuthorization(this.prefixRole.map((pr) => `${pr}:read`))
|
|
64
|
+
});
|
|
65
|
+
this.registerRoute({
|
|
66
|
+
method: "get",
|
|
67
|
+
uri: `${this.uri}`,
|
|
68
|
+
handlerFn: this.searchRunner,
|
|
69
|
+
middlewareAuthorization: () => options.middlewareCheckAuthorization(this.prefixRole.map((pr) => `${pr}:read`))
|
|
70
|
+
});
|
|
71
|
+
this.registerRoute({
|
|
99
72
|
method: "delete",
|
|
100
|
-
uri: `${this.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
})
|
|
73
|
+
uri: `${this.uri}/:id`,
|
|
74
|
+
handlerFn: this.deleteRunner,
|
|
75
|
+
middlewareAuthorization: () => options.middlewareCheckAuthorization(this.prefixRole.map((pr) => `${pr}:delete`))
|
|
76
|
+
});
|
|
104
77
|
}
|
|
105
78
|
}
|
|
106
79
|
async searchRunner(request, response) {
|
|
107
80
|
return await this.search(request, response);
|
|
108
81
|
}
|
|
109
|
-
async deepSearchRunner(request, response) {
|
|
110
|
-
return await this.deepSearch(request, response);
|
|
111
|
-
}
|
|
112
82
|
async getByIdRunner(request, response) {
|
|
113
83
|
return await this.getById(request, response);
|
|
114
84
|
}
|
|
@@ -125,9 +95,6 @@ class CrudController extends Controller_1.Controller {
|
|
|
125
95
|
__decorate([
|
|
126
96
|
Controller_1.HttpDispatchHandling
|
|
127
97
|
], CrudController.prototype, "searchRunner", null);
|
|
128
|
-
__decorate([
|
|
129
|
-
Controller_1.HttpDispatchHandling
|
|
130
|
-
], CrudController.prototype, "deepSearchRunner", null);
|
|
131
98
|
__decorate([
|
|
132
99
|
Controller_1.HttpDispatchHandling
|
|
133
100
|
], CrudController.prototype, "getByIdRunner", null);
|
package/dist/model/Server.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import express
|
|
1
|
+
import express from "express";
|
|
2
|
+
import { Controller } from "./Controller";
|
|
2
3
|
export interface IServerConfig {
|
|
3
4
|
openApiFileOutput: any;
|
|
4
5
|
port: string;
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
closeControllers: Controller[];
|
|
7
|
+
openControllers: Controller[];
|
|
7
8
|
i18n: any;
|
|
8
9
|
}
|
|
9
10
|
export declare class Server {
|
package/dist/model/Server.js
CHANGED
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.Server = void 0;
|
|
7
7
|
const c2_util_1 = require("c2-util");
|
|
8
8
|
const express_1 = __importDefault(require("express"));
|
|
9
|
+
const express_prom_bundle_1 = __importDefault(require("express-prom-bundle"));
|
|
9
10
|
const swagger_ui_express_1 = __importDefault(require("swagger-ui-express"));
|
|
10
11
|
const i18n_1 = require("../i18n");
|
|
11
12
|
const MiddlewareGlobals_1 = __importDefault(require("../middleware/global-middleware/MiddlewareGlobals"));
|
|
@@ -22,15 +23,25 @@ class Server {
|
|
|
22
23
|
(0, c2_util_1.log)("LOG_INIT_SERVER", `Initializing server on ${this.config.port}...`);
|
|
23
24
|
(0, i18n_1.initializei18n)(this.config.i18n);
|
|
24
25
|
MiddlewareGlobals_1.default.config(this.app);
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
let pathsToMetrics = [];
|
|
27
|
+
for (const openController of this.config.openControllers) {
|
|
28
|
+
this.app.use(openController.router);
|
|
29
|
+
pathsToMetrics = [...pathsToMetrics, ...openController.normalizedPaths];
|
|
27
30
|
}
|
|
28
31
|
this.app.use("/docs", swagger_ui_express_1.default.serve, swagger_ui_express_1.default.setup(global.OPEN_API_DOC));
|
|
29
32
|
(0, c2_util_1.log)("LOG", "Rotas abertas OK");
|
|
30
33
|
this.app.use(MiddlewareJwt_1.default.config);
|
|
31
|
-
for (const
|
|
32
|
-
this.app.use(router);
|
|
34
|
+
for (const closeRouter of this.config.closeControllers) {
|
|
35
|
+
this.app.use(closeRouter.router);
|
|
36
|
+
pathsToMetrics = [...pathsToMetrics, ...closeRouter.normalizedPaths];
|
|
33
37
|
}
|
|
38
|
+
const metricsMiddleware = (0, express_prom_bundle_1.default)({
|
|
39
|
+
includeMethod: true,
|
|
40
|
+
includePath: true,
|
|
41
|
+
normalizePath: pathsToMetrics,
|
|
42
|
+
metricsPath: "/metrics"
|
|
43
|
+
});
|
|
44
|
+
this.app.use(metricsMiddleware);
|
|
34
45
|
(0, c2_util_1.log)("LOG", "Rotas fechadas OK");
|
|
35
46
|
this.server = this.app.listen(this.config.port, async () => {
|
|
36
47
|
(0, c2_util_1.log)("LOG_INIT_SERVER", `Server running on port ${this.config.port}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "c2-http",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.110",
|
|
4
4
|
"description": "Biblioteca Typescript para API NodeJS",
|
|
5
5
|
"repository": "https://cabralsilva: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@github.com/cabralsilva/c2-http.git",
|
|
6
6
|
"author": "Daniel Cabral <cabralconsultoriaemsoftware@gmail.com>",
|
|
@@ -30,11 +30,13 @@
|
|
|
30
30
|
"cors": "^2.8.5",
|
|
31
31
|
"express": "^4.19.2",
|
|
32
32
|
"express-http-context": "^1.2.4",
|
|
33
|
+
"express-prom-bundle": "^8.0.0",
|
|
33
34
|
"express-rest-i18n": "^1.0.1",
|
|
34
35
|
"http-status": "^1.7.4",
|
|
35
36
|
"jsonwebtoken": "^9.0.0",
|
|
36
37
|
"moment": "^2.30.1",
|
|
37
38
|
"moment-timezone": "^0.6.0",
|
|
39
|
+
"prom-client": "^15.1.3",
|
|
38
40
|
"qs": "^6.13.0",
|
|
39
41
|
"swagger-autogen": "^2.23.7",
|
|
40
42
|
"swagger-ui-express": "^5.0.0",
|