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