Skip to content

Commit

Permalink
fix(utils/body): support multiple Files (#2665)
Browse files Browse the repository at this point in the history
* fix(utils/body): support multiple `File`s

* denoify

* Make CI `Main` uses Node.js v20
  • Loading branch information
yusukebe authored May 12, 2024
1 parent e1989ff commit 506f7f1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.x'
- uses: oven-sh/setup-bun@v1
with:
bun-version: '1.0.25'
Expand Down
12 changes: 5 additions & 7 deletions deno_dist/utils/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,12 @@ function convertFormDataToBodyData<T extends BodyData = BodyData>(
}

const handleParsingAllValues = (form: BodyData, key: string, value: FormDataEntryValue): void => {
const formKey = form[key] as (string | File)[]
const formKey = form[key]

if (form[key] && Array.isArray(form[key])) {
formKey.push(value)
} else if (form[key]) {
const parsedKey = [...formKey].join('').replace(',', '')

form[key] = [parsedKey, value]
if (formKey && Array.isArray(formKey)) {
;(form[key] as (string | File)[]).push(value)
} else if (formKey) {
form[key] = [formKey, value]
} else {
form[key] = value
}
Expand Down
18 changes: 18 additions & 0 deletions src/utils/body.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ describe('Parse Body Util', () => {
})
})

it('should parse multiple values if values are `File`', async () => {
const file1 = new File(['foo'], 'file1', {
type: 'application/octet-stream',
})
const file2 = new File(['bar'], 'file2', {
type: 'application/octet-stream',
})
const data = new FormData()
data.append('file', file1)
data.append('file', file2)

const req = createRequest(FORM_URL, 'POST', data)

expect(await parseBody(req, { all: true })).toEqual({
file: [file1, file2],
})
})

it('should parse multiple values if key ends with `[]`', async () => {
const data = new FormData()
data.append('file[]', 'aaa')
Expand Down
12 changes: 5 additions & 7 deletions src/utils/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,12 @@ function convertFormDataToBodyData<T extends BodyData = BodyData>(
}

const handleParsingAllValues = (form: BodyData, key: string, value: FormDataEntryValue): void => {
const formKey = form[key] as (string | File)[]
const formKey = form[key]

if (form[key] && Array.isArray(form[key])) {
formKey.push(value)
} else if (form[key]) {
const parsedKey = [...formKey].join('').replace(',', '')

form[key] = [parsedKey, value]
if (formKey && Array.isArray(formKey)) {
;(form[key] as (string | File)[]).push(value)
} else if (formKey) {
form[key] = [formKey, value]
} else {
form[key] = value
}
Expand Down

0 comments on commit 506f7f1

Please sign in to comment.