@nestia/core 0.1.5 → 0.1.6

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 (44) hide show
  1. package/lib/decorators/PlainBody.d.ts +1 -1
  2. package/lib/decorators/PlainBody.js +1 -1
  3. package/lib/executable/core.d.ts +2 -0
  4. package/lib/executable/core.js +100 -0
  5. package/lib/executable/core.js.map +1 -0
  6. package/lib/executable/internal/CommandParser.d.ts +3 -0
  7. package/lib/executable/internal/CommandParser.js +21 -0
  8. package/lib/executable/internal/CommandParser.js.map +1 -0
  9. package/lib/executable/internal/CoreSetupWizard.d.ts +4 -0
  10. package/lib/executable/internal/CoreSetupWizard.js +259 -0
  11. package/lib/executable/internal/CoreSetupWizard.js.map +1 -0
  12. package/package.json +5 -1
  13. package/src/decorators/EncryptedBody.ts +102 -102
  14. package/src/decorators/EncryptedController.ts +43 -43
  15. package/src/decorators/EncryptedModule.ts +77 -77
  16. package/src/decorators/EncryptedRoute.ts +203 -203
  17. package/src/decorators/PlainBody.ts +38 -38
  18. package/src/decorators/TypedBody.ts +49 -49
  19. package/src/decorators/TypedParam.ts +70 -70
  20. package/src/decorators/TypedRoute.ts +149 -149
  21. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  22. package/src/decorators/internal/get_path_and_stringify.ts +77 -77
  23. package/src/decorators/internal/headers_to_object.ts +10 -10
  24. package/src/decorators/internal/route_error.ts +38 -38
  25. package/src/decorators/internal/validate_request_body.ts +59 -59
  26. package/src/executable/core.ts +46 -0
  27. package/src/executable/internal/CommandParser.ts +15 -0
  28. package/src/executable/internal/CoreSetupWizard.ts +177 -0
  29. package/src/index.ts +5 -5
  30. package/src/module.ts +11 -11
  31. package/src/options/INestiaTransformOptions.ts +6 -6
  32. package/src/options/INestiaTransformProject.ts +7 -7
  33. package/src/options/IRequestBodyValidator.ts +20 -20
  34. package/src/options/IResponseBodyStringifier.ts +25 -25
  35. package/src/transform.ts +20 -20
  36. package/src/transformers/BodyTransformer.ts +106 -106
  37. package/src/transformers/FileTransformer.ts +49 -49
  38. package/src/transformers/MethodTransformer.ts +91 -91
  39. package/src/transformers/NodeTransformer.ts +18 -18
  40. package/src/transformers/ParameterTransformer.ts +45 -45
  41. package/src/transformers/RouteTransformer.ts +131 -131
  42. package/src/typings/Creator.ts +3 -3
  43. package/src/utils/ExceptionManager.ts +126 -126
  44. package/src/utils/Singleton.ts +20 -20
@@ -1,102 +1,102 @@
1
- import { AesPkcs5, IEncryptionPassword } from "@nestia/fetcher";
2
- import {
3
- BadRequestException,
4
- ExecutionContext,
5
- createParamDecorator,
6
- } from "@nestjs/common";
7
- import type express from "express";
8
- import raw from "raw-body";
9
- import { assert, is, validate } from "typia";
10
-
11
- import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
12
- import { Singleton } from "../utils/Singleton";
13
- import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
14
- import { headers_to_object } from "./internal/headers_to_object";
15
- import { validate_request_body } from "./internal/validate_request_body";
16
-
17
- /**
18
- * Encrypted body decorator.
19
- *
20
- * `EncryptedBody` is a decorator function getting JSON data from HTTP request who've
21
- * been encrypted by AES-128/256 algorithm. Also, `EncyrptedBody` validates the JSON
22
- * data type through
23
- * [`typia.assert()`](https://github.com/samchon/typia#runtime-type-checkers)
24
- * function and throws `BadRequestException` error (status code: 400), if the JSON
25
- * data is not following the promised type.
26
- *
27
- * For reference, `EncryptedRoute` decrypts request body usnig those options.
28
- *
29
- * - AES-128/256
30
- * - CBC mode
31
- * - PKCS #5 Padding
32
- * - Base64 Encoding
33
- *
34
- * @return Parameter decorator
35
- * @author Jeongho Nam - https://github.com/samchon
36
- */
37
- export function EncryptedBody<T>(validator?: IRequestBodyValidator<T>) {
38
- const checker = validate_request_body("EncryptedBody")(validator);
39
- return createParamDecorator(async function EncryptedBody(
40
- _unknown: any,
41
- ctx: ExecutionContext,
42
- ) {
43
- const request: express.Request = ctx.switchToHttp().getRequest();
44
- if (request.readable === false)
45
- throw new BadRequestException(
46
- "Request body is not the text/plain.",
47
- );
48
-
49
- const param:
50
- | IEncryptionPassword
51
- | IEncryptionPassword.Closure
52
- | undefined = Reflect.getMetadata(
53
- ENCRYPTION_METADATA_KEY,
54
- ctx.getClass(),
55
- );
56
- if (!param)
57
- throw new Error(
58
- "Error on EncryptedBody(): no encryption password is given.",
59
- );
60
-
61
- // GET BODY DATA
62
- const headers: Singleton<Record<string, string>> = new Singleton(() =>
63
- headers_to_object(request.headers),
64
- );
65
- const body: string = (await raw(request, "utf8")).trim();
66
- const password: IEncryptionPassword =
67
- typeof param === "function"
68
- ? param({ headers: headers.get(), body }, false)
69
- : param;
70
- const disabled: boolean =
71
- password.disabled === undefined
72
- ? false
73
- : typeof password.disabled === "function"
74
- ? password.disabled({ headers: headers.get(), body }, true)
75
- : password.disabled;
76
-
77
- // PARSE AND VALIDATE DATA
78
- const data: any = JSON.parse(
79
- disabled ? body : decrypt(body, password.key, password.iv),
80
- );
81
- checker(data);
82
- return data;
83
- })();
84
- }
85
- Object.assign(EncryptedBody, assert);
86
- Object.assign(EncryptedBody, is);
87
- Object.assign(EncryptedBody, validate);
88
-
89
- /**
90
- * @internal
91
- */
92
- function decrypt(body: string, key: string, iv: string): string {
93
- try {
94
- return AesPkcs5.decrypt(body, key, iv);
95
- } catch (exp) {
96
- if (exp instanceof Error)
97
- throw new BadRequestException(
98
- "Failed to decrypt the request body. Check your body content or encryption password.",
99
- );
100
- else throw exp;
101
- }
102
- }
1
+ import { AesPkcs5, IEncryptionPassword } from "@nestia/fetcher";
2
+ import {
3
+ BadRequestException,
4
+ ExecutionContext,
5
+ createParamDecorator,
6
+ } from "@nestjs/common";
7
+ import type express from "express";
8
+ import raw from "raw-body";
9
+ import { assert, is, validate } from "typia";
10
+
11
+ import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
12
+ import { Singleton } from "../utils/Singleton";
13
+ import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
14
+ import { headers_to_object } from "./internal/headers_to_object";
15
+ import { validate_request_body } from "./internal/validate_request_body";
16
+
17
+ /**
18
+ * Encrypted body decorator.
19
+ *
20
+ * `EncryptedBody` is a decorator function getting JSON data from HTTP request who've
21
+ * been encrypted by AES-128/256 algorithm. Also, `EncyrptedBody` validates the JSON
22
+ * data type through
23
+ * [`typia.assert()`](https://github.com/samchon/typia#runtime-type-checkers)
24
+ * function and throws `BadRequestException` error (status code: 400), if the JSON
25
+ * data is not following the promised type.
26
+ *
27
+ * For reference, `EncryptedRoute` decrypts request body usnig those options.
28
+ *
29
+ * - AES-128/256
30
+ * - CBC mode
31
+ * - PKCS #5 Padding
32
+ * - Base64 Encoding
33
+ *
34
+ * @return Parameter decorator
35
+ * @author Jeongho Nam - https://github.com/samchon
36
+ */
37
+ export function EncryptedBody<T>(validator?: IRequestBodyValidator<T>) {
38
+ const checker = validate_request_body("EncryptedBody")(validator);
39
+ return createParamDecorator(async function EncryptedBody(
40
+ _unknown: any,
41
+ ctx: ExecutionContext,
42
+ ) {
43
+ const request: express.Request = ctx.switchToHttp().getRequest();
44
+ if (request.readable === false)
45
+ throw new BadRequestException(
46
+ "Request body is not the text/plain.",
47
+ );
48
+
49
+ const param:
50
+ | IEncryptionPassword
51
+ | IEncryptionPassword.Closure
52
+ | undefined = Reflect.getMetadata(
53
+ ENCRYPTION_METADATA_KEY,
54
+ ctx.getClass(),
55
+ );
56
+ if (!param)
57
+ throw new Error(
58
+ "Error on EncryptedBody(): no encryption password is given.",
59
+ );
60
+
61
+ // GET BODY DATA
62
+ const headers: Singleton<Record<string, string>> = new Singleton(() =>
63
+ headers_to_object(request.headers),
64
+ );
65
+ const body: string = (await raw(request, "utf8")).trim();
66
+ const password: IEncryptionPassword =
67
+ typeof param === "function"
68
+ ? param({ headers: headers.get(), body }, false)
69
+ : param;
70
+ const disabled: boolean =
71
+ password.disabled === undefined
72
+ ? false
73
+ : typeof password.disabled === "function"
74
+ ? password.disabled({ headers: headers.get(), body }, true)
75
+ : password.disabled;
76
+
77
+ // PARSE AND VALIDATE DATA
78
+ const data: any = JSON.parse(
79
+ disabled ? body : decrypt(body, password.key, password.iv),
80
+ );
81
+ checker(data);
82
+ return data;
83
+ })();
84
+ }
85
+ Object.assign(EncryptedBody, assert);
86
+ Object.assign(EncryptedBody, is);
87
+ Object.assign(EncryptedBody, validate);
88
+
89
+ /**
90
+ * @internal
91
+ */
92
+ function decrypt(body: string, key: string, iv: string): string {
93
+ try {
94
+ return AesPkcs5.decrypt(body, key, iv);
95
+ } catch (exp) {
96
+ if (exp instanceof Error)
97
+ throw new BadRequestException(
98
+ "Failed to decrypt the request body. Check your body content or encryption password.",
99
+ );
100
+ else throw exp;
101
+ }
102
+ }
@@ -1,43 +1,43 @@
1
- import { IEncryptionPassword } from "@nestia/fetcher";
2
- import { Controller } from "@nestjs/common";
3
-
4
- import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
5
-
6
- /**
7
- * Encrypted controller.
8
- *
9
- * `EncryptedController` is an extension of the {@link nest.Controller} class decorator
10
- * function who configures encryption password of the AES-128/256 algorithm. The
11
- * encryption algorithm and password would be used by {@link EncryptedRoute} and
12
- * {@link EncryptedBody} to encrypt the request and response body of the HTTP protocol.
13
- *
14
- * > However, if you've configure the {@link IEncryptionPassword.disabled} to be `true`,
15
- * > you can disable the encryption and decryption algorithm. Therefore, when the
16
- * > {@link IEncryptionPassword.disable} becomes the `true`, content like request and
17
- * > response body would be considered as a plain text instead.
18
- *
19
- * By the way, you can configure the encryption password in the global level by using
20
- * {@link EncryptedModule} instead of the {@link nest.Module} in the module level. In
21
- * that case, you don't need to use this `EncryptedController` more. Just use the
22
- * {@link nest.Controller} without duplicated encryption password definitions.
23
- *
24
- * Of course, if you want to use different encryption password from the
25
- * {@link EncryptedModule}, this `EncryptedController` would be useful again. Therefore,
26
- * I recommend to use this `EncryptedController` decorator function only when you must
27
- * configure different encryption password from the {@link EncryptedModule}.
28
- *
29
- * @param path Path of the HTTP request
30
- * @param password Encryption password or its getter function
31
- * @returns Class decorator
32
- *
33
- * @author Jeongho Nam - https://github.com/samchon
34
- */
35
- export function EncryptedController(
36
- path: string,
37
- password: IEncryptionPassword | IEncryptionPassword.Closure,
38
- ): ClassDecorator {
39
- return function (target: any) {
40
- Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, target);
41
- Controller(path)(target);
42
- };
43
- }
1
+ import { IEncryptionPassword } from "@nestia/fetcher";
2
+ import { Controller } from "@nestjs/common";
3
+
4
+ import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
5
+
6
+ /**
7
+ * Encrypted controller.
8
+ *
9
+ * `EncryptedController` is an extension of the {@link nest.Controller} class decorator
10
+ * function who configures encryption password of the AES-128/256 algorithm. The
11
+ * encryption algorithm and password would be used by {@link EncryptedRoute} and
12
+ * {@link EncryptedBody} to encrypt the request and response body of the HTTP protocol.
13
+ *
14
+ * > However, if you've configure the {@link IEncryptionPassword.disabled} to be `true`,
15
+ * > you can disable the encryption and decryption algorithm. Therefore, when the
16
+ * > {@link IEncryptionPassword.disable} becomes the `true`, content like request and
17
+ * > response body would be considered as a plain text instead.
18
+ *
19
+ * By the way, you can configure the encryption password in the global level by using
20
+ * {@link EncryptedModule} instead of the {@link nest.Module} in the module level. In
21
+ * that case, you don't need to use this `EncryptedController` more. Just use the
22
+ * {@link nest.Controller} without duplicated encryption password definitions.
23
+ *
24
+ * Of course, if you want to use different encryption password from the
25
+ * {@link EncryptedModule}, this `EncryptedController` would be useful again. Therefore,
26
+ * I recommend to use this `EncryptedController` decorator function only when you must
27
+ * configure different encryption password from the {@link EncryptedModule}.
28
+ *
29
+ * @param path Path of the HTTP request
30
+ * @param password Encryption password or its getter function
31
+ * @returns Class decorator
32
+ *
33
+ * @author Jeongho Nam - https://github.com/samchon
34
+ */
35
+ export function EncryptedController(
36
+ path: string,
37
+ password: IEncryptionPassword | IEncryptionPassword.Closure,
38
+ ): ClassDecorator {
39
+ return function (target: any) {
40
+ Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, target);
41
+ Controller(path)(target);
42
+ };
43
+ }
@@ -1,77 +1,77 @@
1
- import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
2
- import { Module, ModuleMetadata } from "@nestjs/common";
3
-
4
- import { Creator } from "../typings/Creator";
5
- import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
6
- import { load_controllers } from "./internal/load_controller";
7
-
8
- /**
9
- * Encrypted module.
10
- *
11
- * `EncryptedModule` is an extension of the {@link Module} class decorator function
12
- * who configures encryption password of the AES-128/256 algorithm. The encryption
13
- * algorithm and password would be used by {@link EncryptedRoute} and {@link EncryptedBody}
14
- * to encrypt the request and response bod of the HTTP protocol.
15
- *
16
- * By using this `EncryptedModule` decorator function, all of the
17
- * {@link Controller controllers} configured in the *metadata* would be automatically
18
- * changed to the {@link EncryptedController} with the *password*. If there're some
19
- * original {@link EncryptedController} decorated classes in the *metadata*, their
20
- * encryption password would be kept.
21
- *
22
- * Therefore, if you're planning to place original {@link EncryptedController} decorated
23
- * classes in the *metadata*, I hope them to have different encryption password from the
24
- * module level. If not, I recommend you use the {@link Controller} decorator
25
- * function instead.
26
- *
27
- * In addition, the `EncryptedModule` supports a convenient dynamic controller importing
28
- * function, {@link EncryptedModule.dynamic}. If you utilize the function with directory
29
- * path of the controller classes, it imports and configures the controller classes into
30
- * the `Module`, automatically.
31
- *
32
- * @param metadata Module configuration metadata
33
- * @param password Encryption password or its getter function
34
- * @returns Class decorator
35
- *
36
- * @author Jeongho Nam - https://github.com/samchon
37
- */
38
- export function EncryptedModule(
39
- metadata: ModuleMetadata,
40
- password: IEncryptionPassword | IEncryptionPassword.Closure,
41
- ): ClassDecorator {
42
- return function (target: any) {
43
- Module(metadata)(target);
44
- if (metadata.controllers === undefined) return;
45
-
46
- for (const c of metadata.controllers)
47
- if (Reflect.hasMetadata(ENCRYPTION_METADATA_KEY, c) === false)
48
- Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, c);
49
- };
50
- }
51
-
52
- export namespace EncryptedModule {
53
- /**
54
- * Dynamic encrypted module.
55
- *
56
- * `EncryptedModule.dynamic` is an extension of the {@link EncryptedModule} function
57
- * who configures controller classes by the dynamic importing. By specifying directory
58
- * path of the controller classes, those controllers would be automatically imported
59
- * and configured.
60
- *
61
- * @param path Directory path of the controller classes
62
- * @param password Encryption password or its getter function
63
- * @returns Class decorated module instance
64
- */
65
- export async function dynamic(
66
- path: string,
67
- password: IEncryptionPassword | IEncryptionPassword.Closure,
68
- ): Promise<object> {
69
- // LOAD CONTROLLERS
70
- const controllers: Creator<object>[] = await load_controllers(path);
71
-
72
- // RETURNS WITH DECORATING
73
- @EncryptedModule({ controllers }, password)
74
- class NestiaModule {}
75
- return NestiaModule;
76
- }
77
- }
1
+ import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
2
+ import { Module, ModuleMetadata } from "@nestjs/common";
3
+
4
+ import { Creator } from "../typings/Creator";
5
+ import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
6
+ import { load_controllers } from "./internal/load_controller";
7
+
8
+ /**
9
+ * Encrypted module.
10
+ *
11
+ * `EncryptedModule` is an extension of the {@link Module} class decorator function
12
+ * who configures encryption password of the AES-128/256 algorithm. The encryption
13
+ * algorithm and password would be used by {@link EncryptedRoute} and {@link EncryptedBody}
14
+ * to encrypt the request and response bod of the HTTP protocol.
15
+ *
16
+ * By using this `EncryptedModule` decorator function, all of the
17
+ * {@link Controller controllers} configured in the *metadata* would be automatically
18
+ * changed to the {@link EncryptedController} with the *password*. If there're some
19
+ * original {@link EncryptedController} decorated classes in the *metadata*, their
20
+ * encryption password would be kept.
21
+ *
22
+ * Therefore, if you're planning to place original {@link EncryptedController} decorated
23
+ * classes in the *metadata*, I hope them to have different encryption password from the
24
+ * module level. If not, I recommend you use the {@link Controller} decorator
25
+ * function instead.
26
+ *
27
+ * In addition, the `EncryptedModule` supports a convenient dynamic controller importing
28
+ * function, {@link EncryptedModule.dynamic}. If you utilize the function with directory
29
+ * path of the controller classes, it imports and configures the controller classes into
30
+ * the `Module`, automatically.
31
+ *
32
+ * @param metadata Module configuration metadata
33
+ * @param password Encryption password or its getter function
34
+ * @returns Class decorator
35
+ *
36
+ * @author Jeongho Nam - https://github.com/samchon
37
+ */
38
+ export function EncryptedModule(
39
+ metadata: ModuleMetadata,
40
+ password: IEncryptionPassword | IEncryptionPassword.Closure,
41
+ ): ClassDecorator {
42
+ return function (target: any) {
43
+ Module(metadata)(target);
44
+ if (metadata.controllers === undefined) return;
45
+
46
+ for (const c of metadata.controllers)
47
+ if (Reflect.hasMetadata(ENCRYPTION_METADATA_KEY, c) === false)
48
+ Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, c);
49
+ };
50
+ }
51
+
52
+ export namespace EncryptedModule {
53
+ /**
54
+ * Dynamic encrypted module.
55
+ *
56
+ * `EncryptedModule.dynamic` is an extension of the {@link EncryptedModule} function
57
+ * who configures controller classes by the dynamic importing. By specifying directory
58
+ * path of the controller classes, those controllers would be automatically imported
59
+ * and configured.
60
+ *
61
+ * @param path Directory path of the controller classes
62
+ * @param password Encryption password or its getter function
63
+ * @returns Class decorated module instance
64
+ */
65
+ export async function dynamic(
66
+ path: string,
67
+ password: IEncryptionPassword | IEncryptionPassword.Closure,
68
+ ): Promise<object> {
69
+ // LOAD CONTROLLERS
70
+ const controllers: Creator<object>[] = await load_controllers(path);
71
+
72
+ // RETURNS WITH DECORATING
73
+ @EncryptedModule({ controllers }, password)
74
+ class NestiaModule {}
75
+ return NestiaModule;
76
+ }
77
+ }