@nestia/core 3.0.5-dev.20240418 → 3.1.0-dev.20240426

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 (139) hide show
  1. package/lib/adaptors/WebSocketAdaptor.d.ts +21 -0
  2. package/lib/adaptors/WebSocketAdaptor.js +277 -0
  3. package/lib/adaptors/WebSocketAdaptor.js.map +1 -0
  4. package/lib/decorators/DynamicModule.js +12 -59
  5. package/lib/decorators/DynamicModule.js.map +1 -1
  6. package/lib/decorators/EncryptedBody.js +36 -73
  7. package/lib/decorators/EncryptedBody.js.map +1 -1
  8. package/lib/decorators/EncryptedController.js +2 -2
  9. package/lib/decorators/EncryptedController.js.map +1 -1
  10. package/lib/decorators/EncryptedModule.js +24 -107
  11. package/lib/decorators/EncryptedModule.js.map +1 -1
  12. package/lib/decorators/EncryptedRoute.js +43 -119
  13. package/lib/decorators/EncryptedRoute.js.map +1 -1
  14. package/lib/decorators/PlainBody.js +24 -61
  15. package/lib/decorators/PlainBody.js.map +1 -1
  16. package/lib/decorators/SwaggerCustomizer.js +2 -2
  17. package/lib/decorators/SwaggerCustomizer.js.map +1 -1
  18. package/lib/decorators/TypedBody.js +12 -14
  19. package/lib/decorators/TypedBody.js.map +1 -1
  20. package/lib/decorators/TypedException.js +5 -5
  21. package/lib/decorators/TypedException.js.map +1 -1
  22. package/lib/decorators/TypedFormData.js +88 -291
  23. package/lib/decorators/TypedFormData.js.map +1 -1
  24. package/lib/decorators/TypedHeaders.js +6 -6
  25. package/lib/decorators/TypedHeaders.js.map +1 -1
  26. package/lib/decorators/TypedParam.d.ts +5 -6
  27. package/lib/decorators/TypedParam.js +14 -15
  28. package/lib/decorators/TypedParam.js.map +1 -1
  29. package/lib/decorators/TypedQuery.d.ts +2 -2
  30. package/lib/decorators/TypedQuery.js +52 -129
  31. package/lib/decorators/TypedQuery.js.map +1 -1
  32. package/lib/decorators/TypedRoute.js +31 -107
  33. package/lib/decorators/TypedRoute.js.map +1 -1
  34. package/lib/decorators/WebSocketRoute.d.ts +120 -0
  35. package/lib/decorators/WebSocketRoute.js +202 -0
  36. package/lib/decorators/WebSocketRoute.js.map +1 -0
  37. package/lib/decorators/internal/IWebSocketRouteReflect.d.ts +25 -0
  38. package/lib/decorators/internal/IWebSocketRouteReflect.js +3 -0
  39. package/lib/decorators/internal/IWebSocketRouteReflect.js.map +1 -0
  40. package/lib/decorators/internal/NoTransformConfigureError.js +1 -1
  41. package/lib/decorators/internal/NoTransformConfigureError.js.map +1 -1
  42. package/lib/decorators/internal/get_path_and_querify.js +67 -131
  43. package/lib/decorators/internal/get_path_and_querify.js.map +1 -1
  44. package/lib/decorators/internal/get_path_and_stringify.js +57 -71
  45. package/lib/decorators/internal/get_path_and_stringify.js.map +1 -1
  46. package/lib/decorators/internal/get_text_body.js +7 -46
  47. package/lib/decorators/internal/get_text_body.js.map +1 -1
  48. package/lib/decorators/internal/headers_to_object.js +3 -42
  49. package/lib/decorators/internal/headers_to_object.js.map +1 -1
  50. package/lib/decorators/internal/load_controller.js +29 -106
  51. package/lib/decorators/internal/load_controller.js.map +1 -1
  52. package/lib/decorators/internal/route_error.js +15 -65
  53. package/lib/decorators/internal/route_error.js.map +1 -1
  54. package/lib/decorators/internal/validate_request_body.js +44 -54
  55. package/lib/decorators/internal/validate_request_body.js.map +1 -1
  56. package/lib/decorators/internal/validate_request_form_data.js +36 -44
  57. package/lib/decorators/internal/validate_request_form_data.js.map +1 -1
  58. package/lib/decorators/internal/validate_request_headers.js +36 -44
  59. package/lib/decorators/internal/validate_request_headers.js.map +1 -1
  60. package/lib/decorators/internal/validate_request_query.js +36 -44
  61. package/lib/decorators/internal/validate_request_query.js.map +1 -1
  62. package/lib/index.js +1 -1
  63. package/lib/index.js.map +1 -1
  64. package/lib/module.d.ts +10 -9
  65. package/lib/module.js +10 -9
  66. package/lib/module.js.map +1 -1
  67. package/lib/programmers/PlainBodyProgrammer.js +29 -44
  68. package/lib/programmers/PlainBodyProgrammer.js.map +1 -1
  69. package/lib/programmers/TypedBodyProgrammer.js +57 -82
  70. package/lib/programmers/TypedBodyProgrammer.js.map +1 -1
  71. package/lib/programmers/TypedExceptionProgrammer.js +51 -54
  72. package/lib/programmers/TypedExceptionProgrammer.js.map +1 -1
  73. package/lib/programmers/TypedFormDataBodyProgrammer.js +50 -73
  74. package/lib/programmers/TypedFormDataBodyProgrammer.js.map +1 -1
  75. package/lib/programmers/TypedHeadersProgrammer.js +24 -43
  76. package/lib/programmers/TypedHeadersProgrammer.js.map +1 -1
  77. package/lib/programmers/TypedParamProgrammer.js +11 -28
  78. package/lib/programmers/TypedParamProgrammer.js.map +1 -1
  79. package/lib/programmers/TypedQueryBodyProgrammer.js +24 -43
  80. package/lib/programmers/TypedQueryBodyProgrammer.js.map +1 -1
  81. package/lib/programmers/TypedQueryProgrammer.js +24 -43
  82. package/lib/programmers/TypedQueryProgrammer.js.map +1 -1
  83. package/lib/programmers/TypedQueryRouteProgrammer.js +22 -39
  84. package/lib/programmers/TypedQueryRouteProgrammer.js.map +1 -1
  85. package/lib/programmers/TypedRouteProgrammer.js +22 -39
  86. package/lib/programmers/TypedRouteProgrammer.js.map +1 -1
  87. package/lib/programmers/http/HttpAssertQuerifyProgrammer.js +12 -29
  88. package/lib/programmers/http/HttpAssertQuerifyProgrammer.js.map +1 -1
  89. package/lib/programmers/http/HttpIsQuerifyProgrammer.js +10 -27
  90. package/lib/programmers/http/HttpIsQuerifyProgrammer.js.map +1 -1
  91. package/lib/programmers/http/HttpQuerifyProgrammer.js +33 -69
  92. package/lib/programmers/http/HttpQuerifyProgrammer.js.map +1 -1
  93. package/lib/programmers/http/HttpValidateQuerifyProgrammer.js +11 -28
  94. package/lib/programmers/http/HttpValidateQuerifyProgrammer.js.map +1 -1
  95. package/lib/programmers/internal/CoreMetadataUtil.js +12 -37
  96. package/lib/programmers/internal/CoreMetadataUtil.js.map +1 -1
  97. package/lib/transform.js +8 -8
  98. package/lib/transform.js.map +1 -1
  99. package/lib/transformers/FileTransformer.js +35 -66
  100. package/lib/transformers/FileTransformer.js.map +1 -1
  101. package/lib/transformers/MethodTransformer.js +36 -50
  102. package/lib/transformers/MethodTransformer.js.map +1 -1
  103. package/lib/transformers/NodeTransformer.js +8 -12
  104. package/lib/transformers/NodeTransformer.js.map +1 -1
  105. package/lib/transformers/ParameterDecoratorTransformer.js +75 -90
  106. package/lib/transformers/ParameterDecoratorTransformer.js.map +1 -1
  107. package/lib/transformers/ParameterTransformer.js +18 -24
  108. package/lib/transformers/ParameterTransformer.js.map +1 -1
  109. package/lib/transformers/TypedExceptionTransformer.js +25 -27
  110. package/lib/transformers/TypedExceptionTransformer.js.map +1 -1
  111. package/lib/transformers/TypedRouteTransformer.js +52 -88
  112. package/lib/transformers/TypedRouteTransformer.js.map +1 -1
  113. package/lib/utils/ArrayUtil.d.ts +3 -0
  114. package/lib/utils/ArrayUtil.js +11 -0
  115. package/lib/utils/ArrayUtil.js.map +1 -0
  116. package/lib/utils/ExceptionManager.js +9 -31
  117. package/lib/utils/ExceptionManager.js.map +1 -1
  118. package/lib/utils/Singleton.js +6 -7
  119. package/lib/utils/Singleton.js.map +1 -1
  120. package/lib/utils/SourceFinder.js +38 -215
  121. package/lib/utils/SourceFinder.js.map +1 -1
  122. package/lib/utils/VersioningStrategy.d.ts +9 -0
  123. package/lib/utils/VersioningStrategy.js +17 -0
  124. package/lib/utils/VersioningStrategy.js.map +1 -0
  125. package/package.json +10 -6
  126. package/src/adaptors/WebSocketAdaptor.ts +414 -0
  127. package/src/decorators/TypedParam.ts +5 -6
  128. package/src/decorators/TypedQuery.ts +251 -251
  129. package/src/decorators/WebSocketRoute.ts +249 -0
  130. package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -0
  131. package/src/decorators/internal/validate_request_body.ts +72 -72
  132. package/src/decorators/internal/validate_request_form_data.ts +75 -75
  133. package/src/decorators/internal/validate_request_headers.ts +83 -83
  134. package/src/decorators/internal/validate_request_query.ts +71 -71
  135. package/src/module.ts +20 -16
  136. package/src/transformers/ParameterDecoratorTransformer.ts +129 -120
  137. package/src/typings/get-function-location.d.ts +7 -0
  138. package/src/utils/ArrayUtil.ts +7 -0
  139. package/src/utils/VersioningStrategy.ts +27 -0
@@ -0,0 +1,249 @@
1
+ import typia from "typia";
2
+
3
+ import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
4
+ import { IRequestQueryValidator } from "../options/IRequestQueryValidator";
5
+ import { IWebSocketRouteReflect } from "./internal/IWebSocketRouteReflect";
6
+ import { NoTransformConfigureError } from "./internal/NoTransformConfigureError";
7
+ import { validate_request_body } from "./internal/validate_request_body";
8
+ import { validate_request_query } from "./internal/validate_request_query";
9
+
10
+ /**
11
+ * WebSocket route decorator.
12
+ *
13
+ * `@WebSocketRoute` is a route decorator function for WebSocket routes.
14
+ * If you want to define a WebSocket route with this `@WebSocketRoute` decorator,
15
+ * please don't forget to call the {@link WebSocketAdaptor.upgrade} function
16
+ * to the {@link INestApplication} instance.
17
+ *
18
+ * Also, `WebSocketRoute` is a module containing parameter decorator
19
+ * functions of below for the `@WebSocketRoute` decorated method, at the same
20
+ * time. Note that, every parameters must be decorated by one of the parameter
21
+ * decorators in the `WebSocketRoute` module. One thing more important is,
22
+ * {@link WebSocketRoute.Acceptor} decorated parameter must be defined in the
23
+ * method. If not, it would be both compilation/runtime error.
24
+ *
25
+ * - {@link WebSocketRoute.Acceptor}
26
+ * - {@link WebSocketRoute.Driver}
27
+ * - {@link WebSocketRoute.Header}
28
+ * - {@link WebSocketRoute.Param}
29
+ * - {@link WebSocketRoute.Query}
30
+ *
31
+ * For reference, key difference between `@WebSocketGateway()` of NestJS and
32
+ * `@WebSocketRoute()` of Nestia is, `@WebSocketRoute()` can make multiple
33
+ * WebSocket routes by configuring *paths*, besides `@WebSocketGateway()`
34
+ * can't do it.
35
+ *
36
+ * Furthermore, if you build SDK (Software Development Kit) library through
37
+ * `@nestia/sdk`, you can make safe WebSocket client taking advantages of
38
+ * TypeScript type hints and checkings.
39
+ *
40
+ * @param path Path(s) of the WebSocket request
41
+ * @returns Method decorator
42
+ * @author Jeongho Nam - https://github.com/samchon
43
+ */
44
+ export function WebSocketRoute(
45
+ path?: undefined | string | string[],
46
+ ): MethodDecorator {
47
+ return function WebSocketRoute(
48
+ _target: Object,
49
+ _propertyKey: string | symbol,
50
+ descriptor: TypedPropertyDescriptor<any>,
51
+ ) {
52
+ Reflect.defineMetadata(
53
+ "nestia/WebSocketRoute",
54
+ {
55
+ paths: path === undefined ? [] : Array.isArray(path) ? path : [path],
56
+ } satisfies IWebSocketRouteReflect,
57
+ descriptor.value,
58
+ );
59
+ return descriptor;
60
+ };
61
+ }
62
+ export namespace WebSocketRoute {
63
+ /**
64
+ * Acceptor parameter decorator.
65
+ *
66
+ * `@WebSocketRoute.Acceptor()` is a parameter decorator function for the
67
+ * `WebAcceptor<Header, Provider, Listener>` (of `tgrid`) typed parameter.
68
+ *
69
+ * In the controller method decorated by `@WebSocketRoute()` and
70
+ * `@WebSocketRoute.Acceptor()`, call {@link WebAcceptor.accept} function
71
+ * with `Provider` instance when you want to accept the WebSocket client
72
+ * connection. Otherwise you want to reject the connection, call
73
+ * {@link WebSocketAcceptor.rejcet} function instead.
74
+ *
75
+ * For reference, this `@WebSocketRoute.Acceptor()` parameter decorator is
76
+ * essential for the method decorated by `@WebSocketRoute()` decorator.
77
+ * If you forget it, it would be both compilation/runtime error.
78
+ */
79
+ export function Acceptor(): ParameterDecorator {
80
+ return function WebSocketAcceptor(
81
+ target: Object,
82
+ propertyKey: string | symbol | undefined,
83
+ parameterIndex: number,
84
+ ) {
85
+ emplace(target, propertyKey ?? "", {
86
+ category: "acceptor",
87
+ index: parameterIndex,
88
+ });
89
+ };
90
+ }
91
+
92
+ /**
93
+ * Driver parameter decorator.
94
+ *
95
+ * `@WebSocketRoute.Driver()` is a parameter decorator function for the
96
+ * `Driver<Listener>` (of `tgrid`) typed parameter.
97
+ *
98
+ * With the `@WebSocketRoute.Driver()` decorated parameter, you can call
99
+ * function of `Listener` typed instance provided by remote WebSocket client
100
+ * by calling the `Driver<Listener>` instance.
101
+ *
102
+ * For reference, this `@WebSocketRoute.Driver()` decorator is optional, and
103
+ * can be substituted by `@WebSocketRoute.Acceptor()` decorated parameter
104
+ * by calling the {@link WebAcceptor.getDriver} function.
105
+ */
106
+ export function Driver(): ParameterDecorator {
107
+ return function WebSocketDriver(
108
+ target: Object,
109
+ propertyKey: string | symbol | undefined,
110
+ parameterIndex: number,
111
+ ) {
112
+ emplace(target, propertyKey ?? "", {
113
+ category: "driver",
114
+ index: parameterIndex,
115
+ });
116
+ };
117
+ }
118
+
119
+ /**
120
+ * Header decorator.
121
+ *
122
+ * `@WebSocketRoute.Header()` is a parameter decorator function for the
123
+ * WebSocket header with type casting and assertion.
124
+ *
125
+ * For reference, `@WebSocketRoute.Header()` is different with HTTP headers.
126
+ * It's for WebSocket protocol, especially for TGrid's {@link WebConnector}
127
+ * and {@link WebAcceptor}'s special header.
128
+ *
129
+ * Also, this `@WebSocketRoute.Header()` decorator is optional, and
130
+ * can be substituted by `@WebSocketRoute.Acceptor()` decorated parameter
131
+ * by accessting to the {@link WebAcceptor.header} property.
132
+ */
133
+ export function Header<T>(
134
+ validator?: IRequestBodyValidator<T>,
135
+ ): ParameterDecorator {
136
+ const validate = validate_request_body("WebSocketRoute.Header")(validator);
137
+ return function WebSocketHeader(
138
+ target: Object,
139
+ propertyKey: string | symbol | undefined,
140
+ parameterIndex: number,
141
+ ) {
142
+ emplace(target, propertyKey ?? "", {
143
+ category: "header",
144
+ index: parameterIndex,
145
+ validate,
146
+ });
147
+ };
148
+ }
149
+ Object.assign(Header, typia.is);
150
+ Object.assign(Header, typia.assert);
151
+ Object.assign(Header, typia.validate);
152
+
153
+ /**
154
+ * URL parameter decorator.
155
+ *
156
+ * `@WebSocketRoute.Param()` is a parameter decorator function for the URL
157
+ * parameter with type casting and assertion.
158
+ *
159
+ * It's almost same with the {@link TypedParam}, but
160
+ * `@WebSocketRoute.Param()` is only for WebSocket protocol router function
161
+ * decorated by {@link WebSocketRoute}.
162
+ *
163
+ * @param field URL parameter field name
164
+ */
165
+ export function Param<T extends boolean | bigint | number | string | null>(
166
+ field: string,
167
+ assert?: (value: string) => T,
168
+ ): ParameterDecorator {
169
+ if (assert === undefined)
170
+ throw NoTransformConfigureError("WebSocketRoute.Param");
171
+ return function WebSocketParam(
172
+ target: Object,
173
+ propertyKey: string | symbol | undefined,
174
+ parameterIndex: number,
175
+ ) {
176
+ emplace(target, propertyKey ?? "", {
177
+ category: "param",
178
+ index: parameterIndex,
179
+ field,
180
+ assert,
181
+ });
182
+ };
183
+ }
184
+ Object.assign(Param, typia.http.parameter);
185
+
186
+ /**
187
+ * URL query decorator.
188
+ *
189
+ * `@WebSocketRoute.Query()` is a parameter decorator function for the URL
190
+ * query string with type casting and assertion.
191
+ *
192
+ * It is almost same with {@link TypedQuery}, but
193
+ * `@WebSocketRoute.Query()` is only for WebSocket protocol router function
194
+ * decorated by {@link WebSocketRoute}.
195
+ *
196
+ * For reference, as same with {@link TypedQuery}, `@WebSocketRoute.Query()`
197
+ * has same restriction for the target type `T`. If actual URL query
198
+ * parameter values are different with their promised type `T`,
199
+ * it would be runtime error.
200
+ *
201
+ * 1. Type `T` must be an object type
202
+ * 2. Do not allow dynamic property
203
+ * 3. Only `boolean`, `bigint`, `number`, `string` or their array types are allowed
204
+ * 4. By the way, union type never be not allowed
205
+ */
206
+ export function Query<T extends object>(
207
+ validator?: IRequestQueryValidator<T>,
208
+ ): ParameterDecorator {
209
+ const validate = validate_request_query("WebSocketRoute.Query")(validator);
210
+ return function WebSocketQuery(
211
+ target: Object,
212
+ propertyKey: string | symbol | undefined,
213
+ parameterIndex: number,
214
+ ) {
215
+ emplace(target, propertyKey ?? "", {
216
+ category: "query",
217
+ index: parameterIndex,
218
+ validate,
219
+ });
220
+ };
221
+ }
222
+ Object.assign(Query, typia.http.assertQuery);
223
+ Object.assign(Query, typia.http.isQuery);
224
+ Object.assign(Query, typia.http.validateQuery);
225
+
226
+ /**
227
+ * @internal
228
+ */
229
+ const emplace = (
230
+ target: Object,
231
+ propertyKey: string | symbol,
232
+ value: IWebSocketRouteReflect.IArgument,
233
+ ) => {
234
+ const array: IWebSocketRouteReflect.IArgument[] | undefined =
235
+ Reflect.getMetadata(
236
+ "nestia/WebSocketRoute/Parameters",
237
+ target,
238
+ propertyKey,
239
+ );
240
+ if (array !== undefined) array.push(value);
241
+ else
242
+ Reflect.defineMetadata(
243
+ "nestia/WebSocketRoute/Parameters",
244
+ [value],
245
+ target,
246
+ propertyKey,
247
+ );
248
+ };
249
+ }
@@ -0,0 +1,23 @@
1
+ export interface IWebSocketRouteReflect {
2
+ paths: string[];
3
+ }
4
+ export namespace IWebSocketRouteReflect {
5
+ export type IArgument = IAcceptor | IDriver | IHeader | IParam | IQuery;
6
+ export interface IAcceptor extends IBase<"acceptor"> {}
7
+ export interface IDriver extends IBase<"driver"> {}
8
+ export interface IHeader extends IBase<"header"> {
9
+ validate: (input?: any) => Error | null;
10
+ }
11
+ export interface IParam extends IBase<"param"> {
12
+ field: string;
13
+ assert: (value: string) => any;
14
+ }
15
+ export interface IQuery extends IBase<"query"> {
16
+ validate: (input: URLSearchParams) => any | Error;
17
+ }
18
+
19
+ interface IBase<Category extends string> {
20
+ category: Category;
21
+ index: number;
22
+ }
23
+ }
@@ -1,72 +1,72 @@
1
- import { BadRequestException } from "@nestjs/common";
2
- import typia, { IValidation, TypeGuardError } from "typia";
3
-
4
- import { IRequestBodyValidator } from "../../options/IRequestBodyValidator";
5
- import { NoTransformConfigureError } from "./NoTransformConfigureError";
6
-
7
- /**
8
- * @internal
9
- */
10
- export const validate_request_body =
11
- (method: string) =>
12
- <T>(validator?: IRequestBodyValidator<T>) => {
13
- if (!validator) return () => NoTransformConfigureError(method);
14
- else if (validator.type === "assert") return assert(validator.assert);
15
- else if (validator.type === "is") return is(validator.is);
16
- else if (validator.type === "validate") return validate(validator.validate);
17
- return () =>
18
- new Error(`Error on nestia.core.${method}(): invalid typed validator.`);
19
- };
20
-
21
- /**
22
- * @internal
23
- */
24
- const assert =
25
- <T>(closure: (data: T) => T) =>
26
- (input: T) => {
27
- try {
28
- closure(input);
29
- return null;
30
- } catch (exp) {
31
- if (typia.is<TypeGuardError>(exp)) {
32
- return new BadRequestException({
33
- path: exp.path,
34
- reason: exp.message,
35
- expected: exp.expected,
36
- value: exp.value,
37
- message: MESSAGE,
38
- });
39
- }
40
- throw exp;
41
- }
42
- };
43
-
44
- /**
45
- * @internal
46
- */
47
- const is =
48
- <T>(closure: (data: T) => boolean) =>
49
- (input: T) => {
50
- const success: boolean = closure(input);
51
- return success ? null : new BadRequestException(MESSAGE);
52
- };
53
-
54
- /**
55
- * @internal
56
- */
57
- const validate =
58
- <T>(closure: (data: T) => IValidation<T>) =>
59
- (input: T) => {
60
- const result: IValidation<T> = closure(input);
61
- return result.success
62
- ? null
63
- : new BadRequestException({
64
- errors: result.errors,
65
- message: MESSAGE,
66
- });
67
- };
68
-
69
- /**
70
- * @internal
71
- */
72
- const MESSAGE = "Request body data is not following the promised type.";
1
+ import { BadRequestException } from "@nestjs/common";
2
+ import typia, { IValidation, TypeGuardError } from "typia";
3
+
4
+ import { IRequestBodyValidator } from "../../options/IRequestBodyValidator";
5
+ import { NoTransformConfigureError } from "./NoTransformConfigureError";
6
+
7
+ /**
8
+ * @internal
9
+ */
10
+ export const validate_request_body =
11
+ (method: string) =>
12
+ <T>(validator?: IRequestBodyValidator<T>) => {
13
+ if (!validator) throw NoTransformConfigureError(method);
14
+ else if (validator.type === "assert") return assert(validator.assert);
15
+ else if (validator.type === "is") return is(validator.is);
16
+ else if (validator.type === "validate") return validate(validator.validate);
17
+ return () =>
18
+ new Error(`Error on nestia.core.${method}(): invalid typed validator.`);
19
+ };
20
+
21
+ /**
22
+ * @internal
23
+ */
24
+ const assert =
25
+ <T>(closure: (data: T) => T) =>
26
+ (input: T) => {
27
+ try {
28
+ closure(input);
29
+ return null;
30
+ } catch (exp) {
31
+ if (typia.is<TypeGuardError>(exp)) {
32
+ return new BadRequestException({
33
+ path: exp.path,
34
+ reason: exp.message,
35
+ expected: exp.expected,
36
+ value: exp.value,
37
+ message: MESSAGE,
38
+ });
39
+ }
40
+ throw exp;
41
+ }
42
+ };
43
+
44
+ /**
45
+ * @internal
46
+ */
47
+ const is =
48
+ <T>(closure: (data: T) => boolean) =>
49
+ (input: T) => {
50
+ const success: boolean = closure(input);
51
+ return success ? null : new BadRequestException(MESSAGE);
52
+ };
53
+
54
+ /**
55
+ * @internal
56
+ */
57
+ const validate =
58
+ <T>(closure: (data: T) => IValidation<T>) =>
59
+ (input: T) => {
60
+ const result: IValidation<T> = closure(input);
61
+ return result.success
62
+ ? null
63
+ : new BadRequestException({
64
+ errors: result.errors,
65
+ message: MESSAGE,
66
+ });
67
+ };
68
+
69
+ /**
70
+ * @internal
71
+ */
72
+ const MESSAGE = "Request body data is not following the promised type.";
@@ -1,75 +1,75 @@
1
- import { BadRequestException } from "@nestjs/common";
2
- import typia, { IValidation, TypeGuardError } from "typia";
3
-
4
- import { IRequestFormDataProps } from "../../options/IRequestFormDataProps";
5
- import { NoTransformConfigureError } from "./NoTransformConfigureError";
6
-
7
- /**
8
- * @internal
9
- */
10
- export const validate_request_form_data = <T>(
11
- props?: IRequestFormDataProps<T>,
12
- ) => {
13
- if (!props) return () => NoTransformConfigureError("TypedFormData.Bpdu");
14
- else if (props.validator.type === "assert")
15
- return assert(props.validator.assert);
16
- else if (props.validator.type === "is") return is(props.validator.is);
17
- else if (props.validator.type === "validate")
18
- return validate(props.validator.validate);
19
- return () =>
20
- new Error(
21
- `Error on nestia.core.TypedFormData.Body(): invalid typed validator.`,
22
- );
23
- };
24
-
25
- /**
26
- * @internal
27
- */
28
- const assert =
29
- <T>(closure: (input: FormData) => T) =>
30
- (input: FormData): T | BadRequestException => {
31
- try {
32
- return closure(input);
33
- } catch (exp) {
34
- if (typia.is<TypeGuardError>(exp)) {
35
- return new BadRequestException({
36
- path: exp.path,
37
- reason: exp.message,
38
- expected: exp.expected,
39
- value: exp.value,
40
- message: MESSAGE,
41
- });
42
- }
43
- throw exp;
44
- }
45
- };
46
-
47
- /**
48
- * @internal
49
- */
50
- const is =
51
- <T>(closure: (input: FormData) => T | null) =>
52
- (input: FormData): T | BadRequestException => {
53
- const result: T | null = closure(input);
54
- return result !== null ? result : new BadRequestException(MESSAGE);
55
- };
56
-
57
- /**
58
- * @internal
59
- */
60
- const validate =
61
- <T>(closure: (input: FormData) => IValidation<T>) =>
62
- (input: FormData): T | BadRequestException => {
63
- const result: IValidation<T> = closure(input);
64
- return result.success
65
- ? result.data
66
- : new BadRequestException({
67
- errors: result.errors,
68
- message: MESSAGE,
69
- });
70
- };
71
-
72
- /**
73
- * @internal
74
- */
75
- const MESSAGE = "Request multipart data is not following the promised type.";
1
+ import { BadRequestException } from "@nestjs/common";
2
+ import typia, { IValidation, TypeGuardError } from "typia";
3
+
4
+ import { IRequestFormDataProps } from "../../options/IRequestFormDataProps";
5
+ import { NoTransformConfigureError } from "./NoTransformConfigureError";
6
+
7
+ /**
8
+ * @internal
9
+ */
10
+ export const validate_request_form_data = <T>(
11
+ props?: IRequestFormDataProps<T>,
12
+ ) => {
13
+ if (!props) throw NoTransformConfigureError("TypedFormData.Bpdu");
14
+ else if (props.validator.type === "assert")
15
+ return assert(props.validator.assert);
16
+ else if (props.validator.type === "is") return is(props.validator.is);
17
+ else if (props.validator.type === "validate")
18
+ return validate(props.validator.validate);
19
+ return () =>
20
+ new Error(
21
+ `Error on nestia.core.TypedFormData.Body(): invalid typed validator.`,
22
+ );
23
+ };
24
+
25
+ /**
26
+ * @internal
27
+ */
28
+ const assert =
29
+ <T>(closure: (input: FormData) => T) =>
30
+ (input: FormData): T | BadRequestException => {
31
+ try {
32
+ return closure(input);
33
+ } catch (exp) {
34
+ if (typia.is<TypeGuardError>(exp)) {
35
+ return new BadRequestException({
36
+ path: exp.path,
37
+ reason: exp.message,
38
+ expected: exp.expected,
39
+ value: exp.value,
40
+ message: MESSAGE,
41
+ });
42
+ }
43
+ throw exp;
44
+ }
45
+ };
46
+
47
+ /**
48
+ * @internal
49
+ */
50
+ const is =
51
+ <T>(closure: (input: FormData) => T | null) =>
52
+ (input: FormData): T | BadRequestException => {
53
+ const result: T | null = closure(input);
54
+ return result !== null ? result : new BadRequestException(MESSAGE);
55
+ };
56
+
57
+ /**
58
+ * @internal
59
+ */
60
+ const validate =
61
+ <T>(closure: (input: FormData) => IValidation<T>) =>
62
+ (input: FormData): T | BadRequestException => {
63
+ const result: IValidation<T> = closure(input);
64
+ return result.success
65
+ ? result.data
66
+ : new BadRequestException({
67
+ errors: result.errors,
68
+ message: MESSAGE,
69
+ });
70
+ };
71
+
72
+ /**
73
+ * @internal
74
+ */
75
+ const MESSAGE = "Request multipart data is not following the promised type.";