Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(req): support generics for query/queries/header #1634

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,24 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
return null
}

query(key: string): string | undefined
query(): Record<string, string>
query(key?: string) {
return getQueryParam(this.url, key)
query<T extends Record<string, string | undefined>>(): T
query<T extends Record<string, string | undefined>, K = keyof T>(
key: K
): K extends string ? T[K] : undefined
query<T extends Record<string, string | undefined>>(key?: keyof T) {
return getQueryParam(this.url, key as string)
}

queries(key: string): string[] | undefined
queries(): Record<string, string[]>
queries(key?: string) {
return getQueryParams(this.url, key)
queries<T extends Record<string, string[]>>(): T
queries<T extends Record<string, string[]>>(key: keyof T): string[]
queries<T extends Record<string, string[]>>(key?: keyof T) {
return getQueryParams(this.url, key as string)
}

header(name: string): string | undefined
header(): Record<string, string>
header(name?: string) {
if (name) return this.raw.headers.get(name.toLowerCase()) ?? undefined
header<T extends Record<string, string>>(): T
header<T extends Record<string, string>>(name: keyof T): string
header<T extends Record<string, string>>(name?: keyof T) {
if (name) return this.raw.headers.get((name as string).toLowerCase()) ?? undefined

const headerData: Record<string, string | undefined> = {}
this.raw.headers.forEach((value, key) => {
Expand Down