Skip to content

Commit d806b06

Browse files
committed
DRY formData construction
1 parent aa63ba9 commit d806b06

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

Diff for: lib/client.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -691,11 +691,17 @@ Request.prototype.query = function(val){
691691
*/
692692

693693
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);
696695
return this;
697696
};
698697

698+
Request.prototype._getFormData = function(){
699+
if (!this._formData) {
700+
this._formData = new root.FormData();
701+
}
702+
return this._formData;
703+
};
704+
699705
/**
700706
* Send `data` as the request body, defaulting the `.type()` to "json" when
701707
* an object is given.

Diff for: lib/node/index.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,28 @@ for (var key in requestBase) {
177177
*/
178178

179179
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-
}
187180
if ('string' == typeof file) {
188181
if (!filename) filename = file;
189182
debug('creating `fs.ReadStream` instance for file: %s', file);
190183
file = fs.createReadStream(file);
191-
} else if(!filename && file.path) {
184+
} else if (!filename && file.path) {
192185
filename = file.path;
193186
}
194-
this._formData.append(field, file, { filename: filename });
187+
this._getFormData().append(field, file, { filename: filename });
195188
return this;
196189
};
197190

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+
198202
/**
199203
* Set the max redirects to `n`.
200204
*

Diff for: lib/node/part.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,7 @@ Part.prototype._attach = function(){
125125
if (!this._name) throw new Error('must call `Part#name()` first!');
126126

127127
// 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, {
136129
contentType: this._type,
137130
filename: this._filename
138131
});

Diff for: lib/request-base.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,6 @@ exports.unset = function(field){
161161
* @api public
162162
*/
163163
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);
173165
return this;
174166
};

0 commit comments

Comments
 (0)