@nestia/core 4.4.2-dev.20241217 → 4.4.2

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 (74) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +87 -87
  3. package/package.json +3 -3
  4. package/src/adaptors/WebSocketAdaptor.ts +426 -426
  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/NoTransformConfigurationError.ts +32 -32
  11. package/src/decorators/PlainBody.ts +79 -79
  12. package/src/decorators/SwaggerCustomizer.ts +115 -115
  13. package/src/decorators/SwaggerExample.ts +100 -100
  14. package/src/decorators/TypedBody.ts +59 -59
  15. package/src/decorators/TypedException.ts +128 -128
  16. package/src/decorators/TypedFormData.ts +195 -195
  17. package/src/decorators/TypedHeaders.ts +64 -64
  18. package/src/decorators/TypedParam.ts +77 -77
  19. package/src/decorators/TypedQuery.ts +245 -245
  20. package/src/decorators/TypedRoute.ts +214 -214
  21. package/src/decorators/WebSocketRoute.ts +242 -242
  22. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  23. package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
  24. package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
  25. package/src/decorators/internal/get_path_and_querify.ts +108 -108
  26. package/src/decorators/internal/get_path_and_stringify.ts +122 -122
  27. package/src/decorators/internal/get_text_body.ts +20 -20
  28. package/src/decorators/internal/headers_to_object.ts +13 -13
  29. package/src/decorators/internal/is_request_body_undefined.ts +14 -14
  30. package/src/decorators/internal/load_controller.ts +49 -49
  31. package/src/decorators/internal/route_error.ts +45 -45
  32. package/src/decorators/internal/validate_request_body.ts +74 -74
  33. package/src/decorators/internal/validate_request_form_data.ts +77 -77
  34. package/src/decorators/internal/validate_request_headers.ts +86 -86
  35. package/src/decorators/internal/validate_request_query.ts +74 -74
  36. package/src/index.ts +5 -5
  37. package/src/module.ts +21 -21
  38. package/src/options/INestiaTransformOptions.ts +38 -38
  39. package/src/options/INestiaTransformProject.ts +8 -8
  40. package/src/options/IRequestBodyValidator.ts +20 -20
  41. package/src/options/IRequestFormDataProps.ts +27 -27
  42. package/src/options/IRequestHeadersValidator.ts +22 -22
  43. package/src/options/IRequestQueryValidator.ts +20 -20
  44. package/src/options/IResponseBodyQuerifier.ts +25 -25
  45. package/src/options/IResponseBodyStringifier.ts +30 -30
  46. package/src/programmers/PlainBodyProgrammer.ts +70 -70
  47. package/src/programmers/TypedBodyProgrammer.ts +142 -142
  48. package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
  49. package/src/programmers/TypedHeadersProgrammer.ts +63 -63
  50. package/src/programmers/TypedParamProgrammer.ts +33 -33
  51. package/src/programmers/TypedQueryBodyProgrammer.ts +112 -112
  52. package/src/programmers/TypedQueryProgrammer.ts +114 -114
  53. package/src/programmers/TypedQueryRouteProgrammer.ts +105 -105
  54. package/src/programmers/TypedRouteProgrammer.ts +94 -94
  55. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +72 -72
  56. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +75 -75
  57. package/src/programmers/http/HttpQuerifyProgrammer.ts +108 -108
  58. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +76 -76
  59. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  60. package/src/transform.ts +35 -35
  61. package/src/transformers/FileTransformer.ts +110 -110
  62. package/src/transformers/MethodTransformer.ts +103 -103
  63. package/src/transformers/NodeTransformer.ts +23 -23
  64. package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
  65. package/src/transformers/ParameterTransformer.ts +57 -57
  66. package/src/transformers/TypedRouteTransformer.ts +85 -85
  67. package/src/transformers/WebSocketRouteTransformer.ts +120 -120
  68. package/src/typings/Creator.ts +3 -3
  69. package/src/typings/get-function-location.d.ts +7 -7
  70. package/src/utils/ArrayUtil.ts +7 -7
  71. package/src/utils/ExceptionManager.ts +112 -112
  72. package/src/utils/Singleton.ts +20 -20
  73. package/src/utils/SourceFinder.ts +57 -57
  74. 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 informations 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 informations 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
+ };