@nestia/core 2.6.2 → 2.6.3

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 (32) hide show
  1. package/package.json +4 -4
  2. package/src/decorators/EncryptedBody.ts +105 -105
  3. package/src/decorators/EncryptedModule.ts +96 -96
  4. package/src/decorators/PlainBody.ts +75 -75
  5. package/src/decorators/SwaggerCustomizer.ts +116 -116
  6. package/src/decorators/TypedBody.ts +62 -62
  7. package/src/decorators/TypedException.ts +90 -90
  8. package/src/decorators/TypedFormData.ts +219 -219
  9. package/src/decorators/TypedQuery.ts +251 -251
  10. package/src/decorators/TypedRoute.ts +144 -144
  11. package/src/decorators/internal/get_path_and_querify.ts +106 -106
  12. package/src/decorators/internal/load_controller.ts +51 -51
  13. package/src/decorators/internal/validate_request_body.ts +72 -72
  14. package/src/decorators/internal/validate_request_form_data.ts +75 -75
  15. package/src/decorators/internal/validate_request_headers.ts +83 -83
  16. package/src/decorators/internal/validate_request_query.ts +71 -71
  17. package/src/module.ts +16 -16
  18. package/src/options/IRequestFormDataProps.ts +27 -27
  19. package/src/programmers/PlainBodyProgrammer.ts +52 -52
  20. package/src/programmers/TypedExceptionProgrammer.ts +71 -71
  21. package/src/programmers/TypedFormDataBodyProgrammer.ts +108 -108
  22. package/src/programmers/http/HttpQuerifyProgrammer.ts +96 -96
  23. package/src/structures/ISwagger.ts +91 -91
  24. package/src/structures/ISwaggerComponents.ts +29 -29
  25. package/src/structures/ISwaggerInfo.ts +80 -80
  26. package/src/structures/ISwaggerRoute.ts +50 -50
  27. package/src/structures/ISwaggerSecurityScheme.ts +65 -65
  28. package/src/transformers/NodeTransformer.ts +16 -16
  29. package/src/transformers/ParameterDecoratorTransformer.ts +120 -120
  30. package/src/transformers/TypedExceptionTransformer.ts +48 -48
  31. package/src/transformers/TypedRouteTransformer.ts +88 -88
  32. package/src/utils/Singleton.ts +20 -20
@@ -1,251 +1,251 @@
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 almost
31
- * same with {@link nest.Query}, but it can automatically cast property type following
32
- * its DTO definition. Also, `TypedQuery` performs type validation.
33
- *
34
- * For reference, target type `T` must follow such restriction. Also, if actual URL
35
- * query parameter values are different with their promised type `T`,
36
- * `BadRequestException` error (status code: 400) would be thrown.
37
- *
38
- * 1. Type `T` must be an object type
39
- * 2. Do not allow dynamic property
40
- * 3. Only `boolean`, `bigint`, `number`, `string` or their array types are allowed
41
- * 4. By the way, union type never be not allowed
42
- *
43
- * @returns Parameter decorator
44
- * @author Jeongho Nam - https://github.com/samchon
45
- */
46
- export function TypedQuery<T extends object>(
47
- validator?: IRequestQueryValidator<T>,
48
- ): ParameterDecorator {
49
- const checker = validate_request_query(validator);
50
- return createParamDecorator(function TypedQuery(
51
- _unknown: any,
52
- context: ExecutionContext,
53
- ) {
54
- const request: express.Request | FastifyRequest = context
55
- .switchToHttp()
56
- .getRequest();
57
- const params: URLSearchParams = new URLSearchParams(tail(request.url));
58
-
59
- const output: T | Error = checker(params);
60
- if (output instanceof Error) throw output;
61
- return output;
62
- })();
63
- }
64
- export namespace TypedQuery {
65
- /**
66
- * Request body decorator.
67
- *
68
- * Request body decorator for the `application/x-www-form-urlencoded` type.
69
- */
70
- export function Body<T extends object>(
71
- validator?: IRequestQueryValidator<T>,
72
- ): ParameterDecorator {
73
- const checker = validate_request_query(validator);
74
- return createParamDecorator(function TypedQueryBody(
75
- _unknown: any,
76
- context: ExecutionContext,
77
- ) {
78
- const request: express.Request | FastifyRequest = context
79
- .switchToHttp()
80
- .getRequest();
81
- if (isApplicationQuery(request.headers["content-type"]) === false)
82
- throw new BadRequestException(
83
- `Request body type is not "application/x-www-form-urlencoded".`,
84
- );
85
- const params: URLSearchParams =
86
- request.body instanceof URLSearchParams
87
- ? request.body
88
- : (new FakeURLSearchParams(request.body) as any);
89
-
90
- const output: T | Error = checker(params);
91
- if (output instanceof Error) throw output;
92
- return output;
93
- })();
94
- }
95
- Object.assign(Body, typia.http.assertQuery);
96
- Object.assign(Body, typia.http.isQuery);
97
- Object.assign(Body, typia.http.validateQuery);
98
-
99
- /**
100
- * Router decorator function for the GET method.
101
- *
102
- * @param path Path of the HTTP request
103
- * @returns Method decorator
104
- */
105
- export const Get = Generator("Get");
106
-
107
- /**
108
- * Router decorator function for the POST method.
109
- *
110
- * @param path Path of the HTTP request
111
- * @returns Method decorator
112
- */
113
- export const Post = Generator("Post");
114
-
115
- /**
116
- * Router decorator function for the PATH method.
117
- *
118
- * @param path Path of the HTTP request
119
- * @returns Method decorator
120
- */
121
- export const Patch = Generator("Patch");
122
-
123
- /**
124
- * Router decorator function for the PUT method.
125
- *
126
- * @param path Path of the HTTP request
127
- * @returns Method decorator
128
- */
129
- export const Put = Generator("Put");
130
-
131
- /**
132
- * Router decorator function for the DELETE method.
133
- *
134
- * @param path Path of the HTTP request
135
- * @returns Method decorator
136
- */
137
- export const Delete = Generator("Delete");
138
-
139
- /**
140
- * @internal
141
- */
142
- function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
143
- function route(path?: string | string[]): MethodDecorator;
144
- function route<T>(stringify?: IResponseBodyQuerifier<T>): MethodDecorator;
145
- function route<T>(
146
- path: string | string[],
147
- stringify?: IResponseBodyQuerifier<T>,
148
- ): MethodDecorator;
149
-
150
- function route(...args: any[]): MethodDecorator {
151
- const [path, stringify] = get_path_and_querify(`TypedQuery.${method}`)(
152
- ...args,
153
- );
154
- return applyDecorators(
155
- ROUTERS[method](path),
156
- UseInterceptors(new TypedQueryRouteInterceptor(stringify)),
157
- );
158
- }
159
- return route;
160
- }
161
- for (const method of [typia.assert, typia.is, typia.validate])
162
- for (const [key, value] of Object.entries(method))
163
- for (const deco of [
164
- TypedQuery.Get,
165
- TypedQuery.Delete,
166
- TypedQuery.Post,
167
- TypedQuery.Put,
168
- TypedQuery.Patch,
169
- ])
170
- (deco as any)[key] = value;
171
- }
172
- Object.assign(TypedQuery, typia.http.assertQuery);
173
- Object.assign(TypedQuery, typia.http.isQuery);
174
- Object.assign(TypedQuery, typia.http.validateQuery);
175
-
176
- /**
177
- * @internal
178
- */
179
- function tail(url: string): string {
180
- const index: number = url.indexOf("?");
181
- return index === -1 ? "" : url.substring(index + 1);
182
- }
183
-
184
- /**
185
- * @internal
186
- */
187
- function isApplicationQuery(text?: string): boolean {
188
- return (
189
- text !== undefined &&
190
- text
191
- .split(";")
192
- .map((str) => str.trim())
193
- .some((str) => str === "application/x-www-form-urlencoded")
194
- );
195
- }
196
-
197
- /**
198
- * @internal
199
- */
200
- class FakeURLSearchParams {
201
- public constructor(private readonly target: Record<string, string[]>) {}
202
-
203
- public has(key: string): boolean {
204
- return this.target[key] !== undefined;
205
- }
206
-
207
- public get(key: string): string | null {
208
- const value = this.target[key];
209
- return value === undefined
210
- ? null
211
- : Array.isArray(value)
212
- ? value[0] ?? null
213
- : value;
214
- }
215
-
216
- public getAll(key: string): string[] {
217
- const value = this.target[key];
218
- return value === undefined ? [] : Array.isArray(value) ? value : [value];
219
- }
220
- }
221
-
222
- /**
223
- * @internal
224
- */
225
- class TypedQueryRouteInterceptor implements NestInterceptor {
226
- public constructor(
227
- private readonly toSearchParams: (input: any) => URLSearchParams,
228
- ) {}
229
-
230
- public intercept(context: ExecutionContext, next: CallHandler) {
231
- const http: HttpArgumentsHost = context.switchToHttp();
232
- const response: express.Response = http.getResponse();
233
- response.header("Content-Type", "application/x-www-form-urlencoded");
234
-
235
- return next.handle().pipe(
236
- map((value) => this.toSearchParams(value).toString()),
237
- catchError((err) => route_error(http.getRequest(), err)),
238
- );
239
- }
240
- }
241
-
242
- /**
243
- * @internal
244
- */
245
- const ROUTERS = {
246
- Get,
247
- Post,
248
- Patch,
249
- Put,
250
- Delete,
251
- };
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 almost
31
+ * same with {@link nest.Query}, but it can automatically cast property type following
32
+ * its DTO definition. Also, `TypedQuery` performs type validation.
33
+ *
34
+ * For reference, target type `T` must follow such restriction. Also, if actual URL
35
+ * query parameter values are different with their promised type `T`,
36
+ * `BadRequestException` error (status code: 400) would be thrown.
37
+ *
38
+ * 1. Type `T` must be an object type
39
+ * 2. Do not allow dynamic property
40
+ * 3. Only `boolean`, `bigint`, `number`, `string` or their array types are allowed
41
+ * 4. By the way, union type never be not allowed
42
+ *
43
+ * @returns Parameter decorator
44
+ * @author Jeongho Nam - https://github.com/samchon
45
+ */
46
+ export function TypedQuery<T extends object>(
47
+ validator?: IRequestQueryValidator<T>,
48
+ ): ParameterDecorator {
49
+ const checker = validate_request_query(validator);
50
+ return createParamDecorator(function TypedQuery(
51
+ _unknown: any,
52
+ context: ExecutionContext,
53
+ ) {
54
+ const request: express.Request | FastifyRequest = context
55
+ .switchToHttp()
56
+ .getRequest();
57
+ const params: URLSearchParams = new URLSearchParams(tail(request.url));
58
+
59
+ const output: T | Error = checker(params);
60
+ if (output instanceof Error) throw output;
61
+ return output;
62
+ })();
63
+ }
64
+ export namespace TypedQuery {
65
+ /**
66
+ * Request body decorator.
67
+ *
68
+ * Request body decorator for the `application/x-www-form-urlencoded` type.
69
+ */
70
+ export function Body<T extends object>(
71
+ validator?: IRequestQueryValidator<T>,
72
+ ): ParameterDecorator {
73
+ const checker = validate_request_query(validator);
74
+ return createParamDecorator(function TypedQueryBody(
75
+ _unknown: any,
76
+ context: ExecutionContext,
77
+ ) {
78
+ const request: express.Request | FastifyRequest = context
79
+ .switchToHttp()
80
+ .getRequest();
81
+ if (isApplicationQuery(request.headers["content-type"]) === false)
82
+ throw new BadRequestException(
83
+ `Request body type is not "application/x-www-form-urlencoded".`,
84
+ );
85
+ const params: URLSearchParams =
86
+ request.body instanceof URLSearchParams
87
+ ? request.body
88
+ : (new FakeURLSearchParams(request.body) as any);
89
+
90
+ const output: T | Error = checker(params);
91
+ if (output instanceof Error) throw output;
92
+ return output;
93
+ })();
94
+ }
95
+ Object.assign(Body, typia.http.assertQuery);
96
+ Object.assign(Body, typia.http.isQuery);
97
+ Object.assign(Body, typia.http.validateQuery);
98
+
99
+ /**
100
+ * Router decorator function for the GET method.
101
+ *
102
+ * @param path Path of the HTTP request
103
+ * @returns Method decorator
104
+ */
105
+ export const Get = Generator("Get");
106
+
107
+ /**
108
+ * Router decorator function for the POST method.
109
+ *
110
+ * @param path Path of the HTTP request
111
+ * @returns Method decorator
112
+ */
113
+ export const Post = Generator("Post");
114
+
115
+ /**
116
+ * Router decorator function for the PATH method.
117
+ *
118
+ * @param path Path of the HTTP request
119
+ * @returns Method decorator
120
+ */
121
+ export const Patch = Generator("Patch");
122
+
123
+ /**
124
+ * Router decorator function for the PUT method.
125
+ *
126
+ * @param path Path of the HTTP request
127
+ * @returns Method decorator
128
+ */
129
+ export const Put = Generator("Put");
130
+
131
+ /**
132
+ * Router decorator function for the DELETE method.
133
+ *
134
+ * @param path Path of the HTTP request
135
+ * @returns Method decorator
136
+ */
137
+ export const Delete = Generator("Delete");
138
+
139
+ /**
140
+ * @internal
141
+ */
142
+ function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
143
+ function route(path?: string | string[]): MethodDecorator;
144
+ function route<T>(stringify?: IResponseBodyQuerifier<T>): MethodDecorator;
145
+ function route<T>(
146
+ path: string | string[],
147
+ stringify?: IResponseBodyQuerifier<T>,
148
+ ): MethodDecorator;
149
+
150
+ function route(...args: any[]): MethodDecorator {
151
+ const [path, stringify] = get_path_and_querify(`TypedQuery.${method}`)(
152
+ ...args,
153
+ );
154
+ return applyDecorators(
155
+ ROUTERS[method](path),
156
+ UseInterceptors(new TypedQueryRouteInterceptor(stringify)),
157
+ );
158
+ }
159
+ return route;
160
+ }
161
+ for (const method of [typia.assert, typia.is, typia.validate])
162
+ for (const [key, value] of Object.entries(method))
163
+ for (const deco of [
164
+ TypedQuery.Get,
165
+ TypedQuery.Delete,
166
+ TypedQuery.Post,
167
+ TypedQuery.Put,
168
+ TypedQuery.Patch,
169
+ ])
170
+ (deco as any)[key] = value;
171
+ }
172
+ Object.assign(TypedQuery, typia.http.assertQuery);
173
+ Object.assign(TypedQuery, typia.http.isQuery);
174
+ Object.assign(TypedQuery, typia.http.validateQuery);
175
+
176
+ /**
177
+ * @internal
178
+ */
179
+ function tail(url: string): string {
180
+ const index: number = url.indexOf("?");
181
+ return index === -1 ? "" : url.substring(index + 1);
182
+ }
183
+
184
+ /**
185
+ * @internal
186
+ */
187
+ function isApplicationQuery(text?: string): boolean {
188
+ return (
189
+ text !== undefined &&
190
+ text
191
+ .split(";")
192
+ .map((str) => str.trim())
193
+ .some((str) => str === "application/x-www-form-urlencoded")
194
+ );
195
+ }
196
+
197
+ /**
198
+ * @internal
199
+ */
200
+ class FakeURLSearchParams {
201
+ public constructor(private readonly target: Record<string, string[]>) {}
202
+
203
+ public has(key: string): boolean {
204
+ return this.target[key] !== undefined;
205
+ }
206
+
207
+ public get(key: string): string | null {
208
+ const value = this.target[key];
209
+ return value === undefined
210
+ ? null
211
+ : Array.isArray(value)
212
+ ? value[0] ?? null
213
+ : value;
214
+ }
215
+
216
+ public getAll(key: string): string[] {
217
+ const value = this.target[key];
218
+ return value === undefined ? [] : Array.isArray(value) ? value : [value];
219
+ }
220
+ }
221
+
222
+ /**
223
+ * @internal
224
+ */
225
+ class TypedQueryRouteInterceptor implements NestInterceptor {
226
+ public constructor(
227
+ private readonly toSearchParams: (input: any) => URLSearchParams,
228
+ ) {}
229
+
230
+ public intercept(context: ExecutionContext, next: CallHandler) {
231
+ const http: HttpArgumentsHost = context.switchToHttp();
232
+ const response: express.Response = http.getResponse();
233
+ response.header("Content-Type", "application/x-www-form-urlencoded");
234
+
235
+ return next.handle().pipe(
236
+ map((value) => this.toSearchParams(value).toString()),
237
+ catchError((err) => route_error(http.getRequest(), err)),
238
+ );
239
+ }
240
+ }
241
+
242
+ /**
243
+ * @internal
244
+ */
245
+ const ROUTERS = {
246
+ Get,
247
+ Post,
248
+ Patch,
249
+ Put,
250
+ Delete,
251
+ };