c2-http 1.0.42 → 1.0.44

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,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;
@@ -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
- // Propriedade estática compartilhada por todas as instâncias
13
- // static openApi: any = {};
10
+ routesControlled = [];
14
11
  constructor(_options) {
15
12
  this.options = _options;
16
13
  if (!this?.options?.relativePath?.startsWith("/")) {
@@ -85,7 +82,9 @@ function OpenApi(target) {
85
82
  const methodsUsed = this.getMethodsUsed2(route.route.methods);
86
83
  const methods = {};
87
84
  methodsUsed.forEach((method) => {
88
- methods[method] = {};
85
+ methods[method] = {
86
+ tags: [target.name]
87
+ };
89
88
  });
90
89
  paths[route.route.path] = {
91
90
  ...paths[route.route.path],
@@ -0,0 +1,6 @@
1
+ export declare class ControllerRoleMiddleware {
2
+ roles: string[];
3
+ checkRoleFn: (roles: string[]) => any;
4
+ constructor(roles: string[], checkRoleFn: (roles: string[]) => any);
5
+ exec(): void;
6
+ }
@@ -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.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`, options.middlewareRoles([`${this.options.prefixRole}:read`]), this.getByIdRunner);
28
- this.routers.get(`${this.options.uri}`, options.middlewareRoles([`${this.options.prefixRole}:read`]), this.searchRunner);
29
- this.routers.post(`${this.options.uri}`, options.middlewareRoles([`${this.options.prefixRole}:create`]), this.createRunner);
30
- this.routers.patch(`${this.options.uri}/:id`, options.middlewareRoles([`${this.options.prefixRole}:edit`]), this.updateRunner);
31
- this.routers.delete(`${this.options.uri}/:id`, options.middlewareRoles([`${this.options.prefixRole}:delete`]), this.deleteRunner);
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[]) => RequestHandler<any>;
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.42",
3
+ "version": "1.0.44",
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>",