Skip to content

Commit

Permalink
Merge branch 'v5.x' into auto-router-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley authored Mar 24, 2024
2 parents 201cdce + 14da49b commit 6ab9365
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Changelog

- **v4.2.2**
- fixed: withContent should return undefined if request.body is undefined
- **v4.2.1**
- maintenance: updated deps, test coverage, removed isomorphic-fetch dev dep
- **v4.2.0**
Expand Down
6 changes: 3 additions & 3 deletions src/withContent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ describe('withContent (middleware)', () => {
expect(handler).toHaveReturnedWith('bar')
})

it('will return empty string (but not throw) if no body', async () => {
it('will return undefined (but not throw) if no body', async () => {
const router = Router()
const handler = vi.fn(({ content }) => content ?? true)
const handler = vi.fn(({ content }) => content === undefined ? true : false)
const request = new Request('https://foo.bar', { method: 'POST' })

await router.post('/', withContent, handler).handle(request)

expect(handler).toHaveReturnedWith('')
expect(handler).toHaveReturnedWith(true)
})
})
8 changes: 5 additions & 3 deletions src/withContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export type HasContent<ContentType> = {

// withContent - embeds any request body as request.content
export const withContent = async (request: IRequest): Promise<void> => {
request.content = await request.clone().json()
.catch(() => request.clone().formData())
.catch(() => request.text())
request.content = request.body
? await request.clone().json()
.catch(() => request.clone().formData())
.catch(() => request.text())
: undefined
}

0 comments on commit 6ab9365

Please sign in to comment.