Skip to content
Myr

fix: References this._s only when paramStash is passed from the router (#1715)

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

* fix: References this._s only when paramStash is passed from the router. * chore: denoify * fix: remove debug statement. * test: Fix test for param keys.

authored by Taku Amano · committed by GitHub · parent 1ed742b · Browse files · 6 files+79 added−38 removed
This commit

State

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

Diff stats

6 files+79 added−38 removed

  • tests:+39 added−0 removed
  • other:+40 added−38 removed

6 files+79 added−38 removed

modified:deno_dist/hono-base.ts+1 added-1 removed
277 unmodified lines
278279 const path = this.getPath(request, { env })280 const [handlers, paramStash] = this.matchRoute(method, path)281282 const c = new Context(new HonoRequest(request, path, paramStash || []), {282 const c = new Context(new HonoRequest(request, path, paramStash), {283 env,284 executionCtx,285 notFoundHandler: this.notFoundHandler,286 })
98 unmodified lines
modified:deno_dist/request.ts+19 added-18 removed
27 unmodified lines
2829export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {30 raw: Request3132 private _s: ParamStash32 private _s: ParamStash | undefined33 private vData: { [K in keyof ValidationTargets]?: {} } // Short name of validatedData34 private _p: ParamIndexMap | Params = {}35 path: string36 bodyCache: BodyCache = {}3738 constructor(request: Request, path: string = '/', paramStash: ParamStash = []) {38 constructor(39 request: Request,40 path: string = '/',41 paramStash: ParamStash | undefined = undefined42 ) {43 this.raw = request44 this.path = path45 this._s = paramStash46 this.vData = {}
7 unmodified lines
54 key: RemoveQuestion<ParamKeys<P2>>55 ): UndefinedIfHavingQuestion<ParamKeys<P2>>56 param<P2 extends string = P>(): UnionToIntersection<ParamKeyToRecord<ParamKeys<P2>>>57 param(key?: string): unknown {54 if (this._s) {55 if (key) {56 const param = this._s[this._p[key] as any] ?? this._p[key]57 return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined58 } else {59 const decoded: Record<string, string> = {}58 if (key) {59 const param = (this._s ? this._s[this._p[key] as any] : this._p[key]) as string | undefined60 return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined61 } else {62 const decoded: Record<string, string> = {}6361 const keys = Object.keys(this._p)62 for (let i = 0, len = keys.length; i < len; i++) {63 const key = keys[i]64 const value = this._s[this._p[key] as any] ?? this._p[key]65 if (value && typeof value === 'string') {66 decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value67 }64 const keys = Object.keys(this._p)65 for (let i = 0, len = keys.length; i < len; i++) {66 const key = keys[i]67 const value = (this._s ? this._s[this._p[key] as any] : this._p[key]) as string | undefined68 if (value && typeof value === 'string') {69 decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value70 }6970 return decoded71 }7273 return decoded74 }73 return null75 }7677 query(key: string): string | undefined78 query(): Record<string, string>
172 unmodified lines
modified:src/hono-base.ts+1 added-1 removed
277 unmodified lines
278279 const path = this.getPath(request, { env })280 const [handlers, paramStash] = this.matchRoute(method, path)281282 const c = new Context(new HonoRequest(request, path, paramStash || []), {282 const c = new Context(new HonoRequest(request, path, paramStash), {283 env,284 executionCtx,285 notFoundHandler: this.notFoundHandler,286 })
98 unmodified lines
modified:src/hono.test.ts+6 added
657 unmodified lines
658 expect(res.status).toBe(200)659 expect(await res.text()).toBe('id is 123')660 })661662 it('param of /entry/:id is found, even for Array object method names', async () => {663 const res = await app.request('http://localhost/entry/key')664 expect(res.status).toBe(200)665 expect(await res.text()).toBe('id is key')666 })667668 it('param of /entry/:id is decoded', async () => {669 const res = await app.request('http://localhost/entry/%C3%A7awa%20y%C3%AE%3F')670 expect(res.status).toBe(200)671 expect(await res.text()).toBe('id is çawa yî?')
2079 unmodified lines
modified:src/request.test.ts+33 added
modified:src/request.ts+19 added-18 removed
27 unmodified lines
2829export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {30 raw: Request3132 private _s: ParamStash32 private _s: ParamStash | undefined33 private vData: { [K in keyof ValidationTargets]?: {} } // Short name of validatedData34 private _p: ParamIndexMap | Params = {}35 path: string36 bodyCache: BodyCache = {}3738 constructor(request: Request, path: string = '/', paramStash: ParamStash = []) {38 constructor(39 request: Request,40 path: string = '/',41 paramStash: ParamStash | undefined = undefined42 ) {43 this.raw = request44 this.path = path45 this._s = paramStash46 this.vData = {}
7 unmodified lines
54 key: RemoveQuestion<ParamKeys<P2>>55 ): UndefinedIfHavingQuestion<ParamKeys<P2>>56 param<P2 extends string = P>(): UnionToIntersection<ParamKeyToRecord<ParamKeys<P2>>>57 param(key?: string): unknown {54 if (this._s) {55 if (key) {56 const param = this._s[this._p[key] as any] ?? this._p[key]57 return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined58 } else {59 const decoded: Record<string, string> = {}58 if (key) {59 const param = (this._s ? this._s[this._p[key] as any] : this._p[key]) as string | undefined60 return param ? (/\%/.test(param) ? decodeURIComponent_(param) : param) : undefined61 } else {62 const decoded: Record<string, string> = {}6361 const keys = Object.keys(this._p)62 for (let i = 0, len = keys.length; i < len; i++) {63 const key = keys[i]64 const value = this._s[this._p[key] as any] ?? this._p[key]65 if (value && typeof value === 'string') {66 decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value67 }64 const keys = Object.keys(this._p)65 for (let i = 0, len = keys.length; i < len; i++) {66 const key = keys[i]67 const value = (this._s ? this._s[this._p[key] as any] : this._p[key]) as string | undefined68 if (value && typeof value === 'string') {69 decoded[key] = /\%/.test(value) ? decodeURIComponent_(value) : value70 }6970 return decoded71 }7273 return decoded74 }73 return null75 }7677 query(key: string): string | undefined78 query(): Record<string, string>
172 unmodified lines