Skip to content
Myr

refactor: make `HonoRequest` (#330)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* refactor: make `HonoRequest` * apply to middlewares * fix tests

authored by Yusuke Wada · committed by GitHub · parent 34aa124 · Browse files · 7 files+55 added−52 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+55 added−52 removed

  • tests:+5 added−6 removed
  • other:+50 added−46 removed

7 files+55 added−52 removed

modified:src/middleware/body-parse/index.test.ts+5 added-3 removed
modified:src/middleware/body-parse/index.ts+2 added-2 removed
modified:src/middleware/cookie/index.ts+2 added-2 removed
modified:src/compose.test.ts-3 removed
1import { compose } from './compose'2import { Context } from './context'3import { extendRequestPrototype } from './request'35extendRequestPrototype()64type C = {5 req: Record<string, string>6 res: Record<string, string>7}
666 unmodified lines
modified:src/context.ts+9 added-3 removed
1import type { NotFoundHandler } from './hono'2import { extendHonoRequest } from './request'3import type { HonoRequest } from './request'4import type { StatusCode } from './utils/http-status'5import { isAbsoluteURL } from './utils/url'67type Headers = Record<string, string>
1 unmodified line
910export type Env = Record<string, any>1112export class Context<RequestParamKeyType extends string = string, E = Env> {11 req: Request<RequestParamKeyType>13 req: HonoRequest<RequestParamKeyType>14 env: E15 event: FetchEvent | undefined16 executionCtx: ExecutionContext | undefined17 finalized: boolean
8 unmodified lines
2627 render: (content: string, params?: object, options?: object) => Response | Promise<Response>2829 constructor(28 req: Request<RequestParamKeyType>,30 req: HonoRequest | Request,31 env: E | undefined = undefined,32 eventOrExecutionCtx: FetchEvent | ExecutionContext | undefined = undefined,33 notFoundHandler: NotFoundHandler = () => new Response()34 ) {35 if (req instanceof Request) {36 this.req = extendHonoRequest(req as HonoRequest)37 } else {33 this.req = req38 this.req = req39 }4041 if (env) {42 this.env = env43 }
99 unmodified lines
modified:src/hono.ts+9 added-6 removed
1import { compose } from './compose'2import { Context } from './context'3import type { Env } from './context'4import { extendRequestPrototype } from './request'4import type { HonoRequest } from './request'5import { extendHonoRequest } from './request'6import type { Router } from './router'7import { METHOD_NAME_ALL, METHOD_NAME_ALL_LOWERCASE } from './router'8import { TrieRouter } from './router/trie-router' // Default Router9import { getPathFromURL, mergePath } from './utils/url'
64 unmodified lines
7475 constructor(init: Partial<Pick<Hono, 'router' | 'strict'>> = {}) {76 super()7777 extendRequestPrototype() // FIXME: should be executed at a better timing7878 const allMethods = [...methods, METHOD_NAME_ALL_LOWERCASE]79 allMethods.map((method) => {80 this[method] = <Path extends string = ''>(81 args1: Path | Handler<ParamKeys<Path>, E>,
76 unmodified lines
158 private matchRoute(method: string, path: string) {159 return this.router.match(method, path)160 }161163 private async dispatch(request: Request, executionCtx?: ExecutionContext, env?: E): Promise<Response> {162 private async dispatch(163 request: Request,164 executionCtx?: ExecutionContext,165 env?: E166 ): Promise<Response> {167 request = extendHonoRequest(request as HonoRequest)168 const path = getPathFromURL(request.url, this.strict)169 const method = request.method170171 const result = this.matchRoute(method, path)172 ;(request as HonoRequest).paramData = result?.params173169 request.paramData = result?.params170174 const handlers = result ? result.handlers : [this.notFoundHandler]175176 const c = new Context<string, E>(request, env, executionCtx, this.notFoundHandler)177
38 unmodified lines
modified:src/request.ts+28 added-33 removed
1declare global {2 interface Request<ParamKeyType extends string = string> {3 param: {4 (key: ParamKeyType): string5 (): Record<ParamKeyType, string>1export class HonoRequest<ParamKeyType extends string = string> extends Request {2 param: {3 (key: ParamKeyType): string4 (): Record<ParamKeyType, string>6 }7 paramData?: Record<ParamKeyType, string>8 query: {9 (key: string): string10 (): Record<string, string>11 }12 queries: {13 (key: string): string[]14 (): Record<string, string[]>15 }16 header: {17 (name: string): string18 (): Record<string, string>19 }5 }6 paramData?: Record<ParamKeyType, string>7 query: {8 (key: string): string9 (): Record<string, string>10 }11 queries: {12 (key: string): string[]13 (): Record<string, string[]>14 }15 header: {16 (name: string): string17 (): Record<string, string>18 }19}2023export function extendRequestPrototype() {24 if (!!Request.prototype.param as boolean) {25 // already extended26 return27 }2829 Request.prototype.param = function (this: Request, key?: string) {21export function extendHonoRequest(request: HonoRequest): HonoRequest {22 request.param = function (this: HonoRequest, key?: string) {23 if (this.paramData) {24 if (key) {25 return this.paramData[key]26 } else {27 return this.paramData28 }29 }30 return null38 } as InstanceType<typeof Request>['param']31 } as InstanceType<typeof HonoRequest>['param']3240 Request.prototype.header = function (this: Request, name?: string) {33 request.header = function (this: HonoRequest, name?: string) {34 if (name) {35 return this.headers.get(name)36 } else {37 const result: Record<string, string> = {}
1 unmodified line
39 result[key] = value40 }41 return result42 }50 } as InstanceType<typeof Request>['header']43 } as InstanceType<typeof HonoRequest>['header']4452 Request.prototype.query = function (this: Request, key?: string) {45 request.query = function (this: HonoRequest, key?: string) {46 const url = new URL(this.url)47 if (key) {48 return url.searchParams.get(key)49 } else {
2 unmodified lines
52 result[key] = url.searchParams.get(key) || ''53 }54 return result55 }63 } as InstanceType<typeof Request>['query']56 } as InstanceType<typeof HonoRequest>['query']5765 Request.prototype.queries = function (this: Request, key?: string) {58 request.queries = function (this: HonoRequest, key?: string) {59 const url = new URL(this.url)60 if (key) {61 return url.searchParams.getAll(key)62 } else {
2 unmodified lines
65 result[key] = url.searchParams.getAll(key)66 }67 return result68 }76 } as InstanceType<typeof Request>['queries']69 } as InstanceType<typeof HonoRequest>['queries']7071 return request72}