@nestia/core 7.0.0-dev.20250607 → 7.0.0

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 (75) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +92 -92
  3. package/package.json +3 -3
  4. package/src/adaptors/WebSocketAdaptor.ts +429 -429
  5. package/src/decorators/DynamicModule.ts +43 -43
  6. package/src/decorators/EncryptedBody.ts +101 -101
  7. package/src/decorators/EncryptedController.ts +38 -38
  8. package/src/decorators/EncryptedModule.ts +100 -100
  9. package/src/decorators/EncryptedRoute.ts +219 -219
  10. package/src/decorators/HumanRoute.ts +22 -22
  11. package/src/decorators/NoTransformConfigurationError.ts +32 -32
  12. package/src/decorators/PlainBody.ts +79 -79
  13. package/src/decorators/SwaggerCustomizer.ts +115 -115
  14. package/src/decorators/SwaggerExample.ts +100 -100
  15. package/src/decorators/TypedBody.ts +59 -59
  16. package/src/decorators/TypedException.ts +166 -166
  17. package/src/decorators/TypedFormData.ts +195 -195
  18. package/src/decorators/TypedHeaders.ts +64 -64
  19. package/src/decorators/TypedParam.ts +77 -77
  20. package/src/decorators/TypedQuery.ts +245 -245
  21. package/src/decorators/TypedRoute.ts +214 -214
  22. package/src/decorators/WebSocketRoute.ts +242 -242
  23. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  24. package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
  25. package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
  26. package/src/decorators/internal/get_path_and_querify.ts +108 -108
  27. package/src/decorators/internal/get_path_and_stringify.ts +122 -122
  28. package/src/decorators/internal/get_text_body.ts +20 -20
  29. package/src/decorators/internal/headers_to_object.ts +13 -13
  30. package/src/decorators/internal/is_request_body_undefined.ts +14 -14
  31. package/src/decorators/internal/load_controller.ts +49 -49
  32. package/src/decorators/internal/route_error.ts +45 -45
  33. package/src/decorators/internal/validate_request_body.ts +74 -74
  34. package/src/decorators/internal/validate_request_form_data.ts +77 -77
  35. package/src/decorators/internal/validate_request_headers.ts +86 -86
  36. package/src/decorators/internal/validate_request_query.ts +74 -74
  37. package/src/index.ts +5 -5
  38. package/src/module.ts +22 -22
  39. package/src/options/INestiaTransformOptions.ts +38 -38
  40. package/src/options/INestiaTransformProject.ts +8 -8
  41. package/src/options/IRequestBodyValidator.ts +20 -20
  42. package/src/options/IRequestFormDataProps.ts +27 -27
  43. package/src/options/IRequestHeadersValidator.ts +22 -22
  44. package/src/options/IRequestQueryValidator.ts +20 -20
  45. package/src/options/IResponseBodyQuerifier.ts +25 -25
  46. package/src/options/IResponseBodyStringifier.ts +30 -30
  47. package/src/programmers/PlainBodyProgrammer.ts +70 -70
  48. package/src/programmers/TypedBodyProgrammer.ts +142 -142
  49. package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
  50. package/src/programmers/TypedHeadersProgrammer.ts +63 -63
  51. package/src/programmers/TypedParamProgrammer.ts +33 -33
  52. package/src/programmers/TypedQueryBodyProgrammer.ts +112 -112
  53. package/src/programmers/TypedQueryProgrammer.ts +114 -114
  54. package/src/programmers/TypedQueryRouteProgrammer.ts +105 -105
  55. package/src/programmers/TypedRouteProgrammer.ts +94 -94
  56. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +72 -72
  57. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +75 -75
  58. package/src/programmers/http/HttpQuerifyProgrammer.ts +108 -108
  59. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +76 -76
  60. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  61. package/src/transform.ts +35 -35
  62. package/src/transformers/FileTransformer.ts +110 -110
  63. package/src/transformers/MethodTransformer.ts +103 -103
  64. package/src/transformers/NodeTransformer.ts +23 -23
  65. package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
  66. package/src/transformers/ParameterTransformer.ts +57 -57
  67. package/src/transformers/TypedRouteTransformer.ts +85 -85
  68. package/src/transformers/WebSocketRouteTransformer.ts +120 -120
  69. package/src/typings/Creator.ts +3 -3
  70. package/src/typings/get-function-location.d.ts +7 -7
  71. package/src/utils/ArrayUtil.ts +7 -7
  72. package/src/utils/ExceptionManager.ts +112 -112
  73. package/src/utils/Singleton.ts +20 -20
  74. package/src/utils/SourceFinder.ts +57 -57
  75. package/src/utils/VersioningStrategy.ts +27 -27
@@ -1,214 +1,214 @@
1
- import {
2
- CallHandler,
3
- Delete,
4
- ExecutionContext,
5
- Get,
6
- NestInterceptor,
7
- Patch,
8
- Post,
9
- Put,
10
- UseInterceptors,
11
- applyDecorators,
12
- } from "@nestjs/common";
13
- import { HttpArgumentsHost } from "@nestjs/common/interfaces";
14
- import type express from "express";
15
- import { catchError, map } from "rxjs/operators";
16
- import typia, { IValidation } from "typia";
17
-
18
- import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
19
- import { get_path_and_stringify } from "./internal/get_path_and_stringify";
20
- import { route_error } from "./internal/route_error";
21
-
22
- /**
23
- * Type safe router decorator functions.
24
- *
25
- * `TypedRoute` is a module containing router decorator functions which can boost up
26
- * JSON string conversion speed about 200x times faster than `class-transformer`.
27
- * Furthermore, such JSON string conversion is even type safe through
28
- * [typia](https://github.com/samchon/typia).
29
- *
30
- * For reference, if you try to invalid data that is not following the promised
31
- * type `T`, 500 internal server error would be thrown. Also, as `TypedRoute` composes
32
- * JSON string through `typia.assertStringify<T>()` function, it is not possible to
33
- * modify response data through interceptors.
34
- *
35
- * @author Jeongho Nam - https://github.com/samchon
36
- */
37
- export namespace TypedRoute {
38
- /**
39
- * Router decorator function for the GET method.
40
- *
41
- * @param path Path of the HTTP request
42
- * @returns Method decorator
43
- */
44
- export const Get = Generator("Get");
45
-
46
- /**
47
- * Router decorator function for the POST method.
48
- *
49
- * @param path Path of the HTTP request
50
- * @returns Method decorator
51
- */
52
- export const Post = Generator("Post");
53
-
54
- /**
55
- * Router decorator function for the PATH method.
56
- *
57
- * @param path Path of the HTTP request
58
- * @returns Method decorator
59
- */
60
- export const Patch = Generator("Patch");
61
-
62
- /**
63
- * Router decorator function for the PUT method.
64
- *
65
- * @param path Path of the HTTP request
66
- * @returns Method decorator
67
- */
68
- export const Put = Generator("Put");
69
-
70
- /**
71
- * Router decorator function for the DELETE method.
72
- *
73
- * @param path Path of the HTTP request
74
- * @returns Method decorator
75
- */
76
- export const Delete = Generator("Delete");
77
-
78
- /**
79
- * Set the logger function for the response validation failure.
80
- *
81
- * If you've configured the transformation option to `validate.log`
82
- * in the `tsconfig.json` file, then the error log information of the
83
- * response validation failure would be logged through this function
84
- * instead of throwing the 400 bad request error.
85
- *
86
- * By the way, be careful. If you've configured the response
87
- * transformation option to be `validate.log`, client may get wrong
88
- * response data. Therefore, this way is not recommended in the common
89
- * backend server case.
90
- *
91
- * @param func Logger function
92
- * @default console.log
93
- */
94
- export function setValidateErrorLogger(
95
- func: (log: IValidateErrorLog) => void,
96
- ): void {
97
- __logger = func;
98
- }
99
-
100
- /**
101
- * Error log information of the response validation failure.
102
- *
103
- * `IValidationErrorLog` is a structure representing the error log
104
- * information when the returned value from the `@TypedRoute` or
105
- * `@EncryptedRoute` decorated controller method is not following
106
- * the promised type `T`.
107
- *
108
- * If you've configured the transformation option to `validate.log` or
109
- * `validateEquals.log` in the `tsconfig.json` file, then this error log
110
- * information `IValidateErrorLog` would be logged through the
111
- * {@link setValidateErrorLogger} function instead of throwing the
112
- * 400 bad request error.
113
- */
114
- export interface IValidateErrorLog {
115
- /**
116
- * HTTP method of the request.
117
- */
118
- method: string;
119
-
120
- /**
121
- * HTTP path of the request.
122
- */
123
- path: string;
124
-
125
- /**
126
- * Validation error information with detailed reasons.
127
- */
128
- errors: IValidation.IError[];
129
-
130
- /**
131
- * Data that is not following the promised type `T`.
132
- */
133
- data: unknown;
134
- }
135
-
136
- /**
137
- * @internal
138
- */
139
- export let __logger: (log: IValidateErrorLog) => void = console.log;
140
-
141
- /**
142
- * @internal
143
- */
144
- function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
145
- function route(path?: string | string[]): MethodDecorator;
146
- function route<T>(stringify?: IResponseBodyStringifier<T>): MethodDecorator;
147
- function route<T>(
148
- path: string | string[],
149
- stringify?: IResponseBodyStringifier<T>,
150
- ): MethodDecorator;
151
-
152
- function route(...args: any[]): MethodDecorator {
153
- const [path, stringify] = get_path_and_stringify(() => __logger)(
154
- `TypedRoute.${method}`,
155
- )(...args);
156
- return applyDecorators(
157
- ROUTERS[method](path),
158
- UseInterceptors(new TypedRouteInterceptor(stringify)),
159
- );
160
- }
161
- return route;
162
- }
163
- }
164
- for (const method of [
165
- typia.json.stringify,
166
- typia.json.isStringify,
167
- typia.json.assertStringify,
168
- typia.json.validateStringify,
169
- ])
170
- for (const [key, value] of Object.entries(method))
171
- for (const deco of [
172
- TypedRoute.Get,
173
- TypedRoute.Delete,
174
- TypedRoute.Post,
175
- TypedRoute.Put,
176
- TypedRoute.Patch,
177
- ])
178
- (deco as any)[key] = value;
179
-
180
- /**
181
- * @internal
182
- */
183
- class TypedRouteInterceptor implements NestInterceptor {
184
- public constructor(
185
- private readonly stringify: (
186
- input: any,
187
- method: string,
188
- path: string,
189
- ) => string,
190
- ) {}
191
-
192
- public intercept(context: ExecutionContext, next: CallHandler) {
193
- const http: HttpArgumentsHost = context.switchToHttp();
194
- const request: express.Request = http.getRequest();
195
- const response: express.Response = http.getResponse();
196
- response.header("Content-Type", "application/json");
197
-
198
- return next.handle().pipe(
199
- map((value) => this.stringify(value, request.method, request.url)),
200
- catchError((err) => route_error(http.getRequest(), err)),
201
- );
202
- }
203
- }
204
-
205
- /**
206
- * @internal
207
- */
208
- const ROUTERS = {
209
- Get,
210
- Post,
211
- Patch,
212
- Put,
213
- Delete,
214
- };
1
+ import {
2
+ CallHandler,
3
+ Delete,
4
+ ExecutionContext,
5
+ Get,
6
+ NestInterceptor,
7
+ Patch,
8
+ Post,
9
+ Put,
10
+ UseInterceptors,
11
+ applyDecorators,
12
+ } from "@nestjs/common";
13
+ import { HttpArgumentsHost } from "@nestjs/common/interfaces";
14
+ import type express from "express";
15
+ import { catchError, map } from "rxjs/operators";
16
+ import typia, { IValidation } from "typia";
17
+
18
+ import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
19
+ import { get_path_and_stringify } from "./internal/get_path_and_stringify";
20
+ import { route_error } from "./internal/route_error";
21
+
22
+ /**
23
+ * Type safe router decorator functions.
24
+ *
25
+ * `TypedRoute` is a module containing router decorator functions which can boost up
26
+ * JSON string conversion speed about 200x times faster than `class-transformer`.
27
+ * Furthermore, such JSON string conversion is even type safe through
28
+ * [typia](https://github.com/samchon/typia).
29
+ *
30
+ * For reference, if you try to invalid data that is not following the promised
31
+ * type `T`, 500 internal server error would be thrown. Also, as `TypedRoute` composes
32
+ * JSON string through `typia.assertStringify<T>()` function, it is not possible to
33
+ * modify response data through interceptors.
34
+ *
35
+ * @author Jeongho Nam - https://github.com/samchon
36
+ */
37
+ export namespace TypedRoute {
38
+ /**
39
+ * Router decorator function for the GET method.
40
+ *
41
+ * @param path Path of the HTTP request
42
+ * @returns Method decorator
43
+ */
44
+ export const Get = Generator("Get");
45
+
46
+ /**
47
+ * Router decorator function for the POST method.
48
+ *
49
+ * @param path Path of the HTTP request
50
+ * @returns Method decorator
51
+ */
52
+ export const Post = Generator("Post");
53
+
54
+ /**
55
+ * Router decorator function for the PATH method.
56
+ *
57
+ * @param path Path of the HTTP request
58
+ * @returns Method decorator
59
+ */
60
+ export const Patch = Generator("Patch");
61
+
62
+ /**
63
+ * Router decorator function for the PUT method.
64
+ *
65
+ * @param path Path of the HTTP request
66
+ * @returns Method decorator
67
+ */
68
+ export const Put = Generator("Put");
69
+
70
+ /**
71
+ * Router decorator function for the DELETE method.
72
+ *
73
+ * @param path Path of the HTTP request
74
+ * @returns Method decorator
75
+ */
76
+ export const Delete = Generator("Delete");
77
+
78
+ /**
79
+ * Set the logger function for the response validation failure.
80
+ *
81
+ * If you've configured the transformation option to `validate.log`
82
+ * in the `tsconfig.json` file, then the error log information of the
83
+ * response validation failure would be logged through this function
84
+ * instead of throwing the 400 bad request error.
85
+ *
86
+ * By the way, be careful. If you've configured the response
87
+ * transformation option to be `validate.log`, client may get wrong
88
+ * response data. Therefore, this way is not recommended in the common
89
+ * backend server case.
90
+ *
91
+ * @param func Logger function
92
+ * @default console.log
93
+ */
94
+ export function setValidateErrorLogger(
95
+ func: (log: IValidateErrorLog) => void,
96
+ ): void {
97
+ __logger = func;
98
+ }
99
+
100
+ /**
101
+ * Error log information of the response validation failure.
102
+ *
103
+ * `IValidationErrorLog` is a structure representing the error log
104
+ * information when the returned value from the `@TypedRoute` or
105
+ * `@EncryptedRoute` decorated controller method is not following
106
+ * the promised type `T`.
107
+ *
108
+ * If you've configured the transformation option to `validate.log` or
109
+ * `validateEquals.log` in the `tsconfig.json` file, then this error log
110
+ * information `IValidateErrorLog` would be logged through the
111
+ * {@link setValidateErrorLogger} function instead of throwing the
112
+ * 400 bad request error.
113
+ */
114
+ export interface IValidateErrorLog {
115
+ /**
116
+ * HTTP method of the request.
117
+ */
118
+ method: string;
119
+
120
+ /**
121
+ * HTTP path of the request.
122
+ */
123
+ path: string;
124
+
125
+ /**
126
+ * Validation error information with detailed reasons.
127
+ */
128
+ errors: IValidation.IError[];
129
+
130
+ /**
131
+ * Data that is not following the promised type `T`.
132
+ */
133
+ data: unknown;
134
+ }
135
+
136
+ /**
137
+ * @internal
138
+ */
139
+ export let __logger: (log: IValidateErrorLog) => void = console.log;
140
+
141
+ /**
142
+ * @internal
143
+ */
144
+ function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
145
+ function route(path?: string | string[]): MethodDecorator;
146
+ function route<T>(stringify?: IResponseBodyStringifier<T>): MethodDecorator;
147
+ function route<T>(
148
+ path: string | string[],
149
+ stringify?: IResponseBodyStringifier<T>,
150
+ ): MethodDecorator;
151
+
152
+ function route(...args: any[]): MethodDecorator {
153
+ const [path, stringify] = get_path_and_stringify(() => __logger)(
154
+ `TypedRoute.${method}`,
155
+ )(...args);
156
+ return applyDecorators(
157
+ ROUTERS[method](path),
158
+ UseInterceptors(new TypedRouteInterceptor(stringify)),
159
+ );
160
+ }
161
+ return route;
162
+ }
163
+ }
164
+ for (const method of [
165
+ typia.json.stringify,
166
+ typia.json.isStringify,
167
+ typia.json.assertStringify,
168
+ typia.json.validateStringify,
169
+ ])
170
+ for (const [key, value] of Object.entries(method))
171
+ for (const deco of [
172
+ TypedRoute.Get,
173
+ TypedRoute.Delete,
174
+ TypedRoute.Post,
175
+ TypedRoute.Put,
176
+ TypedRoute.Patch,
177
+ ])
178
+ (deco as any)[key] = value;
179
+
180
+ /**
181
+ * @internal
182
+ */
183
+ class TypedRouteInterceptor implements NestInterceptor {
184
+ public constructor(
185
+ private readonly stringify: (
186
+ input: any,
187
+ method: string,
188
+ path: string,
189
+ ) => string,
190
+ ) {}
191
+
192
+ public intercept(context: ExecutionContext, next: CallHandler) {
193
+ const http: HttpArgumentsHost = context.switchToHttp();
194
+ const request: express.Request = http.getRequest();
195
+ const response: express.Response = http.getResponse();
196
+ response.header("Content-Type", "application/json");
197
+
198
+ return next.handle().pipe(
199
+ map((value) => this.stringify(value, request.method, request.url)),
200
+ catchError((err) => route_error(http.getRequest(), err)),
201
+ );
202
+ }
203
+ }
204
+
205
+ /**
206
+ * @internal
207
+ */
208
+ const ROUTERS = {
209
+ Get,
210
+ Post,
211
+ Patch,
212
+ Put,
213
+ Delete,
214
+ };