Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils/body): support multiple Files #2665

Merged
merged 3 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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