c2-http 1.0.41 → 1.0.43
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/dist/index.js +51 -0
- package/dist/model/Controller.d.ts +2 -1
- package/dist/model/Controller.js +4 -56
- package/dist/model/ControllerRoleMiddleware.d.ts +6 -0
- package/dist/model/ControllerRoleMiddleware.js +16 -0
- package/dist/model/ControllerRoute.d.ts +18 -0
- package/dist/model/ControllerRoute.js +25 -0
- package/dist/model/CrudController.js +87 -10
- package/dist/model/ICrudControllerOptions.d.ts +1 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -22,6 +22,57 @@ Object.defineProperty(exports, "CrudController", { enumerable: true, get: functi
|
|
|
22
22
|
const HttpError_1 = require("./model/HttpError");
|
|
23
23
|
const Server_1 = require("./model/Server");
|
|
24
24
|
Object.defineProperty(exports, "Server", { enumerable: true, get: function () { return Server_1.Server; } });
|
|
25
|
+
global.OPEN_API_DOC = {
|
|
26
|
+
"openapi": "3.1.0",
|
|
27
|
+
"info": {
|
|
28
|
+
"version": "1.0.0",
|
|
29
|
+
"title": "REST API",
|
|
30
|
+
"description": ""
|
|
31
|
+
},
|
|
32
|
+
"servers": [
|
|
33
|
+
{
|
|
34
|
+
"url": "http://localhost:3000/"
|
|
35
|
+
}
|
|
36
|
+
],
|
|
37
|
+
"components": {
|
|
38
|
+
"schemas": {
|
|
39
|
+
"info": {
|
|
40
|
+
"type": "object",
|
|
41
|
+
"properties": {
|
|
42
|
+
"title": {
|
|
43
|
+
"type": "string",
|
|
44
|
+
"example": "Sua API"
|
|
45
|
+
},
|
|
46
|
+
"version": {
|
|
47
|
+
"type": "string",
|
|
48
|
+
"example": "1.0.0"
|
|
49
|
+
},
|
|
50
|
+
"description": {
|
|
51
|
+
"type": "string",
|
|
52
|
+
"example": "Descrição da sua API"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"servers": {
|
|
57
|
+
"type": "array",
|
|
58
|
+
"items": {
|
|
59
|
+
"type": "object",
|
|
60
|
+
"properties": {
|
|
61
|
+
"url": {
|
|
62
|
+
"type": "string",
|
|
63
|
+
"example": "http://localhost:3000"
|
|
64
|
+
},
|
|
65
|
+
"description": {
|
|
66
|
+
"type": "string",
|
|
67
|
+
"example": "Servidor local"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
paths: {}
|
|
75
|
+
};
|
|
25
76
|
const convertErrorToHttpError = (error) => {
|
|
26
77
|
if (error instanceof mongodb_1.MongoServerError) {
|
|
27
78
|
if (error.code === 11000) {
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Router } from "express";
|
|
2
2
|
import { IControllerOptions } from "./IControllerOptions";
|
|
3
|
+
import { ControllerRoute } from "./ControllerRoute";
|
|
3
4
|
export declare abstract class Controller {
|
|
4
5
|
routers: Router;
|
|
5
|
-
documentation: any;
|
|
6
6
|
protected options: IControllerOptions;
|
|
7
|
+
routesControlled: ControllerRoute[];
|
|
7
8
|
constructor(_options: Partial<IControllerOptions>);
|
|
8
9
|
}
|
|
9
10
|
export declare function HttpDispatchHandling(target: any, methodName: string, descriptor: PropertyDescriptor): PropertyDescriptor;
|
package/dist/model/Controller.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpenApi = exports.HttpDispatchDownload = exports.HttpDispatchHandling = exports.Controller = void 0;
|
|
4
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
5
4
|
const c2_util_1 = require("c2-util");
|
|
6
5
|
const express_1 = require("express");
|
|
7
6
|
const http_status_1 = require("http-status");
|
|
8
7
|
class Controller {
|
|
9
8
|
routers = (0, express_1.Router)();
|
|
10
|
-
documentation;
|
|
11
9
|
options;
|
|
12
|
-
|
|
13
|
-
// static openApi: any = {};
|
|
10
|
+
routesControlled = [];
|
|
14
11
|
constructor(_options) {
|
|
15
12
|
this.options = _options;
|
|
16
13
|
if (!this?.options?.relativePath?.startsWith("/")) {
|
|
@@ -30,57 +27,6 @@ class Controller {
|
|
|
30
27
|
this.options.prefixRole = defaultPrefixRole.replaceAll("/", "-");
|
|
31
28
|
}
|
|
32
29
|
this.options.uri = `${this.options.basePath}${this.options.relativePath}`;
|
|
33
|
-
global.OPEN_API_DOC = {
|
|
34
|
-
"openapi": "3.1.0",
|
|
35
|
-
"info": {
|
|
36
|
-
"version": "1.0.0",
|
|
37
|
-
"title": "REST API",
|
|
38
|
-
"description": ""
|
|
39
|
-
},
|
|
40
|
-
"servers": [
|
|
41
|
-
{
|
|
42
|
-
"url": "http://localhost:3000/"
|
|
43
|
-
}
|
|
44
|
-
],
|
|
45
|
-
"components": {
|
|
46
|
-
"schemas": {
|
|
47
|
-
"info": {
|
|
48
|
-
"type": "object",
|
|
49
|
-
"properties": {
|
|
50
|
-
"title": {
|
|
51
|
-
"type": "string",
|
|
52
|
-
"example": "Sua API"
|
|
53
|
-
},
|
|
54
|
-
"version": {
|
|
55
|
-
"type": "string",
|
|
56
|
-
"example": "1.0.0"
|
|
57
|
-
},
|
|
58
|
-
"description": {
|
|
59
|
-
"type": "string",
|
|
60
|
-
"example": "Descrição da sua API"
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
"servers": {
|
|
65
|
-
"type": "array",
|
|
66
|
-
"items": {
|
|
67
|
-
"type": "object",
|
|
68
|
-
"properties": {
|
|
69
|
-
"url": {
|
|
70
|
-
"type": "string",
|
|
71
|
-
"example": "http://localhost:3000"
|
|
72
|
-
},
|
|
73
|
-
"description": {
|
|
74
|
-
"type": "string",
|
|
75
|
-
"example": "Servidor local"
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
},
|
|
82
|
-
paths: {}
|
|
83
|
-
};
|
|
84
30
|
}
|
|
85
31
|
}
|
|
86
32
|
exports.Controller = Controller;
|
|
@@ -136,7 +82,9 @@ function OpenApi(target) {
|
|
|
136
82
|
const methodsUsed = this.getMethodsUsed2(route.route.methods);
|
|
137
83
|
const methods = {};
|
|
138
84
|
methodsUsed.forEach((method) => {
|
|
139
|
-
methods[method] = {
|
|
85
|
+
methods[method] = {
|
|
86
|
+
tags: [target.name]
|
|
87
|
+
};
|
|
140
88
|
});
|
|
141
89
|
paths[route.route.path] = {
|
|
142
90
|
...paths[route.route.path],
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ControllerRoleMiddleware = void 0;
|
|
4
|
+
class ControllerRoleMiddleware {
|
|
5
|
+
roles;
|
|
6
|
+
checkRoleFn;
|
|
7
|
+
// run: (request: Request, response: Response, next: NextFunction) => any
|
|
8
|
+
constructor(roles, checkRoleFn) {
|
|
9
|
+
this.roles = roles;
|
|
10
|
+
this.checkRoleFn = checkRoleFn;
|
|
11
|
+
}
|
|
12
|
+
exec() {
|
|
13
|
+
this.checkRoleFn(this.roles);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.ControllerRoleMiddleware = ControllerRoleMiddleware;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Request, Response, Router } from "express";
|
|
2
|
+
import { ControllerRoleMiddleware } from "./ControllerRoleMiddleware";
|
|
3
|
+
export declare class ControllerRoute {
|
|
4
|
+
router: Router;
|
|
5
|
+
method: string;
|
|
6
|
+
uri: string;
|
|
7
|
+
middlewaresRoles: ControllerRoleMiddleware[];
|
|
8
|
+
controllerFn: (request: Request, response: Response) => Promise<any>;
|
|
9
|
+
openApiPath: IOpenApiPath;
|
|
10
|
+
constructor(options: any);
|
|
11
|
+
initialize(): void;
|
|
12
|
+
}
|
|
13
|
+
export interface IOpenApiPath {
|
|
14
|
+
path: string;
|
|
15
|
+
description: string;
|
|
16
|
+
response: any;
|
|
17
|
+
parameters: any;
|
|
18
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ControllerRoute = void 0;
|
|
4
|
+
class ControllerRoute {
|
|
5
|
+
router;
|
|
6
|
+
method;
|
|
7
|
+
uri;
|
|
8
|
+
middlewaresRoles; //(() => void)[];
|
|
9
|
+
controllerFn;
|
|
10
|
+
openApiPath;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.router = options.router;
|
|
13
|
+
this.method = options.method;
|
|
14
|
+
this.uri = options.uri;
|
|
15
|
+
this.middlewaresRoles = options.middlewaresRoles;
|
|
16
|
+
this.controllerFn = options.controllerFn;
|
|
17
|
+
this.openApiPath = options.openApiPath;
|
|
18
|
+
this.initialize();
|
|
19
|
+
}
|
|
20
|
+
initialize() {
|
|
21
|
+
// router.use(this.method, ...this.middlewares, this.controllerFn)
|
|
22
|
+
this.router[this.method](this.uri, this.middlewaresRoles.map((middl) => middl.exec()), this.controllerFn);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.ControllerRoute = ControllerRoute;
|
|
@@ -8,6 +8,8 @@ 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");
|
|
11
13
|
class CrudController extends Controller_1.Controller {
|
|
12
14
|
constructor(options) {
|
|
13
15
|
super(options);
|
|
@@ -17,18 +19,93 @@ class CrudController extends Controller_1.Controller {
|
|
|
17
19
|
this.updateRunner = this.updateRunner.bind(this);
|
|
18
20
|
this.deleteRunner = this.deleteRunner.bind(this);
|
|
19
21
|
if (options.byPass === true || options.middlewareRoles === undefined) {
|
|
20
|
-
this.routers.get(`${this.options.uri}/:id`, this.getByIdRunner)
|
|
21
|
-
this.routers.get(`${this.options.uri}`, this.searchRunner)
|
|
22
|
-
this.routers.post(`${this.options.uri}`, this.createRunner)
|
|
23
|
-
this.routers.patch(`${this.options.uri}/:id`, this.updateRunner)
|
|
24
|
-
this.routers.delete(`${this.options.uri}/:id`, this.deleteRunner)
|
|
22
|
+
// this.routers.get(`${this.options.uri}/:id`, this.getByIdRunner)
|
|
23
|
+
// this.routers.get(`${this.options.uri}`, this.searchRunner)
|
|
24
|
+
// this.routers.post(`${this.options.uri}`, this.createRunner)
|
|
25
|
+
// this.routers.patch(`${this.options.uri}/:id`, this.updateRunner)
|
|
26
|
+
// this.routers.delete(`${this.options.uri}/:id`, this.deleteRunner)
|
|
27
|
+
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
28
|
+
router: this.routers,
|
|
29
|
+
method: "get",
|
|
30
|
+
uri: `${this.options.uri}/:id`,
|
|
31
|
+
controllerFn: this.getByIdRunner
|
|
32
|
+
}));
|
|
33
|
+
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
34
|
+
router: this.routers,
|
|
35
|
+
method: "get",
|
|
36
|
+
uri: `${this.options.uri}`,
|
|
37
|
+
controllerFn: this.searchRunner
|
|
38
|
+
}));
|
|
39
|
+
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
40
|
+
router: this.routers,
|
|
41
|
+
method: "post",
|
|
42
|
+
uri: `${this.options.uri}`,
|
|
43
|
+
controllerFn: this.createRunner
|
|
44
|
+
}));
|
|
45
|
+
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
46
|
+
router: this.routers,
|
|
47
|
+
method: "patch",
|
|
48
|
+
uri: `${this.options.uri}/:id`,
|
|
49
|
+
controllerFn: this.updateRunner
|
|
50
|
+
}));
|
|
51
|
+
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
52
|
+
router: this.routers,
|
|
53
|
+
method: "delete",
|
|
54
|
+
uri: `${this.options.uri}/:id`,
|
|
55
|
+
controllerFn: this.deleteRunner
|
|
56
|
+
}));
|
|
25
57
|
}
|
|
26
58
|
else {
|
|
27
|
-
this.routers.get(`${this.options.uri}/:id`,
|
|
28
|
-
|
|
29
|
-
this.
|
|
30
|
-
this.routers.
|
|
31
|
-
|
|
59
|
+
// this.routers.get(`${this.options.uri}/:id`,
|
|
60
|
+
// options.middlewareRoles([`${this.options.prefixRole}:read`]),
|
|
61
|
+
// this.getByIdRunner)
|
|
62
|
+
// this.routers.get(`${this.options.uri}`,
|
|
63
|
+
// options.middlewareRoles([`${this.options.prefixRole}:read`]),
|
|
64
|
+
// this.searchRunner)
|
|
65
|
+
// this.routers.post(`${this.options.uri}`,
|
|
66
|
+
// options.middlewareRoles([`${this.options.prefixRole}:create`]),
|
|
67
|
+
// this.createRunner)
|
|
68
|
+
// this.routers.patch(`${this.options.uri}/:id`,
|
|
69
|
+
// options.middlewareRoles([`${this.options.prefixRole}:edit`]),
|
|
70
|
+
// this.updateRunner)
|
|
71
|
+
// this.routers.delete(`${this.options.uri}/:id`,
|
|
72
|
+
// options.middlewareRoles([`${this.options.prefixRole}:delete`]),
|
|
73
|
+
// this.deleteRunner)
|
|
74
|
+
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
75
|
+
router: this.routers,
|
|
76
|
+
method: "get",
|
|
77
|
+
uri: `${this.options.uri}/:id`,
|
|
78
|
+
middlewaresRoles: new ControllerRoleMiddleware_1.ControllerRoleMiddleware([`${this.options.prefixRole}:read`], options.middlewareRoles),
|
|
79
|
+
controllerFn: this.getByIdRunner
|
|
80
|
+
}));
|
|
81
|
+
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
82
|
+
router: this.routers,
|
|
83
|
+
method: "get",
|
|
84
|
+
uri: `${this.options.uri}`,
|
|
85
|
+
middlewaresRoles: new ControllerRoleMiddleware_1.ControllerRoleMiddleware([`${this.options.prefixRole}:read`], options.middlewareRoles),
|
|
86
|
+
controllerFn: this.searchRunner
|
|
87
|
+
}));
|
|
88
|
+
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
89
|
+
router: this.routers,
|
|
90
|
+
method: "post",
|
|
91
|
+
uri: `${this.options.uri}`,
|
|
92
|
+
middlewaresRoles: new ControllerRoleMiddleware_1.ControllerRoleMiddleware([`${this.options.prefixRole}:create`], options.middlewareRoles),
|
|
93
|
+
controllerFn: this.createRunner
|
|
94
|
+
}));
|
|
95
|
+
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
96
|
+
router: this.routers,
|
|
97
|
+
method: "patch",
|
|
98
|
+
uri: `${this.options.uri}/:id`,
|
|
99
|
+
middlewaresRoles: new ControllerRoleMiddleware_1.ControllerRoleMiddleware([`${this.options.prefixRole}:edit`], options.middlewareRoles),
|
|
100
|
+
controllerFn: this.updateRunner
|
|
101
|
+
}));
|
|
102
|
+
this.routesControlled.push(new ControllerRoute_1.ControllerRoute({
|
|
103
|
+
router: this.routers,
|
|
104
|
+
method: "delete",
|
|
105
|
+
uri: `${this.options.uri}/:id`,
|
|
106
|
+
middlewaresRoles: new ControllerRoleMiddleware_1.ControllerRoleMiddleware([`${this.options.prefixRole}:delete`], options.middlewareRoles),
|
|
107
|
+
controllerFn: this.deleteRunner
|
|
108
|
+
}));
|
|
32
109
|
}
|
|
33
110
|
}
|
|
34
111
|
async searchRunner(request, response) {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { RequestHandler } from "express";
|
|
2
1
|
import { IControllerOptions } from "./IControllerOptions";
|
|
3
2
|
export interface ICrudControllerOptions extends IControllerOptions {
|
|
4
|
-
middlewareRoles: (roles: string[]) =>
|
|
3
|
+
middlewareRoles: (roles: string[]) => any;
|
|
5
4
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "c2-http",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.43",
|
|
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>",
|