@nestia/core 4.4.2-dev.20241217 → 4.4.2

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 (74) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +87 -87
  3. package/package.json +3 -3
  4. package/src/adaptors/WebSocketAdaptor.ts +426 -426
  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/NoTransformConfigurationError.ts +32 -32
  11. package/src/decorators/PlainBody.ts +79 -79
  12. package/src/decorators/SwaggerCustomizer.ts +115 -115
  13. package/src/decorators/SwaggerExample.ts +100 -100
  14. package/src/decorators/TypedBody.ts +59 -59
  15. package/src/decorators/TypedException.ts +128 -128
  16. package/src/decorators/TypedFormData.ts +195 -195
  17. package/src/decorators/TypedHeaders.ts +64 -64
  18. package/src/decorators/TypedParam.ts +77 -77
  19. package/src/decorators/TypedQuery.ts +245 -245
  20. package/src/decorators/TypedRoute.ts +214 -214
  21. package/src/decorators/WebSocketRoute.ts +242 -242
  22. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  23. package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
  24. package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
  25. package/src/decorators/internal/get_path_and_querify.ts +108 -108
  26. package/src/decorators/internal/get_path_and_stringify.ts +122 -122
  27. package/src/decorators/internal/get_text_body.ts +20 -20
  28. package/src/decorators/internal/headers_to_object.ts +13 -13
  29. package/src/decorators/internal/is_request_body_undefined.ts +14 -14
  30. package/src/decorators/internal/load_controller.ts +49 -49
  31. package/src/decorators/internal/route_error.ts +45 -45
  32. package/src/decorators/internal/validate_request_body.ts +74 -74
  33. package/src/decorators/internal/validate_request_form_data.ts +77 -77
  34. package/src/decorators/internal/validate_request_headers.ts +86 -86
  35. package/src/decorators/internal/validate_request_query.ts +74 -74
  36. package/src/index.ts +5 -5
  37. package/src/module.ts +21 -21
  38. package/src/options/INestiaTransformOptions.ts +38 -38
  39. package/src/options/INestiaTransformProject.ts +8 -8
  40. package/src/options/IRequestBodyValidator.ts +20 -20
  41. package/src/options/IRequestFormDataProps.ts +27 -27
  42. package/src/options/IRequestHeadersValidator.ts +22 -22
  43. package/src/options/IRequestQueryValidator.ts +20 -20
  44. package/src/options/IResponseBodyQuerifier.ts +25 -25
  45. package/src/options/IResponseBodyStringifier.ts +30 -30
  46. package/src/programmers/PlainBodyProgrammer.ts +70 -70
  47. package/src/programmers/TypedBodyProgrammer.ts +142 -142
  48. package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
  49. package/src/programmers/TypedHeadersProgrammer.ts +63 -63
  50. package/src/programmers/TypedParamProgrammer.ts +33 -33
  51. package/src/programmers/TypedQueryBodyProgrammer.ts +112 -112
  52. package/src/programmers/TypedQueryProgrammer.ts +114 -114
  53. package/src/programmers/TypedQueryRouteProgrammer.ts +105 -105
  54. package/src/programmers/TypedRouteProgrammer.ts +94 -94
  55. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +72 -72
  56. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +75 -75
  57. package/src/programmers/http/HttpQuerifyProgrammer.ts +108 -108
  58. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +76 -76
  59. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  60. package/src/transform.ts +35 -35
  61. package/src/transformers/FileTransformer.ts +110 -110
  62. package/src/transformers/MethodTransformer.ts +103 -103
  63. package/src/transformers/NodeTransformer.ts +23 -23
  64. package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
  65. package/src/transformers/ParameterTransformer.ts +57 -57
  66. package/src/transformers/TypedRouteTransformer.ts +85 -85
  67. package/src/transformers/WebSocketRouteTransformer.ts +120 -120
  68. package/src/typings/Creator.ts +3 -3
  69. package/src/typings/get-function-location.d.ts +7 -7
  70. package/src/utils/ArrayUtil.ts +7 -7
  71. package/src/utils/ExceptionManager.ts +112 -112
  72. package/src/utils/Singleton.ts +20 -20
  73. package/src/utils/SourceFinder.ts +57 -57
  74. package/src/utils/VersioningStrategy.ts +27 -27
@@ -1,219 +1,219 @@
1
- import { AesPkcs5 } from "@nestia/fetcher/lib/AesPkcs5";
2
- import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
3
- import {
4
- CallHandler,
5
- Delete,
6
- ExecutionContext,
7
- Get,
8
- NestInterceptor,
9
- Patch,
10
- Post,
11
- Put,
12
- UseInterceptors,
13
- applyDecorators,
14
- } from "@nestjs/common";
15
- import { HttpArgumentsHost } from "@nestjs/common/interfaces";
16
- import express from "express";
17
- import { catchError, map } from "rxjs/operators";
18
- import typia from "typia";
19
-
20
- import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
21
- import { Singleton } from "../utils/Singleton";
22
- import { TypedRoute } from "./TypedRoute";
23
- import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
24
- import { get_path_and_stringify } from "./internal/get_path_and_stringify";
25
- import { headers_to_object } from "./internal/headers_to_object";
26
- import { route_error } from "./internal/route_error";
27
-
28
- /**
29
- * Encrypted router decorator functions.
30
- *
31
- * `EncryptedRoute` is a module containing router decorator functions which encrypts
32
- * response body data through AES-128/256 encryption. Furthermore, they can boost
33
- * up JSON string conversion speed about 50x times faster than `class-transformer`,
34
- * even type safe through [typia](https://github.com/samchon/typia).
35
- *
36
- * For reference, if you try to invalid data that is not following the promised
37
- * type `T`, 500 internal server error would be thrown. Also, as `EncryptedRoute`
38
- * composes JSON string through `typia.assertStringify<T>()` function, it is not
39
- * possible to modify response data through interceptors.
40
- *
41
- * - AES-128/256
42
- * - CBC mode
43
- * - PKCS #5 Padding
44
- * - Base64 Encoding
45
- *
46
- * @author Jeongho Nam - https://github.com/samchon
47
- */
48
- export namespace EncryptedRoute {
49
- /**
50
- * Encrypted router decorator function for the GET method.
51
- *
52
- * @param paths Path(s) of the HTTP request
53
- * @returns Method decorator
54
- */
55
- export const Get = Generator("Get");
56
-
57
- /**
58
- * Encrypted router decorator function for the GET method.
59
- *
60
- * @param paths Path(s) of the HTTP request
61
- * @returns Method decorator
62
- */
63
- export const Post = Generator("Post");
64
-
65
- /**
66
- * Encrypted router decorator function for the PATCH method.
67
- *
68
- * @param path Path of the HTTP request
69
- * @returns Method decorator
70
- */
71
- export const Patch = Generator("Patch");
72
-
73
- /**
74
- * Encrypted router decorator function for the PUT method.
75
- *
76
- * @param path Path of the HTTP request
77
- * @returns Method decorator
78
- */
79
- export const Put = Generator("Put");
80
-
81
- /**
82
- * Encrypted router decorator function for the DELETE method.
83
- *
84
- * @param path Path of the HTTP request
85
- * @returns Method decorator
86
- */
87
- export const Delete = Generator("Delete");
88
-
89
- /**
90
- * Set the logger function for the response validation failure.
91
- *
92
- * If you've configured the transformation option to `validate.log`
93
- * in the `tsconfig.json` file, then the error log information of the
94
- * response validation failure would be logged through this function
95
- * instead of throwing the 400 bad request error.
96
- *
97
- * By the way, be careful. If you've configured the response
98
- * transformation option to be `validate.log`, client may get wrong
99
- * response data. Therefore, this way is not recommended in the common
100
- * backend server case.
101
- *
102
- * @param func Logger function
103
- * @default console.log
104
- */
105
- export function setValidateErrorLogger(
106
- func: (log: IValidateErrorLog) => void,
107
- ): void {
108
- TypedRoute.setValidateErrorLogger(func);
109
- }
110
-
111
- export import IValidateErrorLog = TypedRoute.IValidateErrorLog;
112
-
113
- /**
114
- * @internal
115
- */
116
- function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
117
- function route(path?: string | string[]): MethodDecorator;
118
- function route<T>(
119
- stringify?: IResponseBodyStringifier<T> | null,
120
- ): MethodDecorator;
121
- function route<T>(
122
- path: string | string[],
123
- stringify?: IResponseBodyStringifier<T> | null,
124
- ): MethodDecorator;
125
-
126
- function route(...args: any[]): MethodDecorator {
127
- const [path, stringify] = get_path_and_stringify(
128
- () => TypedRoute.__logger,
129
- )(`EncryptedRoute.${method}`)(...args);
130
- return applyDecorators(
131
- ROUTERS[method](path),
132
- UseInterceptors(new EncryptedRouteInterceptor(method, stringify)),
133
- );
134
- }
135
- return route;
136
- }
137
- }
138
-
139
- for (const method of [
140
- typia.json.isStringify,
141
- typia.json.assertStringify,
142
- typia.json.validateStringify,
143
- typia.json.stringify,
144
- ])
145
- for (const [key, value] of Object.entries(method))
146
- for (const deco of [
147
- EncryptedRoute.Get,
148
- EncryptedRoute.Delete,
149
- EncryptedRoute.Post,
150
- EncryptedRoute.Put,
151
- EncryptedRoute.Patch,
152
- ])
153
- (deco as any)[key] = value;
154
-
155
- /**
156
- * @internal
157
- */
158
- class EncryptedRouteInterceptor implements NestInterceptor {
159
- public constructor(
160
- private readonly method: string,
161
- private readonly stringify: (
162
- input: any,
163
- method: string,
164
- path: string,
165
- ) => string,
166
- ) {}
167
-
168
- public intercept(context: ExecutionContext, next: CallHandler) {
169
- const http: HttpArgumentsHost = context.switchToHttp();
170
- return next.handle().pipe(
171
- map((value) => {
172
- const param:
173
- | IEncryptionPassword
174
- | IEncryptionPassword.Closure
175
- | undefined = Reflect.getMetadata(
176
- ENCRYPTION_METADATA_KEY,
177
- context.getClass(),
178
- );
179
- if (!param)
180
- return Error(
181
- `Error on EncryptedRoute.${this.method}(): no password found.`,
182
- );
183
-
184
- const request: express.Request = http.getRequest();
185
- const headers: Singleton<Record<string, string>> = new Singleton(() =>
186
- headers_to_object(request.headers),
187
- );
188
- const body: string | undefined = this.stringify(
189
- value,
190
- request.method,
191
- request.url,
192
- );
193
- const password: IEncryptionPassword =
194
- typeof param === "function"
195
- ? param({
196
- headers: headers.get(),
197
- body,
198
- direction: "encode",
199
- })
200
- : param;
201
-
202
- if (body === undefined) return body;
203
- return AesPkcs5.encrypt(body, password.key, password.iv);
204
- }),
205
- catchError((err) => route_error(http.getRequest(), err)),
206
- );
207
- }
208
- }
209
-
210
- /**
211
- * @internal
212
- */
213
- const ROUTERS = {
214
- Get,
215
- Post,
216
- Put,
217
- Patch,
218
- Delete,
219
- };
1
+ import { AesPkcs5 } from "@nestia/fetcher/lib/AesPkcs5";
2
+ import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
3
+ import {
4
+ CallHandler,
5
+ Delete,
6
+ ExecutionContext,
7
+ Get,
8
+ NestInterceptor,
9
+ Patch,
10
+ Post,
11
+ Put,
12
+ UseInterceptors,
13
+ applyDecorators,
14
+ } from "@nestjs/common";
15
+ import { HttpArgumentsHost } from "@nestjs/common/interfaces";
16
+ import express from "express";
17
+ import { catchError, map } from "rxjs/operators";
18
+ import typia from "typia";
19
+
20
+ import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
21
+ import { Singleton } from "../utils/Singleton";
22
+ import { TypedRoute } from "./TypedRoute";
23
+ import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
24
+ import { get_path_and_stringify } from "./internal/get_path_and_stringify";
25
+ import { headers_to_object } from "./internal/headers_to_object";
26
+ import { route_error } from "./internal/route_error";
27
+
28
+ /**
29
+ * Encrypted router decorator functions.
30
+ *
31
+ * `EncryptedRoute` is a module containing router decorator functions which encrypts
32
+ * response body data through AES-128/256 encryption. Furthermore, they can boost
33
+ * up JSON string conversion speed about 50x times faster than `class-transformer`,
34
+ * even type safe through [typia](https://github.com/samchon/typia).
35
+ *
36
+ * For reference, if you try to invalid data that is not following the promised
37
+ * type `T`, 500 internal server error would be thrown. Also, as `EncryptedRoute`
38
+ * composes JSON string through `typia.assertStringify<T>()` function, it is not
39
+ * possible to modify response data through interceptors.
40
+ *
41
+ * - AES-128/256
42
+ * - CBC mode
43
+ * - PKCS #5 Padding
44
+ * - Base64 Encoding
45
+ *
46
+ * @author Jeongho Nam - https://github.com/samchon
47
+ */
48
+ export namespace EncryptedRoute {
49
+ /**
50
+ * Encrypted router decorator function for the GET method.
51
+ *
52
+ * @param paths Path(s) of the HTTP request
53
+ * @returns Method decorator
54
+ */
55
+ export const Get = Generator("Get");
56
+
57
+ /**
58
+ * Encrypted router decorator function for the GET method.
59
+ *
60
+ * @param paths Path(s) of the HTTP request
61
+ * @returns Method decorator
62
+ */
63
+ export const Post = Generator("Post");
64
+
65
+ /**
66
+ * Encrypted router decorator function for the PATCH method.
67
+ *
68
+ * @param path Path of the HTTP request
69
+ * @returns Method decorator
70
+ */
71
+ export const Patch = Generator("Patch");
72
+
73
+ /**
74
+ * Encrypted router decorator function for the PUT method.
75
+ *
76
+ * @param path Path of the HTTP request
77
+ * @returns Method decorator
78
+ */
79
+ export const Put = Generator("Put");
80
+
81
+ /**
82
+ * Encrypted router decorator function for the DELETE method.
83
+ *
84
+ * @param path Path of the HTTP request
85
+ * @returns Method decorator
86
+ */
87
+ export const Delete = Generator("Delete");
88
+
89
+ /**
90
+ * Set the logger function for the response validation failure.
91
+ *
92
+ * If you've configured the transformation option to `validate.log`
93
+ * in the `tsconfig.json` file, then the error log information of the
94
+ * response validation failure would be logged through this function
95
+ * instead of throwing the 400 bad request error.
96
+ *
97
+ * By the way, be careful. If you've configured the response
98
+ * transformation option to be `validate.log`, client may get wrong
99
+ * response data. Therefore, this way is not recommended in the common
100
+ * backend server case.
101
+ *
102
+ * @param func Logger function
103
+ * @default console.log
104
+ */
105
+ export function setValidateErrorLogger(
106
+ func: (log: IValidateErrorLog) => void,
107
+ ): void {
108
+ TypedRoute.setValidateErrorLogger(func);
109
+ }
110
+
111
+ export import IValidateErrorLog = TypedRoute.IValidateErrorLog;
112
+
113
+ /**
114
+ * @internal
115
+ */
116
+ function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
117
+ function route(path?: string | string[]): MethodDecorator;
118
+ function route<T>(
119
+ stringify?: IResponseBodyStringifier<T> | null,
120
+ ): MethodDecorator;
121
+ function route<T>(
122
+ path: string | string[],
123
+ stringify?: IResponseBodyStringifier<T> | null,
124
+ ): MethodDecorator;
125
+
126
+ function route(...args: any[]): MethodDecorator {
127
+ const [path, stringify] = get_path_and_stringify(
128
+ () => TypedRoute.__logger,
129
+ )(`EncryptedRoute.${method}`)(...args);
130
+ return applyDecorators(
131
+ ROUTERS[method](path),
132
+ UseInterceptors(new EncryptedRouteInterceptor(method, stringify)),
133
+ );
134
+ }
135
+ return route;
136
+ }
137
+ }
138
+
139
+ for (const method of [
140
+ typia.json.isStringify,
141
+ typia.json.assertStringify,
142
+ typia.json.validateStringify,
143
+ typia.json.stringify,
144
+ ])
145
+ for (const [key, value] of Object.entries(method))
146
+ for (const deco of [
147
+ EncryptedRoute.Get,
148
+ EncryptedRoute.Delete,
149
+ EncryptedRoute.Post,
150
+ EncryptedRoute.Put,
151
+ EncryptedRoute.Patch,
152
+ ])
153
+ (deco as any)[key] = value;
154
+
155
+ /**
156
+ * @internal
157
+ */
158
+ class EncryptedRouteInterceptor implements NestInterceptor {
159
+ public constructor(
160
+ private readonly method: string,
161
+ private readonly stringify: (
162
+ input: any,
163
+ method: string,
164
+ path: string,
165
+ ) => string,
166
+ ) {}
167
+
168
+ public intercept(context: ExecutionContext, next: CallHandler) {
169
+ const http: HttpArgumentsHost = context.switchToHttp();
170
+ return next.handle().pipe(
171
+ map((value) => {
172
+ const param:
173
+ | IEncryptionPassword
174
+ | IEncryptionPassword.Closure
175
+ | undefined = Reflect.getMetadata(
176
+ ENCRYPTION_METADATA_KEY,
177
+ context.getClass(),
178
+ );
179
+ if (!param)
180
+ return Error(
181
+ `Error on EncryptedRoute.${this.method}(): no password found.`,
182
+ );
183
+
184
+ const request: express.Request = http.getRequest();
185
+ const headers: Singleton<Record<string, string>> = new Singleton(() =>
186
+ headers_to_object(request.headers),
187
+ );
188
+ const body: string | undefined = this.stringify(
189
+ value,
190
+ request.method,
191
+ request.url,
192
+ );
193
+ const password: IEncryptionPassword =
194
+ typeof param === "function"
195
+ ? param({
196
+ headers: headers.get(),
197
+ body,
198
+ direction: "encode",
199
+ })
200
+ : param;
201
+
202
+ if (body === undefined) return body;
203
+ return AesPkcs5.encrypt(body, password.key, password.iv);
204
+ }),
205
+ catchError((err) => route_error(http.getRequest(), err)),
206
+ );
207
+ }
208
+ }
209
+
210
+ /**
211
+ * @internal
212
+ */
213
+ const ROUTERS = {
214
+ Get,
215
+ Post,
216
+ Put,
217
+ Patch,
218
+ Delete,
219
+ };
@@ -1,32 +1,32 @@
1
- export function NoTransformConfigurationError(method: string) {
2
- if (NoTransformConfigurationError.throws === true)
3
- throw new Error(
4
- [
5
- `Error on nestia.core.${method}(): no transform has been configured.`,
6
- `Run "npx typia setup" command, or check if you're using non-standard TypeScript compiler like Babel or SWC.`,
7
- `Otherwise you're running "npx nestia sdk/swagger" or similar, run "npx tsc" command to find the reason why.`,
8
- ].join(" "),
9
- );
10
- return undefined as never;
11
- }
12
- export namespace NoTransformConfigurationError {
13
- /**
14
- * Whether to throw an error or not.
15
- *
16
- * If you set this value to `false`, {@link NoTransformConfigurationError} will
17
- * not throw an error.
18
- *
19
- * Even if you've not configured the plugin transformer of `tsconfig.json` file,
20
- * the error will not be thrown and the program will be continued. Instead, every
21
- * validations and assertions will be skipped and the value will be returned as it is.
22
- * Furthermore, unexpected behaviors may occur.
23
- *
24
- * Therefore, it is not recommended to set this value to `false` in production
25
- * environment. Configure this value to be `false` only when you're debugging or testing.
26
- *
27
- * By the way, if you hope the `false` value to be set, you have to do it before the first.
28
- * If you configure the `false` value after the `@nestia/core` decorator functions are
29
- * composed, the value will not be applied and the error will be thrown.
30
- */
31
- export let throws = true;
32
- }
1
+ export function NoTransformConfigurationError(method: string) {
2
+ if (NoTransformConfigurationError.throws === true)
3
+ throw new Error(
4
+ [
5
+ `Error on nestia.core.${method}(): no transform has been configured.`,
6
+ `Run "npx typia setup" command, or check if you're using non-standard TypeScript compiler like Babel or SWC.`,
7
+ `Otherwise you're running "npx nestia sdk/swagger" or similar, run "npx tsc" command to find the reason why.`,
8
+ ].join(" "),
9
+ );
10
+ return undefined as never;
11
+ }
12
+ export namespace NoTransformConfigurationError {
13
+ /**
14
+ * Whether to throw an error or not.
15
+ *
16
+ * If you set this value to `false`, {@link NoTransformConfigurationError} will
17
+ * not throw an error.
18
+ *
19
+ * Even if you've not configured the plugin transformer of `tsconfig.json` file,
20
+ * the error will not be thrown and the program will be continued. Instead, every
21
+ * validations and assertions will be skipped and the value will be returned as it is.
22
+ * Furthermore, unexpected behaviors may occur.
23
+ *
24
+ * Therefore, it is not recommended to set this value to `false` in production
25
+ * environment. Configure this value to be `false` only when you're debugging or testing.
26
+ *
27
+ * By the way, if you hope the `false` value to be set, you have to do it before the first.
28
+ * If you configure the `false` value after the `@nestia/core` decorator functions are
29
+ * composed, the value will not be applied and the error will be thrown.
30
+ */
31
+ export let throws = true;
32
+ }
@@ -1,79 +1,79 @@
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
-
9
- import { get_text_body } from "./internal/get_text_body";
10
- import { is_request_body_undefined } from "./internal/is_request_body_undefined";
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 (
56
- is_request_body_undefined(request) &&
57
- (checker ?? (() => null))(undefined as any) === null
58
- )
59
- return undefined;
60
- else if (!isTextPlain(request.headers["content-type"]))
61
- throw new BadRequestException(`Request body type is not "text/plain".`);
62
- const value: string = await get_text_body(request);
63
- if (checker) {
64
- const error: Error | null = checker(value);
65
- if (error !== null) throw error;
66
- }
67
- return value;
68
- })();
69
- }
70
-
71
- /**
72
- * @internal
73
- */
74
- const isTextPlain = (text?: string): boolean =>
75
- text !== undefined &&
76
- text
77
- .split(";")
78
- .map((str) => str.trim())
79
- .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
+
9
+ import { get_text_body } from "./internal/get_text_body";
10
+ import { is_request_body_undefined } from "./internal/is_request_body_undefined";
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 (
56
+ is_request_body_undefined(request) &&
57
+ (checker ?? (() => null))(undefined as any) === null
58
+ )
59
+ return undefined;
60
+ else if (!isTextPlain(request.headers["content-type"]))
61
+ throw new BadRequestException(`Request body type is not "text/plain".`);
62
+ const value: string = await get_text_body(request);
63
+ if (checker) {
64
+ const error: Error | null = checker(value);
65
+ if (error !== null) throw error;
66
+ }
67
+ return value;
68
+ })();
69
+ }
70
+
71
+ /**
72
+ * @internal
73
+ */
74
+ const isTextPlain = (text?: string): boolean =>
75
+ text !== undefined &&
76
+ text
77
+ .split(";")
78
+ .map((str) => str.trim())
79
+ .some((str) => str === "text/plain");