Skip to content

Commit e00ea3a

Browse files
committed
style: addressing some feedback from pr review
1 parent 6dbb2b7 commit e00ea3a

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/helpers/form-data.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2323
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2424
* SOFTWARE.
25+
*
26+
* Extracted from https://github.com/node-fetch/node-fetch/blob/64c5c296a0250b852010746c76144cb9e14698d9/src/utils/form-data.js
2527
*/
2628

2729
const carriage = '\r\n'

src/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
var debug = require('debug')('httpsnippet')
66
var es = require('event-stream')
77
var MultiPartForm = require('form-data')
8+
var FormDataPolyfill = require('form-data/lib/form_data')
89
var qs = require('querystring')
910
var reducer = require('./helpers/reducer')
1011
var targets = require('./targets')
@@ -122,7 +123,7 @@ HTTPSnippet.prototype.prepare = function (request) {
122123
// This hack is pretty awful but it's the only way we can use this library in the browser as if we code this
123124
// against just the native FormData object, we can't polyfill that back into Node because Blob and File objects,
124125
// which something like `formdata-polyfill` requires, don't exist there.
125-
const isNativeFormData = (typeof form[Symbol.iterator] === 'function')
126+
const isNativeFormData = !(form instanceof FormDataPolyfill)
126127

127128
// easter egg
128129
const boundary = '---011000010111000001101001'
@@ -131,15 +132,19 @@ HTTPSnippet.prototype.prepare = function (request) {
131132
}
132133

133134
request.postData.params.forEach(function (param) {
135+
const name = param.name
136+
const value = param.value || ''
137+
const filename = param.fileName || null
138+
134139
if (isNativeFormData) {
135-
if (isBlob(param.value)) {
136-
form.append(param.name, param.value || '', param.fileName || null)
140+
if (isBlob(value)) {
141+
form.append(name, value, filename)
137142
} else {
138-
form.append(param.name, param.value || '')
143+
form.append(name, value)
139144
}
140145
} else {
141-
form.append(param.name, param.value || '', {
142-
filename: param.fileName || null,
146+
form.append(name, value, {
147+
filename: filename,
143148
contentType: param.contentType || null
144149
})
145150
}

0 commit comments

Comments
 (0)