Skip to content

Commit

Permalink
fix: cannot send multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
Val-istar-Guo committed May 26, 2022
1 parent e264dd8 commit ec70e66
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/util/clone.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/* eslint-disable @typescript-eslint/no-unsafe-return */
import { isBlob } from './is'
import deepClone from 'clone'
import fromEntries from 'object.fromentries'


export function clone<T>(obj: T): T {
if (typeof obj === 'object' && !Array.isArray(obj)) {
if (Array.isArray(obj)) {
return obj.map(item => isBlob(item) ? item : clone(item)) as unknown as T
} else if (typeof obj === 'object' && !Array.isArray(obj)) {
const entries = Object.entries(obj)
.map(([key, value]): [string, any] => ([
key,
isBlob(value) ? value : deepClone(value),
isBlob(value) ? value : clone(value),
]))

return fromEntries(entries) as T
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/request-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test.only('Form-Data Request', async t => {
.field('key2', 'value2')
.send(formData)
.attach('file1', file)
.attach('file1', file, 'file1.txt')
.attach('file2', file, 'file2.txt')
.option('fetchAPI', fetchAPI)

Expand All @@ -50,9 +51,9 @@ test.only('Form-Data Request', async t => {
t.is(result.get('key2'), 'value2')
t.is(result.get('key3'), 'value3')

const file1 = result.get('file1') as File
t.is(file1.name, 'blob')
t.is(await file1.text(), 'file')
const file1 = result.getAll('file1') as File[]
t.is(file1[0].name, 'blob')
t.is(await file1[0].text(), 'file')

const file2 = result.get('file2') as File
t.is(file2.name, 'file2.txt')
Expand Down

0 comments on commit ec70e66

Please sign in to comment.