@nestia/core 2.4.3 → 2.4.4-dev.20240109-3

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 (57) hide show
  1. package/package.json +5 -5
  2. package/src/decorators/DynamicModule.ts +39 -39
  3. package/src/decorators/EncryptedController.ts +38 -38
  4. package/src/decorators/EncryptedModule.ts +79 -79
  5. package/src/decorators/PlainBody.ts +72 -72
  6. package/src/decorators/TypedBody.ts +59 -59
  7. package/src/decorators/TypedException.ts +89 -89
  8. package/src/decorators/TypedHeaders.ts +69 -69
  9. package/src/decorators/TypedParam.ts +65 -65
  10. package/src/decorators/TypedQuery.ts +246 -246
  11. package/src/decorators/TypedRoute.ts +144 -144
  12. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  13. package/src/decorators/internal/NoTransformConfigureError.ts +8 -8
  14. package/src/decorators/internal/get_path_and_querify.ts +103 -103
  15. package/src/decorators/internal/get_path_and_stringify.ts +91 -91
  16. package/src/decorators/internal/get_text_body.ts +20 -20
  17. package/src/decorators/internal/headers_to_object.ts +13 -13
  18. package/src/decorators/internal/load_controller.ts +51 -51
  19. package/src/decorators/internal/route_error.ts +45 -45
  20. package/src/decorators/internal/validate_request_body.ts +57 -57
  21. package/src/decorators/internal/validate_request_headers.ts +68 -68
  22. package/src/decorators/internal/validate_request_query.ts +56 -56
  23. package/src/index.ts +5 -5
  24. package/src/module.ts +14 -14
  25. package/src/options/INestiaTransformOptions.ts +17 -17
  26. package/src/options/INestiaTransformProject.ts +7 -7
  27. package/src/options/IRequestBodyValidator.ts +20 -20
  28. package/src/options/IRequestHeadersValidator.ts +22 -22
  29. package/src/options/IRequestQueryValidator.ts +20 -20
  30. package/src/options/IResponseBodyQuerifier.ts +25 -25
  31. package/src/options/IResponseBodyStringifier.ts +25 -25
  32. package/src/programmers/PlainBodyProgrammer.ts +52 -52
  33. package/src/programmers/TypedBodyProgrammer.ts +108 -108
  34. package/src/programmers/TypedExceptionProgrammer.ts +72 -72
  35. package/src/programmers/TypedHeadersProgrammer.ts +56 -56
  36. package/src/programmers/TypedParamProgrammer.ts +24 -24
  37. package/src/programmers/TypedQueryBodyProgrammer.ts +56 -56
  38. package/src/programmers/TypedQueryProgrammer.ts +56 -56
  39. package/src/programmers/TypedQueryRouteProgrammer.ts +51 -51
  40. package/src/programmers/TypedRouteProgrammer.ts +51 -51
  41. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +58 -58
  42. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +62 -62
  43. package/src/programmers/http/HttpQuerifyProgrammer.ts +96 -96
  44. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +63 -63
  45. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  46. package/src/transform.ts +35 -35
  47. package/src/transformers/FileTransformer.ts +66 -66
  48. package/src/transformers/MethodTransformer.ts +94 -94
  49. package/src/transformers/NodeTransformer.ts +16 -16
  50. package/src/transformers/ParameterDecoratorTransformer.ts +121 -121
  51. package/src/transformers/ParameterTransformer.ts +48 -48
  52. package/src/transformers/TypedExceptionTransformer.ts +49 -49
  53. package/src/transformers/TypedRouteTransformer.ts +95 -95
  54. package/src/typings/Creator.ts +3 -3
  55. package/src/utils/ExceptionManager.ts +112 -112
  56. package/src/utils/Singleton.ts +20 -20
  57. package/src/utils/SourceFinder.ts +57 -57
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestia/core",
3
- "version": "2.4.3",
3
+ "version": "2.4.4-dev.20240109-3",
4
4
  "description": "Super-fast validation decorators of NestJS",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -38,7 +38,7 @@
38
38
  },
39
39
  "homepage": "https://nestia.io",
40
40
  "dependencies": {
41
- "@nestia/fetcher": "^2.4.3",
41
+ "@nestia/fetcher": "^2.4.4-dev.20240109-3",
42
42
  "@nestjs/common": ">=7.0.1",
43
43
  "@nestjs/core": ">=7.0.1",
44
44
  "@nestjs/platform-express": ">=7.0.1",
@@ -48,10 +48,10 @@
48
48
  "raw-body": ">=2.0.0",
49
49
  "reflect-metadata": ">=0.1.12",
50
50
  "rxjs": ">=6.0.0",
51
- "typia": "^5.3.4"
51
+ "typia": "^5.3.9"
52
52
  },
53
53
  "peerDependencies": {
54
- "@nestia/fetcher": ">=2.4.3",
54
+ "@nestia/fetcher": ">=2.4.4-dev.20240109-3",
55
55
  "@nestjs/common": ">=7.0.1",
56
56
  "@nestjs/core": ">=7.0.1",
57
57
  "@nestjs/platform-express": ">=7.0.1",
@@ -60,7 +60,7 @@
60
60
  "reflect-metadata": ">=0.1.12",
61
61
  "rxjs": ">=6.0.0",
62
62
  "typescript": ">=4.8.0 <5.4.0",
63
- "typia": ">=5.3.4 <6.0.0"
63
+ "typia": ">=5.3.9 <6.0.0"
64
64
  },
65
65
  "devDependencies": {
66
66
  "@types/express": "^4.17.15",
@@ -1,39 +1,39 @@
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 Addtional 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
- ): Promise<object> {
31
- // LOAD CONTROLLERS
32
- const controllers: Creator<object>[] = await load_controllers(path);
33
-
34
- // RETURN WITH DECORATING
35
- @Module({ ...metadata, controllers })
36
- class NestiaModule {}
37
- return NestiaModule;
38
- }
39
- }
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 Addtional 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
+ ): Promise<object> {
31
+ // LOAD CONTROLLERS
32
+ const controllers: Creator<object>[] = await load_controllers(path);
33
+
34
+ // RETURN WITH DECORATING
35
+ @Module({ ...metadata, controllers })
36
+ class NestiaModule {}
37
+ return NestiaModule;
38
+ }
39
+ }
@@ -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,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,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");