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