@nestia/core 0.1.5 → 0.1.7

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 (46) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +336 -0
  3. package/lib/decorators/PlainBody.d.ts +1 -1
  4. package/lib/decorators/PlainBody.js +1 -1
  5. package/lib/executable/core.d.ts +2 -0
  6. package/lib/executable/core.js +100 -0
  7. package/lib/executable/core.js.map +1 -0
  8. package/lib/executable/internal/CommandParser.d.ts +3 -0
  9. package/lib/executable/internal/CommandParser.js +21 -0
  10. package/lib/executable/internal/CommandParser.js.map +1 -0
  11. package/lib/executable/internal/CoreSetupWizard.d.ts +4 -0
  12. package/lib/executable/internal/CoreSetupWizard.js +259 -0
  13. package/lib/executable/internal/CoreSetupWizard.js.map +1 -0
  14. package/package.json +5 -1
  15. package/src/decorators/EncryptedBody.ts +102 -102
  16. package/src/decorators/EncryptedController.ts +43 -43
  17. package/src/decorators/EncryptedModule.ts +77 -77
  18. package/src/decorators/EncryptedRoute.ts +203 -203
  19. package/src/decorators/PlainBody.ts +38 -38
  20. package/src/decorators/TypedBody.ts +49 -49
  21. package/src/decorators/TypedParam.ts +70 -70
  22. package/src/decorators/TypedRoute.ts +149 -149
  23. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  24. package/src/decorators/internal/get_path_and_stringify.ts +77 -77
  25. package/src/decorators/internal/headers_to_object.ts +10 -10
  26. package/src/decorators/internal/route_error.ts +38 -38
  27. package/src/decorators/internal/validate_request_body.ts +59 -59
  28. package/src/executable/core.ts +46 -0
  29. package/src/executable/internal/CommandParser.ts +15 -0
  30. package/src/executable/internal/CoreSetupWizard.ts +177 -0
  31. package/src/index.ts +5 -5
  32. package/src/module.ts +11 -11
  33. package/src/options/INestiaTransformOptions.ts +6 -6
  34. package/src/options/INestiaTransformProject.ts +7 -7
  35. package/src/options/IRequestBodyValidator.ts +20 -20
  36. package/src/options/IResponseBodyStringifier.ts +25 -25
  37. package/src/transform.ts +20 -20
  38. package/src/transformers/BodyTransformer.ts +106 -106
  39. package/src/transformers/FileTransformer.ts +49 -49
  40. package/src/transformers/MethodTransformer.ts +91 -91
  41. package/src/transformers/NodeTransformer.ts +18 -18
  42. package/src/transformers/ParameterTransformer.ts +45 -45
  43. package/src/transformers/RouteTransformer.ts +131 -131
  44. package/src/typings/Creator.ts +3 -3
  45. package/src/utils/ExceptionManager.ts +126 -126
  46. package/src/utils/Singleton.ts +20 -20
@@ -1,77 +1,77 @@
1
- import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
2
- import { Module, ModuleMetadata } 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: ModuleMetadata,
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
- * @returns Class decorated module instance
64
- */
65
- export async function dynamic(
66
- path: string,
67
- password: IEncryptionPassword | IEncryptionPassword.Closure,
68
- ): Promise<object> {
69
- // LOAD CONTROLLERS
70
- const controllers: Creator<object>[] = await load_controllers(path);
71
-
72
- // RETURNS WITH DECORATING
73
- @EncryptedModule({ controllers }, password)
74
- class NestiaModule {}
75
- return NestiaModule;
76
- }
77
- }
1
+ import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
2
+ import { Module, ModuleMetadata } 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: ModuleMetadata,
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
+ * @returns Class decorated module instance
64
+ */
65
+ export async function dynamic(
66
+ path: string,
67
+ password: IEncryptionPassword | IEncryptionPassword.Closure,
68
+ ): Promise<object> {
69
+ // LOAD CONTROLLERS
70
+ const controllers: Creator<object>[] = await load_controllers(path);
71
+
72
+ // RETURNS WITH DECORATING
73
+ @EncryptedModule({ controllers }, password)
74
+ class NestiaModule {}
75
+ return NestiaModule;
76
+ }
77
+ }
@@ -1,203 +1,203 @@
1
- import { AesPkcs5, IEncryptionPassword } from "@nestia/fetcher";
2
- import {
3
- CallHandler,
4
- Delete,
5
- ExecutionContext,
6
- Get,
7
- NestInterceptor,
8
- Patch,
9
- Post,
10
- Put,
11
- UseInterceptors,
12
- applyDecorators,
13
- } from "@nestjs/common";
14
- import { HttpArgumentsHost } from "@nestjs/common/interfaces";
15
- import express from "express";
16
- import { Observable, catchError, map } from "rxjs";
17
- import {
18
- assertStringify,
19
- isStringify,
20
- stringify,
21
- validateStringify,
22
- } from "typia";
23
-
24
- import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
25
- import { Singleton } from "../utils/Singleton";
26
- import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
27
- import { get_path_and_stringify } from "./internal/get_path_and_stringify";
28
- import { headers_to_object } from "./internal/headers_to_object";
29
- import { route_error } from "./internal/route_error";
30
-
31
- /**
32
- * Encrypted router decorator functions.
33
- *
34
- * `EncryptedRoute` is a module containing router decorator functions which encrypts
35
- * response body data through AES-128/250 encryption. Also, those decorator functions
36
- * can boost up JSON string conversion speed about 5x times faster, through
37
- * [`typia.stringify()`](https://github.com/samchon/typia#fastest-json-string-conversion).
38
- *
39
- * For reference, `EncryptedRoute` encrypts response body usnig those options.
40
- *
41
- * - AES-128/256
42
- * - CBC mode
43
- * - PKCS #5 Padding
44
- * - Base64 Encoding
45
- *
46
- * Also, router functions in `EncryptedRoute` can convert custom error classes to the
47
- * regular {@link nest.HttpException} class automatically, through
48
- * {@link ExceptionManager}.
49
- *
50
- * @author Jeongho Nam - https://github.com/samchon
51
- */
52
- export namespace EncryptedRoute {
53
- /**
54
- * Encrypted router decorator function for the GET method.
55
- *
56
- * @param paths Path(s) of the HTTP request
57
- * @returns Method decorator
58
- */
59
- export const Get = Generator("Get");
60
-
61
- /**
62
- * Encrypted router decorator function for the GET method.
63
- *
64
- * @param paths Path(s) of the HTTP request
65
- * @returns Method decorator
66
- */
67
- export const Post = Generator("Post");
68
-
69
- /**
70
- * Encrypted router decorator function for the PATCH method.
71
- *
72
- * @param path Path of the HTTP request
73
- * @returns Method decorator
74
- */
75
- export const Patch = Generator("Patch");
76
-
77
- /**
78
- * Encrypted router decorator function for the PUT method.
79
- *
80
- * @param path Path of the HTTP request
81
- * @returns Method decorator
82
- */
83
- export const Put = Generator("Put");
84
-
85
- /**
86
- * Encrypted router decorator function for the DELETE method.
87
- *
88
- * @param path Path of the HTTP request
89
- * @returns Method decorator
90
- */
91
- export const Delete = Generator("Delete");
92
-
93
- function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
94
- function route(path?: string | string[]): MethodDecorator;
95
- function route<T>(
96
- stringify?: IResponseBodyStringifier<T>,
97
- ): MethodDecorator;
98
- function route<T>(
99
- path: string | string[],
100
- stringify?: IResponseBodyStringifier<T>,
101
- ): MethodDecorator;
102
-
103
- function route(...args: any[]): MethodDecorator {
104
- const [path, stringify] = get_path_and_stringify(
105
- `EncryptedRoute.${method}`,
106
- )(...args);
107
- return applyDecorators(
108
- ROUTERS[method](path),
109
- UseInterceptors(
110
- new EncryptedRouteInterceptor(method, stringify),
111
- ),
112
- );
113
- }
114
- return route;
115
- }
116
- }
117
-
118
- for (const method of [
119
- assertStringify,
120
- isStringify,
121
- stringify,
122
- validateStringify,
123
- ]) {
124
- Object.assign(EncryptedRoute.Get, method);
125
- Object.assign(EncryptedRoute.Delete, method);
126
- Object.assign(EncryptedRoute.Post, method);
127
- Object.assign(EncryptedRoute.Put, method);
128
- Object.assign(EncryptedRoute.Patch, method);
129
- }
130
-
131
- /**
132
- * @internal
133
- */
134
- class EncryptedRouteInterceptor implements NestInterceptor {
135
- public constructor(
136
- private readonly method: string,
137
- private readonly stringify: (input: any) => string,
138
- ) {}
139
-
140
- public intercept(
141
- context: ExecutionContext,
142
- next: CallHandler,
143
- ): Observable<any> {
144
- const http: HttpArgumentsHost = context.switchToHttp();
145
- return next.handle().pipe(
146
- map((value) => {
147
- const param:
148
- | IEncryptionPassword
149
- | IEncryptionPassword.Closure
150
- | undefined = Reflect.getMetadata(
151
- ENCRYPTION_METADATA_KEY,
152
- context.getClass(),
153
- );
154
- if (!param)
155
- throw new Error(
156
- `Error on EncryptedRoute.${this.method}(): no encryption password is given.`,
157
- );
158
-
159
- const headers: Singleton<Record<string, string>> =
160
- new Singleton(() => {
161
- const request: express.Request = http.getRequest();
162
- return headers_to_object(request.headers);
163
- });
164
- const body: string | undefined = this.stringify(value);
165
- const password: IEncryptionPassword =
166
- typeof param === "function"
167
- ? param({ headers: headers.get(), body }, false)
168
- : param;
169
- const disabled: boolean =
170
- password.disabled === undefined
171
- ? false
172
- : typeof password.disabled === "function"
173
- ? password.disabled(
174
- { headers: headers.get(), body },
175
- false,
176
- )
177
- : password.disabled;
178
-
179
- const response: express.Response = http.getResponse();
180
- response.header(
181
- "Content-Type",
182
- disabled ? "application/json" : "text/plain",
183
- );
184
-
185
- if (disabled === true) return body;
186
- else if (body === undefined) return body;
187
- return AesPkcs5.encrypt(body, password.key, password.iv);
188
- }),
189
- catchError((err) => route_error(http.getRequest(), err)),
190
- );
191
- }
192
- }
193
-
194
- /**
195
- * @internal
196
- */
197
- const ROUTERS = {
198
- Get,
199
- Post,
200
- Put,
201
- Patch,
202
- Delete,
203
- };
1
+ import { AesPkcs5, IEncryptionPassword } from "@nestia/fetcher";
2
+ import {
3
+ CallHandler,
4
+ Delete,
5
+ ExecutionContext,
6
+ Get,
7
+ NestInterceptor,
8
+ Patch,
9
+ Post,
10
+ Put,
11
+ UseInterceptors,
12
+ applyDecorators,
13
+ } from "@nestjs/common";
14
+ import { HttpArgumentsHost } from "@nestjs/common/interfaces";
15
+ import express from "express";
16
+ import { Observable, catchError, map } from "rxjs";
17
+ import {
18
+ assertStringify,
19
+ isStringify,
20
+ stringify,
21
+ validateStringify,
22
+ } from "typia";
23
+
24
+ import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
25
+ import { Singleton } from "../utils/Singleton";
26
+ import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
27
+ import { get_path_and_stringify } from "./internal/get_path_and_stringify";
28
+ import { headers_to_object } from "./internal/headers_to_object";
29
+ import { route_error } from "./internal/route_error";
30
+
31
+ /**
32
+ * Encrypted router decorator functions.
33
+ *
34
+ * `EncryptedRoute` is a module containing router decorator functions which encrypts
35
+ * response body data through AES-128/250 encryption. Also, those decorator functions
36
+ * can boost up JSON string conversion speed about 5x times faster, through
37
+ * [`typia.stringify()`](https://github.com/samchon/typia#fastest-json-string-conversion).
38
+ *
39
+ * For reference, `EncryptedRoute` encrypts response body usnig those options.
40
+ *
41
+ * - AES-128/256
42
+ * - CBC mode
43
+ * - PKCS #5 Padding
44
+ * - Base64 Encoding
45
+ *
46
+ * Also, router functions in `EncryptedRoute` can convert custom error classes to the
47
+ * regular {@link nest.HttpException} class automatically, through
48
+ * {@link ExceptionManager}.
49
+ *
50
+ * @author Jeongho Nam - https://github.com/samchon
51
+ */
52
+ export namespace EncryptedRoute {
53
+ /**
54
+ * Encrypted router decorator function for the GET method.
55
+ *
56
+ * @param paths Path(s) of the HTTP request
57
+ * @returns Method decorator
58
+ */
59
+ export const Get = Generator("Get");
60
+
61
+ /**
62
+ * Encrypted router decorator function for the GET method.
63
+ *
64
+ * @param paths Path(s) of the HTTP request
65
+ * @returns Method decorator
66
+ */
67
+ export const Post = Generator("Post");
68
+
69
+ /**
70
+ * Encrypted router decorator function for the PATCH method.
71
+ *
72
+ * @param path Path of the HTTP request
73
+ * @returns Method decorator
74
+ */
75
+ export const Patch = Generator("Patch");
76
+
77
+ /**
78
+ * Encrypted router decorator function for the PUT method.
79
+ *
80
+ * @param path Path of the HTTP request
81
+ * @returns Method decorator
82
+ */
83
+ export const Put = Generator("Put");
84
+
85
+ /**
86
+ * Encrypted router decorator function for the DELETE method.
87
+ *
88
+ * @param path Path of the HTTP request
89
+ * @returns Method decorator
90
+ */
91
+ export const Delete = Generator("Delete");
92
+
93
+ function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
94
+ function route(path?: string | string[]): MethodDecorator;
95
+ function route<T>(
96
+ stringify?: IResponseBodyStringifier<T>,
97
+ ): MethodDecorator;
98
+ function route<T>(
99
+ path: string | string[],
100
+ stringify?: IResponseBodyStringifier<T>,
101
+ ): MethodDecorator;
102
+
103
+ function route(...args: any[]): MethodDecorator {
104
+ const [path, stringify] = get_path_and_stringify(
105
+ `EncryptedRoute.${method}`,
106
+ )(...args);
107
+ return applyDecorators(
108
+ ROUTERS[method](path),
109
+ UseInterceptors(
110
+ new EncryptedRouteInterceptor(method, stringify),
111
+ ),
112
+ );
113
+ }
114
+ return route;
115
+ }
116
+ }
117
+
118
+ for (const method of [
119
+ assertStringify,
120
+ isStringify,
121
+ stringify,
122
+ validateStringify,
123
+ ]) {
124
+ Object.assign(EncryptedRoute.Get, method);
125
+ Object.assign(EncryptedRoute.Delete, method);
126
+ Object.assign(EncryptedRoute.Post, method);
127
+ Object.assign(EncryptedRoute.Put, method);
128
+ Object.assign(EncryptedRoute.Patch, method);
129
+ }
130
+
131
+ /**
132
+ * @internal
133
+ */
134
+ class EncryptedRouteInterceptor implements NestInterceptor {
135
+ public constructor(
136
+ private readonly method: string,
137
+ private readonly stringify: (input: any) => string,
138
+ ) {}
139
+
140
+ public intercept(
141
+ context: ExecutionContext,
142
+ next: CallHandler,
143
+ ): Observable<any> {
144
+ const http: HttpArgumentsHost = context.switchToHttp();
145
+ return next.handle().pipe(
146
+ map((value) => {
147
+ const param:
148
+ | IEncryptionPassword
149
+ | IEncryptionPassword.Closure
150
+ | undefined = Reflect.getMetadata(
151
+ ENCRYPTION_METADATA_KEY,
152
+ context.getClass(),
153
+ );
154
+ if (!param)
155
+ throw new Error(
156
+ `Error on EncryptedRoute.${this.method}(): no encryption password is given.`,
157
+ );
158
+
159
+ const headers: Singleton<Record<string, string>> =
160
+ new Singleton(() => {
161
+ const request: express.Request = http.getRequest();
162
+ return headers_to_object(request.headers);
163
+ });
164
+ const body: string | undefined = this.stringify(value);
165
+ const password: IEncryptionPassword =
166
+ typeof param === "function"
167
+ ? param({ headers: headers.get(), body }, false)
168
+ : param;
169
+ const disabled: boolean =
170
+ password.disabled === undefined
171
+ ? false
172
+ : typeof password.disabled === "function"
173
+ ? password.disabled(
174
+ { headers: headers.get(), body },
175
+ false,
176
+ )
177
+ : password.disabled;
178
+
179
+ const response: express.Response = http.getResponse();
180
+ response.header(
181
+ "Content-Type",
182
+ disabled ? "application/json" : "text/plain",
183
+ );
184
+
185
+ if (disabled === true) return body;
186
+ else if (body === undefined) return body;
187
+ return AesPkcs5.encrypt(body, password.key, password.iv);
188
+ }),
189
+ catchError((err) => route_error(http.getRequest(), err)),
190
+ );
191
+ }
192
+ }
193
+
194
+ /**
195
+ * @internal
196
+ */
197
+ const ROUTERS = {
198
+ Get,
199
+ Post,
200
+ Put,
201
+ Patch,
202
+ Delete,
203
+ };
@@ -1,38 +1,38 @@
1
- import {
2
- BadRequestException,
3
- ExecutionContext,
4
- createParamDecorator,
5
- } from "@nestjs/common";
6
- import type express from "express";
7
- import raw from "raw-body";
8
-
9
- /**
10
- * Plain body decorator.
11
- *
12
- * `PlainBody` is a decorator function getting full body text from the HTTP request.
13
- *
14
- * If you adjust the regular {@link Body} decorator function to the body parameter,
15
- * you can't get the full body text because the {@link Body} tries to convert the
16
- * body text to JSON object. Therefore, `nestia-helper` provides this `PlainBody`
17
- * decorator function to get the full body text.
18
- *
19
- * ```typescript
20
- * \@TypedRoute.Post("memo")
21
- * public store
22
- * (
23
- * \@PlainBody() body: string
24
- * ): void;
25
- * ```
26
- *
27
- * @return Parameter decorator
28
- * @author Jeongho Nam - https://github.com/samchon
29
- */
30
- export const PlainBody: () => ParameterDecorator = createParamDecorator(
31
- async function PlainBody(_data: any, context: ExecutionContext) {
32
- const request: express.Request = context.switchToHttp().getRequest();
33
- if (!request.readable) throw new BadRequestException("Invalid body");
34
-
35
- const body: string = (await raw(request)).toString("utf8").trim();
36
- return body;
37
- },
38
- );
1
+ import {
2
+ BadRequestException,
3
+ ExecutionContext,
4
+ createParamDecorator,
5
+ } from "@nestjs/common";
6
+ import type express from "express";
7
+ import raw from "raw-body";
8
+
9
+ /**
10
+ * Plain body decorator.
11
+ *
12
+ * `PlainBody` is a decorator function getting full body text from the HTTP request.
13
+ *
14
+ * If you adjust the regular {@link Body} decorator function to the body parameter,
15
+ * you can't get the full body text because the {@link Body} tries to convert the
16
+ * body text to JSON object. Therefore, `@nestia/core` provides this `PlainBody`
17
+ * decorator function to get the full body text.
18
+ *
19
+ * ```typescript
20
+ * \@TypedRoute.Post("memo")
21
+ * public store
22
+ * (
23
+ * \@PlainBody() body: string
24
+ * ): void;
25
+ * ```
26
+ *
27
+ * @return Parameter decorator
28
+ * @author Jeongho Nam - https://github.com/samchon
29
+ */
30
+ export const PlainBody: () => ParameterDecorator = createParamDecorator(
31
+ async function PlainBody(_data: any, context: ExecutionContext) {
32
+ const request: express.Request = context.switchToHttp().getRequest();
33
+ if (!request.readable) throw new BadRequestException("Invalid body");
34
+
35
+ const body: string = (await raw(request)).toString("utf8").trim();
36
+ return body;
37
+ },
38
+ );