Skip to content

Commit

Permalink
feat(request): add detailed type for c.req.parseBody()
Browse files Browse the repository at this point in the history
  • Loading branch information
usualoma committed May 23, 2024
1 parent 3b178fc commit 3fb0ef8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
61 changes: 61 additions & 0 deletions src/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { HonoRequest } from './request'
import type { RouterRoute } from './types'

type RecursiveRecord<K extends string, T> = {
[key in K]: T | RecursiveRecord<K, T>
}

describe('Query', () => {
test('req.query() and req.queries()', () => {
const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')
Expand Down Expand Up @@ -251,4 +255,61 @@ describe('Body methods with caching', () => {
expect(async () => await req.arrayBuffer()).not.toThrow()
expect(async () => await req.blob()).not.toThrow()
})

describe('req.parseBody()', async () => {
it('should parse form data', async () => {
const data = new FormData()
data.append('foo', 'bar')
const req = new HonoRequest(
new Request('http://localhost', {
method: 'POST',
body: data,
})
)
expect((await req.parseBody())['foo']).toBe('bar')
expect((await req.parseBody())['foo']).toBe('bar')
expect(async () => await req.text()).not.toThrow()
expect(async () => await req.arrayBuffer()).not.toThrow()
expect(async () => await req.blob()).not.toThrow()
})

describe('Return type', () => {
let req: HonoRequest
beforeEach(() => {
const data = new FormData()
data.append('foo', 'bar')
req = new HonoRequest(
new Request('http://localhost', {
method: 'POST',
body: data,
})
)
})

it('without options', async () => {
expectTypeOf((await req.parseBody())['key']).toEqualTypeOf<string | File>()
})

it('{all: true}', async () => {
expectTypeOf((await req.parseBody({ all: true }))['key']).toEqualTypeOf<
string | File | (string | File)[]
>()
})

it('{dot: true}', async () => {
expectTypeOf((await req.parseBody({ dot: true }))['key']).toEqualTypeOf<
string | File | RecursiveRecord<string, string | File>
>()
})

it('{all: true, dot: true}', async () => {
expectTypeOf((await req.parseBody({ all: true, dot: true }))['key']).toEqualTypeOf<
| string
| File
| (string | File)[]
| RecursiveRecord<string, string | File | (string | File)[]>
>()
})
})
})
})
6 changes: 5 additions & 1 deletion src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
* ```
* @see https://hono.dev/api/request#parsebody
*/
async parseBody<T extends BodyData = BodyData>(options?: ParseBodyOptions): Promise<T> {
async parseBody<Options extends Partial<ParseBodyOptions>, T extends BodyData<Options>>(
options?: Options
): Promise<T>
async parseBody<T extends BodyData>(options?: Partial<ParseBodyOptions>): Promise<T>
async parseBody<T extends BodyData = BodyData>(options?: Partial<ParseBodyOptions>): Promise<T> {
if (this.bodyCache.parsedBody) {
return this.bodyCache.parsedBody as T
}
Expand Down

0 comments on commit 3fb0ef8

Please sign in to comment.