@nestia/core 2.4.5 → 2.4.6-dev.20240123
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/TypedQuery.js.map +1 -1
- package/lib/decorators/internal/load_controller.js.map +1 -1
- package/lib/transformers/NodeTransformer.js.map +1 -1
- package/package.json +4 -3
- package/src/decorators/DynamicModule.ts +39 -39
- package/src/decorators/EncryptedBody.ts +102 -102
- package/src/decorators/EncryptedController.ts +38 -38
- package/src/decorators/EncryptedModule.ts +93 -93
- package/src/decorators/EncryptedRoute.ts +182 -182
- package/src/decorators/PlainBody.ts +72 -72
- package/src/decorators/TypedBody.ts +59 -59
- package/src/decorators/TypedException.ts +89 -89
- package/src/decorators/TypedHeaders.ts +69 -69
- package/src/decorators/TypedParam.ts +65 -65
- package/src/decorators/TypedQuery.ts +2 -2
- package/src/decorators/TypedRoute.ts +1 -1
- 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 -103
- package/src/decorators/internal/get_path_and_stringify.ts +91 -91
- 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 +2 -2
- package/src/decorators/internal/route_error.ts +45 -45
- package/src/decorators/internal/validate_request_body.ts +57 -57
- package/src/decorators/internal/validate_request_headers.ts +68 -68
- package/src/decorators/internal/validate_request_query.ts +56 -56
- 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 -52
- package/src/programmers/TypedBodyProgrammer.ts +108 -108
- package/src/programmers/TypedExceptionProgrammer.ts +72 -72
- package/src/programmers/TypedHeadersProgrammer.ts +56 -56
- package/src/programmers/TypedParamProgrammer.ts +24 -24
- package/src/programmers/TypedQueryBodyProgrammer.ts +56 -56
- package/src/programmers/TypedQueryProgrammer.ts +56 -56
- package/src/programmers/TypedQueryRouteProgrammer.ts +51 -51
- package/src/programmers/TypedRouteProgrammer.ts +51 -51
- package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +58 -58
- package/src/programmers/http/HttpIsQuerifyProgrammer.ts +62 -62
- package/src/programmers/http/HttpQuerifyProgrammer.ts +96 -96
- package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +63 -63
- package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
- package/src/transform.ts +35 -35
- package/src/transformers/FileTransformer.ts +66 -66
- package/src/transformers/MethodTransformer.ts +94 -94
- package/src/transformers/NodeTransformer.ts +2 -2
- package/src/transformers/ParameterDecoratorTransformer.ts +121 -121
- package/src/transformers/ParameterTransformer.ts +48 -48
- package/src/transformers/TypedExceptionTransformer.ts +49 -49
- package/src/transformers/TypedRouteTransformer.ts +95 -95
- 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 -57
|
@@ -1,182 +1,182 @@
|
|
|
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 { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
|
|
23
|
-
import { NoTransformConfigureError } from "./internal/NoTransformConfigureError";
|
|
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
|
-
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
|
-
);
|
|
107
|
-
}
|
|
108
|
-
return route;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
for (const method of [
|
|
113
|
-
typia.json.isStringify,
|
|
114
|
-
typia.json.assertStringify,
|
|
115
|
-
typia.json.validateStringify,
|
|
116
|
-
typia.json.stringify,
|
|
117
|
-
])
|
|
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;
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* @internal
|
|
130
|
-
*/
|
|
131
|
-
class EncryptedRouteInterceptor implements NestInterceptor {
|
|
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(),
|
|
147
|
-
);
|
|
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
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* @internal
|
|
175
|
-
*/
|
|
176
|
-
const ROUTERS = {
|
|
177
|
-
Get,
|
|
178
|
-
Post,
|
|
179
|
-
Put,
|
|
180
|
-
Patch,
|
|
181
|
-
Delete,
|
|
182
|
-
};
|
|
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 { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
|
|
23
|
+
import { NoTransformConfigureError } from "./internal/NoTransformConfigureError";
|
|
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
|
+
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
|
+
);
|
|
107
|
+
}
|
|
108
|
+
return route;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
for (const method of [
|
|
113
|
+
typia.json.isStringify,
|
|
114
|
+
typia.json.assertStringify,
|
|
115
|
+
typia.json.validateStringify,
|
|
116
|
+
typia.json.stringify,
|
|
117
|
+
])
|
|
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;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @internal
|
|
130
|
+
*/
|
|
131
|
+
class EncryptedRouteInterceptor implements NestInterceptor {
|
|
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(),
|
|
147
|
+
);
|
|
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
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @internal
|
|
175
|
+
*/
|
|
176
|
+
const ROUTERS = {
|
|
177
|
+
Get,
|
|
178
|
+
Post,
|
|
179
|
+
Put,
|
|
180
|
+
Patch,
|
|
181
|
+
Delete,
|
|
182
|
+
};
|
|
@@ -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");
|