c2-http 0.0.1 → 1.0.1
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/README.md +4 -17
- package/dist/Controller.d.ts +17 -0
- package/dist/Controller.js +73 -0
- package/package.json +21 -25
- package/dist/config/contants.d.ts +0 -2
- package/dist/config/contants.js +0 -5
- package/dist/config/i18n.d.ts +0 -3
- package/dist/config/i18n.js +0 -35
- package/dist/controller/AController.d.ts +0 -8
- package/dist/controller/AController.js +0 -29
- package/dist/controller/CrudController.d.ts +0 -17
- package/dist/controller/CrudController.js +0 -61
- package/dist/flow/authorization/MiddlewareCheckRolesFlow.d.ts +0 -7
- package/dist/flow/authorization/MiddlewareCheckRolesFlow.js +0 -48
- package/dist/flow/dispatcher/HttpDownloadDispatchHandlingFlow.d.ts +0 -2
- package/dist/flow/dispatcher/HttpDownloadDispatchHandlingFlow.js +0 -23
- package/dist/flow/dispatcher/HttpDownloadDispatchSuccessFlow.d.ts +0 -6
- package/dist/flow/dispatcher/HttpDownloadDispatchSuccessFlow.js +0 -8
- package/dist/flow/dispatcher/HttpJsonDispatchErrorFlow.d.ts +0 -7
- package/dist/flow/dispatcher/HttpJsonDispatchErrorFlow.js +0 -9
- package/dist/flow/dispatcher/HttpJsonDispatchHandlingFlow.d.ts +0 -2
- package/dist/flow/dispatcher/HttpJsonDispatchHandlingFlow.js +0 -22
- package/dist/flow/dispatcher/HttpJsonDispatchSuccessFlow.d.ts +0 -6
- package/dist/flow/dispatcher/HttpJsonDispatchSuccessFlow.js +0 -8
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -7
- package/dist/model/HttpError.d.ts +0 -6
- package/dist/model/HttpError.js +0 -11
- package/dist/model/TControllerOptions.d.ts +0 -7
- package/dist/model/TControllerOptions.js +0 -2
- package/dist/model/TI18n.d.ts +0 -3
- package/dist/model/TI18n.js +0 -2
- package/dist/utils/Utils.d.ts +0 -2
- package/dist/utils/Utils.js +0 -31
- package/src/config/contants.ts +0 -2
- package/src/config/i18n.ts +0 -33
- package/src/controller/AController.ts +0 -40
- package/src/controller/CrudController.ts +0 -57
- package/src/flow/authorization/MiddlewareCheckRolesFlow.ts +0 -47
- package/src/flow/dispatcher/HttpDownloadDispatchHandlingFlow.ts +0 -21
- package/src/flow/dispatcher/HttpDownloadDispatchSuccessFlow.ts +0 -9
- package/src/flow/dispatcher/HttpJsonDispatchErrorFlow.ts +0 -11
- package/src/flow/dispatcher/HttpJsonDispatchHandlingFlow.ts +0 -20
- package/src/flow/dispatcher/HttpJsonDispatchSuccessFlow.ts +0 -9
- package/src/index.ts +0 -12
- package/src/model/HttpError.ts +0 -12
- package/src/model/TControllerOptions.ts +0 -7
- package/src/model/TI18n.ts +0 -3
- package/src/utils/Utils.ts +0 -34
- package/tsconfig.json +0 -46
- package/yarn-error.log +0 -630
package/README.md
CHANGED
|
@@ -1,18 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Biblioteca de HTTP
|
|
2
|
+
Biblioteca com configurações HTTP para APIs
|
|
2
3
|
|
|
3
|
-
##
|
|
4
|
-
|
|
5
|
-
Commit all changes with:
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
git add .
|
|
9
|
-
git commit -m 'your changes details'
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
Running one of below, according with your changes:
|
|
13
|
-
|
|
14
|
-
```
|
|
15
|
-
npm run publish-patch --force -m "Message to tag publish" // to a new patch
|
|
16
|
-
npm run publish-minor --force -m "Message to tag publish" // to a new minor
|
|
17
|
-
npm run publish-major --force -m "Message to tag publish" // to a new major
|
|
18
|
-
```
|
|
4
|
+
## Pré-requisitos
|
|
5
|
+
Utilizar o express na API
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Router } from "express";
|
|
2
|
+
interface ControllerOptions {
|
|
3
|
+
uri: string;
|
|
4
|
+
basePath: string;
|
|
5
|
+
relativePath: string;
|
|
6
|
+
prefixRole: string;
|
|
7
|
+
roles: string[];
|
|
8
|
+
byPass: boolean;
|
|
9
|
+
}
|
|
10
|
+
declare abstract class Controller {
|
|
11
|
+
routers: Router;
|
|
12
|
+
protected options: Partial<ControllerOptions>;
|
|
13
|
+
constructor(_options: Partial<ControllerOptions>);
|
|
14
|
+
}
|
|
15
|
+
declare function HttpDispatchHandling(target: any, methodName: string, descriptor: PropertyDescriptor): PropertyDescriptor;
|
|
16
|
+
declare function HttpDispatchDownload(target: any, methodName: string, descriptor: PropertyDescriptor): PropertyDescriptor;
|
|
17
|
+
export { Controller, ControllerOptions, HttpDispatchDownload, HttpDispatchHandling };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpDispatchHandling = exports.HttpDispatchDownload = exports.Controller = void 0;
|
|
4
|
+
const c2_util_1 = require("c2-util");
|
|
5
|
+
const express_1 = require("express");
|
|
6
|
+
// import { PAYMENT_REQUIRED } from "http-status";
|
|
7
|
+
// import ValidateSignatureFlow from "../flow/signature/ValidateSignatureFlow";
|
|
8
|
+
const http_status_1 = require("http-status");
|
|
9
|
+
class Controller {
|
|
10
|
+
routers = (0, express_1.Router)();
|
|
11
|
+
options;
|
|
12
|
+
constructor(_options) {
|
|
13
|
+
this.options = _options;
|
|
14
|
+
if (!this?.options?.relativePath?.startsWith("/")) {
|
|
15
|
+
throw new Error("the 'relativePatch' must start with a slash '/'");
|
|
16
|
+
}
|
|
17
|
+
if ((0, c2_util_1.isEmpty)(this.options.basePath)) {
|
|
18
|
+
this.options.basePath = "/api/v1";
|
|
19
|
+
}
|
|
20
|
+
if ((0, c2_util_1.isEmpty)(this.options.roles)) {
|
|
21
|
+
this.options.roles = [];
|
|
22
|
+
}
|
|
23
|
+
if ((0, c2_util_1.isEmpty)(this.options.prefixRole)) {
|
|
24
|
+
let defaultPrefixRole = this.options.relativePath;
|
|
25
|
+
if (defaultPrefixRole.startsWith("/")) {
|
|
26
|
+
defaultPrefixRole = defaultPrefixRole.replace("/", "");
|
|
27
|
+
}
|
|
28
|
+
this.options.prefixRole = defaultPrefixRole.replaceAll("/", "-");
|
|
29
|
+
}
|
|
30
|
+
this.options.uri = `${this.options.basePath}${this.options.relativePath}`;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.Controller = Controller;
|
|
34
|
+
function HttpDispatchHandling(target, methodName, descriptor) {
|
|
35
|
+
const originalMethod = descriptor.value;
|
|
36
|
+
descriptor.value = async function (...args) {
|
|
37
|
+
const response = args[1];
|
|
38
|
+
try {
|
|
39
|
+
const returnHttp = await originalMethod.apply(this, args);
|
|
40
|
+
// const signatureValidation = await ValidateSignatureFlow.validate();
|
|
41
|
+
// if (signatureValidation.expired) {
|
|
42
|
+
// returnHttp[0] = PAYMENT_REQUIRED;
|
|
43
|
+
// }
|
|
44
|
+
return response.status(returnHttp[0]).json(returnHttp[1]);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
console.error(error);
|
|
48
|
+
response
|
|
49
|
+
.status(error.status || http_status_1.INTERNAL_SERVER_ERROR)
|
|
50
|
+
.json({ message: error.message, detail: error.detail?.data });
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
return descriptor;
|
|
54
|
+
}
|
|
55
|
+
exports.HttpDispatchHandling = HttpDispatchHandling;
|
|
56
|
+
function HttpDispatchDownload(target, methodName, descriptor) {
|
|
57
|
+
const originalMethod = descriptor.value;
|
|
58
|
+
descriptor.value = async function (...args) {
|
|
59
|
+
const [response] = args;
|
|
60
|
+
try {
|
|
61
|
+
const [status, data] = await originalMethod.apply(this, args);
|
|
62
|
+
return response.status(status).send(data);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
console.error(error);
|
|
66
|
+
response
|
|
67
|
+
.status(error.status || http_status_1.INTERNAL_SERVER_ERROR)
|
|
68
|
+
.json({ message: error.message, detail: error.detail?.data });
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
return descriptor;
|
|
72
|
+
}
|
|
73
|
+
exports.HttpDispatchDownload = HttpDispatchDownload;
|
package/package.json
CHANGED
|
@@ -1,41 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "c2-http",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Biblioteca Typescript para API NodeJS",
|
|
5
|
+
"repository": "https://cabralsilva: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@github.com/cabralsilva/c2-http.git",
|
|
6
|
+
"author": "Daniel Cabral <cabralconsultoriaemsoftware@gmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
5
8
|
"main": "dist/index.js",
|
|
6
9
|
"types": "dist/index.d.ts",
|
|
7
10
|
"scripts": {
|
|
8
|
-
"
|
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
12
|
+
"lint": "eslint ./src/*",
|
|
13
|
+
"lint:fix": "eslint ./src/* --fix",
|
|
9
14
|
"build": "tsc --skipLibCheck",
|
|
10
|
-
"preversion": "tsc
|
|
15
|
+
"preversion": "tsc --skipLibCheck",
|
|
11
16
|
"postversion": "git push && git push --tags && npm publish",
|
|
12
|
-
"publish-patch": "npm version patch",
|
|
17
|
+
"publish-patch": "npm version patch --force",
|
|
13
18
|
"publish-minor": "npm version minor",
|
|
14
19
|
"publish-major": "npm version major"
|
|
15
20
|
},
|
|
16
|
-
"repository": {
|
|
17
|
-
"type": "git",
|
|
18
|
-
"url": "git+https://gitlab.com/cabral_consultoria/c2-http.git"
|
|
19
|
-
},
|
|
20
|
-
"keywords": [
|
|
21
|
-
"http",
|
|
22
|
-
"crud"
|
|
23
|
-
],
|
|
24
|
-
"author": "Cabral Consultoria e Assessoria em Software",
|
|
25
|
-
"license": "MIT",
|
|
26
|
-
"bugs": {
|
|
27
|
-
"url": "https://gitlab.com/cabral_consultoria/c2-http/issues"
|
|
28
|
-
},
|
|
29
|
-
"homepage": "https://gitlab.com/cabral_consultoria/c2-http#readme",
|
|
30
21
|
"dependencies": {
|
|
31
|
-
"@types/express": "^4.17.
|
|
22
|
+
"@types/express": "^4.17.21",
|
|
32
23
|
"@types/http-status": "^1.1.2",
|
|
33
|
-
"
|
|
34
|
-
"express": "^4.
|
|
35
|
-
"
|
|
36
|
-
"
|
|
24
|
+
"c2-util": "^1.0.13",
|
|
25
|
+
"express": "^4.19.2",
|
|
26
|
+
"http-status": "^1.7.4",
|
|
27
|
+
"ts-node": "^10.8.1",
|
|
28
|
+
"typescript": "^4.7.4"
|
|
37
29
|
},
|
|
38
30
|
"devDependencies": {
|
|
39
|
-
"
|
|
31
|
+
"@types/node": "^20.12.11",
|
|
32
|
+
"@typescript-eslint/eslint-plugin": "^7.8.0",
|
|
33
|
+
"@typescript-eslint/parser": "^7.8.0",
|
|
34
|
+
"eslint": "^9.2.0",
|
|
35
|
+
"typescript-eslint": "^7.8.0"
|
|
40
36
|
}
|
|
41
37
|
}
|
package/dist/config/contants.js
DELETED
package/dist/config/i18n.d.ts
DELETED
package/dist/config/i18n.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getMessage = exports.i18n = void 0;
|
|
7
|
-
const express_http_context_1 = __importDefault(require("express-http-context"));
|
|
8
|
-
const Utils_1 = require("../utils/Utils");
|
|
9
|
-
const i18nCreate = require('express-rest-i18n');
|
|
10
|
-
exports.i18n = i18nCreate({
|
|
11
|
-
defaultLocale: 'pt-br',
|
|
12
|
-
warn: false,
|
|
13
|
-
allowFallback: true,
|
|
14
|
-
messages: global.i18nMessages,
|
|
15
|
-
});
|
|
16
|
-
const getMessage = (message, ...parameters) => {
|
|
17
|
-
const headers = express_http_context_1.default.get('headers');
|
|
18
|
-
var language = [];
|
|
19
|
-
if (typeof headers !== 'undefined') {
|
|
20
|
-
if (headers["accept-language"] !== undefined) {
|
|
21
|
-
language = headers["accept-language"].split(',');
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
var t = exports.i18n.t(message);
|
|
25
|
-
if ((0, Utils_1.isNotEmpty)(language)) {
|
|
26
|
-
t = exports.i18n.t(message, language);
|
|
27
|
-
}
|
|
28
|
-
for (var i = 0; i < parameters.length; i++) {
|
|
29
|
-
var param = parameters[i];
|
|
30
|
-
t = t.replaceAll(`{${i}}`, param);
|
|
31
|
-
}
|
|
32
|
-
return t;
|
|
33
|
-
};
|
|
34
|
-
exports.getMessage = getMessage;
|
|
35
|
-
exports.default = exports.i18n;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Router } from "express";
|
|
2
|
-
import { TControllerOptions } from "../model/TControllerOptions";
|
|
3
|
-
declare abstract class Controller {
|
|
4
|
-
routers: Router;
|
|
5
|
-
protected options: Partial<TControllerOptions>;
|
|
6
|
-
constructor(_options: Partial<TControllerOptions>);
|
|
7
|
-
}
|
|
8
|
-
export default Controller;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const express_1 = require("express");
|
|
4
|
-
const contants_1 = require("../config/contants");
|
|
5
|
-
const Utils_1 = require("../utils/Utils");
|
|
6
|
-
class Controller {
|
|
7
|
-
constructor(_options) {
|
|
8
|
-
this.routers = (0, express_1.Router)();
|
|
9
|
-
this.options = _options;
|
|
10
|
-
if (!this.options?.relativePath?.startsWith("/")) {
|
|
11
|
-
throw new Error("the 'relativePatch' must start with a slash '/'");
|
|
12
|
-
}
|
|
13
|
-
if ((0, Utils_1.isEmpty)(this.options.basePath)) {
|
|
14
|
-
this.options.basePath = `/api/v1`;
|
|
15
|
-
}
|
|
16
|
-
if ((0, Utils_1.isEmpty)(this.options.baseRoles)) {
|
|
17
|
-
this.options.baseRoles = [...contants_1.rolesFullAcess];
|
|
18
|
-
}
|
|
19
|
-
if ((0, Utils_1.isEmpty)(this.options.prefixRole)) {
|
|
20
|
-
let defaultPrefixRole = this.options.relativePath;
|
|
21
|
-
if (defaultPrefixRole.startsWith("/")) {
|
|
22
|
-
defaultPrefixRole = defaultPrefixRole.replace("/", "");
|
|
23
|
-
}
|
|
24
|
-
this.options.prefixRole = defaultPrefixRole.replaceAll("/", "-");
|
|
25
|
-
}
|
|
26
|
-
this.options.uri = `${this.options.basePath}${this.options.relativePath}`;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
exports.default = Controller;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { Request, Response } from "express";
|
|
2
|
-
import { TControllerOptions } from "../model/TControllerOptions";
|
|
3
|
-
import Controller from "./AController";
|
|
4
|
-
declare abstract class CrudController extends Controller {
|
|
5
|
-
constructor(options: Partial<TControllerOptions>);
|
|
6
|
-
abstract search(request: Request, response: Response): Promise<[number, any]>;
|
|
7
|
-
abstract getById(request: Request, response: Response): Promise<[number, any]>;
|
|
8
|
-
abstract create(request: Request, response: Response): Promise<[number, any]>;
|
|
9
|
-
abstract update(request: Request, response: Response): Promise<[number, any]>;
|
|
10
|
-
abstract delete(request: Request, response: Response): Promise<[number, any]>;
|
|
11
|
-
searchRunner(request: Request, response: Response): Promise<[number, any]>;
|
|
12
|
-
getByIdRunner(request: Request, response: Response): Promise<[number, any]>;
|
|
13
|
-
createRunner(request: Request, response: Response): Promise<[number, any]>;
|
|
14
|
-
updateRunner(request: Request, response: Response): Promise<[number, any]>;
|
|
15
|
-
deleteRunner(request: Request, response: Response): Promise<[number, any]>;
|
|
16
|
-
}
|
|
17
|
-
export { CrudController };
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.CrudController = void 0;
|
|
13
|
-
const MiddlewareCheckRolesFlow_1 = __importDefault(require("../flow/authorization/MiddlewareCheckRolesFlow"));
|
|
14
|
-
const HttpJsonDispatchHandlingFlow_1 = __importDefault(require("../flow/dispatcher/HttpJsonDispatchHandlingFlow"));
|
|
15
|
-
const AController_1 = __importDefault(require("./AController"));
|
|
16
|
-
class CrudController extends AController_1.default {
|
|
17
|
-
constructor(options) {
|
|
18
|
-
super(options);
|
|
19
|
-
this.searchRunner = this.searchRunner.bind(this);
|
|
20
|
-
this.getByIdRunner = this.getByIdRunner.bind(this);
|
|
21
|
-
this.createRunner = this.createRunner.bind(this);
|
|
22
|
-
this.updateRunner = this.updateRunner.bind(this);
|
|
23
|
-
this.deleteRunner = this.deleteRunner.bind(this);
|
|
24
|
-
this.routers.get(`${this.options.uri}/:id`, MiddlewareCheckRolesFlow_1.default.middleware(...this.options.baseRoles), this.getByIdRunner);
|
|
25
|
-
this.routers.get(`${this.options.uri}`, MiddlewareCheckRolesFlow_1.default.middleware(...this.options.baseRoles), this.searchRunner);
|
|
26
|
-
this.routers.post(`${this.options.uri}`, MiddlewareCheckRolesFlow_1.default.middleware(...this.options.baseRoles, `${this.options.prefixRole}:write`), this.createRunner);
|
|
27
|
-
this.routers.patch(`${this.options.uri}/:id`, MiddlewareCheckRolesFlow_1.default.middleware(...this.options.baseRoles, `${this.options.prefixRole}:write`), this.updateRunner);
|
|
28
|
-
this.routers.delete(`${this.options.uri}/:id`, MiddlewareCheckRolesFlow_1.default.middleware(...this.options.baseRoles, `${this.options.prefixRole}:write`), this.deleteRunner);
|
|
29
|
-
}
|
|
30
|
-
async searchRunner(request, response) {
|
|
31
|
-
return await this.search(request, response);
|
|
32
|
-
}
|
|
33
|
-
async getByIdRunner(request, response) {
|
|
34
|
-
return await this.getById(request, response);
|
|
35
|
-
}
|
|
36
|
-
async createRunner(request, response) {
|
|
37
|
-
return await this.create(request, response);
|
|
38
|
-
}
|
|
39
|
-
async updateRunner(request, response) {
|
|
40
|
-
return await this.update(request, response);
|
|
41
|
-
}
|
|
42
|
-
async deleteRunner(request, response) {
|
|
43
|
-
return await this.delete(request, response);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
__decorate([
|
|
47
|
-
HttpJsonDispatchHandlingFlow_1.default
|
|
48
|
-
], CrudController.prototype, "searchRunner", null);
|
|
49
|
-
__decorate([
|
|
50
|
-
HttpJsonDispatchHandlingFlow_1.default
|
|
51
|
-
], CrudController.prototype, "getByIdRunner", null);
|
|
52
|
-
__decorate([
|
|
53
|
-
HttpJsonDispatchHandlingFlow_1.default
|
|
54
|
-
], CrudController.prototype, "createRunner", null);
|
|
55
|
-
__decorate([
|
|
56
|
-
HttpJsonDispatchHandlingFlow_1.default
|
|
57
|
-
], CrudController.prototype, "updateRunner", null);
|
|
58
|
-
__decorate([
|
|
59
|
-
HttpJsonDispatchHandlingFlow_1.default
|
|
60
|
-
], CrudController.prototype, "deleteRunner", null);
|
|
61
|
-
exports.CrudController = CrudController;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { NextFunction, Request, Response } from "express";
|
|
2
|
-
declare class MiddlewareCheckRolesFlow {
|
|
3
|
-
middleware: (...roles: string[]) => (request: Request, response: Response, next: NextFunction) => void;
|
|
4
|
-
mustRoles: (roles: string[], request: Request, response: Response, next: NextFunction) => void;
|
|
5
|
-
}
|
|
6
|
-
declare const _default: MiddlewareCheckRolesFlow;
|
|
7
|
-
export default _default;
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const express_http_context_1 = __importDefault(require("express-http-context"));
|
|
7
|
-
const http_status_1 = require("http-status");
|
|
8
|
-
const contants_1 = require("../../config/contants");
|
|
9
|
-
const i18n_1 = require("../../config/i18n");
|
|
10
|
-
const HttpError_1 = __importDefault(require("../../model/HttpError"));
|
|
11
|
-
const Utils_1 = require("../../utils/Utils");
|
|
12
|
-
const HttpJsonDispatchErrorFlow_1 = __importDefault(require("../dispatcher/HttpJsonDispatchErrorFlow"));
|
|
13
|
-
class MiddlewareCheckRolesFlow {
|
|
14
|
-
constructor() {
|
|
15
|
-
this.middleware = (...roles) => {
|
|
16
|
-
return (request, response, next) => {
|
|
17
|
-
try {
|
|
18
|
-
this.mustRoles([...contants_1.rolesFullAcess, ...roles], request, response, next);
|
|
19
|
-
next();
|
|
20
|
-
}
|
|
21
|
-
catch (error) {
|
|
22
|
-
HttpJsonDispatchErrorFlow_1.default.dispatch(response, error);
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
this.mustRoles = (roles, request, response, next) => {
|
|
27
|
-
if ((0, Utils_1.isEmpty)(roles)) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
try {
|
|
31
|
-
const userContext = express_http_context_1.default.get("user");
|
|
32
|
-
if (typeof userContext !== 'undefined') {
|
|
33
|
-
if ((0, Utils_1.isNotEmpty)(userContext.roles)) {
|
|
34
|
-
const isAllowed = userContext.roles.some((role) => roles.includes(role));
|
|
35
|
-
if (isAllowed) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
42
|
-
throw new HttpError_1.default(http_status_1.UNAUTHORIZED, (0, i18n_1.getMessage)("message.accessNotAllowed"));
|
|
43
|
-
}
|
|
44
|
-
throw new HttpError_1.default(http_status_1.UNAUTHORIZED, (0, i18n_1.getMessage)("message.accessNotAllowed"));
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
exports.default = new MiddlewareCheckRolesFlow;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const HttpDownloadDispatchSuccessFlow_1 = __importDefault(require("./HttpDownloadDispatchSuccessFlow"));
|
|
7
|
-
const HttpJSONDispatchErrorFlow_1 = __importDefault(require("./HttpJSONDispatchErrorFlow"));
|
|
8
|
-
const HttpDownloadDispatchHandlingFlow = (target, methodName, descriptor) => {
|
|
9
|
-
const originalMethod = descriptor.value;
|
|
10
|
-
descriptor.value = async (...args) => {
|
|
11
|
-
let [request, response, next] = args;
|
|
12
|
-
try {
|
|
13
|
-
let [status, data] = await originalMethod.apply(this, args);
|
|
14
|
-
return HttpDownloadDispatchSuccessFlow_1.default.dispactch(response, status, data);
|
|
15
|
-
}
|
|
16
|
-
catch (error) {
|
|
17
|
-
console.error(error);
|
|
18
|
-
HttpJSONDispatchErrorFlow_1.default.dispatch(response, error);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
return descriptor;
|
|
22
|
-
};
|
|
23
|
-
exports.default = HttpDownloadDispatchHandlingFlow;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { Response } from "express";
|
|
2
|
-
import HttpError from "../../model/HttpError";
|
|
3
|
-
declare class HttpJsonDispatchErrorFlow {
|
|
4
|
-
dispatch(response: Response, error: HttpError): void;
|
|
5
|
-
}
|
|
6
|
-
declare const _default: HttpJsonDispatchErrorFlow;
|
|
7
|
-
export default _default;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const http_status_1 = require("http-status");
|
|
4
|
-
class HttpJsonDispatchErrorFlow {
|
|
5
|
-
dispatch(response, error) {
|
|
6
|
-
response.status(error.status || http_status_1.INTERNAL_SERVER_ERROR).json({ message: error.message, detail: error.detail });
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
exports.default = new HttpJsonDispatchErrorFlow;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const HttpJsonDispatchErrorFlow_1 = __importDefault(require("./HttpJsonDispatchErrorFlow"));
|
|
7
|
-
const HttpJsonDispatchSuccessFlow_1 = __importDefault(require("./HttpJsonDispatchSuccessFlow"));
|
|
8
|
-
const HttpJsonDispatchHandlingFlow = (target, methodName, descriptor) => {
|
|
9
|
-
const originalMethod = descriptor.value;
|
|
10
|
-
descriptor.value = async (...args) => {
|
|
11
|
-
let [request, response, next] = args;
|
|
12
|
-
try {
|
|
13
|
-
let [status, data] = await originalMethod.apply(this, args);
|
|
14
|
-
return HttpJsonDispatchSuccessFlow_1.default.dispactch(response, status, data);
|
|
15
|
-
}
|
|
16
|
-
catch (error) {
|
|
17
|
-
HttpJsonDispatchErrorFlow_1.default.dispatch(response, error);
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
return descriptor;
|
|
21
|
-
};
|
|
22
|
-
exports.default = HttpJsonDispatchHandlingFlow;
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
package/dist/model/HttpError.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
class HttpError extends Error {
|
|
4
|
-
constructor(status, message, detail = undefined) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.status = status;
|
|
7
|
-
this.detail = detail;
|
|
8
|
-
Object.setPrototypeOf(this, HttpError.prototype);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
exports.default = HttpError;
|
package/dist/model/TI18n.d.ts
DELETED
package/dist/model/TI18n.js
DELETED
package/dist/utils/Utils.d.ts
DELETED
package/dist/utils/Utils.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isEmpty = exports.isNotEmpty = void 0;
|
|
4
|
-
const isNotEmpty = (obj) => {
|
|
5
|
-
return !(0, exports.isEmpty)(obj);
|
|
6
|
-
};
|
|
7
|
-
exports.isNotEmpty = isNotEmpty;
|
|
8
|
-
const isEmpty = (obj) => {
|
|
9
|
-
if (typeof obj === 'undefined') {
|
|
10
|
-
return true;
|
|
11
|
-
}
|
|
12
|
-
if (typeof obj === 'number') {
|
|
13
|
-
if (isNaN(obj)) {
|
|
14
|
-
return true;
|
|
15
|
-
}
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
if (!obj) {
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
21
|
-
if (Array.isArray(obj)) {
|
|
22
|
-
if (obj.length == 0) {
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
if (obj === "") {
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
return false;
|
|
30
|
-
};
|
|
31
|
-
exports.isEmpty = isEmpty;
|
package/src/config/contants.ts
DELETED