c2-http 0.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.
Files changed (49) hide show
  1. package/README.md +18 -0
  2. package/dist/config/contants.d.ts +2 -0
  3. package/dist/config/contants.js +5 -0
  4. package/dist/config/i18n.d.ts +3 -0
  5. package/dist/config/i18n.js +35 -0
  6. package/dist/controller/AController.d.ts +8 -0
  7. package/dist/controller/AController.js +29 -0
  8. package/dist/controller/CrudController.d.ts +17 -0
  9. package/dist/controller/CrudController.js +61 -0
  10. package/dist/flow/authorization/MiddlewareCheckRolesFlow.d.ts +7 -0
  11. package/dist/flow/authorization/MiddlewareCheckRolesFlow.js +48 -0
  12. package/dist/flow/dispatcher/HttpDownloadDispatchHandlingFlow.d.ts +2 -0
  13. package/dist/flow/dispatcher/HttpDownloadDispatchHandlingFlow.js +23 -0
  14. package/dist/flow/dispatcher/HttpDownloadDispatchSuccessFlow.d.ts +6 -0
  15. package/dist/flow/dispatcher/HttpDownloadDispatchSuccessFlow.js +8 -0
  16. package/dist/flow/dispatcher/HttpJsonDispatchErrorFlow.d.ts +7 -0
  17. package/dist/flow/dispatcher/HttpJsonDispatchErrorFlow.js +9 -0
  18. package/dist/flow/dispatcher/HttpJsonDispatchHandlingFlow.d.ts +2 -0
  19. package/dist/flow/dispatcher/HttpJsonDispatchHandlingFlow.js +22 -0
  20. package/dist/flow/dispatcher/HttpJsonDispatchSuccessFlow.d.ts +6 -0
  21. package/dist/flow/dispatcher/HttpJsonDispatchSuccessFlow.js +8 -0
  22. package/dist/index.d.ts +4 -0
  23. package/dist/index.js +7 -0
  24. package/dist/model/HttpError.d.ts +6 -0
  25. package/dist/model/HttpError.js +11 -0
  26. package/dist/model/TControllerOptions.d.ts +7 -0
  27. package/dist/model/TControllerOptions.js +2 -0
  28. package/dist/model/TI18n.d.ts +3 -0
  29. package/dist/model/TI18n.js +2 -0
  30. package/dist/utils/Utils.d.ts +2 -0
  31. package/dist/utils/Utils.js +31 -0
  32. package/package.json +41 -0
  33. package/src/config/contants.ts +2 -0
  34. package/src/config/i18n.ts +33 -0
  35. package/src/controller/AController.ts +40 -0
  36. package/src/controller/CrudController.ts +57 -0
  37. package/src/flow/authorization/MiddlewareCheckRolesFlow.ts +47 -0
  38. package/src/flow/dispatcher/HttpDownloadDispatchHandlingFlow.ts +21 -0
  39. package/src/flow/dispatcher/HttpDownloadDispatchSuccessFlow.ts +9 -0
  40. package/src/flow/dispatcher/HttpJsonDispatchErrorFlow.ts +11 -0
  41. package/src/flow/dispatcher/HttpJsonDispatchHandlingFlow.ts +20 -0
  42. package/src/flow/dispatcher/HttpJsonDispatchSuccessFlow.ts +9 -0
  43. package/src/index.ts +12 -0
  44. package/src/model/HttpError.ts +12 -0
  45. package/src/model/TControllerOptions.ts +7 -0
  46. package/src/model/TI18n.ts +3 -0
  47. package/src/utils/Utils.ts +34 -0
  48. package/tsconfig.json +46 -0
  49. package/yarn-error.log +630 -0
package/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # c2-mongoose
2
+
3
+ ## Guide to publish
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
+ ```
@@ -0,0 +1,2 @@
1
+ export declare const rolesFullAcess: string[];
2
+ export declare const rolesAdminFullAcess: string[];
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rolesAdminFullAcess = exports.rolesFullAcess = void 0;
4
+ exports.rolesFullAcess = ["full-access"];
5
+ exports.rolesAdminFullAcess = ["admin-full-access"];
@@ -0,0 +1,3 @@
1
+ export declare const i18n: any;
2
+ export declare const getMessage: (message: string, ...parameters: string[]) => string;
3
+ export default i18n;
@@ -0,0 +1,35 @@
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;
@@ -0,0 +1,8 @@
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;
@@ -0,0 +1,29 @@
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;
@@ -0,0 +1,17 @@
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 };
@@ -0,0 +1,61 @@
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;
@@ -0,0 +1,7 @@
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;
@@ -0,0 +1,48 @@
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;
@@ -0,0 +1,2 @@
1
+ declare const HttpDownloadDispatchHandlingFlow: (target: any, methodName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
2
+ export default HttpDownloadDispatchHandlingFlow;
@@ -0,0 +1,23 @@
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;
@@ -0,0 +1,6 @@
1
+ import { Response } from "express";
2
+ declare class HttpDownloadDispatchSuccessFlow {
3
+ dispactch(response: Response, status: number, file?: any): void;
4
+ }
5
+ declare const _default: HttpDownloadDispatchSuccessFlow;
6
+ export default _default;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class HttpDownloadDispatchSuccessFlow {
4
+ dispactch(response, status, file) {
5
+ response.status(status).send(file);
6
+ }
7
+ }
8
+ exports.default = new HttpDownloadDispatchSuccessFlow;
@@ -0,0 +1,7 @@
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;
@@ -0,0 +1,9 @@
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;
@@ -0,0 +1,2 @@
1
+ declare const HttpJsonDispatchHandlingFlow: (target: any, methodName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
2
+ export default HttpJsonDispatchHandlingFlow;
@@ -0,0 +1,22 @@
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;
@@ -0,0 +1,6 @@
1
+ import { Response } from "express";
2
+ declare class HttpJsonDispatchSuccessFlow {
3
+ dispactch(response: Response, status: number, payload?: any): void;
4
+ }
5
+ declare const _default: HttpJsonDispatchSuccessFlow;
6
+ export default _default;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class HttpJsonDispatchSuccessFlow {
4
+ dispactch(response, status, payload) {
5
+ response.status(status).json(payload);
6
+ }
7
+ }
8
+ exports.default = new HttpJsonDispatchSuccessFlow;
@@ -0,0 +1,4 @@
1
+ import { TControllerOptions } from "./model/TControllerOptions";
2
+ import { TI18n } from "./model/TI18n";
3
+ declare const initialize: (i18n: TI18n) => void;
4
+ export { initialize, TControllerOptions };
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.initialize = void 0;
4
+ const initialize = (i18n) => {
5
+ global.i18nMessages = i18n;
6
+ };
7
+ exports.initialize = initialize;
@@ -0,0 +1,6 @@
1
+ declare class HttpError extends Error {
2
+ status: number;
3
+ detail: any;
4
+ constructor(status: number, message: string, detail?: any);
5
+ }
6
+ export default HttpError;
@@ -0,0 +1,11 @@
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;
@@ -0,0 +1,7 @@
1
+ export type TControllerOptions = {
2
+ uri: string;
3
+ basePath: string;
4
+ relativePath: string;
5
+ prefixRole: string;
6
+ baseRoles: string[];
7
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export type TI18n = {
2
+ [key: string]: {};
3
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export declare const isNotEmpty: (obj: any) => boolean;
2
+ export declare const isEmpty: (obj: any) => boolean;
@@ -0,0 +1,31 @@
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/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "c2-http",
3
+ "version": "0.0.1",
4
+ "description": "Lib to make any main request http and use as basic crud",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "watch": "tsc -w --skipLibCheck",
9
+ "build": "tsc --skipLibCheck",
10
+ "preversion": "tsc --skipLibCheck",
11
+ "postversion": "git push && git push --tags && npm publish",
12
+ "publish-patch": "npm version patch",
13
+ "publish-minor": "npm version minor",
14
+ "publish-major": "npm version major"
15
+ },
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
+ "dependencies": {
31
+ "@types/express": "^4.17.17",
32
+ "@types/http-status": "^1.1.2",
33
+ "dotenv": "^16.0.3",
34
+ "express": "^4.18.2",
35
+ "express-http-context": "^1.2.4",
36
+ "http-status": "^1.6.2"
37
+ },
38
+ "devDependencies": {
39
+ "typescript": "^4.9.5"
40
+ }
41
+ }
@@ -0,0 +1,2 @@
1
+ export const rolesFullAcess = ["full-access"]
2
+ export const rolesAdminFullAcess = ["admin-full-access"]
@@ -0,0 +1,33 @@
1
+ import httpContext from "express-http-context"
2
+ import { isNotEmpty } from "../utils/Utils";
3
+ const i18nCreate = require('express-rest-i18n')
4
+
5
+ export const i18n = i18nCreate({
6
+ defaultLocale: 'pt-br',
7
+ warn: false, // optional
8
+ allowFallback: true, // optional
9
+ messages: (global as any).i18nMessages,
10
+ });
11
+
12
+ export const getMessage = (message: string, ...parameters: string[]): string => {
13
+ const headers = httpContext.get('headers') as any
14
+ var language: [] = []
15
+ if (typeof headers !== 'undefined') {
16
+ if (headers["accept-language"] !== undefined) {
17
+ language = headers["accept-language"].split(',')
18
+ }
19
+ }
20
+
21
+ var t = i18n.t(message)
22
+ if (isNotEmpty(language)) {
23
+ t = i18n.t(message, language)
24
+ }
25
+
26
+ for (var i = 0; i < parameters.length; i++) {
27
+ var param = parameters[i]
28
+ t = t.replaceAll(`{${i}}`, param)
29
+ }
30
+ return t
31
+ }
32
+
33
+ export default i18n
@@ -0,0 +1,40 @@
1
+ import { Router } from "express";
2
+ import { rolesFullAcess } from "../config/contants";
3
+ import { TControllerOptions } from "../model/TControllerOptions";
4
+ import { isEmpty } from "../utils/Utils";
5
+
6
+
7
+ abstract class Controller {
8
+
9
+ public routers: Router = Router();
10
+ protected options: Partial<TControllerOptions>
11
+
12
+ constructor(_options: Partial<TControllerOptions>) {
13
+
14
+ this.options = _options
15
+
16
+ if (!this.options?.relativePath?.startsWith("/")) {
17
+ throw new Error("the 'relativePatch' must start with a slash '/'")
18
+ }
19
+
20
+ if (isEmpty(this.options.basePath)) {
21
+ this.options.basePath = `/api/v1`
22
+ }
23
+
24
+ if (isEmpty(this.options.baseRoles)) {
25
+ this.options.baseRoles = [...rolesFullAcess];
26
+ }
27
+
28
+ if (isEmpty(this.options.prefixRole)) {
29
+ let defaultPrefixRole = this.options.relativePath as string
30
+ if (defaultPrefixRole.startsWith("/")) {
31
+ defaultPrefixRole = defaultPrefixRole.replace("/", "")
32
+ }
33
+ this.options.prefixRole = defaultPrefixRole.replaceAll("/", "-")
34
+ }
35
+
36
+ this.options.uri = `${this.options.basePath}${this.options.relativePath}`
37
+ }
38
+ }
39
+
40
+ export default Controller
@@ -0,0 +1,57 @@
1
+ import { Request, Response } from "express";
2
+ import MiddlewareCheckRolesFlow from "../flow/authorization/MiddlewareCheckRolesFlow";
3
+ import HttpJsonDispatchHandlingFlow from "../flow/dispatcher/HttpJsonDispatchHandlingFlow";
4
+ import { TControllerOptions } from "../model/TControllerOptions";
5
+ import Controller from "./AController";
6
+
7
+ abstract class CrudController extends Controller {
8
+
9
+ constructor(options: Partial<TControllerOptions>) {
10
+ super(options)
11
+
12
+ this.searchRunner = this.searchRunner.bind(this);
13
+ this.getByIdRunner = this.getByIdRunner.bind(this);
14
+ this.createRunner = this.createRunner.bind(this);
15
+ this.updateRunner = this.updateRunner.bind(this);
16
+ this.deleteRunner = this.deleteRunner.bind(this);
17
+
18
+ this.routers.get(`${this.options.uri}/:id`, MiddlewareCheckRolesFlow.middleware(...this.options.baseRoles!), this.getByIdRunner)
19
+ this.routers.get(`${this.options.uri}`, MiddlewareCheckRolesFlow.middleware(...this.options.baseRoles!), this.searchRunner)
20
+ this.routers.post(`${this.options.uri}`, MiddlewareCheckRolesFlow.middleware(...this.options.baseRoles!, `${this.options.prefixRole!}:write`), this.createRunner)
21
+ this.routers.patch(`${this.options.uri}/:id`, MiddlewareCheckRolesFlow.middleware(...this.options.baseRoles!, `${this.options.prefixRole}:write`), this.updateRunner)
22
+ this.routers.delete(`${this.options.uri}/:id`, MiddlewareCheckRolesFlow.middleware(...this.options.baseRoles!, `${this.options.prefixRole}:write`), this.deleteRunner)
23
+ }
24
+
25
+ abstract search(request: Request, response: Response): Promise<[number, any]>
26
+ abstract getById(request: Request, response: Response): Promise<[number, any]>
27
+ abstract create(request: Request, response: Response): Promise<[number, any]>
28
+ abstract update(request: Request, response: Response): Promise<[number, any]>
29
+ abstract delete(request: Request, response: Response): Promise<[number, any]>
30
+
31
+ @HttpJsonDispatchHandlingFlow
32
+ async searchRunner(request: Request, response: Response): Promise<[number, any]> {
33
+ return await this.search(request, response)
34
+ }
35
+
36
+ @HttpJsonDispatchHandlingFlow
37
+ async getByIdRunner(request: Request, response: Response): Promise<[number, any]> {
38
+ return await this.getById(request, response)
39
+ }
40
+
41
+ @HttpJsonDispatchHandlingFlow
42
+ async createRunner(request: Request, response: Response): Promise<[number, any]> {
43
+ return await this.create(request, response)
44
+ }
45
+
46
+ @HttpJsonDispatchHandlingFlow
47
+ async updateRunner(request: Request, response: Response): Promise<[number, any]> {
48
+ return await this.update(request, response)
49
+ }
50
+
51
+ @HttpJsonDispatchHandlingFlow
52
+ async deleteRunner(request: Request, response: Response): Promise<[number, any]> {
53
+ return await this.delete(request, response)
54
+ }
55
+ }
56
+
57
+ export { CrudController };