Skip to content

Commit

Permalink
add text and formData support to withContent
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Oct 28, 2023
1 parent a8fd805 commit 35e3665
Show file tree
Hide file tree
Showing 3 changed files with 1,611 additions and 1,599 deletions.
52 changes: 44 additions & 8 deletions src/withContent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,56 @@ import { describe, expect, it, vi } from 'vitest'
import { Router } from './Router'
import { withContent } from './withContent'

describe('withContent (middleware)', () => {
it('can access the awaited Response body as request.content', async () => {
// Utility function to create a test case
function createTestCase({ contentType, body, expected }) {
return async () => {
const router = Router()
const handler = vi.fn(({ content }) => content)
const request = new Request('https://foo.bar', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({ foo: 'bar' }),
headers: { 'content-type': contentType },
body: body,
})

await router.post('/', withContent, handler).handle(request)
expect(handler).toHaveReturnedWith(expected)
}
}

describe('withContent (middleware)', () => {
const testCases = [
{
contentType: 'application/json',
body: JSON.stringify({ foo: 'bar' }),
expected: { foo: 'bar' },
description: 'JSON payload',
},
{
contentType: 'text/plain',
body: 'Hello, World!',
expected: 'Hello, World!',
description: 'text payload',
},
// {
// contentType: 'multipart/form-data',
// body: (() => {
// const formData = new FormData()
// formData.append('foo', 'bar')
// return formData
// })(),
// expected: { foo: 'bar' },
// description: 'formData payload',
// },
{
contentType: 'application/x-www-form-urlencoded',
body: new URLSearchParams({ foo: 'bar' }).toString(),
expected: { foo: 'bar' },
description: 'form-urlencoded payload',
},
]

expect(handler).toHaveReturnedWith({ foo: 'bar' })
})
// Iterate over the test cases and create individual tests
for (const testCase of testCases) {
it(`can access the awaited Response body as request.content for ${testCase.description}`, createTestCase(testCase))
}
})
16 changes: 14 additions & 2 deletions src/withContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ export type HasContent<ContentType> = {

// withContent - embeds any request body as request.content
export const withContent = async (request: IRequest): Promise<void> => {
if (request.headers.get('content-type')?.includes('json'))
request.content = await request.json()
const { headers } = request
const type = headers.get('content-type')

console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(request)))

request.content = type?.includes('json')
? await request.json()
: type?.includes('form-urlencoded')
? Object.fromEntries(new URLSearchParams(await request.text()))
: type?.includes('form-data')
? await request.formData()
: type?.includes('text')
? await request.text()
: undefined
}
Loading

0 comments on commit 35e3665

Please sign in to comment.