@nestia/core 8.0.8 → 9.0.0-dev.20251107
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/LICENSE +21 -21
- package/README.md +93 -93
- package/package.json +7 -8
- package/src/adaptors/WebSocketAdaptor.ts +429 -429
- package/src/decorators/DynamicModule.ts +44 -44
- package/src/decorators/EncryptedBody.ts +97 -97
- package/src/decorators/EncryptedController.ts +40 -40
- package/src/decorators/EncryptedModule.ts +98 -98
- package/src/decorators/EncryptedRoute.ts +213 -213
- package/src/decorators/HumanRoute.ts +22 -22
- package/src/decorators/NoTransformConfigurationError.ts +34 -34
- package/src/decorators/PlainBody.ts +76 -76
- package/src/decorators/SwaggerCustomizer.ts +97 -97
- package/src/decorators/SwaggerExample.ts +100 -100
- package/src/decorators/TypedBody.ts +57 -57
- package/src/decorators/TypedException.ts +147 -147
- package/src/decorators/TypedFormData.ts +195 -195
- package/src/decorators/TypedHeaders.ts +66 -66
- package/src/decorators/TypedParam.ts +77 -77
- package/src/decorators/TypedQuery.ts +234 -234
- package/src/decorators/TypedRoute.ts +196 -196
- package/src/decorators/WebSocketRoute.ts +242 -242
- package/src/decorators/internal/EncryptedConstant.ts +2 -2
- package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
- package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
- package/src/decorators/internal/get_path_and_querify.ts +94 -94
- package/src/decorators/internal/get_path_and_stringify.ts +110 -110
- package/src/decorators/internal/get_text_body.ts +16 -16
- package/src/decorators/internal/headers_to_object.ts +11 -11
- package/src/decorators/internal/is_request_body_undefined.ts +12 -12
- package/src/decorators/internal/load_controller.ts +45 -45
- package/src/decorators/internal/route_error.ts +43 -43
- package/src/decorators/internal/validate_request_body.ts +64 -64
- package/src/decorators/internal/validate_request_form_data.ts +67 -67
- package/src/decorators/internal/validate_request_headers.ts +76 -76
- package/src/decorators/internal/validate_request_query.ts +64 -64
- package/src/index.ts +5 -5
- package/src/module.ts +22 -22
- package/src/options/INestiaTransformOptions.ts +38 -38
- package/src/options/INestiaTransformProject.ts +8 -8
- package/src/options/IRequestBodyValidator.ts +20 -20
- package/src/options/IRequestFormDataProps.ts +27 -27
- 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 +30 -30
- package/src/programmers/PlainBodyProgrammer.ts +70 -70
- package/src/programmers/TypedBodyProgrammer.ts +142 -142
- package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
- package/src/programmers/TypedHeadersProgrammer.ts +63 -63
- package/src/programmers/TypedParamProgrammer.ts +33 -33
- package/src/programmers/TypedQueryBodyProgrammer.ts +112 -112
- package/src/programmers/TypedQueryProgrammer.ts +114 -114
- package/src/programmers/TypedQueryRouteProgrammer.ts +105 -105
- package/src/programmers/TypedRouteProgrammer.ts +94 -94
- package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +72 -72
- package/src/programmers/http/HttpIsQuerifyProgrammer.ts +75 -75
- package/src/programmers/http/HttpQuerifyProgrammer.ts +108 -108
- package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +76 -76
- package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
- package/src/transform.ts +35 -35
- package/src/transformers/FileTransformer.ts +110 -110
- package/src/transformers/MethodTransformer.ts +103 -103
- package/src/transformers/NodeTransformer.ts +23 -23
- package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
- package/src/transformers/ParameterTransformer.ts +57 -57
- package/src/transformers/TypedRouteTransformer.ts +85 -85
- package/src/transformers/WebSocketRouteTransformer.ts +120 -120
- package/src/typings/Creator.ts +3 -3
- package/src/typings/get-function-location.d.ts +7 -7
- package/src/utils/ArrayUtil.ts +7 -7
- package/src/utils/ExceptionManager.ts +115 -115
- package/src/utils/Singleton.ts +16 -16
- package/src/utils/SourceFinder.ts +54 -54
- package/src/utils/VersioningStrategy.ts +27 -27
|
@@ -1,77 +1,77 @@
|
|
|
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 typia, { IValidation, TypeGuardError } from "typia";
|
|
9
|
-
|
|
10
|
-
import { NoTransformConfigurationError } from "./NoTransformConfigurationError";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Type safe URL parameter decorator.
|
|
14
|
-
*
|
|
15
|
-
* `TypedParam` is a decorator function getting specific typed parameter from
|
|
16
|
-
* the HTTP request URL. It's almost same with the {@link nest.Param}, but
|
|
17
|
-
* `TypedParam` automatically casts parameter value to be following its type,
|
|
18
|
-
* and validates it.
|
|
19
|
-
*
|
|
20
|
-
* ```typescript
|
|
21
|
-
* import { tags } from "typia";
|
|
22
|
-
*
|
|
23
|
-
* \@TypedRoute.Get("shopping/sales/:id/:no/:paused")
|
|
24
|
-
* public async pause(
|
|
25
|
-
* \@TypedParam("id", "uuid"), id: string & tags.Format<"uuid">,
|
|
26
|
-
* \@TypedParam("no") id: number & tags.Type<"uint32">
|
|
27
|
-
* \@TypedParam("paused") paused: boolean | null
|
|
28
|
-
* ): Promise<void>;
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
32
|
-
* @param name URL Parameter name
|
|
33
|
-
* @returns Parameter decorator
|
|
34
|
-
*/
|
|
35
|
-
export function TypedParam<T extends boolean | bigint | number | string | null>(
|
|
36
|
-
name: string,
|
|
37
|
-
assert?: (value: string) => T,
|
|
38
|
-
validate?: boolean,
|
|
39
|
-
): ParameterDecorator {
|
|
40
|
-
if (assert === undefined) {
|
|
41
|
-
NoTransformConfigurationError("TypedParam");
|
|
42
|
-
assert = (value) => value as T;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return createParamDecorator(function TypedParam(
|
|
46
|
-
{}: any,
|
|
47
|
-
context: ExecutionContext,
|
|
48
|
-
) {
|
|
49
|
-
const request: express.Request | FastifyRequest = context
|
|
50
|
-
.switchToHttp()
|
|
51
|
-
.getRequest();
|
|
52
|
-
const str: string = (request.params as any)[name];
|
|
53
|
-
try {
|
|
54
|
-
return assert(str);
|
|
55
|
-
} catch (exp) {
|
|
56
|
-
if (typia.is<TypeGuardError>(exp)) {
|
|
57
|
-
const trace: IValidation.IError = {
|
|
58
|
-
path: exp.path ?? "$input",
|
|
59
|
-
expected: exp.expected,
|
|
60
|
-
value: exp.value,
|
|
61
|
-
};
|
|
62
|
-
throw new BadRequestException({
|
|
63
|
-
message: `Invalid URL parameter value on "${name}".`,
|
|
64
|
-
...(validate === true
|
|
65
|
-
? {
|
|
66
|
-
errors: [trace],
|
|
67
|
-
}
|
|
68
|
-
: {
|
|
69
|
-
...trace,
|
|
70
|
-
reason: exp.message,
|
|
71
|
-
}),
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
throw exp;
|
|
75
|
-
}
|
|
76
|
-
})(name);
|
|
77
|
-
}
|
|
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 typia, { IValidation, TypeGuardError } from "typia";
|
|
9
|
+
|
|
10
|
+
import { NoTransformConfigurationError } from "./NoTransformConfigurationError";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Type safe URL parameter decorator.
|
|
14
|
+
*
|
|
15
|
+
* `TypedParam` is a decorator function getting specific typed parameter from
|
|
16
|
+
* the HTTP request URL. It's almost same with the {@link nest.Param}, but
|
|
17
|
+
* `TypedParam` automatically casts parameter value to be following its type,
|
|
18
|
+
* and validates it.
|
|
19
|
+
*
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { tags } from "typia";
|
|
22
|
+
*
|
|
23
|
+
* \@TypedRoute.Get("shopping/sales/:id/:no/:paused")
|
|
24
|
+
* public async pause(
|
|
25
|
+
* \@TypedParam("id", "uuid"), id: string & tags.Format<"uuid">,
|
|
26
|
+
* \@TypedParam("no") id: number & tags.Type<"uint32">
|
|
27
|
+
* \@TypedParam("paused") paused: boolean | null
|
|
28
|
+
* ): Promise<void>;
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
32
|
+
* @param name URL Parameter name
|
|
33
|
+
* @returns Parameter decorator
|
|
34
|
+
*/
|
|
35
|
+
export function TypedParam<T extends boolean | bigint | number | string | null>(
|
|
36
|
+
name: string,
|
|
37
|
+
assert?: (value: string) => T,
|
|
38
|
+
validate?: boolean,
|
|
39
|
+
): ParameterDecorator {
|
|
40
|
+
if (assert === undefined) {
|
|
41
|
+
NoTransformConfigurationError("TypedParam");
|
|
42
|
+
assert = (value) => value as T;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return createParamDecorator(function TypedParam(
|
|
46
|
+
{}: any,
|
|
47
|
+
context: ExecutionContext,
|
|
48
|
+
) {
|
|
49
|
+
const request: express.Request | FastifyRequest = context
|
|
50
|
+
.switchToHttp()
|
|
51
|
+
.getRequest();
|
|
52
|
+
const str: string = (request.params as any)[name];
|
|
53
|
+
try {
|
|
54
|
+
return assert(str);
|
|
55
|
+
} catch (exp) {
|
|
56
|
+
if (typia.is<TypeGuardError>(exp)) {
|
|
57
|
+
const trace: IValidation.IError = {
|
|
58
|
+
path: exp.path ?? "$input",
|
|
59
|
+
expected: exp.expected,
|
|
60
|
+
value: exp.value,
|
|
61
|
+
};
|
|
62
|
+
throw new BadRequestException({
|
|
63
|
+
message: `Invalid URL parameter value on "${name}".`,
|
|
64
|
+
...(validate === true
|
|
65
|
+
? {
|
|
66
|
+
errors: [trace],
|
|
67
|
+
}
|
|
68
|
+
: {
|
|
69
|
+
...trace,
|
|
70
|
+
reason: exp.message,
|
|
71
|
+
}),
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
throw exp;
|
|
75
|
+
}
|
|
76
|
+
})(name);
|
|
77
|
+
}
|
|
@@ -1,234 +1,234 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BadRequestException,
|
|
3
|
-
CallHandler,
|
|
4
|
-
Delete,
|
|
5
|
-
ExecutionContext,
|
|
6
|
-
Get,
|
|
7
|
-
NestInterceptor,
|
|
8
|
-
Patch,
|
|
9
|
-
Post,
|
|
10
|
-
Put,
|
|
11
|
-
UseInterceptors,
|
|
12
|
-
applyDecorators,
|
|
13
|
-
createParamDecorator,
|
|
14
|
-
} from "@nestjs/common";
|
|
15
|
-
import { HttpArgumentsHost } from "@nestjs/common/interfaces";
|
|
16
|
-
import type express from "express";
|
|
17
|
-
import type { FastifyRequest } from "fastify";
|
|
18
|
-
import { catchError, map } from "rxjs";
|
|
19
|
-
import typia from "typia";
|
|
20
|
-
|
|
21
|
-
import { IRequestQueryValidator } from "../options/IRequestQueryValidator";
|
|
22
|
-
import { IResponseBodyQuerifier } from "../options/IResponseBodyQuerifier";
|
|
23
|
-
import { get_path_and_querify } from "./internal/get_path_and_querify";
|
|
24
|
-
import { route_error } from "./internal/route_error";
|
|
25
|
-
import { validate_request_query } from "./internal/validate_request_query";
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Type safe URL query decorator.
|
|
29
|
-
*
|
|
30
|
-
* `TypedQuery` is a decorator function that can parse URL query string. It is
|
|
31
|
-
* almost same with {@link nest.Query}, but it can automatically cast property
|
|
32
|
-
* type following its DTO definition. Also, `TypedQuery` performs type
|
|
33
|
-
* validation.
|
|
34
|
-
*
|
|
35
|
-
* For reference, target type `T` must follow such restriction. Also, if actual
|
|
36
|
-
* URL query parameter values are different with their promised type `T`,
|
|
37
|
-
* `BadRequestException` error (status code: 400) would be thrown.
|
|
38
|
-
*
|
|
39
|
-
* 1. Type `T` must be an object type
|
|
40
|
-
* 2. Do not allow dynamic property
|
|
41
|
-
* 3. Only `boolean`, `bigint`, `number`, `string` or their array types are allowed
|
|
42
|
-
* 4. By the way, union type never be not allowed
|
|
43
|
-
*
|
|
44
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
45
|
-
* @returns Parameter decorator
|
|
46
|
-
*/
|
|
47
|
-
export function TypedQuery<T extends object>(
|
|
48
|
-
validator?: IRequestQueryValidator<T>,
|
|
49
|
-
): ParameterDecorator {
|
|
50
|
-
const checker = validate_request_query("TypedQuery")(validator);
|
|
51
|
-
return createParamDecorator(function TypedQuery(
|
|
52
|
-
_unknown: any,
|
|
53
|
-
context: ExecutionContext,
|
|
54
|
-
) {
|
|
55
|
-
const request: express.Request | FastifyRequest = context
|
|
56
|
-
.switchToHttp()
|
|
57
|
-
.getRequest();
|
|
58
|
-
const params: URLSearchParams = new URLSearchParams(tail(request.url));
|
|
59
|
-
|
|
60
|
-
const output: T | Error = checker(params);
|
|
61
|
-
if (output instanceof Error) throw output;
|
|
62
|
-
return output;
|
|
63
|
-
})();
|
|
64
|
-
}
|
|
65
|
-
export namespace TypedQuery {
|
|
66
|
-
/**
|
|
67
|
-
* Request body decorator.
|
|
68
|
-
*
|
|
69
|
-
* Request body decorator for the `application/x-www-form-urlencoded` type.
|
|
70
|
-
*/
|
|
71
|
-
export function Body<T extends object>(
|
|
72
|
-
validator?: IRequestQueryValidator<T>,
|
|
73
|
-
): ParameterDecorator {
|
|
74
|
-
const checker = validate_request_query("TypedQuery.Body")(validator);
|
|
75
|
-
return createParamDecorator(function TypedQueryBody(
|
|
76
|
-
_unknown: any,
|
|
77
|
-
context: ExecutionContext,
|
|
78
|
-
) {
|
|
79
|
-
const request: express.Request | FastifyRequest = context
|
|
80
|
-
.switchToHttp()
|
|
81
|
-
.getRequest();
|
|
82
|
-
if (isApplicationQuery(request.headers["content-type"]) === false)
|
|
83
|
-
throw new BadRequestException(
|
|
84
|
-
`Request body type is not "application/x-www-form-urlencoded".`,
|
|
85
|
-
);
|
|
86
|
-
const params: URLSearchParams =
|
|
87
|
-
request.body instanceof URLSearchParams
|
|
88
|
-
? request.body
|
|
89
|
-
: (new FakeURLSearchParams(request.body) as any);
|
|
90
|
-
|
|
91
|
-
const output: T | Error = checker(params);
|
|
92
|
-
if (output instanceof Error) throw output;
|
|
93
|
-
return output;
|
|
94
|
-
})();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Router decorator function for the GET method.
|
|
99
|
-
*
|
|
100
|
-
* @param path Path of the HTTP request
|
|
101
|
-
* @returns Method decorator
|
|
102
|
-
*/
|
|
103
|
-
export const Get = Generator("Get");
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Router decorator function for the POST method.
|
|
107
|
-
*
|
|
108
|
-
* @param path Path of the HTTP request
|
|
109
|
-
* @returns Method decorator
|
|
110
|
-
*/
|
|
111
|
-
export const Post = Generator("Post");
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Router decorator function for the PATH method.
|
|
115
|
-
*
|
|
116
|
-
* @param path Path of the HTTP request
|
|
117
|
-
* @returns Method decorator
|
|
118
|
-
*/
|
|
119
|
-
export const Patch = Generator("Patch");
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Router decorator function for the PUT method.
|
|
123
|
-
*
|
|
124
|
-
* @param path Path of the HTTP request
|
|
125
|
-
* @returns Method decorator
|
|
126
|
-
*/
|
|
127
|
-
export const Put = Generator("Put");
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Router decorator function for the DELETE method.
|
|
131
|
-
*
|
|
132
|
-
* @param path Path of the HTTP request
|
|
133
|
-
* @returns Method decorator
|
|
134
|
-
*/
|
|
135
|
-
export const Delete = Generator("Delete");
|
|
136
|
-
|
|
137
|
-
/** @internal */
|
|
138
|
-
function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
|
|
139
|
-
function route(path?: string | string[]): MethodDecorator;
|
|
140
|
-
function route<T>(stringify?: IResponseBodyQuerifier<T>): MethodDecorator;
|
|
141
|
-
function route<T>(
|
|
142
|
-
path: string | string[],
|
|
143
|
-
stringify?: IResponseBodyQuerifier<T>,
|
|
144
|
-
): MethodDecorator;
|
|
145
|
-
|
|
146
|
-
function route(...args: any[]): MethodDecorator {
|
|
147
|
-
const [path, stringify] = get_path_and_querify(`TypedQuery.${method}`)(
|
|
148
|
-
...args,
|
|
149
|
-
);
|
|
150
|
-
return applyDecorators(
|
|
151
|
-
ROUTERS[method](path),
|
|
152
|
-
UseInterceptors(new TypedQueryRouteInterceptor(stringify)),
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
return route;
|
|
156
|
-
}
|
|
157
|
-
for (const method of [typia.assert, typia.is, typia.validate])
|
|
158
|
-
for (const [key, value] of Object.entries(method))
|
|
159
|
-
for (const deco of [
|
|
160
|
-
TypedQuery.Get,
|
|
161
|
-
TypedQuery.Delete,
|
|
162
|
-
TypedQuery.Post,
|
|
163
|
-
TypedQuery.Put,
|
|
164
|
-
TypedQuery.Patch,
|
|
165
|
-
])
|
|
166
|
-
(deco as any)[key] = value;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/** @internal */
|
|
170
|
-
function tail(url: string): string {
|
|
171
|
-
const index: number = url.indexOf("?");
|
|
172
|
-
return index === -1 ? "" : url.substring(index + 1);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/** @internal */
|
|
176
|
-
function isApplicationQuery(text?: string): boolean {
|
|
177
|
-
return (
|
|
178
|
-
text !== undefined &&
|
|
179
|
-
text
|
|
180
|
-
.split(";")
|
|
181
|
-
.map((str) => str.trim())
|
|
182
|
-
.some((str) => str === "application/x-www-form-urlencoded")
|
|
183
|
-
);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/** @internal */
|
|
187
|
-
class FakeURLSearchParams {
|
|
188
|
-
public constructor(private readonly target: Record<string, string[]>) {}
|
|
189
|
-
|
|
190
|
-
public has(key: string): boolean {
|
|
191
|
-
return this.target[key] !== undefined;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
public get(key: string): string | null {
|
|
195
|
-
const value = this.target[key];
|
|
196
|
-
return value === undefined
|
|
197
|
-
? null
|
|
198
|
-
: Array.isArray(value)
|
|
199
|
-
? (value[0] ?? null)
|
|
200
|
-
: value;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
public getAll(key: string): string[] {
|
|
204
|
-
const value = this.target[key];
|
|
205
|
-
return value === undefined ? [] : Array.isArray(value) ? value : [value];
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/** @internal */
|
|
210
|
-
class TypedQueryRouteInterceptor implements NestInterceptor {
|
|
211
|
-
public constructor(
|
|
212
|
-
private readonly toSearchParams: (input: any) => URLSearchParams,
|
|
213
|
-
) {}
|
|
214
|
-
|
|
215
|
-
public intercept(context: ExecutionContext, next: CallHandler) {
|
|
216
|
-
const http: HttpArgumentsHost = context.switchToHttp();
|
|
217
|
-
const response: express.Response = http.getResponse();
|
|
218
|
-
response.header("Content-Type", "application/x-www-form-urlencoded");
|
|
219
|
-
|
|
220
|
-
return next.handle().pipe(
|
|
221
|
-
map((value) => this.toSearchParams(value).toString()),
|
|
222
|
-
catchError((err) => route_error(http.getRequest(), err)),
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/** @internal */
|
|
228
|
-
const ROUTERS = {
|
|
229
|
-
Get,
|
|
230
|
-
Post,
|
|
231
|
-
Patch,
|
|
232
|
-
Put,
|
|
233
|
-
Delete,
|
|
234
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
BadRequestException,
|
|
3
|
+
CallHandler,
|
|
4
|
+
Delete,
|
|
5
|
+
ExecutionContext,
|
|
6
|
+
Get,
|
|
7
|
+
NestInterceptor,
|
|
8
|
+
Patch,
|
|
9
|
+
Post,
|
|
10
|
+
Put,
|
|
11
|
+
UseInterceptors,
|
|
12
|
+
applyDecorators,
|
|
13
|
+
createParamDecorator,
|
|
14
|
+
} from "@nestjs/common";
|
|
15
|
+
import { HttpArgumentsHost } from "@nestjs/common/interfaces";
|
|
16
|
+
import type express from "express";
|
|
17
|
+
import type { FastifyRequest } from "fastify";
|
|
18
|
+
import { catchError, map } from "rxjs";
|
|
19
|
+
import typia from "typia";
|
|
20
|
+
|
|
21
|
+
import { IRequestQueryValidator } from "../options/IRequestQueryValidator";
|
|
22
|
+
import { IResponseBodyQuerifier } from "../options/IResponseBodyQuerifier";
|
|
23
|
+
import { get_path_and_querify } from "./internal/get_path_and_querify";
|
|
24
|
+
import { route_error } from "./internal/route_error";
|
|
25
|
+
import { validate_request_query } from "./internal/validate_request_query";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Type safe URL query decorator.
|
|
29
|
+
*
|
|
30
|
+
* `TypedQuery` is a decorator function that can parse URL query string. It is
|
|
31
|
+
* almost same with {@link nest.Query}, but it can automatically cast property
|
|
32
|
+
* type following its DTO definition. Also, `TypedQuery` performs type
|
|
33
|
+
* validation.
|
|
34
|
+
*
|
|
35
|
+
* For reference, target type `T` must follow such restriction. Also, if actual
|
|
36
|
+
* URL query parameter values are different with their promised type `T`,
|
|
37
|
+
* `BadRequestException` error (status code: 400) would be thrown.
|
|
38
|
+
*
|
|
39
|
+
* 1. Type `T` must be an object type
|
|
40
|
+
* 2. Do not allow dynamic property
|
|
41
|
+
* 3. Only `boolean`, `bigint`, `number`, `string` or their array types are allowed
|
|
42
|
+
* 4. By the way, union type never be not allowed
|
|
43
|
+
*
|
|
44
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
45
|
+
* @returns Parameter decorator
|
|
46
|
+
*/
|
|
47
|
+
export function TypedQuery<T extends object>(
|
|
48
|
+
validator?: IRequestQueryValidator<T>,
|
|
49
|
+
): ParameterDecorator {
|
|
50
|
+
const checker = validate_request_query("TypedQuery")(validator);
|
|
51
|
+
return createParamDecorator(function TypedQuery(
|
|
52
|
+
_unknown: any,
|
|
53
|
+
context: ExecutionContext,
|
|
54
|
+
) {
|
|
55
|
+
const request: express.Request | FastifyRequest = context
|
|
56
|
+
.switchToHttp()
|
|
57
|
+
.getRequest();
|
|
58
|
+
const params: URLSearchParams = new URLSearchParams(tail(request.url));
|
|
59
|
+
|
|
60
|
+
const output: T | Error = checker(params);
|
|
61
|
+
if (output instanceof Error) throw output;
|
|
62
|
+
return output;
|
|
63
|
+
})();
|
|
64
|
+
}
|
|
65
|
+
export namespace TypedQuery {
|
|
66
|
+
/**
|
|
67
|
+
* Request body decorator.
|
|
68
|
+
*
|
|
69
|
+
* Request body decorator for the `application/x-www-form-urlencoded` type.
|
|
70
|
+
*/
|
|
71
|
+
export function Body<T extends object>(
|
|
72
|
+
validator?: IRequestQueryValidator<T>,
|
|
73
|
+
): ParameterDecorator {
|
|
74
|
+
const checker = validate_request_query("TypedQuery.Body")(validator);
|
|
75
|
+
return createParamDecorator(function TypedQueryBody(
|
|
76
|
+
_unknown: any,
|
|
77
|
+
context: ExecutionContext,
|
|
78
|
+
) {
|
|
79
|
+
const request: express.Request | FastifyRequest = context
|
|
80
|
+
.switchToHttp()
|
|
81
|
+
.getRequest();
|
|
82
|
+
if (isApplicationQuery(request.headers["content-type"]) === false)
|
|
83
|
+
throw new BadRequestException(
|
|
84
|
+
`Request body type is not "application/x-www-form-urlencoded".`,
|
|
85
|
+
);
|
|
86
|
+
const params: URLSearchParams =
|
|
87
|
+
request.body instanceof URLSearchParams
|
|
88
|
+
? request.body
|
|
89
|
+
: (new FakeURLSearchParams(request.body) as any);
|
|
90
|
+
|
|
91
|
+
const output: T | Error = checker(params);
|
|
92
|
+
if (output instanceof Error) throw output;
|
|
93
|
+
return output;
|
|
94
|
+
})();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Router decorator function for the GET method.
|
|
99
|
+
*
|
|
100
|
+
* @param path Path of the HTTP request
|
|
101
|
+
* @returns Method decorator
|
|
102
|
+
*/
|
|
103
|
+
export const Get = Generator("Get");
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Router decorator function for the POST method.
|
|
107
|
+
*
|
|
108
|
+
* @param path Path of the HTTP request
|
|
109
|
+
* @returns Method decorator
|
|
110
|
+
*/
|
|
111
|
+
export const Post = Generator("Post");
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Router decorator function for the PATH method.
|
|
115
|
+
*
|
|
116
|
+
* @param path Path of the HTTP request
|
|
117
|
+
* @returns Method decorator
|
|
118
|
+
*/
|
|
119
|
+
export const Patch = Generator("Patch");
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Router decorator function for the PUT method.
|
|
123
|
+
*
|
|
124
|
+
* @param path Path of the HTTP request
|
|
125
|
+
* @returns Method decorator
|
|
126
|
+
*/
|
|
127
|
+
export const Put = Generator("Put");
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Router decorator function for the DELETE method.
|
|
131
|
+
*
|
|
132
|
+
* @param path Path of the HTTP request
|
|
133
|
+
* @returns Method decorator
|
|
134
|
+
*/
|
|
135
|
+
export const Delete = Generator("Delete");
|
|
136
|
+
|
|
137
|
+
/** @internal */
|
|
138
|
+
function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
|
|
139
|
+
function route(path?: string | string[]): MethodDecorator;
|
|
140
|
+
function route<T>(stringify?: IResponseBodyQuerifier<T>): MethodDecorator;
|
|
141
|
+
function route<T>(
|
|
142
|
+
path: string | string[],
|
|
143
|
+
stringify?: IResponseBodyQuerifier<T>,
|
|
144
|
+
): MethodDecorator;
|
|
145
|
+
|
|
146
|
+
function route(...args: any[]): MethodDecorator {
|
|
147
|
+
const [path, stringify] = get_path_and_querify(`TypedQuery.${method}`)(
|
|
148
|
+
...args,
|
|
149
|
+
);
|
|
150
|
+
return applyDecorators(
|
|
151
|
+
ROUTERS[method](path),
|
|
152
|
+
UseInterceptors(new TypedQueryRouteInterceptor(stringify)),
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
return route;
|
|
156
|
+
}
|
|
157
|
+
for (const method of [typia.assert, typia.is, typia.validate])
|
|
158
|
+
for (const [key, value] of Object.entries(method))
|
|
159
|
+
for (const deco of [
|
|
160
|
+
TypedQuery.Get,
|
|
161
|
+
TypedQuery.Delete,
|
|
162
|
+
TypedQuery.Post,
|
|
163
|
+
TypedQuery.Put,
|
|
164
|
+
TypedQuery.Patch,
|
|
165
|
+
])
|
|
166
|
+
(deco as any)[key] = value;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** @internal */
|
|
170
|
+
function tail(url: string): string {
|
|
171
|
+
const index: number = url.indexOf("?");
|
|
172
|
+
return index === -1 ? "" : url.substring(index + 1);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** @internal */
|
|
176
|
+
function isApplicationQuery(text?: string): boolean {
|
|
177
|
+
return (
|
|
178
|
+
text !== undefined &&
|
|
179
|
+
text
|
|
180
|
+
.split(";")
|
|
181
|
+
.map((str) => str.trim())
|
|
182
|
+
.some((str) => str === "application/x-www-form-urlencoded")
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** @internal */
|
|
187
|
+
class FakeURLSearchParams {
|
|
188
|
+
public constructor(private readonly target: Record<string, string[]>) {}
|
|
189
|
+
|
|
190
|
+
public has(key: string): boolean {
|
|
191
|
+
return this.target[key] !== undefined;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
public get(key: string): string | null {
|
|
195
|
+
const value = this.target[key];
|
|
196
|
+
return value === undefined
|
|
197
|
+
? null
|
|
198
|
+
: Array.isArray(value)
|
|
199
|
+
? (value[0] ?? null)
|
|
200
|
+
: value;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
public getAll(key: string): string[] {
|
|
204
|
+
const value = this.target[key];
|
|
205
|
+
return value === undefined ? [] : Array.isArray(value) ? value : [value];
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** @internal */
|
|
210
|
+
class TypedQueryRouteInterceptor implements NestInterceptor {
|
|
211
|
+
public constructor(
|
|
212
|
+
private readonly toSearchParams: (input: any) => URLSearchParams,
|
|
213
|
+
) {}
|
|
214
|
+
|
|
215
|
+
public intercept(context: ExecutionContext, next: CallHandler) {
|
|
216
|
+
const http: HttpArgumentsHost = context.switchToHttp();
|
|
217
|
+
const response: express.Response = http.getResponse();
|
|
218
|
+
response.header("Content-Type", "application/x-www-form-urlencoded");
|
|
219
|
+
|
|
220
|
+
return next.handle().pipe(
|
|
221
|
+
map((value) => this.toSearchParams(value).toString()),
|
|
222
|
+
catchError((err) => route_error(http.getRequest(), err)),
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** @internal */
|
|
228
|
+
const ROUTERS = {
|
|
229
|
+
Get,
|
|
230
|
+
Post,
|
|
231
|
+
Patch,
|
|
232
|
+
Put,
|
|
233
|
+
Delete,
|
|
234
|
+
};
|