File tree 4 files changed +23
-28
lines changed
4 files changed +23
-28
lines changed Original file line number Diff line number Diff line change @@ -691,11 +691,17 @@ Request.prototype.query = function(val){
691
691
*/
692
692
693
693
Request . prototype . attach = function ( field , file , filename ) {
694
- if ( ! this . _formData ) this . _formData = new root . FormData ( ) ;
695
- this . _formData . append ( field , file , filename || file . name ) ;
694
+ this . _getFormData ( ) . append ( field , file , filename || file . name ) ;
696
695
return this ;
697
696
} ;
698
697
698
+ Request . prototype . _getFormData = function ( ) {
699
+ if ( ! this . _formData ) {
700
+ this . _formData = new root . FormData ( ) ;
701
+ }
702
+ return this . _formData ;
703
+ } ;
704
+
699
705
/**
700
706
* Send `data` as the request body, defaulting the `.type()` to "json" when
701
707
* an object is given.
Original file line number Diff line number Diff line change @@ -177,24 +177,28 @@ for (var key in requestBase) {
177
177
*/
178
178
179
179
Request . prototype . attach = function ( field , file , filename ) {
180
- if ( ! this . _formData ) {
181
- this . _formData = new FormData ( ) ;
182
- this . _formData . on ( 'error' , function ( err ) {
183
- this . emit ( 'error' , err ) ;
184
- this . abort ( ) ;
185
- } . bind ( this ) ) ;
186
- }
187
180
if ( 'string' == typeof file ) {
188
181
if ( ! filename ) filename = file ;
189
182
debug ( 'creating `fs.ReadStream` instance for file: %s' , file ) ;
190
183
file = fs . createReadStream ( file ) ;
191
- } else if ( ! filename && file . path ) {
184
+ } else if ( ! filename && file . path ) {
192
185
filename = file . path ;
193
186
}
194
- this . _formData . append ( field , file , { filename : filename } ) ;
187
+ this . _getFormData ( ) . append ( field , file , { filename : filename } ) ;
195
188
return this ;
196
189
} ;
197
190
191
+ Request . prototype . _getFormData = function ( ) {
192
+ if ( ! this . _formData ) {
193
+ this . _formData = new FormData ( ) ;
194
+ this . _formData . on ( 'error' , function ( err ) {
195
+ this . emit ( 'error' , err ) ;
196
+ this . abort ( ) ;
197
+ } . bind ( this ) ) ;
198
+ }
199
+ return this . _formData ;
200
+ } ;
201
+
198
202
/**
199
203
* Set the max redirects to `n`.
200
204
*
Original file line number Diff line number Diff line change @@ -125,14 +125,7 @@ Part.prototype._attach = function(){
125
125
if ( ! this . _name ) throw new Error ( 'must call `Part#name()` first!' ) ;
126
126
127
127
// add `this` Stream's readable side as a stream for this Part
128
- if ( ! this . _req . _formData ) {
129
- this . _req . _formData = new FormData ( ) ;
130
- this . _req . _formData . on ( 'error' , function ( err ) {
131
- this . _req . emit ( 'error' , err ) ;
132
- this . _req . abort ( ) ;
133
- } . bind ( this ) ) ;
134
- }
135
- this . _req . _formData . append ( this . _name , this , {
128
+ this . _req . _getFormData ( ) . append ( this . _name , this , {
136
129
contentType : this . _type ,
137
130
filename : this . _filename
138
131
} ) ;
Original file line number Diff line number Diff line change @@ -161,14 +161,6 @@ exports.unset = function(field){
161
161
* @api public
162
162
*/
163
163
exports . field = function ( name , val ) {
164
- if ( ! this . _formData ) {
165
- var FormData = require ( 'form-data' ) ; // browserify compatible. May throw if FormData is not supported natively.
166
- this . _formData = new FormData ( ) ;
167
- this . _formData . on ( 'error' , function ( err ) {
168
- this . emit ( 'error' , err ) ;
169
- this . abort ( ) ;
170
- } . bind ( this ) ) ;
171
- }
172
- this . _formData . append ( name , val ) ;
164
+ this . _getFormData ( ) . append ( name , val ) ;
173
165
return this ;
174
166
} ;
You can’t perform that action at this time.
0 commit comments