@nestia/core 11.0.0-dev.20260316 → 11.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +93 -93
- package/lib/decorators/NoTransformConfigurationError.d.ts +1 -24
- package/lib/decorators/NoTransformConfigurationError.js +2 -0
- package/lib/decorators/NoTransformConfigurationError.js.map +1 -1
- package/lib/decorators/doNotThrowTransformError.d.ts +1 -0
- package/lib/decorators/doNotThrowTransformError.js +9 -0
- package/lib/decorators/doNotThrowTransformError.js.map +1 -0
- package/lib/module.d.ts +1 -1
- package/lib/module.js +1 -1
- package/lib/module.js.map +1 -1
- package/package.json +8 -8
- package/src/adaptors/WebSocketAdaptor.ts +429 -429
- package/src/decorators/EncryptedBody.ts +96 -96
- package/src/decorators/EncryptedController.ts +40 -40
- package/src/decorators/EncryptedModule.ts +98 -98
- package/src/decorators/EncryptedRoute.ts +212 -212
- package/src/decorators/HumanRoute.ts +21 -21
- package/src/decorators/NoTransformConfigurationError.ts +37 -34
- package/src/decorators/SwaggerCustomizer.ts +97 -97
- package/src/decorators/TypedFormData.ts +187 -187
- package/src/decorators/TypedRoute.ts +196 -196
- package/src/decorators/doNotThrowTransformError.ts +5 -0
- package/src/decorators/internal/headers_to_object.ts +11 -11
- package/src/module.ts +23 -23
- package/src/options/INestiaTransformOptions.ts +34 -34
- package/src/options/INestiaTransformProject.ts +10 -10
- package/src/programmers/PlainBodyProgrammer.ts +72 -72
- package/src/programmers/TypedBodyProgrammer.ts +148 -148
- package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
- package/src/programmers/TypedHeadersProgrammer.ts +65 -65
- package/src/programmers/TypedParamProgrammer.ts +33 -33
- package/src/programmers/TypedQueryBodyProgrammer.ts +113 -113
- package/src/programmers/TypedQueryProgrammer.ts +115 -115
- package/src/programmers/TypedQueryRouteProgrammer.ts +107 -107
- package/src/programmers/TypedRouteProgrammer.ts +103 -103
- package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +74 -74
- package/src/programmers/http/HttpIsQuerifyProgrammer.ts +77 -77
- package/src/programmers/http/HttpQuerifyProgrammer.ts +110 -110
- package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +78 -78
- package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
- package/src/transform.ts +35 -35
- package/src/transformers/FileTransformer.ts +109 -109
- package/src/transformers/MethodTransformer.ts +103 -103
- package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
- package/src/transformers/TypedRouteTransformer.ts +85 -85
- package/src/transformers/WebSocketRouteTransformer.ts +120 -120
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
import { OpenApi } from "@typia/interface";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Swagger customization decorator.
|
|
5
|
-
*
|
|
6
|
-
* `SwaggerCustomizer` is a method decorator function which can used for
|
|
7
|
-
* customizing the swagger data with `npx nestia swagger` command. Furthermore,
|
|
8
|
-
* it is possible to add plugin properties starting with `x-` characters.
|
|
9
|
-
*
|
|
10
|
-
* In other words, this decorator function does not affect to the runtime, but
|
|
11
|
-
* only for the swagger data customization.
|
|
12
|
-
*
|
|
13
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
14
|
-
* @param closure Callback function which can customize the swagger data
|
|
15
|
-
* @returns Method decorator
|
|
16
|
-
*/
|
|
17
|
-
export function SwaggerCustomizer(
|
|
18
|
-
closure: (props: SwaggerCustomizer.IProps) => unknown,
|
|
19
|
-
): MethodDecorator {
|
|
20
|
-
return function SwaggerCustomizer(
|
|
21
|
-
target: Object,
|
|
22
|
-
propertyKey: string | symbol,
|
|
23
|
-
descriptor: TypedPropertyDescriptor<any>,
|
|
24
|
-
) {
|
|
25
|
-
const array: Array<(props: SwaggerCustomizer.IProps) => unknown> = (() => {
|
|
26
|
-
if (Reflect.hasMetadata("nestia/SwaggerCustomizer", target, propertyKey))
|
|
27
|
-
return Reflect.getMetadata(
|
|
28
|
-
"nestia/SwaggerCustomizer",
|
|
29
|
-
target,
|
|
30
|
-
propertyKey,
|
|
31
|
-
);
|
|
32
|
-
const array: Array<(props: SwaggerCustomizer.IProps) => unknown> = [];
|
|
33
|
-
Reflect.defineMetadata(
|
|
34
|
-
"nestia/SwaggerCustomizer",
|
|
35
|
-
array,
|
|
36
|
-
target,
|
|
37
|
-
propertyKey,
|
|
38
|
-
);
|
|
39
|
-
return array;
|
|
40
|
-
})();
|
|
41
|
-
array.push(closure);
|
|
42
|
-
return descriptor;
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
export namespace SwaggerCustomizer {
|
|
46
|
-
/**
|
|
47
|
-
* Properties for the `SwaggerCustomizer` decorator.
|
|
48
|
-
*
|
|
49
|
-
* `SwaggerCustomizer.IProps` is a type for the `closure` parameter of the
|
|
50
|
-
* `SwaggerCustomizer` decorator. It's a callback function which can customize
|
|
51
|
-
* the swagger data.
|
|
52
|
-
*/
|
|
53
|
-
export interface IProps {
|
|
54
|
-
/** Swagger data. */
|
|
55
|
-
swagger: OpenApi.IDocument;
|
|
56
|
-
|
|
57
|
-
/** Method of the route. */
|
|
58
|
-
method: string;
|
|
59
|
-
|
|
60
|
-
/** Path of the route. */
|
|
61
|
-
path: string;
|
|
62
|
-
|
|
63
|
-
/** Route data. */
|
|
64
|
-
route: OpenApi.IOperation;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Get neighbor endpoint data through the controller method.
|
|
68
|
-
*
|
|
69
|
-
* @param func Controller method to find the neighbor endpoint
|
|
70
|
-
* @returns Neighbor endpoint data
|
|
71
|
-
*/
|
|
72
|
-
at(func: Function): ISwaggerEndpoint | undefined;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Get neighbor route data.
|
|
76
|
-
*
|
|
77
|
-
* @param accessor Accessor for getting neighbor route data
|
|
78
|
-
* @returns Neighbor route data
|
|
79
|
-
*/
|
|
80
|
-
get(accessor: IAccessor): OpenApi.IOperation | undefined;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Accessor for getting neighbor route data. */
|
|
84
|
-
export interface IAccessor {
|
|
85
|
-
/** Path of the neighbor route. */
|
|
86
|
-
path: string;
|
|
87
|
-
|
|
88
|
-
/** Method of the neighbor route. */
|
|
89
|
-
method: string;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** Endpoint info of the route. */
|
|
93
|
-
export interface ISwaggerEndpoint extends IAccessor {
|
|
94
|
-
/** Route data. */
|
|
95
|
-
route: OpenApi.IOperation;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
1
|
+
import { OpenApi } from "@typia/interface";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Swagger customization decorator.
|
|
5
|
+
*
|
|
6
|
+
* `SwaggerCustomizer` is a method decorator function which can used for
|
|
7
|
+
* customizing the swagger data with `npx nestia swagger` command. Furthermore,
|
|
8
|
+
* it is possible to add plugin properties starting with `x-` characters.
|
|
9
|
+
*
|
|
10
|
+
* In other words, this decorator function does not affect to the runtime, but
|
|
11
|
+
* only for the swagger data customization.
|
|
12
|
+
*
|
|
13
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
14
|
+
* @param closure Callback function which can customize the swagger data
|
|
15
|
+
* @returns Method decorator
|
|
16
|
+
*/
|
|
17
|
+
export function SwaggerCustomizer(
|
|
18
|
+
closure: (props: SwaggerCustomizer.IProps) => unknown,
|
|
19
|
+
): MethodDecorator {
|
|
20
|
+
return function SwaggerCustomizer(
|
|
21
|
+
target: Object,
|
|
22
|
+
propertyKey: string | symbol,
|
|
23
|
+
descriptor: TypedPropertyDescriptor<any>,
|
|
24
|
+
) {
|
|
25
|
+
const array: Array<(props: SwaggerCustomizer.IProps) => unknown> = (() => {
|
|
26
|
+
if (Reflect.hasMetadata("nestia/SwaggerCustomizer", target, propertyKey))
|
|
27
|
+
return Reflect.getMetadata(
|
|
28
|
+
"nestia/SwaggerCustomizer",
|
|
29
|
+
target,
|
|
30
|
+
propertyKey,
|
|
31
|
+
);
|
|
32
|
+
const array: Array<(props: SwaggerCustomizer.IProps) => unknown> = [];
|
|
33
|
+
Reflect.defineMetadata(
|
|
34
|
+
"nestia/SwaggerCustomizer",
|
|
35
|
+
array,
|
|
36
|
+
target,
|
|
37
|
+
propertyKey,
|
|
38
|
+
);
|
|
39
|
+
return array;
|
|
40
|
+
})();
|
|
41
|
+
array.push(closure);
|
|
42
|
+
return descriptor;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export namespace SwaggerCustomizer {
|
|
46
|
+
/**
|
|
47
|
+
* Properties for the `SwaggerCustomizer` decorator.
|
|
48
|
+
*
|
|
49
|
+
* `SwaggerCustomizer.IProps` is a type for the `closure` parameter of the
|
|
50
|
+
* `SwaggerCustomizer` decorator. It's a callback function which can customize
|
|
51
|
+
* the swagger data.
|
|
52
|
+
*/
|
|
53
|
+
export interface IProps {
|
|
54
|
+
/** Swagger data. */
|
|
55
|
+
swagger: OpenApi.IDocument;
|
|
56
|
+
|
|
57
|
+
/** Method of the route. */
|
|
58
|
+
method: string;
|
|
59
|
+
|
|
60
|
+
/** Path of the route. */
|
|
61
|
+
path: string;
|
|
62
|
+
|
|
63
|
+
/** Route data. */
|
|
64
|
+
route: OpenApi.IOperation;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Get neighbor endpoint data through the controller method.
|
|
68
|
+
*
|
|
69
|
+
* @param func Controller method to find the neighbor endpoint
|
|
70
|
+
* @returns Neighbor endpoint data
|
|
71
|
+
*/
|
|
72
|
+
at(func: Function): ISwaggerEndpoint | undefined;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Get neighbor route data.
|
|
76
|
+
*
|
|
77
|
+
* @param accessor Accessor for getting neighbor route data
|
|
78
|
+
* @returns Neighbor route data
|
|
79
|
+
*/
|
|
80
|
+
get(accessor: IAccessor): OpenApi.IOperation | undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Accessor for getting neighbor route data. */
|
|
84
|
+
export interface IAccessor {
|
|
85
|
+
/** Path of the neighbor route. */
|
|
86
|
+
path: string;
|
|
87
|
+
|
|
88
|
+
/** Method of the neighbor route. */
|
|
89
|
+
method: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Endpoint info of the route. */
|
|
93
|
+
export interface ISwaggerEndpoint extends IAccessor {
|
|
94
|
+
/** Route data. */
|
|
95
|
+
route: OpenApi.IOperation;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -1,187 +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([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
|
+
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");
|