Skip to content
Myr

fix(request): `req.param()` should return `undefined` not "undefined" (#721)

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

authored by Yusuke Wada · committed by GitHub · parent c6b434a · Browse files · 3 files+26 added−5 removed
This commit

State

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

Diff stats

3 files+26 added−5 removed

  • tests:+16 added−1 removed
  • other:+10 added−4 removed

3 files+26 added−5 removed

modified:deno_dist/request.ts+5 added-2 removed
modified:src/hono.test.ts+16 added-1 removed
371 unmodified lines
372 expect(res.status).toBe(200)373 expect(await res.text()).toBe('foo is Bar')374 })375 })376377 describe('param with undefined', () => {378 const app = new Hono()379 app.get('/foo/:foo', (c) => {380 // eslint-disable-next-line @typescript-eslint/ban-ts-comment381 /* @ts-ignore */382 const bar = c.req.param('bar')383 return c.json({ foo: bar })384 })385 it('param of /foo/foo should return undefined not "undefined"', async () => {386 const res = await app.request('http://localhost/foo/foo')387 expect(res.status).toBe(200)388 expect(await res.json()).toEqual({ foo: undefined })389 })390 })391})392393describe('Middleware', () => {394 describe('Basic', () => {
865 unmodified lines
1260 const res = await app.request('http://localhost/')1261 expect(res).not.toBeNull()1262 expect(res.status).toBe(200)1263 expect(await res.text()).toBe('Bar')1249 expect(res.headers.get('content-type')).toBe('text/plain; charset=UTF-8')1264 expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/)1265 })1266})12671268describe('Count of logger called', () => {
106 unmodified lines
modified:src/request.ts+5 added-2 removed
55 unmodified lines
5657 Request.prototype.param = function (this: Request, key?: string) {58 if (this.paramData) {59 if (key) {60 return decodeURIComponent(this.paramData[key])60 const param = this.paramData[key]61 return param ? decodeURIComponent(param) : undefined62 } else {63 const decoded: Record<string, string> = {}6465 for (const [key, value] of Object.entries(this.paramData)) {66 if (value) {65 decoded[key] = decodeURIComponent(value)67 decoded[key] = decodeURIComponent(value)68 }69 }7071 return decoded72 }
96 unmodified lines