c2-http 1.0.106 → 1.0.109
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,23 @@
|
|
|
1
|
-
import { Router } from "express";
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { RequestHandler, Router } from "express";
|
|
2
|
+
import { NormalizePathEntry } from "express-prom-bundle";
|
|
3
|
+
export interface IRouteParam {
|
|
4
|
+
method: "get" | "post" | "patch" | "delete";
|
|
5
|
+
uri: string;
|
|
6
|
+
middlewareAuthorization?: RequestHandler;
|
|
7
|
+
middlewares?: RequestHandler[];
|
|
8
|
+
handlerFn: RequestHandler;
|
|
9
|
+
}
|
|
4
10
|
export declare abstract class Controller {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
router: Router;
|
|
12
|
+
normalizedPaths: NormalizePathEntry[];
|
|
13
|
+
uri: string;
|
|
14
|
+
basePath: string;
|
|
15
|
+
relativePath: string;
|
|
16
|
+
prefixRole: string[];
|
|
17
|
+
roles: string[];
|
|
18
|
+
byPass: boolean;
|
|
19
|
+
constructor(options: any);
|
|
20
|
+
registerRoute(options: IRouteParam): void;
|
|
9
21
|
}
|
|
10
22
|
export declare function HttpDispatchHandling(target: any, methodName: string, descriptor: PropertyDescriptor): PropertyDescriptor;
|
|
11
23
|
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,96 +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
|
-
middlewareCheckAuthorization: new ControllerRoleMiddleware_1.ControllerRoleMiddleware([`${this.options.prefixRole}:read`], options.middlewareCheckAuthorization),
|
|
66
|
-
controllerFn: this.getByIdRunner
|
|
67
|
-
}));
|
|
68
|
-
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
69
|
-
router: this.routers,
|
|
70
|
-
method: "get",
|
|
71
|
-
uri: `${this.options.uri}`,
|
|
72
|
-
middlewareCheckAuthorization: new ControllerRoleMiddleware_1.ControllerRoleMiddleware([`${this.options.prefixRole}:read`], options.middlewareCheckAuthorization),
|
|
73
|
-
controllerFn: this.searchRunner
|
|
74
|
-
}));
|
|
75
|
-
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
76
|
-
router: this.routers,
|
|
77
|
-
method: "patch",
|
|
78
|
-
uri: `${this.options.uri}`,
|
|
79
|
-
middlewareCheckAuthorization: new ControllerRoleMiddleware_1.ControllerRoleMiddleware([`${this.options.prefixRole}:read`], options.middlewareCheckAuthorization),
|
|
80
|
-
controllerFn: this.deepSearchRunner
|
|
81
|
-
}));
|
|
82
|
-
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
83
|
-
router: this.routers,
|
|
47
|
+
this.registerRoute({
|
|
84
48
|
method: "post",
|
|
85
|
-
uri: `${this.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
})
|
|
89
|
-
this.
|
|
90
|
-
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({
|
|
91
54
|
method: "patch",
|
|
92
|
-
uri: `${this.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
})
|
|
96
|
-
this.
|
|
97
|
-
|
|
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({
|
|
98
72
|
method: "delete",
|
|
99
|
-
uri: `${this.
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
})
|
|
73
|
+
uri: `${this.uri}/:id`,
|
|
74
|
+
handlerFn: this.deleteRunner,
|
|
75
|
+
middlewareAuthorization: () => options.middlewareCheckAuthorization(this.prefixRole.map((pr) => `${pr}:delete`))
|
|
76
|
+
});
|
|
103
77
|
}
|
|
104
78
|
}
|
|
105
79
|
async searchRunner(request, response) {
|
|
106
80
|
return await this.search(request, response);
|
|
107
81
|
}
|
|
108
|
-
async deepSearchRunner(request, response) {
|
|
109
|
-
return await this.deepSearch(request, response);
|
|
110
|
-
}
|
|
111
82
|
async getByIdRunner(request, response) {
|
|
112
83
|
return await this.getById(request, response);
|
|
113
84
|
}
|
|
@@ -124,9 +95,6 @@ class CrudController extends Controller_1.Controller {
|
|
|
124
95
|
__decorate([
|
|
125
96
|
Controller_1.HttpDispatchHandling
|
|
126
97
|
], CrudController.prototype, "searchRunner", null);
|
|
127
|
-
__decorate([
|
|
128
|
-
Controller_1.HttpDispatchHandling
|
|
129
|
-
], CrudController.prototype, "deepSearchRunner", null);
|
|
130
98
|
__decorate([
|
|
131
99
|
Controller_1.HttpDispatchHandling
|
|
132
100
|
], CrudController.prototype, "getByIdRunner", null);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "c2-http",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.109",
|
|
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",
|