@nestia/core 8.0.8 → 9.0.0-dev.20251107
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/package.json +7 -8
- package/src/adaptors/WebSocketAdaptor.ts +429 -429
- package/src/decorators/DynamicModule.ts +44 -44
- package/src/decorators/EncryptedBody.ts +97 -97
- package/src/decorators/EncryptedController.ts +40 -40
- package/src/decorators/EncryptedModule.ts +98 -98
- package/src/decorators/EncryptedRoute.ts +213 -213
- package/src/decorators/HumanRoute.ts +22 -22
- package/src/decorators/NoTransformConfigurationError.ts +34 -34
- package/src/decorators/PlainBody.ts +76 -76
- package/src/decorators/SwaggerCustomizer.ts +97 -97
- package/src/decorators/SwaggerExample.ts +100 -100
- package/src/decorators/TypedBody.ts +57 -57
- package/src/decorators/TypedException.ts +147 -147
- package/src/decorators/TypedFormData.ts +195 -195
- package/src/decorators/TypedHeaders.ts +66 -66
- package/src/decorators/TypedParam.ts +77 -77
- package/src/decorators/TypedQuery.ts +234 -234
- package/src/decorators/TypedRoute.ts +196 -196
- package/src/decorators/WebSocketRoute.ts +242 -242
- package/src/decorators/internal/EncryptedConstant.ts +2 -2
- package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
- package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
- package/src/decorators/internal/get_path_and_querify.ts +94 -94
- package/src/decorators/internal/get_path_and_stringify.ts +110 -110
- package/src/decorators/internal/get_text_body.ts +16 -16
- package/src/decorators/internal/headers_to_object.ts +11 -11
- package/src/decorators/internal/is_request_body_undefined.ts +12 -12
- package/src/decorators/internal/load_controller.ts +45 -45
- package/src/decorators/internal/route_error.ts +43 -43
- package/src/decorators/internal/validate_request_body.ts +64 -64
- package/src/decorators/internal/validate_request_form_data.ts +67 -67
- package/src/decorators/internal/validate_request_headers.ts +76 -76
- package/src/decorators/internal/validate_request_query.ts +64 -64
- package/src/index.ts +5 -5
- package/src/module.ts +22 -22
- package/src/options/INestiaTransformOptions.ts +38 -38
- package/src/options/INestiaTransformProject.ts +8 -8
- package/src/options/IRequestBodyValidator.ts +20 -20
- package/src/options/IRequestFormDataProps.ts +27 -27
- package/src/options/IRequestHeadersValidator.ts +22 -22
- package/src/options/IRequestQueryValidator.ts +20 -20
- package/src/options/IResponseBodyQuerifier.ts +25 -25
- package/src/options/IResponseBodyStringifier.ts +30 -30
- package/src/programmers/PlainBodyProgrammer.ts +70 -70
- package/src/programmers/TypedBodyProgrammer.ts +142 -142
- package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
- package/src/programmers/TypedHeadersProgrammer.ts +63 -63
- package/src/programmers/TypedParamProgrammer.ts +33 -33
- package/src/programmers/TypedQueryBodyProgrammer.ts +112 -112
- package/src/programmers/TypedQueryProgrammer.ts +114 -114
- package/src/programmers/TypedQueryRouteProgrammer.ts +105 -105
- package/src/programmers/TypedRouteProgrammer.ts +94 -94
- package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +72 -72
- package/src/programmers/http/HttpIsQuerifyProgrammer.ts +75 -75
- package/src/programmers/http/HttpQuerifyProgrammer.ts +108 -108
- package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +76 -76
- package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
- package/src/transform.ts +35 -35
- package/src/transformers/FileTransformer.ts +110 -110
- package/src/transformers/MethodTransformer.ts +103 -103
- package/src/transformers/NodeTransformer.ts +23 -23
- package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
- package/src/transformers/ParameterTransformer.ts +57 -57
- package/src/transformers/TypedRouteTransformer.ts +85 -85
- package/src/transformers/WebSocketRouteTransformer.ts +120 -120
- package/src/typings/Creator.ts +3 -3
- package/src/typings/get-function-location.d.ts +7 -7
- package/src/utils/ArrayUtil.ts +7 -7
- package/src/utils/ExceptionManager.ts +115 -115
- package/src/utils/Singleton.ts +16 -16
- package/src/utils/SourceFinder.ts +54 -54
- package/src/utils/VersioningStrategy.ts +27 -27
|
@@ -1,195 +1,195 @@
|
|
|
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(
|
|
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,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
|
+
}
|