Skip to content

Commit e06f5cc

Browse files
Adding backwards compatibility for those passing a FormData instance as the previously unexposed second parameter
1 parent d17ccc1 commit e06f5cc

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ function isFile (value) {
2929
typeof value.name === 'string'
3030
}
3131

32+
function isFormData (value) {
33+
return value instanceof FormData
34+
}
35+
3236
function objectToFormData (obj, cfg, fd, pre) {
37+
if (isFormData(cfg)) {
38+
pre = fd
39+
fd = cfg
40+
cfg = null
41+
}
42+
3343
cfg = cfg || {}
3444
cfg.indices = cfg.indices || false
3545
fd = fd || new FormData()

test/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,19 @@ test('File', t => {
338338
])
339339
t.is(formData.get('foo'), foo)
340340
})
341+
342+
test('FormData instance as second parameter', t => {
343+
const foo = 'foo'
344+
const data = new FormData()
345+
const formData = objectToFormData({
346+
foo
347+
}, data)
348+
349+
t.true(formData.append.calledOnce)
350+
t.deepEqual(formData.append.getCall(0).args, [
351+
'foo',
352+
foo
353+
])
354+
t.is(formData.get('foo'), foo)
355+
t.is(data, formData)
356+
})

0 commit comments

Comments
 (0)