@nestia/core 8.0.8 → 9.0.0-dev.20251107

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 (75) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +93 -93
  3. package/package.json +7 -8
  4. package/src/adaptors/WebSocketAdaptor.ts +429 -429
  5. package/src/decorators/DynamicModule.ts +44 -44
  6. package/src/decorators/EncryptedBody.ts +97 -97
  7. package/src/decorators/EncryptedController.ts +40 -40
  8. package/src/decorators/EncryptedModule.ts +98 -98
  9. package/src/decorators/EncryptedRoute.ts +213 -213
  10. package/src/decorators/HumanRoute.ts +22 -22
  11. package/src/decorators/NoTransformConfigurationError.ts +34 -34
  12. package/src/decorators/PlainBody.ts +76 -76
  13. package/src/decorators/SwaggerCustomizer.ts +97 -97
  14. package/src/decorators/SwaggerExample.ts +100 -100
  15. package/src/decorators/TypedBody.ts +57 -57
  16. package/src/decorators/TypedException.ts +147 -147
  17. package/src/decorators/TypedFormData.ts +195 -195
  18. package/src/decorators/TypedHeaders.ts +66 -66
  19. package/src/decorators/TypedParam.ts +77 -77
  20. package/src/decorators/TypedQuery.ts +234 -234
  21. package/src/decorators/TypedRoute.ts +196 -196
  22. package/src/decorators/WebSocketRoute.ts +242 -242
  23. package/src/decorators/internal/EncryptedConstant.ts +2 -2
  24. package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
  25. package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
  26. package/src/decorators/internal/get_path_and_querify.ts +94 -94
  27. package/src/decorators/internal/get_path_and_stringify.ts +110 -110
  28. package/src/decorators/internal/get_text_body.ts +16 -16
  29. package/src/decorators/internal/headers_to_object.ts +11 -11
  30. package/src/decorators/internal/is_request_body_undefined.ts +12 -12
  31. package/src/decorators/internal/load_controller.ts +45 -45
  32. package/src/decorators/internal/route_error.ts +43 -43
  33. package/src/decorators/internal/validate_request_body.ts +64 -64
  34. package/src/decorators/internal/validate_request_form_data.ts +67 -67
  35. package/src/decorators/internal/validate_request_headers.ts +76 -76
  36. package/src/decorators/internal/validate_request_query.ts +64 -64
  37. package/src/index.ts +5 -5
  38. package/src/module.ts +22 -22
  39. package/src/options/INestiaTransformOptions.ts +38 -38
  40. package/src/options/INestiaTransformProject.ts +8 -8
  41. package/src/options/IRequestBodyValidator.ts +20 -20
  42. package/src/options/IRequestFormDataProps.ts +27 -27
  43. package/src/options/IRequestHeadersValidator.ts +22 -22
  44. package/src/options/IRequestQueryValidator.ts +20 -20
  45. package/src/options/IResponseBodyQuerifier.ts +25 -25
  46. package/src/options/IResponseBodyStringifier.ts +30 -30
  47. package/src/programmers/PlainBodyProgrammer.ts +70 -70
  48. package/src/programmers/TypedBodyProgrammer.ts +142 -142
  49. package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
  50. package/src/programmers/TypedHeadersProgrammer.ts +63 -63
  51. package/src/programmers/TypedParamProgrammer.ts +33 -33
  52. package/src/programmers/TypedQueryBodyProgrammer.ts +112 -112
  53. package/src/programmers/TypedQueryProgrammer.ts +114 -114
  54. package/src/programmers/TypedQueryRouteProgrammer.ts +105 -105
  55. package/src/programmers/TypedRouteProgrammer.ts +94 -94
  56. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +72 -72
  57. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +75 -75
  58. package/src/programmers/http/HttpQuerifyProgrammer.ts +108 -108
  59. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +76 -76
  60. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  61. package/src/transform.ts +35 -35
  62. package/src/transformers/FileTransformer.ts +110 -110
  63. package/src/transformers/MethodTransformer.ts +103 -103
  64. package/src/transformers/NodeTransformer.ts +23 -23
  65. package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
  66. package/src/transformers/ParameterTransformer.ts +57 -57
  67. package/src/transformers/TypedRouteTransformer.ts +85 -85
  68. package/src/transformers/WebSocketRouteTransformer.ts +120 -120
  69. package/src/typings/Creator.ts +3 -3
  70. package/src/typings/get-function-location.d.ts +7 -7
  71. package/src/utils/ArrayUtil.ts +7 -7
  72. package/src/utils/ExceptionManager.ts +115 -115
  73. package/src/utils/Singleton.ts +16 -16
  74. package/src/utils/SourceFinder.ts +54 -54
  75. package/src/utils/VersioningStrategy.ts +27 -27
@@ -1,44 +1,44 @@
1
- import { Module } from "@nestjs/common";
2
- import { ModuleMetadata } from "@nestjs/common/interfaces";
3
-
4
- import { Creator } from "../typings/Creator";
5
- import { load_controllers } from "./internal/load_controller";
6
-
7
- /**
8
- * Dynamic module.
9
- *
10
- * `DynamicModule` is a namespace wrapping a convenient function, which can load
11
- * controller classes dynamically just by specifying their directory path.
12
- *
13
- * @author Jeongho Nam - https://github.com/samchon
14
- */
15
- export namespace DynamicModule {
16
- /**
17
- * Mount dynamic module.
18
- *
19
- * Constructs a module instance with directory path of controller classes.
20
- *
21
- * Every controller classes in the target directory would be dynamically
22
- * mounted.
23
- *
24
- * @param path Path of controllers
25
- * @param metadata Additional metadata except controllers
26
- * @returns Module instance
27
- */
28
- export async function mount(
29
- path: string | string[] | { include: string[]; exclude?: string[] },
30
- metadata: Omit<ModuleMetadata, "controllers"> = {},
31
- isTsNode?: boolean,
32
- ) {
33
- // LOAD CONTROLLERS
34
- const controllers: Creator<object>[] = await load_controllers(
35
- path,
36
- isTsNode,
37
- );
38
-
39
- // RETURN WITH DECORATING
40
- @Module({ ...metadata, controllers })
41
- class NestiaModule {}
42
- return NestiaModule;
43
- }
44
- }
1
+ import { Module } from "@nestjs/common";
2
+ import { ModuleMetadata } from "@nestjs/common/interfaces";
3
+
4
+ import { Creator } from "../typings/Creator";
5
+ import { load_controllers } from "./internal/load_controller";
6
+
7
+ /**
8
+ * Dynamic module.
9
+ *
10
+ * `DynamicModule` is a namespace wrapping a convenient function, which can load
11
+ * controller classes dynamically just by specifying their directory path.
12
+ *
13
+ * @author Jeongho Nam - https://github.com/samchon
14
+ */
15
+ export namespace DynamicModule {
16
+ /**
17
+ * Mount dynamic module.
18
+ *
19
+ * Constructs a module instance with directory path of controller classes.
20
+ *
21
+ * Every controller classes in the target directory would be dynamically
22
+ * mounted.
23
+ *
24
+ * @param path Path of controllers
25
+ * @param metadata Additional metadata except controllers
26
+ * @returns Module instance
27
+ */
28
+ export async function mount(
29
+ path: string | string[] | { include: string[]; exclude?: string[] },
30
+ metadata: Omit<ModuleMetadata, "controllers"> = {},
31
+ isTsNode?: boolean,
32
+ ) {
33
+ // LOAD CONTROLLERS
34
+ const controllers: Creator<object>[] = await load_controllers(
35
+ path,
36
+ isTsNode,
37
+ );
38
+
39
+ // RETURN WITH DECORATING
40
+ @Module({ ...metadata, controllers })
41
+ class NestiaModule {}
42
+ return NestiaModule;
43
+ }
44
+ }
@@ -1,97 +1,97 @@
1
- import { AesPkcs5 } from "@nestia/fetcher/lib/AesPkcs5";
2
- import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
3
- import {
4
- BadRequestException,
5
- ExecutionContext,
6
- createParamDecorator,
7
- } from "@nestjs/common";
8
- import type express from "express";
9
- import type { FastifyRequest } from "fastify";
10
-
11
- import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
12
- import { Singleton } from "../utils/Singleton";
13
- import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
14
- import { get_text_body } from "./internal/get_text_body";
15
- import { headers_to_object } from "./internal/headers_to_object";
16
- import { validate_request_body } from "./internal/validate_request_body";
17
-
18
- /**
19
- * Encrypted body decorator.
20
- *
21
- * `EncryptedBody` is a decorator function getting `application/json` typed data
22
- * from request body which has been encrypted by AES-128/256 algorithm. Also,
23
- * `EncryptedBody` validates the request body data type through
24
- * [typia](https://github.com/samchon/typia) ad the validation speed is maximum
25
- * 15,000x times faster than `class-validator`.
26
- *
27
- * For reference, when the request body data is not following the promised type
28
- * `T`, `BadRequestException` error (status code: 400) would be thrown. Also,
29
- * `EncryptedRoute` decrypts request body using those options.
30
- *
31
- * - AES-128/256
32
- * - CBC mode
33
- * - PKCS #5 Padding
34
- * - Base64 Encoding
35
- *
36
- * @author Jeongho Nam - https://github.com/samchon
37
- * @returns Parameter decorator
38
- */
39
- export function EncryptedBody<T>(
40
- validator?: IRequestBodyValidator<T>,
41
- ): ParameterDecorator {
42
- const checker = validate_request_body("EncryptedBody")(validator);
43
- return createParamDecorator(async function EncryptedBody(
44
- _unknown: any,
45
- context: ExecutionContext,
46
- ) {
47
- const request: express.Request | FastifyRequest = context
48
- .switchToHttp()
49
- .getRequest();
50
- if (isTextPlain(request.headers["content-type"]) === false)
51
- throw new BadRequestException(`Request body type is not "text/plain".`);
52
-
53
- const param: IEncryptionPassword | IEncryptionPassword.Closure | undefined =
54
- Reflect.getMetadata(ENCRYPTION_METADATA_KEY, context.getClass());
55
- if (!param)
56
- throw new Error(
57
- "Error on nestia.core.EncryptedBody(): no encryption password is given.",
58
- );
59
-
60
- // GET BODY DATA
61
- const headers: Singleton<Record<string, string>> = new Singleton(() =>
62
- headers_to_object(request.headers),
63
- );
64
- const body: string = await get_text_body(request);
65
- const password: IEncryptionPassword =
66
- typeof param === "function"
67
- ? param({ headers: headers.get(), body, direction: "decode" })
68
- : param;
69
-
70
- // PARSE AND VALIDATE DATA
71
- const data: any = JSON.parse(decrypt(body, password.key, password.iv));
72
- const error: Error | null = checker(data);
73
- if (error !== null) throw error;
74
- return data;
75
- })();
76
- }
77
-
78
- /** @internal */
79
- const decrypt = (body: string, key: string, iv: string): string => {
80
- try {
81
- return AesPkcs5.decrypt(body, key, iv);
82
- } catch (exp) {
83
- if (exp instanceof Error)
84
- throw new BadRequestException(
85
- "Failed to decrypt the request body. Check your body content or encryption password.",
86
- );
87
- else throw exp;
88
- }
89
- };
90
-
91
- /** @internal */
92
- const isTextPlain = (text?: string): boolean =>
93
- text !== undefined &&
94
- text
95
- .split(";")
96
- .map((str) => str.trim())
97
- .some((str) => str === "text/plain");
1
+ import { AesPkcs5 } from "@nestia/fetcher/lib/AesPkcs5";
2
+ import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
3
+ import {
4
+ BadRequestException,
5
+ ExecutionContext,
6
+ createParamDecorator,
7
+ } from "@nestjs/common";
8
+ import type express from "express";
9
+ import type { FastifyRequest } from "fastify";
10
+
11
+ import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
12
+ import { Singleton } from "../utils/Singleton";
13
+ import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
14
+ import { get_text_body } from "./internal/get_text_body";
15
+ import { headers_to_object } from "./internal/headers_to_object";
16
+ import { validate_request_body } from "./internal/validate_request_body";
17
+
18
+ /**
19
+ * Encrypted body decorator.
20
+ *
21
+ * `EncryptedBody` is a decorator function getting `application/json` typed data
22
+ * from request body which has been encrypted by AES-128/256 algorithm. Also,
23
+ * `EncryptedBody` validates the request body data type through
24
+ * [typia](https://github.com/samchon/typia) ad the validation speed is maximum
25
+ * 15,000x times faster than `class-validator`.
26
+ *
27
+ * For reference, when the request body data is not following the promised type
28
+ * `T`, `BadRequestException` error (status code: 400) would be thrown. Also,
29
+ * `EncryptedRoute` decrypts request body using those options.
30
+ *
31
+ * - AES-128/256
32
+ * - CBC mode
33
+ * - PKCS #5 Padding
34
+ * - Base64 Encoding
35
+ *
36
+ * @author Jeongho Nam - https://github.com/samchon
37
+ * @returns Parameter decorator
38
+ */
39
+ export function EncryptedBody<T>(
40
+ validator?: IRequestBodyValidator<T>,
41
+ ): ParameterDecorator {
42
+ const checker = validate_request_body("EncryptedBody")(validator);
43
+ return createParamDecorator(async function EncryptedBody(
44
+ _unknown: any,
45
+ context: ExecutionContext,
46
+ ) {
47
+ const request: express.Request | FastifyRequest = context
48
+ .switchToHttp()
49
+ .getRequest();
50
+ if (isTextPlain(request.headers["content-type"]) === false)
51
+ throw new BadRequestException(`Request body type is not "text/plain".`);
52
+
53
+ const param: IEncryptionPassword | IEncryptionPassword.Closure | undefined =
54
+ Reflect.getMetadata(ENCRYPTION_METADATA_KEY, context.getClass());
55
+ if (!param)
56
+ throw new Error(
57
+ "Error on nestia.core.EncryptedBody(): no encryption password is given.",
58
+ );
59
+
60
+ // GET BODY DATA
61
+ const headers: Singleton<Record<string, string>> = new Singleton(() =>
62
+ headers_to_object(request.headers),
63
+ );
64
+ const body: string = await get_text_body(request);
65
+ const password: IEncryptionPassword =
66
+ typeof param === "function"
67
+ ? param({ headers: headers.get(), body, direction: "decode" })
68
+ : param;
69
+
70
+ // PARSE AND VALIDATE DATA
71
+ const data: any = JSON.parse(decrypt(body, password.key, password.iv));
72
+ const error: Error | null = checker(data);
73
+ if (error !== null) throw error;
74
+ return data;
75
+ })();
76
+ }
77
+
78
+ /** @internal */
79
+ const decrypt = (body: string, key: string, iv: string): string => {
80
+ try {
81
+ return AesPkcs5.decrypt(body, key, iv);
82
+ } catch (exp) {
83
+ if (exp instanceof Error)
84
+ throw new BadRequestException(
85
+ "Failed to decrypt the request body. Check your body content or encryption password.",
86
+ );
87
+ else throw exp;
88
+ }
89
+ };
90
+
91
+ /** @internal */
92
+ const isTextPlain = (text?: string): boolean =>
93
+ text !== undefined &&
94
+ text
95
+ .split(";")
96
+ .map((str) => str.trim())
97
+ .some((str) => str === "text/plain");
@@ -1,40 +1,40 @@
1
- import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
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
10
- * decorator function who configures encryption password of the AES-128/256
11
- * algorithm. The encryption algorithm and password would be used by
12
- * {@link EncryptedRoute} and {@link EncryptedBody} to encrypt the request and
13
- * response body of the HTTP protocol.
14
- *
15
- * By the way, you can configure the encryption password in the global level by
16
- * using {@link EncryptedModule} instead of the {@link nest.Module} in the module
17
- * level. In that case, you don't need to use this `EncryptedController` more.
18
- * Just use the {@link nest.Controller} without duplicated encryption password
19
- * definitions.
20
- *
21
- * Of course, if you want to use different encryption password from the
22
- * {@link EncryptedModule}, this `EncryptedController` would be useful again.
23
- * Therefore, I recommend to use this `EncryptedController` decorator function
24
- * only when you must configure different encryption password from the
25
- * {@link EncryptedModule}.
26
- *
27
- * @author Jeongho Nam - https://github.com/samchon
28
- * @param path Path of the HTTP request
29
- * @param password Encryption password or its getter function
30
- * @returns Class decorator
31
- */
32
- export function EncryptedController(
33
- path: string,
34
- password: IEncryptionPassword | IEncryptionPassword.Closure,
35
- ): ClassDecorator {
36
- return function (target: any) {
37
- Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, target);
38
- Controller(path)(target);
39
- };
40
- }
1
+ import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
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
10
+ * decorator function who configures encryption password of the AES-128/256
11
+ * algorithm. The encryption algorithm and password would be used by
12
+ * {@link EncryptedRoute} and {@link EncryptedBody} to encrypt the request and
13
+ * response body of the HTTP protocol.
14
+ *
15
+ * By the way, you can configure the encryption password in the global level by
16
+ * using {@link EncryptedModule} instead of the {@link nest.Module} in the module
17
+ * level. In that case, you don't need to use this `EncryptedController` more.
18
+ * Just use the {@link nest.Controller} without duplicated encryption password
19
+ * definitions.
20
+ *
21
+ * Of course, if you want to use different encryption password from the
22
+ * {@link EncryptedModule}, this `EncryptedController` would be useful again.
23
+ * Therefore, I recommend to use this `EncryptedController` decorator function
24
+ * only when you must configure different encryption password from the
25
+ * {@link EncryptedModule}.
26
+ *
27
+ * @author Jeongho Nam - https://github.com/samchon
28
+ * @param path Path of the HTTP request
29
+ * @param password Encryption password or its getter function
30
+ * @returns Class decorator
31
+ */
32
+ export function EncryptedController(
33
+ path: string,
34
+ password: IEncryptionPassword | IEncryptionPassword.Closure,
35
+ ): ClassDecorator {
36
+ return function (target: any) {
37
+ Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, target);
38
+ Controller(path)(target);
39
+ };
40
+ }
@@ -1,98 +1,98 @@
1
- import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
2
- import { Module } 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
12
- * function who configures encryption password of the AES-128/256 algorithm. The
13
- * encryption algorithm and password would be used by {@link EncryptedRoute} and
14
- * {@link EncryptedBody} to encrypt the request and response bod of the HTTP
15
- * protocol.
16
- *
17
- * By using this `EncryptedModule` decorator function, all of the
18
- * {@link Controller controllers} configured in the _metadata_ would be
19
- * automatically changed to the {@link EncryptedController} with the _password_.
20
- * If there're some original {@link EncryptedController} decorated classes in the
21
- * _metadata_, their encryption password would be kept.
22
- *
23
- * Therefore, if you're planning to place original {@link EncryptedController}
24
- * decorated classes in the _metadata_, I hope them to have different encryption
25
- * password from the module level. If not, I recommend you use the
26
- * {@link Controller} decorator function instead.
27
- *
28
- * In addition, the `EncryptedModule` supports a convenient dynamic controller
29
- * importing function, {@link EncryptedModule.dynamic}. If you utilize the
30
- * function with directory path of the controller classes, it imports and
31
- * configures the controller classes into the `Module`, automatically.
32
- *
33
- * @author Jeongho Nam - https://github.com/samchon
34
- * @param metadata Module configuration metadata
35
- * @param password Encryption password or its getter function
36
- * @returns Class decorator
37
- */
38
- export function EncryptedModule(
39
- metadata: Parameters<typeof Module>[0],
40
- password: IEncryptionPassword.Closure,
41
- ): ClassDecorator {
42
- return function (target: any) {
43
- Module(metadata)(target);
44
- iterate(password)(target);
45
- };
46
- }
47
-
48
- export namespace EncryptedModule {
49
- /**
50
- * Dynamic encrypted module.
51
- *
52
- * `EncryptedModule.dynamic` is an extension of the {@link EncryptedModule}
53
- * function who configures controller classes by the dynamic importing. By
54
- * specifying directory path of the controller classes, those controllers
55
- * would be automatically imported and configured.
56
- *
57
- * @param path Directory path of the controller classes
58
- * @param password Encryption password or its getter function
59
- * @param options Additional options except controller
60
- * @returns Class decorated module instance
61
- */
62
- export async function dynamic(
63
- path: string | string[] | { include: string[]; exclude?: string[] },
64
- password: IEncryptionPassword | IEncryptionPassword.Closure,
65
- options: Omit<Parameters<typeof Module>[0], "controllers"> = {},
66
- isTsNode?: boolean,
67
- ) {
68
- // LOAD CONTROLLERS
69
- const controllers: Creator<object>[] = await load_controllers(
70
- path,
71
- isTsNode,
72
- );
73
-
74
- // RETURNS WITH DECORATING
75
- @EncryptedModule(
76
- { ...options, controllers },
77
- typeof password === "object" ? () => password : password,
78
- )
79
- class NestiaModule {}
80
- return NestiaModule;
81
- }
82
- }
83
-
84
- /** @internal */
85
- const iterate =
86
- (password: IEncryptionPassword.Closure) =>
87
- (modulo: any): void => {
88
- const imports = Reflect.getMetadata("imports", modulo);
89
- if (Array.isArray(imports))
90
- for (const imp of imports)
91
- if (typeof imp === "function") iterate(password)(imp);
92
-
93
- const controllers = Reflect.getMetadata("controllers", modulo);
94
- if (Array.isArray(controllers))
95
- for (const c of controllers)
96
- if (typeof c === "function")
97
- Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, c);
98
- };
1
+ import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
2
+ import { Module } 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
12
+ * function who configures encryption password of the AES-128/256 algorithm. The
13
+ * encryption algorithm and password would be used by {@link EncryptedRoute} and
14
+ * {@link EncryptedBody} to encrypt the request and response bod of the HTTP
15
+ * protocol.
16
+ *
17
+ * By using this `EncryptedModule` decorator function, all of the
18
+ * {@link Controller controllers} configured in the _metadata_ would be
19
+ * automatically changed to the {@link EncryptedController} with the _password_.
20
+ * If there're some original {@link EncryptedController} decorated classes in the
21
+ * _metadata_, their encryption password would be kept.
22
+ *
23
+ * Therefore, if you're planning to place original {@link EncryptedController}
24
+ * decorated classes in the _metadata_, I hope them to have different encryption
25
+ * password from the module level. If not, I recommend you use the
26
+ * {@link Controller} decorator function instead.
27
+ *
28
+ * In addition, the `EncryptedModule` supports a convenient dynamic controller
29
+ * importing function, {@link EncryptedModule.dynamic}. If you utilize the
30
+ * function with directory path of the controller classes, it imports and
31
+ * configures the controller classes into the `Module`, automatically.
32
+ *
33
+ * @author Jeongho Nam - https://github.com/samchon
34
+ * @param metadata Module configuration metadata
35
+ * @param password Encryption password or its getter function
36
+ * @returns Class decorator
37
+ */
38
+ export function EncryptedModule(
39
+ metadata: Parameters<typeof Module>[0],
40
+ password: IEncryptionPassword.Closure,
41
+ ): ClassDecorator {
42
+ return function (target: any) {
43
+ Module(metadata)(target);
44
+ iterate(password)(target);
45
+ };
46
+ }
47
+
48
+ export namespace EncryptedModule {
49
+ /**
50
+ * Dynamic encrypted module.
51
+ *
52
+ * `EncryptedModule.dynamic` is an extension of the {@link EncryptedModule}
53
+ * function who configures controller classes by the dynamic importing. By
54
+ * specifying directory path of the controller classes, those controllers
55
+ * would be automatically imported and configured.
56
+ *
57
+ * @param path Directory path of the controller classes
58
+ * @param password Encryption password or its getter function
59
+ * @param options Additional options except controller
60
+ * @returns Class decorated module instance
61
+ */
62
+ export async function dynamic(
63
+ path: string | string[] | { include: string[]; exclude?: string[] },
64
+ password: IEncryptionPassword | IEncryptionPassword.Closure,
65
+ options: Omit<Parameters<typeof Module>[0], "controllers"> = {},
66
+ isTsNode?: boolean,
67
+ ) {
68
+ // LOAD CONTROLLERS
69
+ const controllers: Creator<object>[] = await load_controllers(
70
+ path,
71
+ isTsNode,
72
+ );
73
+
74
+ // RETURNS WITH DECORATING
75
+ @EncryptedModule(
76
+ { ...options, controllers },
77
+ typeof password === "object" ? () => password : password,
78
+ )
79
+ class NestiaModule {}
80
+ return NestiaModule;
81
+ }
82
+ }
83
+
84
+ /** @internal */
85
+ const iterate =
86
+ (password: IEncryptionPassword.Closure) =>
87
+ (modulo: any): void => {
88
+ const imports = Reflect.getMetadata("imports", modulo);
89
+ if (Array.isArray(imports))
90
+ for (const imp of imports)
91
+ if (typeof imp === "function") iterate(password)(imp);
92
+
93
+ const controllers = Reflect.getMetadata("controllers", modulo);
94
+ if (Array.isArray(controllers))
95
+ for (const c of controllers)
96
+ if (typeof c === "function")
97
+ Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, c);
98
+ };