@nestia/core 2.4.3 → 3.0.0-dev.20231209

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 (67) hide show
  1. package/lib/decorators/EncryptedBody.js +13 -11
  2. package/lib/decorators/EncryptedBody.js.map +1 -1
  3. package/lib/decorators/EncryptedRoute.js +29 -12
  4. package/lib/decorators/EncryptedRoute.js.map +1 -1
  5. package/lib/decorators/internal/get_binary_body.d.ts +1 -0
  6. package/lib/decorators/internal/get_binary_body.js +66 -0
  7. package/lib/decorators/internal/get_binary_body.js.map +1 -0
  8. package/package.json +3 -3
  9. package/src/decorators/DynamicModule.ts +39 -39
  10. package/src/decorators/EncryptedBody.ts +17 -12
  11. package/src/decorators/EncryptedController.ts +38 -38
  12. package/src/decorators/EncryptedModule.ts +79 -79
  13. package/src/decorators/EncryptedRoute.ts +38 -14
  14. package/src/decorators/PlainBody.ts +72 -72
  15. package/src/decorators/TypedBody.ts +59 -59
  16. package/src/decorators/TypedException.ts +89 -89
  17. package/src/decorators/TypedHeaders.ts +69 -69
  18. package/src/decorators/TypedParam.ts +65 -65
  19. package/src/decorators/TypedQuery.ts +246 -246
  20. package/src/decorators/TypedRoute.ts +144 -144
  21. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  22. package/src/decorators/internal/NoTransformConfigureError.ts +8 -8
  23. package/src/decorators/internal/get_binary_body.ts +18 -0
  24. package/src/decorators/internal/get_path_and_querify.ts +103 -103
  25. package/src/decorators/internal/get_path_and_stringify.ts +91 -91
  26. package/src/decorators/internal/get_text_body.ts +20 -20
  27. package/src/decorators/internal/headers_to_object.ts +13 -13
  28. package/src/decorators/internal/load_controller.ts +51 -51
  29. package/src/decorators/internal/route_error.ts +45 -45
  30. package/src/decorators/internal/validate_request_body.ts +57 -57
  31. package/src/decorators/internal/validate_request_headers.ts +68 -68
  32. package/src/decorators/internal/validate_request_query.ts +56 -56
  33. package/src/index.ts +5 -5
  34. package/src/module.ts +14 -14
  35. package/src/options/INestiaTransformOptions.ts +17 -17
  36. package/src/options/INestiaTransformProject.ts +7 -7
  37. package/src/options/IRequestBodyValidator.ts +20 -20
  38. package/src/options/IRequestHeadersValidator.ts +22 -22
  39. package/src/options/IRequestQueryValidator.ts +20 -20
  40. package/src/options/IResponseBodyQuerifier.ts +25 -25
  41. package/src/options/IResponseBodyStringifier.ts +25 -25
  42. package/src/programmers/PlainBodyProgrammer.ts +52 -52
  43. package/src/programmers/TypedBodyProgrammer.ts +108 -108
  44. package/src/programmers/TypedExceptionProgrammer.ts +72 -72
  45. package/src/programmers/TypedHeadersProgrammer.ts +56 -56
  46. package/src/programmers/TypedParamProgrammer.ts +24 -24
  47. package/src/programmers/TypedQueryBodyProgrammer.ts +56 -56
  48. package/src/programmers/TypedQueryProgrammer.ts +56 -56
  49. package/src/programmers/TypedQueryRouteProgrammer.ts +51 -51
  50. package/src/programmers/TypedRouteProgrammer.ts +51 -51
  51. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +58 -58
  52. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +62 -62
  53. package/src/programmers/http/HttpQuerifyProgrammer.ts +96 -96
  54. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +63 -63
  55. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  56. package/src/transform.ts +35 -35
  57. package/src/transformers/FileTransformer.ts +66 -66
  58. package/src/transformers/MethodTransformer.ts +94 -94
  59. package/src/transformers/NodeTransformer.ts +16 -16
  60. package/src/transformers/ParameterDecoratorTransformer.ts +121 -121
  61. package/src/transformers/ParameterTransformer.ts +48 -48
  62. package/src/transformers/TypedExceptionTransformer.ts +49 -49
  63. package/src/transformers/TypedRouteTransformer.ts +95 -95
  64. package/src/typings/Creator.ts +3 -3
  65. package/src/utils/ExceptionManager.ts +112 -112
  66. package/src/utils/Singleton.ts +20 -20
  67. package/src/utils/SourceFinder.ts +57 -57
@@ -1,79 +1,79 @@
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 | 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
- * @param options Additional options except controller
64
- * @returns Class decorated module instance
65
- */
66
- export async function dynamic(
67
- path: string | string[] | { include: string[]; exclude?: string[] },
68
- password: IEncryptionPassword | IEncryptionPassword.Closure,
69
- options: Omit<Parameters<typeof Module>[0], "controllers"> = {},
70
- ): Promise<object> {
71
- // LOAD CONTROLLERS
72
- const controllers: Creator<object>[] = await load_controllers(path);
73
-
74
- // RETURNS WITH DECORATING
75
- @EncryptedModule({ ...options, controllers }, password)
76
- class NestiaModule {}
77
- return NestiaModule;
78
- }
79
- }
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 | 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
+ * @param options Additional options except controller
64
+ * @returns Class decorated module instance
65
+ */
66
+ export async function dynamic(
67
+ path: string | string[] | { include: string[]; exclude?: string[] },
68
+ password: IEncryptionPassword | IEncryptionPassword.Closure,
69
+ options: Omit<Parameters<typeof Module>[0], "controllers"> = {},
70
+ ): Promise<object> {
71
+ // LOAD CONTROLLERS
72
+ const controllers: Creator<object>[] = await load_controllers(path);
73
+
74
+ // RETURNS WITH DECORATING
75
+ @EncryptedModule({ ...options, controllers }, password)
76
+ class NestiaModule {}
77
+ return NestiaModule;
78
+ }
79
+ }
@@ -1,5 +1,5 @@
1
- import { AesPkcs5 } from "@nestia/fetcher/lib/AesPkcs5";
2
1
  import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
2
+ import { AesPkcs5 } from "@nestia/fetcher/lib/internal/AesPkcs5";
3
3
  import {
4
4
  CallHandler,
5
5
  Delete,
@@ -14,6 +14,7 @@ import {
14
14
  } from "@nestjs/common";
15
15
  import { HttpArgumentsHost } from "@nestjs/common/interfaces";
16
16
  import express from "express";
17
+ import type { FastifyRequest } from "fastify";
17
18
  import { catchError, map } from "rxjs/operators";
18
19
  import typia from "typia";
19
20
 
@@ -129,10 +130,15 @@ for (const method of [
129
130
  * @internal
130
131
  */
131
132
  class EncryptedRouteInterceptor implements NestInterceptor {
133
+ private readonly success: number;
134
+
132
135
  public constructor(
133
136
  private readonly method: string,
134
137
  private readonly stringify: (input: any) => string,
135
- ) {}
138
+ ) {
139
+ this.success =
140
+ this.method === "GET" || this.method === "DELETE" ? 200 : 201;
141
+ }
136
142
 
137
143
  public intercept(context: ExecutionContext, next: CallHandler) {
138
144
  const http: HttpArgumentsHost = context.switchToHttp();
@@ -152,18 +158,29 @@ class EncryptedRouteInterceptor implements NestInterceptor {
152
158
  const request: express.Request = http.getRequest();
153
159
  return headers_to_object(request.headers);
154
160
  });
155
- const body: string | undefined = this.stringify(value);
156
- const password: IEncryptionPassword =
157
- typeof param === "function"
158
- ? param({
159
- headers: headers.get(),
160
- body,
161
- direction: "encode",
162
- })
163
- : param;
164
-
165
- if (body === undefined) return body;
166
- return AesPkcs5.encrypt(body, password.key, password.iv);
161
+ const body: Uint8Array = (() => {
162
+ const body = this.stringify(value);
163
+ if (body === undefined) return Buffer.from([]);
164
+
165
+ const password: IEncryptionPassword =
166
+ typeof param === "function"
167
+ ? param({
168
+ body,
169
+ direction: "encode",
170
+ headers: headers.get(),
171
+ })
172
+ : param;
173
+ return AesPkcs5.encrypt(body, password.key, password.iv);
174
+ })();
175
+ // return body;
176
+ const response: express.Response = http.getResponse();
177
+ if (isExpressRequest(http.getRequest()))
178
+ response
179
+ .header("Content-Type", "application/octet-stream")
180
+ .status(this.success)
181
+ .send(body)
182
+ .end();
183
+ return body;
167
184
  }),
168
185
  catchError((err) => route_error(http.getRequest(), err)),
169
186
  );
@@ -180,3 +197,10 @@ const ROUTERS = {
180
197
  Patch,
181
198
  Delete,
182
199
  };
200
+
201
+ /**
202
+ * @internal
203
+ */
204
+ const isExpressRequest = (
205
+ request: express.Request | FastifyRequest,
206
+ ): request is express.Request => (request as express.Request).app !== undefined;
@@ -1,72 +1,72 @@
1
- import {
2
- BadRequestException,
3
- ExecutionContext,
4
- createParamDecorator,
5
- } from "@nestjs/common";
6
- import type express from "express";
7
- import type { FastifyRequest } from "fastify";
8
- import { assert } from "typia";
9
-
10
- import { get_text_body } from "./internal/get_text_body";
11
- import { validate_request_body } from "./internal/validate_request_body";
12
-
13
- /**
14
- * Plain body decorator.
15
- *
16
- * `PlainBody` is a decorator function getting full body text from the HTTP request.
17
- *
18
- * If you adjust the regular {@link Body} decorator function to the body parameter,
19
- * you can't get the full body text because the {@link Body} tries to convert the
20
- * body text to JSON object. Therefore, `@nestia/core` provides this `PlainBody`
21
- * decorator function to get the full body text.
22
- *
23
- * ```typescript
24
- * \@TypedRoute.Post("memo")
25
- * public store
26
- * (
27
- * \@PlainBody() body: string
28
- * ): void;
29
- * ```
30
- *
31
- * @return Parameter decorator
32
- * @author Jeongho Nam - https://github.com/samchon
33
- */
34
- export function PlainBody(): ParameterDecorator;
35
-
36
- /**
37
- * @internal
38
- */
39
- export function PlainBody(
40
- assert?: (input: unknown) => string,
41
- ): ParameterDecorator {
42
- const checker = assert
43
- ? validate_request_body("PlainBody")({
44
- type: "assert",
45
- assert,
46
- })
47
- : null;
48
- return createParamDecorator(async function PlainBody(
49
- _data: any,
50
- context: ExecutionContext,
51
- ) {
52
- const request: express.Request | FastifyRequest = context
53
- .switchToHttp()
54
- .getRequest();
55
- if (!isTextPlain(request.headers["content-type"]))
56
- throw new BadRequestException(`Request body type is not "text/plain".`);
57
- const value: string = await get_text_body(request);
58
- if (checker) {
59
- const error: Error | null = checker(value);
60
- if (error !== null) throw error;
61
- }
62
- return value;
63
- })();
64
- }
65
- Object.assign(PlainBody, assert);
66
-
67
- const isTextPlain = (text?: string): boolean =>
68
- text !== undefined &&
69
- text
70
- .split(";")
71
- .map((str) => str.trim())
72
- .some((str) => str === "text/plain");
1
+ import {
2
+ BadRequestException,
3
+ ExecutionContext,
4
+ createParamDecorator,
5
+ } from "@nestjs/common";
6
+ import type express from "express";
7
+ import type { FastifyRequest } from "fastify";
8
+ import { assert } from "typia";
9
+
10
+ import { get_text_body } from "./internal/get_text_body";
11
+ import { validate_request_body } from "./internal/validate_request_body";
12
+
13
+ /**
14
+ * Plain body decorator.
15
+ *
16
+ * `PlainBody` is a decorator function getting full body text from the HTTP request.
17
+ *
18
+ * If you adjust the regular {@link Body} decorator function to the body parameter,
19
+ * you can't get the full body text because the {@link Body} tries to convert the
20
+ * body text to JSON object. Therefore, `@nestia/core` provides this `PlainBody`
21
+ * decorator function to get the full body text.
22
+ *
23
+ * ```typescript
24
+ * \@TypedRoute.Post("memo")
25
+ * public store
26
+ * (
27
+ * \@PlainBody() body: string
28
+ * ): void;
29
+ * ```
30
+ *
31
+ * @return Parameter decorator
32
+ * @author Jeongho Nam - https://github.com/samchon
33
+ */
34
+ export function PlainBody(): ParameterDecorator;
35
+
36
+ /**
37
+ * @internal
38
+ */
39
+ export function PlainBody(
40
+ assert?: (input: unknown) => string,
41
+ ): ParameterDecorator {
42
+ const checker = assert
43
+ ? validate_request_body("PlainBody")({
44
+ type: "assert",
45
+ assert,
46
+ })
47
+ : null;
48
+ return createParamDecorator(async function PlainBody(
49
+ _data: any,
50
+ context: ExecutionContext,
51
+ ) {
52
+ const request: express.Request | FastifyRequest = context
53
+ .switchToHttp()
54
+ .getRequest();
55
+ if (!isTextPlain(request.headers["content-type"]))
56
+ throw new BadRequestException(`Request body type is not "text/plain".`);
57
+ const value: string = await get_text_body(request);
58
+ if (checker) {
59
+ const error: Error | null = checker(value);
60
+ if (error !== null) throw error;
61
+ }
62
+ return value;
63
+ })();
64
+ }
65
+ Object.assign(PlainBody, assert);
66
+
67
+ const isTextPlain = (text?: string): boolean =>
68
+ text !== undefined &&
69
+ text
70
+ .split(";")
71
+ .map((str) => str.trim())
72
+ .some((str) => str === "text/plain");
@@ -1,59 +1,59 @@
1
- import {
2
- BadRequestException,
3
- ExecutionContext,
4
- createParamDecorator,
5
- } from "@nestjs/common";
6
- import type express from "express";
7
- import type { FastifyRequest } from "fastify";
8
- import { assert, is, misc, validate } from "typia";
9
-
10
- import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
11
- import { validate_request_body } from "./internal/validate_request_body";
12
-
13
- /**
14
- * Type safe body decorator.
15
- *
16
- * `TypedBody` is a decorator function getting `application/json` typed data from
17
- * request body. Also, it validates the request body data type through
18
- * [typia](https://github.com/samchon/typia) and the validation speed is
19
- * maximum 20,000x times faster than `class-validator`.
20
- *
21
- * For reference, when the request body data is not following the promised type `T`,
22
- * `BadRequestException` error (status code: 400) would be thrown.
23
- *
24
- * @param validator Custom validator if required. Default is `typia.assert()`
25
- * @author Jeongho Nam - https://github.com/samchon
26
- */
27
- export function TypedBody<T>(
28
- validator?: IRequestBodyValidator<T>,
29
- ): ParameterDecorator {
30
- const checker = validate_request_body("TypedBody")(validator);
31
- return createParamDecorator(function TypedBody(
32
- _unknown: any,
33
- context: ExecutionContext,
34
- ) {
35
- const request: express.Request | FastifyRequest = context
36
- .switchToHttp()
37
- .getRequest();
38
- if (isApplicationJson(request.headers["content-type"]) === false)
39
- throw new BadRequestException(
40
- `Request body type is not "application/json".`,
41
- );
42
-
43
- const error: Error | null = checker(request.body);
44
- if (error !== null) throw error;
45
- return request.body;
46
- })();
47
- }
48
-
49
- Object.assign(TypedBody, misc.clone);
50
- Object.assign(TypedBody, is);
51
- Object.assign(TypedBody, assert);
52
- Object.assign(TypedBody, validate);
53
-
54
- const isApplicationJson = (text?: string): boolean =>
55
- text !== undefined &&
56
- text
57
- .split(";")
58
- .map((str) => str.trim())
59
- .some((str) => str === "application/json");
1
+ import {
2
+ BadRequestException,
3
+ ExecutionContext,
4
+ createParamDecorator,
5
+ } from "@nestjs/common";
6
+ import type express from "express";
7
+ import type { FastifyRequest } from "fastify";
8
+ import { assert, is, misc, validate } from "typia";
9
+
10
+ import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
11
+ import { validate_request_body } from "./internal/validate_request_body";
12
+
13
+ /**
14
+ * Type safe body decorator.
15
+ *
16
+ * `TypedBody` is a decorator function getting `application/json` typed data from
17
+ * request body. Also, it validates the request body data type through
18
+ * [typia](https://github.com/samchon/typia) and the validation speed is
19
+ * maximum 20,000x times faster than `class-validator`.
20
+ *
21
+ * For reference, when the request body data is not following the promised type `T`,
22
+ * `BadRequestException` error (status code: 400) would be thrown.
23
+ *
24
+ * @param validator Custom validator if required. Default is `typia.assert()`
25
+ * @author Jeongho Nam - https://github.com/samchon
26
+ */
27
+ export function TypedBody<T>(
28
+ validator?: IRequestBodyValidator<T>,
29
+ ): ParameterDecorator {
30
+ const checker = validate_request_body("TypedBody")(validator);
31
+ return createParamDecorator(function TypedBody(
32
+ _unknown: any,
33
+ context: ExecutionContext,
34
+ ) {
35
+ const request: express.Request | FastifyRequest = context
36
+ .switchToHttp()
37
+ .getRequest();
38
+ if (isApplicationJson(request.headers["content-type"]) === false)
39
+ throw new BadRequestException(
40
+ `Request body type is not "application/json".`,
41
+ );
42
+
43
+ const error: Error | null = checker(request.body);
44
+ if (error !== null) throw error;
45
+ return request.body;
46
+ })();
47
+ }
48
+
49
+ Object.assign(TypedBody, misc.clone);
50
+ Object.assign(TypedBody, is);
51
+ Object.assign(TypedBody, assert);
52
+ Object.assign(TypedBody, validate);
53
+
54
+ const isApplicationJson = (text?: string): boolean =>
55
+ text !== undefined &&
56
+ text
57
+ .split(";")
58
+ .map((str) => str.trim())
59
+ .some((str) => str === "application/json");