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
@@ -0,0 +1,47 @@
1
+ import { NextFunction, Request, Response } from "express"
2
+ import httpContext from "express-http-context"
3
+ import { UNAUTHORIZED } from "http-status"
4
+ import { rolesFullAcess } from "../../config/contants"
5
+ import { getMessage } from "../../config/i18n"
6
+ import HttpError from "../../model/HttpError"
7
+ import { isEmpty, isNotEmpty } from "../../utils/Utils"
8
+ import HttpJsonDispatchErrorFlow from "../dispatcher/HttpJsonDispatchErrorFlow"
9
+
10
+ class MiddlewareCheckRolesFlow {
11
+ public middleware = (...roles: string[]) => {
12
+ return (request: Request, response: Response, next: NextFunction) => {
13
+
14
+ try {
15
+ this.mustRoles([...rolesFullAcess, ...roles], request, response, next)
16
+ next()
17
+ } catch (error: any) {
18
+ HttpJsonDispatchErrorFlow.dispatch(response, error)
19
+ }
20
+ }
21
+ }
22
+
23
+
24
+
25
+ public mustRoles = (roles: string[], request: Request, response: Response, next: NextFunction) => {
26
+ if (isEmpty(roles)) {
27
+ return
28
+ }
29
+
30
+ try {
31
+ const userContext = httpContext.get("user")
32
+ if (typeof userContext !== 'undefined') {
33
+ if (isNotEmpty(userContext.roles)) {
34
+ const isAllowed = userContext.roles.some((role: string) => roles.includes(role));
35
+ if (isAllowed) {
36
+ return
37
+ }
38
+ }
39
+ }
40
+ } catch (error: any) {
41
+ throw new HttpError(UNAUTHORIZED, getMessage("message.accessNotAllowed"))
42
+ }
43
+ throw new HttpError(UNAUTHORIZED, getMessage("message.accessNotAllowed"))
44
+ }
45
+ }
46
+
47
+ export default new MiddlewareCheckRolesFlow
@@ -0,0 +1,21 @@
1
+ import HttpDownloadDispatchSuccessFlow from "./HttpDownloadDispatchSuccessFlow";
2
+ import HttpJsonDispatchErrorFlow from "./HttpJSONDispatchErrorFlow";
3
+
4
+ const HttpDownloadDispatchHandlingFlow = (target: any, methodName: string, descriptor: PropertyDescriptor) => {
5
+ const originalMethod = descriptor.value;
6
+
7
+ descriptor.value = async (...args: any[]) => {
8
+ let [request, response, next] = args
9
+ try {
10
+ let [status, data] = await originalMethod.apply(this, args);
11
+ return HttpDownloadDispatchSuccessFlow.dispactch(response, status, data);
12
+ } catch (error: any) {
13
+ console.error(error)
14
+ HttpJsonDispatchErrorFlow.dispatch(response, error)
15
+ }
16
+ };
17
+
18
+ return descriptor;
19
+ }
20
+
21
+ export default HttpDownloadDispatchHandlingFlow
@@ -0,0 +1,9 @@
1
+ import { Response } from "express";
2
+
3
+ class HttpDownloadDispatchSuccessFlow {
4
+ dispactch(response: Response, status: number, file?: any) {
5
+ response.status(status).send(file)
6
+ }
7
+ }
8
+
9
+ export default new HttpDownloadDispatchSuccessFlow
@@ -0,0 +1,11 @@
1
+ import { Response } from "express";
2
+ import { INTERNAL_SERVER_ERROR } from "http-status";
3
+ import HttpError from "../../model/HttpError";
4
+
5
+ class HttpJsonDispatchErrorFlow {
6
+ dispatch(response: Response, error: HttpError) {
7
+ response.status(error.status || INTERNAL_SERVER_ERROR).json({ message: error.message, detail: error.detail })
8
+ }
9
+ }
10
+
11
+ export default new HttpJsonDispatchErrorFlow
@@ -0,0 +1,20 @@
1
+ import HttpJsonDispatchErrorFlow from "./HttpJsonDispatchErrorFlow";
2
+ import HttpJsonDispatchSuccessFlow from "./HttpJsonDispatchSuccessFlow";
3
+
4
+ const HttpJsonDispatchHandlingFlow = (target: any, methodName: string, descriptor: PropertyDescriptor) => {
5
+ const originalMethod = descriptor.value;
6
+
7
+ descriptor.value = async (...args: any[]) => {
8
+ let [request, response, next] = args
9
+ try {
10
+ let [status, data] = await originalMethod.apply(this, args);
11
+ return HttpJsonDispatchSuccessFlow.dispactch(response, status, data);
12
+ } catch (error: any) {
13
+ HttpJsonDispatchErrorFlow.dispatch(response, error)
14
+ }
15
+ };
16
+
17
+ return descriptor;
18
+ }
19
+
20
+ export default HttpJsonDispatchHandlingFlow
@@ -0,0 +1,9 @@
1
+ import { Response } from "express";
2
+
3
+ class HttpJsonDispatchSuccessFlow {
4
+ dispactch(response: Response, status: number, payload?: any) {
5
+ response.status(status).json(payload)
6
+ }
7
+ }
8
+
9
+ export default new HttpJsonDispatchSuccessFlow
package/src/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { TControllerOptions } from "./model/TControllerOptions";
2
+ import { TI18n } from "./model/TI18n";
3
+
4
+ const initialize = (i18n: TI18n) => {
5
+ (global as any).i18nMessages = i18n
6
+ }
7
+
8
+ export {
9
+ initialize,
10
+ TControllerOptions
11
+ }
12
+
@@ -0,0 +1,12 @@
1
+ class HttpError extends Error {
2
+ status: number
3
+ detail: any
4
+ constructor(status: number, message: string, detail: any = undefined) {
5
+ super(message);
6
+ this.status = status
7
+ this.detail = detail
8
+ Object.setPrototypeOf(this, HttpError.prototype);
9
+ }
10
+ }
11
+
12
+ export 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,3 @@
1
+ export type TI18n = {
2
+ [key: string]: {}
3
+ }
@@ -0,0 +1,34 @@
1
+
2
+ export const isNotEmpty = (obj: any) => {
3
+ return !isEmpty(obj)
4
+ }
5
+
6
+ export const isEmpty = (obj: any) => {
7
+ if (typeof obj === 'undefined') {
8
+ return true
9
+ }
10
+
11
+ if (typeof obj === 'number') {
12
+ if (isNaN(obj)) {
13
+ return true
14
+ }
15
+ return false
16
+ }
17
+
18
+ if (!obj) {
19
+ return true
20
+ }
21
+
22
+ if (Array.isArray(obj)) {
23
+ if (obj.length == 0) {
24
+ return true
25
+ }
26
+ }
27
+
28
+ if (obj === "") {
29
+ return true
30
+ }
31
+
32
+ return false
33
+
34
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "compilerOptions": {
3
+ "experimentalDecorators": true,
4
+ "declaration": true,
5
+ "declarationDir": "./dist",
6
+ "outDir": "./dist",
7
+ "target": "es2021",
8
+ "module": "commonjs",
9
+ "strict": true,
10
+ "esModuleInterop": true
11
+ },
12
+ "include": ["src/**/*"]
13
+ }
14
+ // Property 'replaceAll' does not exist on type 'string'.
15
+ // Do you need to change your target library? Try changing the 'lib' compiler option to 'es2021' or later.ts(2550)
16
+
17
+
18
+ // {
19
+ // "compilerOptions": {
20
+ // "esModuleInterop": true,
21
+ // "experimentalDecorators": true,
22
+ // // "target": "es6",
23
+ // "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
24
+ // "module": "commonjs",
25
+ // "outDir": "dist",
26
+ // "typeRoots": [
27
+ // "../node_modules/@types"
28
+ // ],
29
+ // "types": [
30
+ // "node"
31
+ // ],
32
+ // "lib": [
33
+ // "ES2021.String"
34
+ // ]
35
+ // },
36
+ // "include": [
37
+ // "**/*.ts",
38
+ // "*.ts",
39
+ // "config/Configs.ts",
40
+ // ],
41
+ // "exclude": [
42
+ // "node_modules"
43
+ // ],
44
+ // "buildOnSave": true,
45
+ // "compileOnSave": true
46
+ // }