@nestia/core 10.0.2 → 11.0.0-dev.20260305

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 (155) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +93 -93
  3. package/lib/adaptors/WebSocketAdaptor.js.map +1 -1
  4. package/lib/decorators/DynamicModule.js +54 -10
  5. package/lib/decorators/DynamicModule.js.map +1 -1
  6. package/lib/decorators/EncryptedBody.js +2 -2
  7. package/lib/decorators/EncryptedBody.js.map +1 -1
  8. package/lib/decorators/EncryptedController.d.ts +1 -1
  9. package/lib/decorators/EncryptedModule.d.ts +1 -1
  10. package/lib/decorators/EncryptedModule.js +54 -10
  11. package/lib/decorators/EncryptedModule.js.map +1 -1
  12. package/lib/decorators/EncryptedRoute.js +2 -2
  13. package/lib/decorators/EncryptedRoute.js.map +1 -1
  14. package/lib/decorators/HumanRoute.d.ts +1 -2
  15. package/lib/decorators/HumanRoute.js +1 -2
  16. package/lib/decorators/HumanRoute.js.map +1 -1
  17. package/lib/decorators/NoTransformConfigurationError.js +1 -1
  18. package/lib/decorators/NoTransformConfigurationError.js.map +1 -1
  19. package/lib/decorators/SwaggerCustomizer.d.ts +1 -1
  20. package/lib/decorators/TypedFormData.js.map +1 -1
  21. package/lib/decorators/TypedParam.js +1 -1
  22. package/lib/decorators/TypedParam.js.map +1 -1
  23. package/lib/decorators/internal/get_path_and_querify.js +1 -1
  24. package/lib/decorators/internal/get_path_and_querify.js.map +1 -1
  25. package/lib/decorators/internal/get_path_and_stringify.js +1 -1
  26. package/lib/decorators/internal/get_path_and_stringify.js.map +1 -1
  27. package/lib/decorators/internal/headers_to_object.js.map +1 -1
  28. package/lib/decorators/internal/validate_request_body.js +1 -1
  29. package/lib/decorators/internal/validate_request_body.js.map +1 -1
  30. package/lib/decorators/internal/validate_request_form_data.js +1 -1
  31. package/lib/decorators/internal/validate_request_form_data.js.map +1 -1
  32. package/lib/decorators/internal/validate_request_headers.js +1 -1
  33. package/lib/decorators/internal/validate_request_headers.js.map +1 -1
  34. package/lib/decorators/internal/validate_request_query.js +1 -1
  35. package/lib/decorators/internal/validate_request_query.js.map +1 -1
  36. package/lib/module.d.ts +1 -0
  37. package/lib/module.js +1 -0
  38. package/lib/module.js.map +1 -1
  39. package/lib/options/INestiaTransformProject.d.ts +1 -1
  40. package/lib/programmers/PlainBodyProgrammer.js +5 -8
  41. package/lib/programmers/PlainBodyProgrammer.js.map +1 -1
  42. package/lib/programmers/TypedBodyProgrammer.js +13 -21
  43. package/lib/programmers/TypedBodyProgrammer.js.map +1 -1
  44. package/lib/programmers/TypedFormDataBodyProgrammer.js +11 -20
  45. package/lib/programmers/TypedFormDataBodyProgrammer.js.map +1 -1
  46. package/lib/programmers/TypedHeadersProgrammer.js +4 -6
  47. package/lib/programmers/TypedHeadersProgrammer.js.map +1 -1
  48. package/lib/programmers/TypedParamProgrammer.js +2 -2
  49. package/lib/programmers/TypedParamProgrammer.js.map +1 -1
  50. package/lib/programmers/TypedQueryBodyProgrammer.js +9 -16
  51. package/lib/programmers/TypedQueryBodyProgrammer.js.map +1 -1
  52. package/lib/programmers/TypedQueryProgrammer.js +9 -16
  53. package/lib/programmers/TypedQueryProgrammer.js.map +1 -1
  54. package/lib/programmers/TypedQueryRouteProgrammer.js +6 -10
  55. package/lib/programmers/TypedQueryRouteProgrammer.js.map +1 -1
  56. package/lib/programmers/TypedRouteProgrammer.js +8 -13
  57. package/lib/programmers/TypedRouteProgrammer.js.map +1 -1
  58. package/lib/programmers/http/HttpAssertQuerifyProgrammer.d.ts +1 -1
  59. package/lib/programmers/http/HttpAssertQuerifyProgrammer.js +5 -7
  60. package/lib/programmers/http/HttpAssertQuerifyProgrammer.js.map +1 -1
  61. package/lib/programmers/http/HttpIsQuerifyProgrammer.d.ts +1 -1
  62. package/lib/programmers/http/HttpIsQuerifyProgrammer.js +5 -7
  63. package/lib/programmers/http/HttpIsQuerifyProgrammer.js.map +1 -1
  64. package/lib/programmers/http/HttpQuerifyProgrammer.d.ts +1 -1
  65. package/lib/programmers/http/HttpQuerifyProgrammer.js +13 -19
  66. package/lib/programmers/http/HttpQuerifyProgrammer.js.map +1 -1
  67. package/lib/programmers/http/HttpValidateQuerifyProgrammer.d.ts +1 -1
  68. package/lib/programmers/http/HttpValidateQuerifyProgrammer.js +6 -8
  69. package/lib/programmers/http/HttpValidateQuerifyProgrammer.js.map +1 -1
  70. package/lib/programmers/internal/CoreMetadataUtil.d.ts +3 -3
  71. package/lib/programmers/internal/CoreMetadataUtil.js.map +1 -1
  72. package/lib/transform.d.ts +1 -1
  73. package/lib/transform.js.map +1 -1
  74. package/lib/transformers/FileTransformer.js +2 -2
  75. package/lib/transformers/FileTransformer.js.map +1 -1
  76. package/lib/transformers/MethodTransformer.js.map +1 -1
  77. package/lib/transformers/ParameterDecoratorTransformer.js +1 -1
  78. package/lib/transformers/ParameterDecoratorTransformer.js.map +1 -1
  79. package/lib/transformers/TypedRouteTransformer.js +1 -1
  80. package/lib/transformers/TypedRouteTransformer.js.map +1 -1
  81. package/lib/transformers/WebSocketRouteTransformer.js +1 -1
  82. package/lib/transformers/WebSocketRouteTransformer.js.map +1 -1
  83. package/package.json +34 -26
  84. package/src/adaptors/WebSocketAdaptor.ts +429 -429
  85. package/src/decorators/DynamicModule.ts +44 -44
  86. package/src/decorators/EncryptedBody.ts +96 -97
  87. package/src/decorators/EncryptedController.ts +40 -40
  88. package/src/decorators/EncryptedModule.ts +98 -98
  89. package/src/decorators/EncryptedRoute.ts +212 -213
  90. package/src/decorators/HumanRoute.ts +21 -22
  91. package/src/decorators/NoTransformConfigurationError.ts +34 -34
  92. package/src/decorators/PlainBody.ts +76 -76
  93. package/src/decorators/SwaggerCustomizer.ts +97 -97
  94. package/src/decorators/SwaggerExample.ts +100 -100
  95. package/src/decorators/TypedBody.ts +57 -57
  96. package/src/decorators/TypedException.ts +147 -147
  97. package/src/decorators/TypedFormData.ts +187 -195
  98. package/src/decorators/TypedHeaders.ts +66 -66
  99. package/src/decorators/TypedParam.ts +77 -77
  100. package/src/decorators/TypedQuery.ts +234 -234
  101. package/src/decorators/TypedRoute.ts +196 -196
  102. package/src/decorators/WebSocketRoute.ts +242 -242
  103. package/src/decorators/internal/EncryptedConstant.ts +2 -2
  104. package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
  105. package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
  106. package/src/decorators/internal/get_path_and_querify.ts +94 -94
  107. package/src/decorators/internal/get_path_and_stringify.ts +110 -110
  108. package/src/decorators/internal/get_text_body.ts +16 -16
  109. package/src/decorators/internal/headers_to_object.ts +11 -11
  110. package/src/decorators/internal/is_request_body_undefined.ts +12 -12
  111. package/src/decorators/internal/load_controller.ts +45 -45
  112. package/src/decorators/internal/route_error.ts +43 -43
  113. package/src/decorators/internal/validate_request_body.ts +64 -64
  114. package/src/decorators/internal/validate_request_form_data.ts +67 -67
  115. package/src/decorators/internal/validate_request_headers.ts +76 -76
  116. package/src/decorators/internal/validate_request_query.ts +64 -64
  117. package/src/index.ts +5 -5
  118. package/src/module.ts +23 -22
  119. package/src/options/INestiaTransformOptions.ts +34 -34
  120. package/src/options/INestiaTransformProject.ts +10 -8
  121. package/src/options/IRequestBodyValidator.ts +20 -20
  122. package/src/options/IRequestFormDataProps.ts +27 -27
  123. package/src/options/IRequestHeadersValidator.ts +22 -22
  124. package/src/options/IRequestQueryValidator.ts +20 -20
  125. package/src/options/IResponseBodyQuerifier.ts +25 -25
  126. package/src/options/IResponseBodyStringifier.ts +30 -30
  127. package/src/programmers/PlainBodyProgrammer.ts +72 -70
  128. package/src/programmers/TypedBodyProgrammer.ts +144 -142
  129. package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
  130. package/src/programmers/TypedHeadersProgrammer.ts +65 -63
  131. package/src/programmers/TypedParamProgrammer.ts +33 -33
  132. package/src/programmers/TypedQueryBodyProgrammer.ts +111 -109
  133. package/src/programmers/TypedQueryProgrammer.ts +113 -111
  134. package/src/programmers/TypedQueryRouteProgrammer.ts +104 -102
  135. package/src/programmers/TypedRouteProgrammer.ts +96 -94
  136. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +74 -72
  137. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +77 -75
  138. package/src/programmers/http/HttpQuerifyProgrammer.ts +110 -108
  139. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +78 -76
  140. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  141. package/src/transform.ts +35 -35
  142. package/src/transformers/FileTransformer.ts +109 -110
  143. package/src/transformers/MethodTransformer.ts +103 -103
  144. package/src/transformers/NodeTransformer.ts +23 -23
  145. package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
  146. package/src/transformers/ParameterTransformer.ts +57 -57
  147. package/src/transformers/TypedRouteTransformer.ts +85 -85
  148. package/src/transformers/WebSocketRouteTransformer.ts +120 -120
  149. package/src/typings/Creator.ts +3 -3
  150. package/src/typings/get-function-location.d.ts +7 -7
  151. package/src/utils/ArrayUtil.ts +7 -7
  152. package/src/utils/ExceptionManager.ts +115 -115
  153. package/src/utils/Singleton.ts +16 -16
  154. package/src/utils/SourceFinder.ts +54 -54
  155. package/src/utils/VersioningStrategy.ts +27 -27
@@ -1,195 +1,187 @@
1
- import {
2
- BadRequestException,
3
- ExecutionContext,
4
- createParamDecorator,
5
- } from "@nestjs/common";
6
- import type { HttpArgumentsHost } from "@nestjs/common/interfaces";
7
- import type express from "express";
8
- import type ExpressMulter from "multer";
9
-
10
- import type { IRequestFormDataProps } from "../options/IRequestFormDataProps";
11
- import { Singleton } from "../utils/Singleton";
12
- import { validate_request_form_data } from "./internal/validate_request_form_data";
13
-
14
- /**
15
- * Type safe multipart/form-data decorator.
16
- *
17
- * `TypedFormData.Body()` is a request body decorator function for the
18
- * `multipart/form-data` content type. It automatically casts property type
19
- * following its DTO definition, and performs the type validation too.
20
- *
21
- * Also, `TypedFormData.Body()` is much easier and type safer than
22
- * `@nest.UploadFile()`. If you're considering the [SDK
23
- * library](https://nestia.io/docs/sdk/sdk) generation, only
24
- * `TypedFormData.Body()` can do it. Therefore, I recommend you to use
25
- * `TypedFormData.Body()` instead of the `@nest.UploadFile()` function.
26
- *
27
- * For reference, target type `T` must follow such restriction. Of course, if
28
- * actual form-data values are different with their promised type `T`,
29
- * `BadRequestException` error (status code: 400) would be thrown.
30
- *
31
- * 1. Type `T` must be an object type
32
- * 2. Do not allow dynamic property
33
- * 3. Only `boolean`, `bigint`, `number`, `string`, `Blob`, `File` or their array
34
- * types are allowed
35
- * 4. By the way, union type never be not allowed
36
- *
37
- * By the way, if you're using `fastify`, you have to setup `fastify-multer` and
38
- * configure like below when composing the NestJS application. If you don't do
39
- * that, `@TypedFormData.Body()` will not work properly, and throw 500 internal
40
- * server error when `Blob` or `File` type being utilized.
41
- *
42
- * ```typescript
43
- * import { NestFactory } from "@nestjs/core";
44
- * import {
45
- * FastifyAdapter,
46
- * NestFastifyApplication,
47
- * } from "@nestjs/platform-fastify";
48
- * import fastifyMulter from "fastify-multer";
49
- *
50
- * export async function main() {
51
- * const app = await NestFactory.create<NestFastifyApplication>(
52
- * AppModule,
53
- * new FastifyAdapter(),
54
- * );
55
- * app.register(fastifyMulter.contentParser);
56
- * await app.listen(3000);
57
- * }
58
- * ```
59
- *
60
- * @author Jeongho Nam - https://github.com/samchon
61
- * @todo Change to ReadableStream through configuring storage engine of multer
62
- */
63
- export namespace TypedFormData {
64
- /**
65
- * Request body decorator.
66
- *
67
- * Request body decorator for the `multipart/form-data` type.
68
- *
69
- * Much easier and type safer than `@nest.UploadFile()` decorator.
70
- *
71
- * @param factory Factory function ncreating the `multer` or `fastify-multer`
72
- * instance. In the factory function, you also can specify the multer
73
- * composition options like `storage` engine.
74
- */
75
- export function Body<Multer extends IMulterBase>(
76
- factory: () => Multer | Promise<Multer>,
77
- ): ParameterDecorator;
78
-
79
- /** @internal */
80
- export function Body<T extends object>(
81
- factory: () => Promise<IMulterBase>,
82
- props?: IRequestFormDataProps<T> | undefined,
83
- ): ParameterDecorator {
84
- if (typeof File === "undefined")
85
- throw new Error(
86
- "Error on TypedFormData.Body(): 'File' class is not supported in the older version of NodeJS. Upgrade the NodeJS to the modern.",
87
- );
88
- const checker = validate_request_form_data(props);
89
- const uploader = new Singleton(async () =>
90
- decode((await factory()) as ExpressMulter.Multer, props!),
91
- );
92
- return createParamDecorator(async function TypedFormDataBody(
93
- _unknown: any,
94
- context: ExecutionContext,
95
- ): Promise<T> {
96
- const http: HttpArgumentsHost = context.switchToHttp();
97
- const request: express.Request = http.getRequest();
98
- if (isMultipartFormData(request.headers["content-type"]) === false)
99
- throw new BadRequestException(
100
- `Request body type is not "multipart/form-data".`,
101
- );
102
- const data: FormData = await (
103
- await uploader.get()
104
- )({
105
- request: request as any,
106
- response: http.getResponse(),
107
- });
108
- const output: T | Error = checker(data);
109
- if (output instanceof Error) throw output;
110
- return output;
111
- })();
112
- }
113
-
114
- /** Base type of the `multer` or `fastify-multer`. */
115
- export interface IMulterBase {
116
- single(fieldName: string): any;
117
- array(fieldName: string, maxCount?: number): any;
118
- fields(fields: readonly object[]): any;
119
- any(): any;
120
- none(): any;
121
- }
122
- }
123
-
124
- /** @internal */
125
- const decode = <T>(
126
- multer: ExpressMulter.Multer,
127
- props: IRequestFormDataProps<T>,
128
- ) => {
129
- const upload = multer.fields(
130
- props!.files.map((file) => ({
131
- name: file.name,
132
- ...(file.limit === 1 ? { maxCount: 1 } : {}),
133
- })),
134
- );
135
- const interceptor = (request: express.Request, response: express.Response) =>
136
- new Promise<void>((resolve, reject) =>
137
- upload(request, response, (error) => {
138
- if (error) reject(error);
139
- else resolve();
140
- }),
141
- );
142
- return async (socket: {
143
- request: express.Request;
144
- response: express.Response;
145
- }): Promise<FormData> => {
146
- await interceptor(socket.request, socket.response);
147
-
148
- const data: FormData = new FormData();
149
- for (const [key, value] of Object.entries(socket.request.body))
150
- if (Array.isArray(value))
151
- for (const elem of value) data.append(key, String(elem));
152
- else data.append(key, String(value));
153
- if (socket.request.files) parseFiles(data)(socket.request.files);
154
- return data;
155
- };
156
- };
157
-
158
- /** @internal */
159
- const parseFiles =
160
- (data: FormData) =>
161
- (files: Express.Multer.File[] | Record<string, Express.Multer.File[]>) => {
162
- if (Array.isArray(files))
163
- for (const file of files)
164
- data.append(
165
- file.fieldname,
166
- new File(
167
- [file.buffer satisfies Uint8Array as any],
168
- file.originalname,
169
- {
170
- type: file.mimetype,
171
- },
172
- ),
173
- );
174
- else
175
- for (const [key, value] of Object.entries(files))
176
- for (const file of value)
177
- data.append(
178
- key,
179
- new File(
180
- [file.buffer satisfies Uint8Array as any],
181
- file.originalname,
182
- {
183
- type: file.mimetype,
184
- },
185
- ),
186
- );
187
- };
188
-
189
- /** @internal */
190
- const isMultipartFormData = (text?: string): boolean =>
191
- text !== undefined &&
192
- text
193
- .split(";")
194
- .map((str) => str.trim())
195
- .some((str) => str === "multipart/form-data");
1
+ import {
2
+ BadRequestException,
3
+ ExecutionContext,
4
+ createParamDecorator,
5
+ } from "@nestjs/common";
6
+ import type { HttpArgumentsHost } from "@nestjs/common/interfaces";
7
+ import type express from "express";
8
+ import type ExpressMulter from "multer";
9
+
10
+ import type { IRequestFormDataProps } from "../options/IRequestFormDataProps";
11
+ import { Singleton } from "../utils/Singleton";
12
+ import { validate_request_form_data } from "./internal/validate_request_form_data";
13
+
14
+ /**
15
+ * Type safe multipart/form-data decorator.
16
+ *
17
+ * `TypedFormData.Body()` is a request body decorator function for the
18
+ * `multipart/form-data` content type. It automatically casts property type
19
+ * following its DTO definition, and performs the type validation too.
20
+ *
21
+ * Also, `TypedFormData.Body()` is much easier and type safer than
22
+ * `@nest.UploadFile()`. If you're considering the [SDK
23
+ * library](https://nestia.io/docs/sdk/sdk) generation, only
24
+ * `TypedFormData.Body()` can do it. Therefore, I recommend you to use
25
+ * `TypedFormData.Body()` instead of the `@nest.UploadFile()` function.
26
+ *
27
+ * For reference, target type `T` must follow such restriction. Of course, if
28
+ * actual form-data values are different with their promised type `T`,
29
+ * `BadRequestException` error (status code: 400) would be thrown.
30
+ *
31
+ * 1. Type `T` must be an object type
32
+ * 2. Do not allow dynamic property
33
+ * 3. Only `boolean`, `bigint`, `number`, `string`, `Blob`, `File` or their array
34
+ * types are allowed
35
+ * 4. By the way, union type never be not allowed
36
+ *
37
+ * By the way, if you're using `fastify`, you have to setup `fastify-multer` and
38
+ * configure like below when composing the NestJS application. If you don't do
39
+ * that, `@TypedFormData.Body()` will not work properly, and throw 500 internal
40
+ * server error when `Blob` or `File` type being utilized.
41
+ *
42
+ * ```typescript
43
+ * import { NestFactory } from "@nestjs/core";
44
+ * import {
45
+ * FastifyAdapter,
46
+ * NestFastifyApplication,
47
+ * } from "@nestjs/platform-fastify";
48
+ * import fastifyMulter from "fastify-multer";
49
+ *
50
+ * export async function main() {
51
+ * const app = await NestFactory.create<NestFastifyApplication>(
52
+ * AppModule,
53
+ * new FastifyAdapter(),
54
+ * );
55
+ * app.register(fastifyMulter.contentParser);
56
+ * await app.listen(3000);
57
+ * }
58
+ * ```
59
+ *
60
+ * @author Jeongho Nam - https://github.com/samchon
61
+ * @todo Change to ReadableStream through configuring storage engine of multer
62
+ */
63
+ export namespace TypedFormData {
64
+ /**
65
+ * Request body decorator.
66
+ *
67
+ * Request body decorator for the `multipart/form-data` type.
68
+ *
69
+ * Much easier and type safer than `@nest.UploadFile()` decorator.
70
+ *
71
+ * @param factory Factory function ncreating the `multer` or `fastify-multer`
72
+ * instance. In the factory function, you also can specify the multer
73
+ * composition options like `storage` engine.
74
+ */
75
+ export function Body<Multer extends IMulterBase>(
76
+ factory: () => Multer | Promise<Multer>,
77
+ ): ParameterDecorator;
78
+
79
+ /** @internal */
80
+ export function Body<T extends object>(
81
+ factory: () => Promise<IMulterBase>,
82
+ props?: IRequestFormDataProps<T> | undefined,
83
+ ): ParameterDecorator {
84
+ if (typeof File === "undefined")
85
+ throw new Error(
86
+ "Error on TypedFormData.Body(): 'File' class is not supported in the older version of NodeJS. Upgrade the NodeJS to the modern.",
87
+ );
88
+ const checker = validate_request_form_data(props);
89
+ const uploader = new Singleton(async () =>
90
+ decode((await factory()) as ExpressMulter.Multer, props!),
91
+ );
92
+ return createParamDecorator(async function TypedFormDataBody(
93
+ _unknown: any,
94
+ context: ExecutionContext,
95
+ ): Promise<T> {
96
+ const http: HttpArgumentsHost = context.switchToHttp();
97
+ const request: express.Request = http.getRequest();
98
+ if (isMultipartFormData(request.headers["content-type"]) === false)
99
+ throw new BadRequestException(
100
+ `Request body type is not "multipart/form-data".`,
101
+ );
102
+ const data: FormData = await (
103
+ await uploader.get()
104
+ )({
105
+ request: request as any,
106
+ response: http.getResponse(),
107
+ });
108
+ const output: T | Error = checker(data);
109
+ if (output instanceof Error) throw output;
110
+ return output;
111
+ })();
112
+ }
113
+
114
+ /** Base type of the `multer` or `fastify-multer`. */
115
+ export interface IMulterBase {
116
+ single(fieldName: string): any;
117
+ array(fieldName: string, maxCount?: number): any;
118
+ fields(fields: readonly object[]): any;
119
+ any(): any;
120
+ none(): any;
121
+ }
122
+ }
123
+
124
+ /** @internal */
125
+ const decode = <T>(
126
+ multer: ExpressMulter.Multer,
127
+ props: IRequestFormDataProps<T>,
128
+ ) => {
129
+ const upload = multer.fields(
130
+ props!.files.map((file) => ({
131
+ name: file.name,
132
+ ...(file.limit === 1 ? { maxCount: 1 } : {}),
133
+ })),
134
+ );
135
+ const interceptor = (request: express.Request, response: express.Response) =>
136
+ new Promise<void>((resolve, reject) =>
137
+ upload(request, response, (error) => {
138
+ if (error) reject(error);
139
+ else resolve();
140
+ }),
141
+ );
142
+ return async (socket: {
143
+ request: express.Request;
144
+ response: express.Response;
145
+ }): Promise<FormData> => {
146
+ await interceptor(socket.request, socket.response);
147
+
148
+ const data: FormData = new FormData();
149
+ for (const [key, value] of Object.entries(socket.request.body))
150
+ if (Array.isArray(value))
151
+ for (const elem of value) data.append(key, String(elem));
152
+ else data.append(key, String(value));
153
+ if (socket.request.files) parseFiles(data)(socket.request.files);
154
+ return data;
155
+ };
156
+ };
157
+
158
+ /** @internal */
159
+ const parseFiles =
160
+ (data: FormData) =>
161
+ (files: Express.Multer.File[] | Record<string, Express.Multer.File[]>) => {
162
+ if (Array.isArray(files))
163
+ for (const file of files)
164
+ data.append(
165
+ file.fieldname,
166
+ new File([file.buffer as any], file.originalname, {
167
+ type: file.mimetype,
168
+ }),
169
+ );
170
+ else
171
+ for (const [key, value] of Object.entries(files))
172
+ for (const file of value)
173
+ data.append(
174
+ key,
175
+ new File([file.buffer as any], file.originalname, {
176
+ type: file.mimetype,
177
+ }),
178
+ );
179
+ };
180
+
181
+ /** @internal */
182
+ const isMultipartFormData = (text?: string): boolean =>
183
+ text !== undefined &&
184
+ text
185
+ .split(";")
186
+ .map((str) => str.trim())
187
+ .some((str) => str === "multipart/form-data");
@@ -1,66 +1,66 @@
1
- import { ExecutionContext, createParamDecorator } from "@nestjs/common";
2
- import type express from "express";
3
- import type { FastifyRequest } from "fastify";
4
-
5
- import { IRequestHeadersValidator } from "../options/IRequestHeadersValidator";
6
- import { validate_request_headers } from "./internal/validate_request_headers";
7
-
8
- /**
9
- * Type safe HTTP headers decorator.
10
- *
11
- * `TypedHeaders` is a decorator function that can parse HTTP headers. It is
12
- * almost same with {@link nest.Headers}, but it can automatically cast property
13
- * type following its DTO definition. Also, `TypedHeaders` performs type
14
- * validation.
15
- *
16
- * For reference, target type `T` must follow such restrictions. Also, if actual
17
- * HTTP header values are different with their promised type `T`,
18
- * `BadRequestException` error (status code: 400) would be thrown.
19
- *
20
- * 1. Type `T` must be an object type
21
- * 2. Do not allow dynamic property
22
- * 3. Property key must be lower case
23
- * 4. Property value cannot be `null`, but `undefined` is possible
24
- * 5. Only `boolean`, `bigint`, `number`, `string` or their array types are allowed
25
- * 6. By the way, union type never be not allowed
26
- * 7. Property `set-cookie` must be array type
27
- * 8. Those properties cannot be array type
28
- *
29
- * - Age
30
- * - Authorization
31
- * - Content-length
32
- * - Content-type
33
- * - Etag
34
- * - Expires
35
- * - From
36
- * - Host
37
- * - If-modified-since
38
- * - If-unmodified-since
39
- * - Last-modified
40
- * - Location
41
- * - Max-forwards
42
- * - Proxy-authorization
43
- * - Referer
44
- * - Retry-after
45
- * - Server
46
- * - User-agent
47
- *
48
- * @author Jeongho Nam - https://github.com/samchon
49
- * @returns Parameter decorator
50
- */
51
- export function TypedHeaders<T extends object>(
52
- validator?: IRequestHeadersValidator<T>,
53
- ): ParameterDecorator {
54
- const checker = validate_request_headers(validator);
55
- return createParamDecorator(function TypedHeaders(
56
- _unknown: any,
57
- context: ExecutionContext,
58
- ): T {
59
- const request: express.Request | FastifyRequest = context
60
- .switchToHttp()
61
- .getRequest();
62
- const output: T | Error = checker(request.headers);
63
- if (output instanceof Error) throw output;
64
- return output;
65
- })();
66
- }
1
+ import { ExecutionContext, createParamDecorator } from "@nestjs/common";
2
+ import type express from "express";
3
+ import type { FastifyRequest } from "fastify";
4
+
5
+ import { IRequestHeadersValidator } from "../options/IRequestHeadersValidator";
6
+ import { validate_request_headers } from "./internal/validate_request_headers";
7
+
8
+ /**
9
+ * Type safe HTTP headers decorator.
10
+ *
11
+ * `TypedHeaders` is a decorator function that can parse HTTP headers. It is
12
+ * almost same with {@link nest.Headers}, but it can automatically cast property
13
+ * type following its DTO definition. Also, `TypedHeaders` performs type
14
+ * validation.
15
+ *
16
+ * For reference, target type `T` must follow such restrictions. Also, if actual
17
+ * HTTP header values are different with their promised type `T`,
18
+ * `BadRequestException` error (status code: 400) would be thrown.
19
+ *
20
+ * 1. Type `T` must be an object type
21
+ * 2. Do not allow dynamic property
22
+ * 3. Property key must be lower case
23
+ * 4. Property value cannot be `null`, but `undefined` is possible
24
+ * 5. Only `boolean`, `bigint`, `number`, `string` or their array types are allowed
25
+ * 6. By the way, union type never be not allowed
26
+ * 7. Property `set-cookie` must be array type
27
+ * 8. Those properties cannot be array type
28
+ *
29
+ * - Age
30
+ * - Authorization
31
+ * - Content-length
32
+ * - Content-type
33
+ * - Etag
34
+ * - Expires
35
+ * - From
36
+ * - Host
37
+ * - If-modified-since
38
+ * - If-unmodified-since
39
+ * - Last-modified
40
+ * - Location
41
+ * - Max-forwards
42
+ * - Proxy-authorization
43
+ * - Referer
44
+ * - Retry-after
45
+ * - Server
46
+ * - User-agent
47
+ *
48
+ * @author Jeongho Nam - https://github.com/samchon
49
+ * @returns Parameter decorator
50
+ */
51
+ export function TypedHeaders<T extends object>(
52
+ validator?: IRequestHeadersValidator<T>,
53
+ ): ParameterDecorator {
54
+ const checker = validate_request_headers(validator);
55
+ return createParamDecorator(function TypedHeaders(
56
+ _unknown: any,
57
+ context: ExecutionContext,
58
+ ): T {
59
+ const request: express.Request | FastifyRequest = context
60
+ .switchToHttp()
61
+ .getRequest();
62
+ const output: T | Error = checker(request.headers);
63
+ if (output instanceof Error) throw output;
64
+ return output;
65
+ })();
66
+ }
@@ -1,77 +1,77 @@
1
- import {
2
- BadRequestException,
3
- ExecutionContext,
4
- createParamDecorator,
5
- } from "@nestjs/common";
6
- import type express from "express";
7
- import type { FastifyRequest } from "fastify";
8
- import typia, { IValidation, TypeGuardError } from "typia";
9
-
10
- import { NoTransformConfigurationError } from "./NoTransformConfigurationError";
11
-
12
- /**
13
- * Type safe URL parameter decorator.
14
- *
15
- * `TypedParam` is a decorator function getting specific typed parameter from
16
- * the HTTP request URL. It's almost same with the {@link nest.Param}, but
17
- * `TypedParam` automatically casts parameter value to be following its type,
18
- * and validates it.
19
- *
20
- * ```typescript
21
- * import { tags } from "typia";
22
- *
23
- * \@TypedRoute.Get("shopping/sales/:id/:no/:paused")
24
- * public async pause(
25
- * \@TypedParam("id", "uuid"), id: string & tags.Format<"uuid">,
26
- * \@TypedParam("no") id: number & tags.Type<"uint32">
27
- * \@TypedParam("paused") paused: boolean | null
28
- * ): Promise<void>;
29
- * ```
30
- *
31
- * @author Jeongho Nam - https://github.com/samchon
32
- * @param name URL Parameter name
33
- * @returns Parameter decorator
34
- */
35
- export function TypedParam<T extends boolean | bigint | number | string | null>(
36
- name: string,
37
- assert?: (value: string) => T,
38
- validate?: boolean,
39
- ): ParameterDecorator {
40
- if (assert === undefined) {
41
- NoTransformConfigurationError("TypedParam");
42
- assert = (value) => value as T;
43
- }
44
-
45
- return createParamDecorator(function TypedParam(
46
- {}: any,
47
- context: ExecutionContext,
48
- ) {
49
- const request: express.Request | FastifyRequest = context
50
- .switchToHttp()
51
- .getRequest();
52
- const str: string = (request.params as any)[name];
53
- try {
54
- return assert(str);
55
- } catch (exp) {
56
- if (typia.is<TypeGuardError>(exp)) {
57
- const trace: IValidation.IError = {
58
- path: exp.path ?? "$input",
59
- expected: exp.expected,
60
- value: exp.value,
61
- };
62
- throw new BadRequestException({
63
- message: `Invalid URL parameter value on "${name}".`,
64
- ...(validate === true
65
- ? {
66
- errors: [trace],
67
- }
68
- : {
69
- ...trace,
70
- reason: exp.message,
71
- }),
72
- });
73
- }
74
- throw exp;
75
- }
76
- })(name);
77
- }
1
+ import {
2
+ BadRequestException,
3
+ ExecutionContext,
4
+ createParamDecorator,
5
+ } from "@nestjs/common";
6
+ import type express from "express";
7
+ import type { FastifyRequest } from "fastify";
8
+ import typia, { IValidation, TypeGuardError } from "typia";
9
+
10
+ import { NoTransformConfigurationError } from "./NoTransformConfigurationError";
11
+
12
+ /**
13
+ * Type safe URL parameter decorator.
14
+ *
15
+ * `TypedParam` is a decorator function getting specific typed parameter from
16
+ * the HTTP request URL. It's almost same with the {@link nest.Param}, but
17
+ * `TypedParam` automatically casts parameter value to be following its type,
18
+ * and validates it.
19
+ *
20
+ * ```typescript
21
+ * import { tags } from "typia";
22
+ *
23
+ * \@TypedRoute.Get("shopping/sales/:id/:no/:paused")
24
+ * public async pause(
25
+ * \@TypedParam("id", "uuid"), id: string & tags.Format<"uuid">,
26
+ * \@TypedParam("no") id: number & tags.Type<"uint32">
27
+ * \@TypedParam("paused") paused: boolean | null
28
+ * ): Promise<void>;
29
+ * ```
30
+ *
31
+ * @author Jeongho Nam - https://github.com/samchon
32
+ * @param name URL Parameter name
33
+ * @returns Parameter decorator
34
+ */
35
+ export function TypedParam<T extends boolean | bigint | number | string | null>(
36
+ name: string,
37
+ assert?: (value: string) => T,
38
+ validate?: boolean,
39
+ ): ParameterDecorator {
40
+ if (assert === undefined) {
41
+ NoTransformConfigurationError("TypedParam");
42
+ assert = (value) => value as T;
43
+ }
44
+
45
+ return createParamDecorator(function TypedParam(
46
+ {}: any,
47
+ context: ExecutionContext,
48
+ ) {
49
+ const request: express.Request | FastifyRequest = context
50
+ .switchToHttp()
51
+ .getRequest();
52
+ const str: string = (request.params as any)[name];
53
+ try {
54
+ return assert(str);
55
+ } catch (exp) {
56
+ if (typia.is<TypeGuardError>(exp)) {
57
+ const trace: IValidation.IError = {
58
+ path: exp.path ?? "$input",
59
+ expected: exp.expected,
60
+ value: exp.value,
61
+ };
62
+ throw new BadRequestException({
63
+ message: `Invalid URL parameter value on "${name}".`,
64
+ ...(validate === true
65
+ ? {
66
+ errors: [trace],
67
+ }
68
+ : {
69
+ ...trace,
70
+ reason: exp.message,
71
+ }),
72
+ });
73
+ }
74
+ throw exp;
75
+ }
76
+ })(name);
77
+ }