@nestia/core 7.0.0-dev.20250607 → 7.0.0

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 +92 -92
  3. package/package.json +3 -3
  4. package/src/adaptors/WebSocketAdaptor.ts +429 -429
  5. package/src/decorators/DynamicModule.ts +43 -43
  6. package/src/decorators/EncryptedBody.ts +101 -101
  7. package/src/decorators/EncryptedController.ts +38 -38
  8. package/src/decorators/EncryptedModule.ts +100 -100
  9. package/src/decorators/EncryptedRoute.ts +219 -219
  10. package/src/decorators/HumanRoute.ts +22 -22
  11. package/src/decorators/NoTransformConfigurationError.ts +32 -32
  12. package/src/decorators/PlainBody.ts +79 -79
  13. package/src/decorators/SwaggerCustomizer.ts +115 -115
  14. package/src/decorators/SwaggerExample.ts +100 -100
  15. package/src/decorators/TypedBody.ts +59 -59
  16. package/src/decorators/TypedException.ts +166 -166
  17. package/src/decorators/TypedFormData.ts +195 -195
  18. package/src/decorators/TypedHeaders.ts +64 -64
  19. package/src/decorators/TypedParam.ts +77 -77
  20. package/src/decorators/TypedQuery.ts +245 -245
  21. package/src/decorators/TypedRoute.ts +214 -214
  22. package/src/decorators/WebSocketRoute.ts +242 -242
  23. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  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 +108 -108
  27. package/src/decorators/internal/get_path_and_stringify.ts +122 -122
  28. package/src/decorators/internal/get_text_body.ts +20 -20
  29. package/src/decorators/internal/headers_to_object.ts +13 -13
  30. package/src/decorators/internal/is_request_body_undefined.ts +14 -14
  31. package/src/decorators/internal/load_controller.ts +49 -49
  32. package/src/decorators/internal/route_error.ts +45 -45
  33. package/src/decorators/internal/validate_request_body.ts +74 -74
  34. package/src/decorators/internal/validate_request_form_data.ts +77 -77
  35. package/src/decorators/internal/validate_request_headers.ts +86 -86
  36. package/src/decorators/internal/validate_request_query.ts +74 -74
  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 +112 -112
  73. package/src/utils/Singleton.ts +20 -20
  74. package/src/utils/SourceFinder.ts +57 -57
  75. package/src/utils/VersioningStrategy.ts +27 -27
@@ -1,43 +1,43 @@
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 mounted.
22
- *
23
- * @param path Path of controllers
24
- * @param metadata Additional metadata except controllers
25
- * @returns module instance
26
- */
27
- export async function mount(
28
- path: string | string[] | { include: string[]; exclude?: string[] },
29
- metadata: Omit<ModuleMetadata, "controllers"> = {},
30
- isTsNode?: boolean,
31
- ) {
32
- // LOAD CONTROLLERS
33
- const controllers: Creator<object>[] = await load_controllers(
34
- path,
35
- isTsNode,
36
- );
37
-
38
- // RETURN WITH DECORATING
39
- @Module({ ...metadata, controllers })
40
- class NestiaModule {}
41
- return NestiaModule;
42
- }
43
- }
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 mounted.
22
+ *
23
+ * @param path Path of controllers
24
+ * @param metadata Additional metadata except controllers
25
+ * @returns module instance
26
+ */
27
+ export async function mount(
28
+ path: string | string[] | { include: string[]; exclude?: string[] },
29
+ metadata: Omit<ModuleMetadata, "controllers"> = {},
30
+ isTsNode?: boolean,
31
+ ) {
32
+ // LOAD CONTROLLERS
33
+ const controllers: Creator<object>[] = await load_controllers(
34
+ path,
35
+ isTsNode,
36
+ );
37
+
38
+ // RETURN WITH DECORATING
39
+ @Module({ ...metadata, controllers })
40
+ class NestiaModule {}
41
+ return NestiaModule;
42
+ }
43
+ }
@@ -1,101 +1,101 @@
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 from
22
- * 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
25
- * maximum 15,000x times faster than `class-validator`.
26
- *
27
- * For reference, when the request body data is not following the promised type `T`,
28
- * `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
- * @return Parameter decorator
37
- * @author Jeongho Nam - https://github.com/samchon
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
- /**
79
- * @internal
80
- */
81
- const decrypt = (body: string, key: string, iv: string): string => {
82
- try {
83
- return AesPkcs5.decrypt(body, key, iv);
84
- } catch (exp) {
85
- if (exp instanceof Error)
86
- throw new BadRequestException(
87
- "Failed to decrypt the request body. Check your body content or encryption password.",
88
- );
89
- else throw exp;
90
- }
91
- };
92
-
93
- /**
94
- * @internal
95
- */
96
- const isTextPlain = (text?: string): boolean =>
97
- text !== undefined &&
98
- text
99
- .split(";")
100
- .map((str) => str.trim())
101
- .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 from
22
+ * 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
25
+ * maximum 15,000x times faster than `class-validator`.
26
+ *
27
+ * For reference, when the request body data is not following the promised type `T`,
28
+ * `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
+ * @return Parameter decorator
37
+ * @author Jeongho Nam - https://github.com/samchon
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
+ /**
79
+ * @internal
80
+ */
81
+ const decrypt = (body: string, key: string, iv: string): string => {
82
+ try {
83
+ return AesPkcs5.decrypt(body, key, iv);
84
+ } catch (exp) {
85
+ if (exp instanceof Error)
86
+ throw new BadRequestException(
87
+ "Failed to decrypt the request body. Check your body content or encryption password.",
88
+ );
89
+ else throw exp;
90
+ }
91
+ };
92
+
93
+ /**
94
+ * @internal
95
+ */
96
+ const isTextPlain = (text?: string): boolean =>
97
+ text !== undefined &&
98
+ text
99
+ .split(";")
100
+ .map((str) => str.trim())
101
+ .some((str) => str === "text/plain");
@@ -1,38 +1,38 @@
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 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
- * By the way, you can configure the encryption password in the global level by using
15
- * {@link EncryptedModule} instead of the {@link nest.Module} in the module level. In
16
- * that case, you don't need to use this `EncryptedController` more. Just use the
17
- * {@link nest.Controller} without duplicated encryption password definitions.
18
- *
19
- * Of course, if you want to use different encryption password from the
20
- * {@link EncryptedModule}, this `EncryptedController` would be useful again. Therefore,
21
- * I recommend to use this `EncryptedController` decorator function only when you must
22
- * configure different encryption password from the {@link EncryptedModule}.
23
- *
24
- * @param path Path of the HTTP request
25
- * @param password Encryption password or its getter function
26
- * @returns Class decorator
27
- *
28
- * @author Jeongho Nam - https://github.com/samchon
29
- */
30
- export function EncryptedController(
31
- path: string,
32
- password: IEncryptionPassword | IEncryptionPassword.Closure,
33
- ): ClassDecorator {
34
- return function (target: any) {
35
- Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, target);
36
- Controller(path)(target);
37
- };
38
- }
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 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
+ * By the way, you can configure the encryption password in the global level by using
15
+ * {@link EncryptedModule} instead of the {@link nest.Module} in the module level. In
16
+ * that case, you don't need to use this `EncryptedController` more. Just use the
17
+ * {@link nest.Controller} without duplicated encryption password definitions.
18
+ *
19
+ * Of course, if you want to use different encryption password from the
20
+ * {@link EncryptedModule}, this `EncryptedController` would be useful again. Therefore,
21
+ * I recommend to use this `EncryptedController` decorator function only when you must
22
+ * configure different encryption password from the {@link EncryptedModule}.
23
+ *
24
+ * @param path Path of the HTTP request
25
+ * @param password Encryption password or its getter function
26
+ * @returns Class decorator
27
+ *
28
+ * @author Jeongho Nam - https://github.com/samchon
29
+ */
30
+ export function EncryptedController(
31
+ path: string,
32
+ password: IEncryptionPassword | IEncryptionPassword.Closure,
33
+ ): ClassDecorator {
34
+ return function (target: any) {
35
+ Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, target);
36
+ Controller(path)(target);
37
+ };
38
+ }
@@ -1,100 +1,100 @@
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 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: 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} function
53
- * who configures controller classes by the dynamic importing. By specifying directory
54
- * path of the controller classes, those controllers would be automatically imported
55
- * 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
- /**
85
- * @internal
86
- */
87
- const iterate =
88
- (password: IEncryptionPassword.Closure) =>
89
- (modulo: any): void => {
90
- const imports = Reflect.getMetadata("imports", modulo);
91
- if (Array.isArray(imports))
92
- for (const imp of imports)
93
- if (typeof imp === "function") iterate(password)(imp);
94
-
95
- const controllers = Reflect.getMetadata("controllers", modulo);
96
- if (Array.isArray(controllers))
97
- for (const c of controllers)
98
- if (typeof c === "function")
99
- Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, c);
100
- };
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 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: 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} function
53
+ * who configures controller classes by the dynamic importing. By specifying directory
54
+ * path of the controller classes, those controllers would be automatically imported
55
+ * 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
+ /**
85
+ * @internal
86
+ */
87
+ const iterate =
88
+ (password: IEncryptionPassword.Closure) =>
89
+ (modulo: any): void => {
90
+ const imports = Reflect.getMetadata("imports", modulo);
91
+ if (Array.isArray(imports))
92
+ for (const imp of imports)
93
+ if (typeof imp === "function") iterate(password)(imp);
94
+
95
+ const controllers = Reflect.getMetadata("controllers", modulo);
96
+ if (Array.isArray(controllers))
97
+ for (const c of controllers)
98
+ if (typeof c === "function")
99
+ Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, c);
100
+ };