Skip to content
Myr

feat: multiple query values `c.req.queries` (#260)

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 fa82e48 · Browse files · 4 files+56 added−7 removed
This commit

State

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

Diff stats

4 files+56 added−7 removed

  • tests:+23 added−0 removed
  • other:+33 added−7 removed

4 files+56 added−7 removed

modified:src/context.ts+16 added-2 removed
modified:src/hono.test.ts+23 added
196 unmodified lines
197 const name = c.req.query('name')198 return c.text(`name is ${name}`)199 })200201 app.get('/multiple-values', (c) => {202 const queries = c.req.queries('q')203 const limit = c.req.queries('limit')204 return c.text(`q is ${queries[0]} and ${queries[1]}, limit is ${limit[0]}`)205 })206207 app.get('/add-header', (c) => {208 const bar = c.req.header('X-Foo')209 return c.text(`foo is ${bar}`)210 })
18 unmodified lines
229 const { name } = c.req.query()230 return c.text(`name is ${name}`)231 })232233 app.get('/multiple-values', (c) => {234 const { q, limit } = c.req.queries()235 return c.text(`q is ${q[0]} and ${q[1]}, limit is ${limit[0]}`)236 })237238 app.get('/add-header', (c) => {239 const { 'x-foo': bar } = c.req.header()240 return c.text(`foo is ${bar}`)241 })
21 unmodified lines
263 expect(res.status).toBe(200)264 expect(await res.text()).toBe('name is sam')265 })266267 it('query of /search?name=sam&name=tom is found', async () => {268 const res = await app.request('http://localhost/search?name=sam&name=tom')269 expect(res.status).toBe(200)270 expect(await res.text()).toBe('name is sam')271 })272273 it('query of /multiple-values?q=foo&q=bar&limit=10 is found', async () => {274 const res = await app.request('http://localhost/multiple-values?q=foo&q=bar&limit=10')275 expect(res.status).toBe(200)276 expect(await res.text()).toBe('q is foo and bar, limit is 10')277 })278279 it('/add-header header - X-Foo is Bar', async () => {280 const req = new Request('http://localhost/add-header')281 req.headers.append('X-Foo', 'Bar')282 const res = await app.request(req)
543 unmodified lines
modified:src/hono.ts+4 added
15 unmodified lines
16 query: {17 (key: string): string18 (): Record<string, string>19 }20 queries: {21 (key: string): string[]22 (): Record<string, string[]>23 }24 header: {25 (name: string): string26 (): Record<string, string>27 }
219 unmodified lines
modified:README.md+13 added-5 removed
135 unmodified lines
136## Methods137138An instance of `Hono` has these methods.139140- app.**HTTP_METHOD**(\[path,\] handler|middleware...)141- app.**all**(\[path,\] handler|middleware...)142- app.**route**(path, \[app\])143- app.**use**(\[path,\] middleware)140- app.**HTTP_METHOD**(\[パス,\]handler|middleware...)141- app.**all**(\[パス,\]handler|middleware...)142- app.**route**(パス, \[app\])143- app.**use**(\[パス,\]middleware)144- app.**notFound**(handler)145- app.**onError**(err, handler)146- app.**fire**()147- app.**fetch**(request, env, event)148- app.**request**(path, options)148- app.**request**(パス, options)149150## Routing151152### Basic
200 unmodified lines
353 const { q, limit, offset } = c.req.query()354 ...355})356357// Multiple query values358app.get('/search', (c) => {359 const queries = c.req.queries('q')360 // ---> GET search?q=foo&q=bar361 // queries[0] => foo, queries[1] => bar362 ...363})364365// Captured params366app.get('/entry/:id', (c) => {367 const id = c.req.param('id')368 ...
319 unmodified lines