From 976b2c310ef15bc0e2bff2ce319a81c01c6cc841 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 20 Sep 2022 22:01:03 +0900 Subject: [PATCH] feat: `c.req.body` and `c.req.json` accept generics (#529) --- deno_dist/request.ts | 25 +++++++++++-------------- deno_dist/utils/body.ts | 8 ++++++-- src/request.ts | 25 +++++++++++-------------- src/utils/body.ts | 8 ++++++-- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/deno_dist/request.ts b/deno_dist/request.ts index c70102bd..1e471b0d 100644 --- a/deno_dist/request.ts +++ b/deno_dist/request.ts @@ -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 - parseBody: { - (): Promise> - } + bodyData?: BodyData + parseBody(): Promise jsonData?: any - json: { - (): Promise - } + json(): Promise data: Data valid: { (key: string, value: any): Data @@ -121,23 +118,23 @@ export function extendRequestPrototype() { } } as InstanceType['cookie'] - Request.prototype.parseBody = async function ( + Request.prototype.parseBody = async function ( this: Request - ): Promise> { + ): Promise { // Cache the parsed body - let body: Record + let body: BodyType if (!this.bodyData) { - body = await parseBody(this) + body = await parseBody(this) this.bodyData = body } else { - body = this.bodyData + body = this.bodyData as BodyType } return body } as InstanceType['parseBody'] - Request.prototype.json = async function (this: Request): Promise { + Request.prototype.json = async function (this: Request): Promise { // Cache the JSON body - let jsonData: any + let jsonData: JSONData if (!this.jsonData) { jsonData = JSON.parse(await this.text()) this.jsonData = jsonData diff --git a/deno_dist/utils/body.ts b/deno_dist/utils/body.ts index 2823cf78..39c278a9 100644 --- a/deno_dist/utils/body.ts +++ b/deno_dist/utils/body.ts @@ -1,4 +1,8 @@ -export async function parseBody(r: Request | Response): Promise> { +export type BodyData = Record + +export async function parseBody( + r: Request | Response +): Promise { let body: Record = {} const contentType = r.headers.get('Content-Type') if ( @@ -12,5 +16,5 @@ export async function parseBody(r: Request | Response): Promise - parseBody: { - (): Promise> - } + bodyData?: BodyData + parseBody(): Promise jsonData?: any - json: { - (): Promise - } + json(): Promise data: Data valid: { (key: string, value: any): Data @@ -121,23 +118,23 @@ export function extendRequestPrototype() { } } as InstanceType['cookie'] - Request.prototype.parseBody = async function ( + Request.prototype.parseBody = async function ( this: Request - ): Promise> { + ): Promise { // Cache the parsed body - let body: Record + let body: BodyType if (!this.bodyData) { - body = await parseBody(this) + body = await parseBody(this) this.bodyData = body } else { - body = this.bodyData + body = this.bodyData as BodyType } return body } as InstanceType['parseBody'] - Request.prototype.json = async function (this: Request): Promise { + Request.prototype.json = async function (this: Request): Promise { // Cache the JSON body - let jsonData: any + let jsonData: JSONData if (!this.jsonData) { jsonData = JSON.parse(await this.text()) this.jsonData = jsonData diff --git a/src/utils/body.ts b/src/utils/body.ts index 2823cf78..39c278a9 100644 --- a/src/utils/body.ts +++ b/src/utils/body.ts @@ -1,4 +1,8 @@ -export async function parseBody(r: Request | Response): Promise> { +export type BodyData = Record + +export async function parseBody( + r: Request | Response +): Promise { let body: Record = {} const contentType = r.headers.get('Content-Type') if ( @@ -12,5 +16,5 @@ export async function parseBody(r: Request | Response): Promise