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
Provenance
- Commit
ae3546e- Tree
20cb791- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
1ed742b
6 files changed+79 added−38 removed
modified:deno_dist/hono-base.ts+1 added-1 removed
277 unmodified lines278 279 const path = this.getPath(request, { env })280 const [handlers, paramStash] = this.matchRoute(method, path)281 282- 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 linesmodified:deno_dist/request.ts+19 added-18 removed
27 unmodified lines28 29 export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {30 raw: Request31 32- 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 = {}37 38- 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 lines54 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> = {}63 61- 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 }69-70- return decoded71 }72+73+ return decoded74 }73- return null75 }76 77 query(key: string): string | undefined78 query(): Record<string, string>172 unmodified linesmodified:src/hono-base.ts+1 added-1 removed
277 unmodified lines278 279 const path = this.getPath(request, { env })280 const [handlers, paramStash] = this.matchRoute(method, path)281 282- 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 linesmodified:src/hono.test.ts+6 added
657 unmodified lines658 expect(res.status).toBe(200)659 expect(await res.text()).toBe('id is 123')660 })661 662+ 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+ })667+668 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 linesmodified:src/request.test.ts+33 added
30 unmodified lines3132333435363738394041424344454647484950515253545556575859606162636465666768697071103 unmodified linesExpand all30 unmodified lines expect(tags).toEqual(['@1', '@2']) })})
describe('Param', () => { test('req.param() withth ParamStash', () => { const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B') const req = new HonoRequest<'/:id/:name'>(rawRequest, '/123/key', ['123', 'key'])
req.setParams({ id: 0, }) expect(req.param('id')).toBe('123') expect(req.param('name')).toBe(undefined)
req.setParams({ id: 0, name: 1, }) expect(req.param('id')).toBe('123') expect(req.param('name')).toBe('key') })
test('req.param() without ParamStash', () => { const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B') const req = new HonoRequest<'/:id/:name'>(rawRequest)
req.setParams({ id: '456', name: 'key', })
expect(req.param('id')).toBe('456') expect(req.param('name')).toBe('key') })})
describe('req.addValidatedData() and req.data()', () => { const rawRequest = new Request('http://localhost')
const payload = {103 unmodified linesExpand allmodified:src/request.ts+19 added-18 removed
27 unmodified lines28 29 export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {30 raw: Request31 32- 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 = {}37 38- 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 lines54 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> = {}63 61- 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 }69-70- return decoded71 }72+73+ return decoded74 }73- return null75 }76 77 query(key: string): string | undefined78 query(): Record<string, string>172 unmodified lines