From 99fef654e06c2362db2715755c07b10b426aa926 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 31 Oct 2023 22:45:23 +0900 Subject: [PATCH] feat(req): support generics for query/queries/header --- src/request.ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/request.ts b/src/request.ts index 69bc3084..ce2148c5 100644 --- a/src/request.ts +++ b/src/request.ts @@ -73,22 +73,24 @@ export class HonoRequest

{ return null } - query(key: string): string | undefined - query(): Record - query(key?: string) { - return getQueryParam(this.url, key) + query>(): T + query, K = keyof T>( + key: K + ): K extends string ? T[K] : undefined + query>(key?: keyof T) { + return getQueryParam(this.url, key as string) } - queries(key: string): string[] | undefined - queries(): Record - queries(key?: string) { - return getQueryParams(this.url, key) + queries>(): T + queries>(key: keyof T): string[] + queries>(key?: keyof T) { + return getQueryParams(this.url, key as string) } - header(name: string): string | undefined - header(): Record - header(name?: string) { - if (name) return this.raw.headers.get(name.toLowerCase()) ?? undefined + header>(): T + header>(name: keyof T): string + header>(name?: keyof T) { + if (name) return this.raw.headers.get((name as string).toLowerCase()) ?? undefined const headerData: Record = {} this.raw.headers.forEach((value, key) => {