@nestia/core 2.4.2 → 2.4.3-dev.20231207
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.
- package/lib/decorators/DynamicModule.js.map +1 -1
- package/lib/decorators/EncryptedBody.js.map +1 -1
- package/lib/decorators/EncryptedController.js.map +1 -1
- package/lib/decorators/EncryptedModule.js.map +1 -1
- package/lib/decorators/EncryptedRoute.js +0 -2
- package/lib/decorators/EncryptedRoute.js.map +1 -1
- package/lib/decorators/PlainBody.js.map +1 -1
- package/lib/decorators/TypedBody.js.map +1 -1
- package/lib/decorators/TypedException.js.map +1 -1
- package/lib/decorators/TypedHeaders.js.map +1 -1
- package/lib/decorators/TypedParam.js.map +1 -1
- package/lib/decorators/TypedQuery.js +1 -5
- package/lib/decorators/TypedQuery.js.map +1 -1
- package/lib/decorators/TypedRoute.js.map +1 -1
- package/lib/decorators/internal/NoTransformConfigureError.js.map +1 -1
- package/lib/decorators/internal/get_path_and_querify.js.map +1 -1
- package/lib/decorators/internal/get_path_and_stringify.js.map +1 -1
- package/lib/decorators/internal/get_text_body.js.map +1 -1
- package/lib/decorators/internal/headers_to_object.js.map +1 -1
- package/lib/decorators/internal/load_controller.js.map +1 -1
- package/lib/decorators/internal/route_error.js.map +1 -1
- package/lib/decorators/internal/validate_request_body.js.map +1 -1
- package/lib/decorators/internal/validate_request_headers.js.map +1 -1
- package/lib/decorators/internal/validate_request_query.js.map +1 -1
- package/lib/programmers/PlainBodyProgrammer.js.map +1 -1
- package/lib/programmers/TypedBodyProgrammer.js.map +1 -1
- package/lib/programmers/TypedExceptionProgrammer.js.map +1 -1
- package/lib/programmers/TypedHeadersProgrammer.js.map +1 -1
- package/lib/programmers/TypedParamProgrammer.js.map +1 -1
- package/lib/programmers/TypedQueryBodyProgrammer.js.map +1 -1
- package/lib/programmers/TypedQueryProgrammer.js.map +1 -1
- package/lib/programmers/TypedQueryRouteProgrammer.js.map +1 -1
- package/lib/programmers/TypedRouteProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpAssertQuerifyProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpIsQuerifyProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpQuerifyProgrammer.js.map +1 -1
- package/lib/programmers/http/HttpValidateQuerifyProgrammer.js.map +1 -1
- package/lib/programmers/internal/CoreMetadataUtil.js.map +1 -1
- package/lib/transform.js.map +1 -1
- package/lib/transformers/FileTransformer.js.map +1 -1
- package/lib/transformers/MethodTransformer.js +1 -2
- package/lib/transformers/MethodTransformer.js.map +1 -1
- package/lib/transformers/NodeTransformer.js.map +1 -1
- package/lib/transformers/ParameterDecoratorTransformer.js +1 -3
- package/lib/transformers/ParameterDecoratorTransformer.js.map +1 -1
- package/lib/transformers/ParameterTransformer.js.map +1 -1
- package/lib/transformers/TypedExceptionTransformer.js.map +1 -1
- package/lib/transformers/TypedRouteTransformer.js +1 -2
- package/lib/transformers/TypedRouteTransformer.js.map +1 -1
- package/lib/utils/ExceptionManager.js.map +1 -1
- package/lib/utils/Singleton.js.map +1 -1
- package/lib/utils/SourceFinder.js +1 -3
- package/lib/utils/SourceFinder.js.map +1 -1
- package/package.json +3 -6
- package/src/decorators/DynamicModule.ts +39 -39
- package/src/decorators/EncryptedBody.ts +49 -57
- package/src/decorators/EncryptedController.ts +38 -38
- package/src/decorators/EncryptedModule.ts +79 -79
- package/src/decorators/EncryptedRoute.ts +126 -135
- package/src/decorators/PlainBody.ts +72 -75
- package/src/decorators/TypedBody.ts +59 -60
- package/src/decorators/TypedException.ts +89 -89
- package/src/decorators/TypedHeaders.ts +69 -70
- package/src/decorators/TypedParam.ts +65 -66
- package/src/decorators/TypedQuery.ts +246 -253
- package/src/decorators/TypedRoute.ts +144 -147
- package/src/decorators/internal/EncryptedConstant.ts +4 -4
- package/src/decorators/internal/NoTransformConfigureError.ts +8 -8
- package/src/decorators/internal/get_path_and_querify.ts +103 -104
- package/src/decorators/internal/get_path_and_stringify.ts +91 -94
- package/src/decorators/internal/get_text_body.ts +20 -20
- package/src/decorators/internal/headers_to_object.ts +13 -13
- package/src/decorators/internal/load_controller.ts +51 -51
- package/src/decorators/internal/route_error.ts +45 -45
- package/src/decorators/internal/validate_request_body.ts +57 -61
- package/src/decorators/internal/validate_request_headers.ts +68 -73
- package/src/decorators/internal/validate_request_query.ts +56 -59
- package/src/index.ts +5 -5
- package/src/module.ts +14 -14
- package/src/options/INestiaTransformOptions.ts +17 -17
- package/src/options/INestiaTransformProject.ts +7 -7
- package/src/options/IRequestBodyValidator.ts +20 -20
- package/src/options/IRequestHeadersValidator.ts +22 -22
- package/src/options/IRequestQueryValidator.ts +20 -20
- package/src/options/IResponseBodyQuerifier.ts +25 -25
- package/src/options/IResponseBodyStringifier.ts +25 -25
- package/src/programmers/PlainBodyProgrammer.ts +52 -56
- package/src/programmers/TypedBodyProgrammer.ts +108 -112
- package/src/programmers/TypedExceptionProgrammer.ts +72 -76
- package/src/programmers/TypedHeadersProgrammer.ts +56 -59
- package/src/programmers/TypedParamProgrammer.ts +24 -25
- package/src/programmers/TypedQueryBodyProgrammer.ts +56 -57
- package/src/programmers/TypedQueryProgrammer.ts +56 -57
- package/src/programmers/TypedQueryRouteProgrammer.ts +51 -55
- package/src/programmers/TypedRouteProgrammer.ts +51 -55
- package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +58 -59
- package/src/programmers/http/HttpIsQuerifyProgrammer.ts +62 -63
- package/src/programmers/http/HttpQuerifyProgrammer.ts +96 -105
- package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +63 -64
- package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
- package/src/transform.ts +35 -36
- package/src/transformers/FileTransformer.ts +66 -67
- package/src/transformers/MethodTransformer.ts +94 -97
- package/src/transformers/NodeTransformer.ts +16 -16
- package/src/transformers/ParameterDecoratorTransformer.ts +121 -126
- package/src/transformers/ParameterTransformer.ts +48 -53
- package/src/transformers/TypedExceptionTransformer.ts +49 -51
- package/src/transformers/TypedRouteTransformer.ts +95 -96
- package/src/typings/Creator.ts +3 -3
- package/src/utils/ExceptionManager.ts +112 -112
- package/src/utils/Singleton.ts +20 -20
- package/src/utils/SourceFinder.ts +57 -60
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import { AesPkcs5 } from "@nestia/fetcher/lib/AesPkcs5";
|
|
2
2
|
import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
4
|
+
CallHandler,
|
|
5
|
+
Delete,
|
|
6
|
+
ExecutionContext,
|
|
7
|
+
Get,
|
|
8
|
+
NestInterceptor,
|
|
9
|
+
Patch,
|
|
10
|
+
Post,
|
|
11
|
+
Put,
|
|
12
|
+
UseInterceptors,
|
|
13
|
+
applyDecorators,
|
|
14
14
|
} from "@nestjs/common";
|
|
15
15
|
import { HttpArgumentsHost } from "@nestjs/common/interfaces";
|
|
16
16
|
import express from "express";
|
|
17
17
|
import { catchError, map } from "rxjs/operators";
|
|
18
|
-
|
|
19
18
|
import typia from "typia";
|
|
20
19
|
|
|
21
20
|
import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
|
|
@@ -47,145 +46,137 @@ import { route_error } from "./internal/route_error";
|
|
|
47
46
|
* @author Jeongho Nam - https://github.com/samchon
|
|
48
47
|
*/
|
|
49
48
|
export namespace EncryptedRoute {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
),
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
return route;
|
|
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
|
+
function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
|
|
90
|
+
function route(path?: string | string[]): MethodDecorator;
|
|
91
|
+
function route<T>(
|
|
92
|
+
stringify?: IResponseBodyStringifier<T> | null,
|
|
93
|
+
): MethodDecorator;
|
|
94
|
+
function route<T>(
|
|
95
|
+
path: string | string[],
|
|
96
|
+
stringify?: IResponseBodyStringifier<T> | null,
|
|
97
|
+
): MethodDecorator;
|
|
98
|
+
|
|
99
|
+
function route(...args: any[]): MethodDecorator {
|
|
100
|
+
const [path, stringify] = get_path_and_stringify(
|
|
101
|
+
`EncryptedRoute.${method}`,
|
|
102
|
+
)(...args);
|
|
103
|
+
return applyDecorators(
|
|
104
|
+
ROUTERS[method](path),
|
|
105
|
+
UseInterceptors(new EncryptedRouteInterceptor(method, stringify)),
|
|
106
|
+
);
|
|
112
107
|
}
|
|
108
|
+
return route;
|
|
109
|
+
}
|
|
113
110
|
}
|
|
114
111
|
|
|
115
112
|
for (const method of [
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
113
|
+
typia.json.isStringify,
|
|
114
|
+
typia.json.assertStringify,
|
|
115
|
+
typia.json.validateStringify,
|
|
116
|
+
typia.json.stringify,
|
|
120
117
|
])
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
118
|
+
for (const [key, value] of Object.entries(method))
|
|
119
|
+
for (const deco of [
|
|
120
|
+
EncryptedRoute.Get,
|
|
121
|
+
EncryptedRoute.Delete,
|
|
122
|
+
EncryptedRoute.Post,
|
|
123
|
+
EncryptedRoute.Put,
|
|
124
|
+
EncryptedRoute.Patch,
|
|
125
|
+
])
|
|
126
|
+
(deco as any)[key] = value;
|
|
130
127
|
|
|
131
128
|
/**
|
|
132
129
|
* @internal
|
|
133
130
|
*/
|
|
134
131
|
class EncryptedRouteInterceptor implements NestInterceptor {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
);
|
|
151
|
-
if (!param)
|
|
152
|
-
throw NoTransformConfigureError(
|
|
153
|
-
`EncryptedRoute.${this.method}`,
|
|
154
|
-
);
|
|
155
|
-
|
|
156
|
-
const headers: Singleton<Record<string, string>> =
|
|
157
|
-
new Singleton(() => {
|
|
158
|
-
const request: express.Request = http.getRequest();
|
|
159
|
-
return headers_to_object(request.headers);
|
|
160
|
-
});
|
|
161
|
-
const body: string | undefined = this.stringify(value);
|
|
162
|
-
const password: IEncryptionPassword =
|
|
163
|
-
typeof param === "function"
|
|
164
|
-
? param({
|
|
165
|
-
headers: headers.get(),
|
|
166
|
-
body,
|
|
167
|
-
direction: "encode",
|
|
168
|
-
})
|
|
169
|
-
: param;
|
|
170
|
-
|
|
171
|
-
const response: express.Response = http.getResponse();
|
|
172
|
-
response.header("Content-Type", "text/plain");
|
|
173
|
-
|
|
174
|
-
if (body === undefined) return body;
|
|
175
|
-
return AesPkcs5.encrypt(body, password.key, password.iv);
|
|
176
|
-
}),
|
|
177
|
-
catchError((err) => route_error(http.getRequest(), err)),
|
|
132
|
+
public constructor(
|
|
133
|
+
private readonly method: string,
|
|
134
|
+
private readonly stringify: (input: any) => string,
|
|
135
|
+
) {}
|
|
136
|
+
|
|
137
|
+
public intercept(context: ExecutionContext, next: CallHandler) {
|
|
138
|
+
const http: HttpArgumentsHost = context.switchToHttp();
|
|
139
|
+
return next.handle().pipe(
|
|
140
|
+
map((value) => {
|
|
141
|
+
const param:
|
|
142
|
+
| IEncryptionPassword
|
|
143
|
+
| IEncryptionPassword.Closure
|
|
144
|
+
| undefined = Reflect.getMetadata(
|
|
145
|
+
ENCRYPTION_METADATA_KEY,
|
|
146
|
+
context.getClass(),
|
|
178
147
|
);
|
|
179
|
-
|
|
148
|
+
if (!param)
|
|
149
|
+
throw NoTransformConfigureError(`EncryptedRoute.${this.method}`);
|
|
150
|
+
|
|
151
|
+
const headers: Singleton<Record<string, string>> = new Singleton(() => {
|
|
152
|
+
const request: express.Request = http.getRequest();
|
|
153
|
+
return headers_to_object(request.headers);
|
|
154
|
+
});
|
|
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);
|
|
167
|
+
}),
|
|
168
|
+
catchError((err) => route_error(http.getRequest(), err)),
|
|
169
|
+
);
|
|
170
|
+
}
|
|
180
171
|
}
|
|
181
172
|
|
|
182
173
|
/**
|
|
183
174
|
* @internal
|
|
184
175
|
*/
|
|
185
176
|
const ROUTERS = {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
177
|
+
Get,
|
|
178
|
+
Post,
|
|
179
|
+
Put,
|
|
180
|
+
Patch,
|
|
181
|
+
Delete,
|
|
191
182
|
};
|
|
@@ -1,75 +1,72 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
} from "@nestjs/common";
|
|
6
|
-
import type express from "express";
|
|
7
|
-
import type { FastifyRequest } from "fastify";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* @
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
text
|
|
73
|
-
.split(";")
|
|
74
|
-
.map((str) => str.trim())
|
|
75
|
-
.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,60 +1,59 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
} from "@nestjs/common";
|
|
6
|
-
import type express from "express";
|
|
7
|
-
import type { FastifyRequest } from "fastify";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* @
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
Object.assign(TypedBody,
|
|
51
|
-
Object.assign(TypedBody,
|
|
52
|
-
Object.assign(TypedBody,
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
.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");
|