forked from fastify/fastify-multipart
/
index.d.ts
211 lines (180 loc) · 5.08 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { Busboy, BusboyConfig, BusboyFileStream } from "@fastify/busboy";
import { FastifyPluginCallback } from "fastify";
import { Readable } from "stream";
import { FastifyErrorConstructor } from "@fastify/error";
declare module "fastify" {
interface FastifyRequest {
isMultipart: () => boolean;
// promise api
parts: (
options?: Omit<BusboyConfig, "headers">
) => AsyncIterableIterator<fastifyMultipart.Multipart>;
// legacy
multipart: (
handler: MultipartHandler,
next: (err: Error) => void,
options?: Omit<BusboyConfig, "headers">
) => Busboy;
// Stream mode
file: (
options?: Omit<BusboyConfig, "headers">
) => Promise<fastifyMultipart.MultipartFile | undefined>;
files: (
options?: Omit<BusboyConfig, "headers">
) => AsyncIterableIterator<fastifyMultipart.MultipartFile>;
// Disk mode
saveRequestFiles: (
options?: Omit<BusboyConfig, "headers"> & { tmpdir?: string }
) => Promise<Array<fastifyMultipart.SavedMultipartFile>>;
cleanRequestFiles: () => Promise<void>;
tmpUploads: Array<string> | null;
}
interface FastifyInstance {
multipartErrors: MultipartErrors;
}
}
type FastifyMultipartPlugin = FastifyPluginCallback<
| fastifyMultipart.FastifyMultipartBaseOptions
| fastifyMultipart.FastifyMultipartOptions
| fastifyMultipart.FastifyMultipartAttachFieldsToBodyOptions
>;
type MultipartHandler = (
field: string,
file: BusboyFileStream,
filename: string,
encoding: string,
mimetype: string
) => void;
interface BodyEntry {
data: Buffer;
filename: string;
encoding: string;
mimetype: string;
limit: false;
}
interface MultipartErrors {
PartsLimitError: FastifyErrorConstructor;
FilesLimitError: FastifyErrorConstructor;
FieldsLimitError: FastifyErrorConstructor;
PrototypeViolationError: FastifyErrorConstructor;
InvalidMultipartContentTypeError: FastifyErrorConstructor;
RequestFileTooLargeError: FastifyErrorConstructor;
}
declare namespace fastifyMultipart {
export interface SavedMultipartFile extends MultipartFile {
/**
* Path to the temporary file
*/
filepath: string;
}
export type Multipart = MultipartFile | MultipartValue;
export interface MultipartFile {
toBuffer: () => Promise<Buffer>;
file: BusboyFileStream;
fieldname: string;
filename: string;
encoding: string;
mimetype: string;
fields: MultipartFields;
}
export interface MultipartValue<T = unknown> {
value: T;
fieldname: string;
mimetype: string;
encoding: string;
fieldnameTruncated: boolean;
valueTruncated: boolean;
fields: MultipartFields;
}
export interface MultipartFields {
[fieldname: string]: Multipart | Multipart[] | undefined;
}
export interface FastifyMultipartBaseOptions {
/**
* Append the multipart parameters to the body object
*/
addToBody?: boolean;
/**
* Add a shared schema to validate the input fields
*/
sharedSchemaId?: string;
/**
* Allow throwing error when file size limit reached.
*/
throwFileSizeLimit?: boolean;
/**
* Detect if a Part is a file.
*
* By default a file is detected if contentType
* is application/octet-stream or fileName is not
* undefined.
*
* Modify this to handle e.g. Blobs.
*/
isPartAFile?: (
fieldName: string | undefined,
contentType: string | undefined,
fileName: string | undefined
) => boolean;
limits?: {
/**
* Max field name size in bytes
*/
fieldNameSize?: number;
/**
* Max field value size in bytes
*/
fieldSize?: number;
/**
* Max number of non-file fields
*/
fields?: number;
/**
* For multipart forms, the max file size
*/
fileSize?: number;
/**
* Max number of file fields
*/
files?: number;
/**
* Max number of header key=>value pairs
*/
headerPairs?: number;
};
}
export interface FastifyMultipartOptions extends FastifyMultipartBaseOptions {
/**
* Only valid in the promise api. Append the multipart parameters to the body object.
*/
attachFieldsToBody?: false;
/**
* Manage the file stream like you need
*/
onFile?: (
fieldName: string,
stream: Readable,
filename: string,
encoding: string,
mimetype: string,
body: Record<string, BodyEntry>
) => void | Promise<void>;
}
export interface FastifyMultipartAttachFieldsToBodyOptions
extends FastifyMultipartBaseOptions {
/**
* Only valid in the promise api. Append the multipart parameters to the body object.
*/
attachFieldsToBody: true | "keyValues";
/**
* Manage the file stream like you need
*/
onFile?: (part: MultipartFile) => void | Promise<void>;
}
export const fastifyMultipart: FastifyMultipartPlugin;
export { fastifyMultipart as default };
}
declare function fastifyMultipart(
...params: Parameters<FastifyMultipartPlugin>
): ReturnType<FastifyMultipartPlugin>;
export = fastifyMultipart;