@nestia/core 2.4.3 → 3.0.0-dev.20231209

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 (67) hide show
  1. package/lib/decorators/EncryptedBody.js +13 -11
  2. package/lib/decorators/EncryptedBody.js.map +1 -1
  3. package/lib/decorators/EncryptedRoute.js +29 -12
  4. package/lib/decorators/EncryptedRoute.js.map +1 -1
  5. package/lib/decorators/internal/get_binary_body.d.ts +1 -0
  6. package/lib/decorators/internal/get_binary_body.js +66 -0
  7. package/lib/decorators/internal/get_binary_body.js.map +1 -0
  8. package/package.json +3 -3
  9. package/src/decorators/DynamicModule.ts +39 -39
  10. package/src/decorators/EncryptedBody.ts +17 -12
  11. package/src/decorators/EncryptedController.ts +38 -38
  12. package/src/decorators/EncryptedModule.ts +79 -79
  13. package/src/decorators/EncryptedRoute.ts +38 -14
  14. package/src/decorators/PlainBody.ts +72 -72
  15. package/src/decorators/TypedBody.ts +59 -59
  16. package/src/decorators/TypedException.ts +89 -89
  17. package/src/decorators/TypedHeaders.ts +69 -69
  18. package/src/decorators/TypedParam.ts +65 -65
  19. package/src/decorators/TypedQuery.ts +246 -246
  20. package/src/decorators/TypedRoute.ts +144 -144
  21. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  22. package/src/decorators/internal/NoTransformConfigureError.ts +8 -8
  23. package/src/decorators/internal/get_binary_body.ts +18 -0
  24. package/src/decorators/internal/get_path_and_querify.ts +103 -103
  25. package/src/decorators/internal/get_path_and_stringify.ts +91 -91
  26. package/src/decorators/internal/get_text_body.ts +20 -20
  27. package/src/decorators/internal/headers_to_object.ts +13 -13
  28. package/src/decorators/internal/load_controller.ts +51 -51
  29. package/src/decorators/internal/route_error.ts +45 -45
  30. package/src/decorators/internal/validate_request_body.ts +57 -57
  31. package/src/decorators/internal/validate_request_headers.ts +68 -68
  32. package/src/decorators/internal/validate_request_query.ts +56 -56
  33. package/src/index.ts +5 -5
  34. package/src/module.ts +14 -14
  35. package/src/options/INestiaTransformOptions.ts +17 -17
  36. package/src/options/INestiaTransformProject.ts +7 -7
  37. package/src/options/IRequestBodyValidator.ts +20 -20
  38. package/src/options/IRequestHeadersValidator.ts +22 -22
  39. package/src/options/IRequestQueryValidator.ts +20 -20
  40. package/src/options/IResponseBodyQuerifier.ts +25 -25
  41. package/src/options/IResponseBodyStringifier.ts +25 -25
  42. package/src/programmers/PlainBodyProgrammer.ts +52 -52
  43. package/src/programmers/TypedBodyProgrammer.ts +108 -108
  44. package/src/programmers/TypedExceptionProgrammer.ts +72 -72
  45. package/src/programmers/TypedHeadersProgrammer.ts +56 -56
  46. package/src/programmers/TypedParamProgrammer.ts +24 -24
  47. package/src/programmers/TypedQueryBodyProgrammer.ts +56 -56
  48. package/src/programmers/TypedQueryProgrammer.ts +56 -56
  49. package/src/programmers/TypedQueryRouteProgrammer.ts +51 -51
  50. package/src/programmers/TypedRouteProgrammer.ts +51 -51
  51. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +58 -58
  52. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +62 -62
  53. package/src/programmers/http/HttpQuerifyProgrammer.ts +96 -96
  54. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +63 -63
  55. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  56. package/src/transform.ts +35 -35
  57. package/src/transformers/FileTransformer.ts +66 -66
  58. package/src/transformers/MethodTransformer.ts +94 -94
  59. package/src/transformers/NodeTransformer.ts +16 -16
  60. package/src/transformers/ParameterDecoratorTransformer.ts +121 -121
  61. package/src/transformers/ParameterTransformer.ts +48 -48
  62. package/src/transformers/TypedExceptionTransformer.ts +49 -49
  63. package/src/transformers/TypedRouteTransformer.ts +95 -95
  64. package/src/typings/Creator.ts +3 -3
  65. package/src/utils/ExceptionManager.ts +112 -112
  66. package/src/utils/Singleton.ts +20 -20
  67. package/src/utils/SourceFinder.ts +57 -57
@@ -1,246 +1,246 @@
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 follw 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
- export function Body<T extends object>(
66
- validator?: IRequestQueryValidator<T>,
67
- ): ParameterDecorator {
68
- const checker = validate_request_query(validator);
69
- return createParamDecorator(function TypedQueryBody(
70
- _unknown: any,
71
- context: ExecutionContext,
72
- ) {
73
- const request: express.Request | FastifyRequest = context
74
- .switchToHttp()
75
- .getRequest();
76
- if (isApplicationQuery(request.headers["content-type"]) === false)
77
- throw new BadRequestException(
78
- `Request body type is not "application/x-www-form-urlencoded".`,
79
- );
80
- const params: URLSearchParams =
81
- request.body instanceof URLSearchParams
82
- ? request.body
83
- : (new FakeURLSearchParams(request.body) as any);
84
-
85
- const output: T | Error = checker(params);
86
- if (output instanceof Error) throw output;
87
- return output;
88
- })();
89
- }
90
- Object.assign(Body, typia.http.assertQuery);
91
- Object.assign(Body, typia.http.isQuery);
92
- Object.assign(Body, typia.http.validateQuery);
93
-
94
- /**
95
- * Router decorator function for the GET method.
96
- *
97
- * @param path Path of the HTTP request
98
- * @returns Method decorator
99
- */
100
- export const Get = Generator("Get");
101
-
102
- /**
103
- * Router decorator function for the POST method.
104
- *
105
- * @param path Path of the HTTP request
106
- * @returns Method decorator
107
- */
108
- export const Post = Generator("Post");
109
-
110
- /**
111
- * Router decorator function for the PATH method.
112
- *
113
- * @param path Path of the HTTP request
114
- * @returns Method decorator
115
- */
116
- export const Patch = Generator("Patch");
117
-
118
- /**
119
- * Router decorator function for the PUT method.
120
- *
121
- * @param path Path of the HTTP request
122
- * @returns Method decorator
123
- */
124
- export const Put = Generator("Put");
125
-
126
- /**
127
- * Router decorator function for the DELETE method.
128
- *
129
- * @param path Path of the HTTP request
130
- * @returns Method decorator
131
- */
132
- export const Delete = Generator("Delete");
133
-
134
- /**
135
- * @internal
136
- */
137
- function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
138
- function route(path?: string | string[]): MethodDecorator;
139
- function route<T>(stringify?: IResponseBodyQuerifier<T>): MethodDecorator;
140
- function route<T>(
141
- path: string | string[],
142
- stringify?: IResponseBodyQuerifier<T>,
143
- ): MethodDecorator;
144
-
145
- function route(...args: any[]): MethodDecorator {
146
- const [path, stringify] = get_path_and_querify(`TypedQuery.${method}`)(
147
- ...args,
148
- );
149
- return applyDecorators(
150
- ROUTERS[method](path),
151
- UseInterceptors(new TypedQueryRouteInterceptor(stringify)),
152
- );
153
- }
154
- return route;
155
- }
156
- for (const method of [typia.assert, typia.is, typia.validate])
157
- for (const [key, value] of Object.entries(method))
158
- for (const deco of [
159
- TypedQuery.Get,
160
- TypedQuery.Delete,
161
- TypedQuery.Post,
162
- TypedQuery.Put,
163
- TypedQuery.Patch,
164
- ])
165
- (deco as any)[key] = value;
166
- }
167
- Object.assign(TypedQuery, typia.http.assertQuery);
168
- Object.assign(TypedQuery, typia.http.isQuery);
169
- Object.assign(TypedQuery, typia.http.validateQuery);
170
-
171
- /**
172
- * @internal
173
- */
174
- function tail(url: string): string {
175
- const index: number = url.indexOf("?");
176
- return index === -1 ? "" : url.substring(index + 1);
177
- }
178
-
179
- /**
180
- * @internal
181
- */
182
- function isApplicationQuery(text?: string): boolean {
183
- return (
184
- text !== undefined &&
185
- text
186
- .split(";")
187
- .map((str) => str.trim())
188
- .some((str) => str === "application/x-www-form-urlencoded")
189
- );
190
- }
191
-
192
- /**
193
- * @internal
194
- */
195
- class FakeURLSearchParams {
196
- public constructor(private readonly target: Record<string, string[]>) {}
197
-
198
- public has(key: string): boolean {
199
- return this.target[key] !== undefined;
200
- }
201
-
202
- public get(key: string): string | null {
203
- const value = this.target[key];
204
- return value === undefined
205
- ? null
206
- : Array.isArray(value)
207
- ? value[0] ?? null
208
- : value;
209
- }
210
-
211
- public getAll(key: string): string[] {
212
- const value = this.target[key];
213
- return value === undefined ? [] : Array.isArray(value) ? value : [value];
214
- }
215
- }
216
-
217
- /**
218
- * @internal
219
- */
220
- class TypedQueryRouteInterceptor implements NestInterceptor {
221
- public constructor(
222
- private readonly toSearchParams: (input: any) => URLSearchParams,
223
- ) {}
224
-
225
- public intercept(context: ExecutionContext, next: CallHandler) {
226
- const http: HttpArgumentsHost = context.switchToHttp();
227
- const response: express.Response = http.getResponse();
228
- response.header("Content-Type", "application/x-www-form-urlencoded");
229
-
230
- return next.handle().pipe(
231
- map((value) => this.toSearchParams(value).toString()),
232
- catchError((err) => route_error(http.getRequest(), err)),
233
- );
234
- }
235
- }
236
-
237
- /**
238
- * @internal
239
- */
240
- const ROUTERS = {
241
- Get,
242
- Post,
243
- Patch,
244
- Put,
245
- Delete,
246
- };
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 follw 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
+ export function Body<T extends object>(
66
+ validator?: IRequestQueryValidator<T>,
67
+ ): ParameterDecorator {
68
+ const checker = validate_request_query(validator);
69
+ return createParamDecorator(function TypedQueryBody(
70
+ _unknown: any,
71
+ context: ExecutionContext,
72
+ ) {
73
+ const request: express.Request | FastifyRequest = context
74
+ .switchToHttp()
75
+ .getRequest();
76
+ if (isApplicationQuery(request.headers["content-type"]) === false)
77
+ throw new BadRequestException(
78
+ `Request body type is not "application/x-www-form-urlencoded".`,
79
+ );
80
+ const params: URLSearchParams =
81
+ request.body instanceof URLSearchParams
82
+ ? request.body
83
+ : (new FakeURLSearchParams(request.body) as any);
84
+
85
+ const output: T | Error = checker(params);
86
+ if (output instanceof Error) throw output;
87
+ return output;
88
+ })();
89
+ }
90
+ Object.assign(Body, typia.http.assertQuery);
91
+ Object.assign(Body, typia.http.isQuery);
92
+ Object.assign(Body, typia.http.validateQuery);
93
+
94
+ /**
95
+ * Router decorator function for the GET method.
96
+ *
97
+ * @param path Path of the HTTP request
98
+ * @returns Method decorator
99
+ */
100
+ export const Get = Generator("Get");
101
+
102
+ /**
103
+ * Router decorator function for the POST method.
104
+ *
105
+ * @param path Path of the HTTP request
106
+ * @returns Method decorator
107
+ */
108
+ export const Post = Generator("Post");
109
+
110
+ /**
111
+ * Router decorator function for the PATH method.
112
+ *
113
+ * @param path Path of the HTTP request
114
+ * @returns Method decorator
115
+ */
116
+ export const Patch = Generator("Patch");
117
+
118
+ /**
119
+ * Router decorator function for the PUT method.
120
+ *
121
+ * @param path Path of the HTTP request
122
+ * @returns Method decorator
123
+ */
124
+ export const Put = Generator("Put");
125
+
126
+ /**
127
+ * Router decorator function for the DELETE method.
128
+ *
129
+ * @param path Path of the HTTP request
130
+ * @returns Method decorator
131
+ */
132
+ export const Delete = Generator("Delete");
133
+
134
+ /**
135
+ * @internal
136
+ */
137
+ function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
138
+ function route(path?: string | string[]): MethodDecorator;
139
+ function route<T>(stringify?: IResponseBodyQuerifier<T>): MethodDecorator;
140
+ function route<T>(
141
+ path: string | string[],
142
+ stringify?: IResponseBodyQuerifier<T>,
143
+ ): MethodDecorator;
144
+
145
+ function route(...args: any[]): MethodDecorator {
146
+ const [path, stringify] = get_path_and_querify(`TypedQuery.${method}`)(
147
+ ...args,
148
+ );
149
+ return applyDecorators(
150
+ ROUTERS[method](path),
151
+ UseInterceptors(new TypedQueryRouteInterceptor(stringify)),
152
+ );
153
+ }
154
+ return route;
155
+ }
156
+ for (const method of [typia.assert, typia.is, typia.validate])
157
+ for (const [key, value] of Object.entries(method))
158
+ for (const deco of [
159
+ TypedQuery.Get,
160
+ TypedQuery.Delete,
161
+ TypedQuery.Post,
162
+ TypedQuery.Put,
163
+ TypedQuery.Patch,
164
+ ])
165
+ (deco as any)[key] = value;
166
+ }
167
+ Object.assign(TypedQuery, typia.http.assertQuery);
168
+ Object.assign(TypedQuery, typia.http.isQuery);
169
+ Object.assign(TypedQuery, typia.http.validateQuery);
170
+
171
+ /**
172
+ * @internal
173
+ */
174
+ function tail(url: string): string {
175
+ const index: number = url.indexOf("?");
176
+ return index === -1 ? "" : url.substring(index + 1);
177
+ }
178
+
179
+ /**
180
+ * @internal
181
+ */
182
+ function isApplicationQuery(text?: string): boolean {
183
+ return (
184
+ text !== undefined &&
185
+ text
186
+ .split(";")
187
+ .map((str) => str.trim())
188
+ .some((str) => str === "application/x-www-form-urlencoded")
189
+ );
190
+ }
191
+
192
+ /**
193
+ * @internal
194
+ */
195
+ class FakeURLSearchParams {
196
+ public constructor(private readonly target: Record<string, string[]>) {}
197
+
198
+ public has(key: string): boolean {
199
+ return this.target[key] !== undefined;
200
+ }
201
+
202
+ public get(key: string): string | null {
203
+ const value = this.target[key];
204
+ return value === undefined
205
+ ? null
206
+ : Array.isArray(value)
207
+ ? value[0] ?? null
208
+ : value;
209
+ }
210
+
211
+ public getAll(key: string): string[] {
212
+ const value = this.target[key];
213
+ return value === undefined ? [] : Array.isArray(value) ? value : [value];
214
+ }
215
+ }
216
+
217
+ /**
218
+ * @internal
219
+ */
220
+ class TypedQueryRouteInterceptor implements NestInterceptor {
221
+ public constructor(
222
+ private readonly toSearchParams: (input: any) => URLSearchParams,
223
+ ) {}
224
+
225
+ public intercept(context: ExecutionContext, next: CallHandler) {
226
+ const http: HttpArgumentsHost = context.switchToHttp();
227
+ const response: express.Response = http.getResponse();
228
+ response.header("Content-Type", "application/x-www-form-urlencoded");
229
+
230
+ return next.handle().pipe(
231
+ map((value) => this.toSearchParams(value).toString()),
232
+ catchError((err) => route_error(http.getRequest(), err)),
233
+ );
234
+ }
235
+ }
236
+
237
+ /**
238
+ * @internal
239
+ */
240
+ const ROUTERS = {
241
+ Get,
242
+ Post,
243
+ Patch,
244
+ Put,
245
+ Delete,
246
+ };