0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-22 11:17:33 +01:00

feat: c.req.body and c.req.json accept generics (#529)

This commit is contained in:
Yusuke Wada 2022-09-20 22:01:03 +09:00 committed by GitHub
parent 5133a93925
commit 976b2c310e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 32 deletions

View File

@ -1,4 +1,5 @@
import { parseBody } from './utils/body.ts'
import type { BodyData } from './utils/body.ts'
import type { Cookie } from './utils/cookie.ts'
import { parse } from './utils/cookie.ts'
import { getQueryStringFromURL } from './utils/url.ts'
@ -33,14 +34,10 @@ declare global {
(name: string): string
(): Cookie
}
bodyData?: Record<string, string | File>
parseBody: {
(): Promise<Record<string, string | File>>
}
bodyData?: BodyData
parseBody<BodyType extends BodyData>(): Promise<BodyType>
jsonData?: any
json: {
(): Promise<any>
}
json<JSONData = any>(): Promise<JSONData>
data: Data
valid: {
(key: string, value: any): Data
@ -121,23 +118,23 @@ export function extendRequestPrototype() {
}
} as InstanceType<typeof Request>['cookie']
Request.prototype.parseBody = async function (
Request.prototype.parseBody = async function <BodyType extends BodyData>(
this: Request
): Promise<Record<string, string | File>> {
): Promise<BodyType> {
// Cache the parsed body
let body: Record<string, string | File>
let body: BodyType
if (!this.bodyData) {
body = await parseBody(this)
body = await parseBody<BodyType>(this)
this.bodyData = body
} else {
body = this.bodyData
body = this.bodyData as BodyType
}
return body
} as InstanceType<typeof Request>['parseBody']
Request.prototype.json = async function (this: Request): Promise<any> {
Request.prototype.json = async function <JSONData>(this: Request): Promise<JSONData> {
// Cache the JSON body
let jsonData: any
let jsonData: JSONData
if (!this.jsonData) {
jsonData = JSON.parse(await this.text())
this.jsonData = jsonData

View File

@ -1,4 +1,8 @@
export async function parseBody(r: Request | Response): Promise<Record<string, string | File>> {
export type BodyData = Record<string, string | number | boolean | File>
export async function parseBody<BodyType extends BodyData>(
r: Request | Response
): Promise<BodyType> {
let body: Record<string, string | File> = {}
const contentType = r.headers.get('Content-Type')
if (
@ -12,5 +16,5 @@ export async function parseBody(r: Request | Response): Promise<Record<string, s
return acc
}, form)
}
return body
return body as BodyType
}

View File

@ -1,4 +1,5 @@
import { parseBody } from './utils/body'
import type { BodyData } from './utils/body'
import type { Cookie } from './utils/cookie'
import { parse } from './utils/cookie'
import { getQueryStringFromURL } from './utils/url'
@ -33,14 +34,10 @@ declare global {
(name: string): string
(): Cookie
}
bodyData?: Record<string, string | File>
parseBody: {
(): Promise<Record<string, string | File>>
}
bodyData?: BodyData
parseBody<BodyType extends BodyData>(): Promise<BodyType>
jsonData?: any
json: {
(): Promise<any>
}
json<JSONData = any>(): Promise<JSONData>
data: Data
valid: {
(key: string, value: any): Data
@ -121,23 +118,23 @@ export function extendRequestPrototype() {
}
} as InstanceType<typeof Request>['cookie']
Request.prototype.parseBody = async function (
Request.prototype.parseBody = async function <BodyType extends BodyData>(
this: Request
): Promise<Record<string, string | File>> {
): Promise<BodyType> {
// Cache the parsed body
let body: Record<string, string | File>
let body: BodyType
if (!this.bodyData) {
body = await parseBody(this)
body = await parseBody<BodyType>(this)
this.bodyData = body
} else {
body = this.bodyData
body = this.bodyData as BodyType
}
return body
} as InstanceType<typeof Request>['parseBody']
Request.prototype.json = async function (this: Request): Promise<any> {
Request.prototype.json = async function <JSONData>(this: Request): Promise<JSONData> {
// Cache the JSON body
let jsonData: any
let jsonData: JSONData
if (!this.jsonData) {
jsonData = JSON.parse(await this.text())
this.jsonData = jsonData

View File

@ -1,4 +1,8 @@
export async function parseBody(r: Request | Response): Promise<Record<string, string | File>> {
export type BodyData = Record<string, string | number | boolean | File>
export async function parseBody<BodyType extends BodyData>(
r: Request | Response
): Promise<BodyType> {
let body: Record<string, string | File> = {}
const contentType = r.headers.get('Content-Type')
if (
@ -12,5 +16,5 @@ export async function parseBody(r: Request | Response): Promise<Record<string, s
return acc
}, form)
}
return body
return body as BodyType
}