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
Provenance
- Commit
ba58356- Tree
03c1e37- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
fa82e48
4 files changed+56 added−7 removed
modified:src/context.ts+16 added-2 removed
55 unmodified lines56575859606160616263646566676869707172737475767778798081828385 unmodified lines55 unmodified lines if (key) { return url.searchParams.get(key) } else { const result: Record<string, string> = {} for (const [key, value] of url.searchParams) { result[key] = value for (const key of url.searchParams.keys()) { result[key] = url.searchParams.get(key) } return result } }) as typeof req.query
req.queries = ((key?: string): string[] | Record<string, string[]> => { const url = new URL(req.url) if (key) { return url.searchParams.getAll(key) } else { const result: Record<string, string[]> = {} for (const key of url.searchParams.keys()) { result[key] = url.searchParams.getAll(key) } return result } }) as typeof req.queries
return req }
header(name: string, value: string): void {85 unmodified linesmodified:src/hono.test.ts+23 added
196 unmodified lines197 const name = c.req.query('name')198 return c.text(`name is ${name}`)199 })200 201+ 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+ })206+207 app.get('/add-header', (c) => {208 const bar = c.req.header('X-Foo')209 return c.text(`foo is ${bar}`)210 })18 unmodified lines229 const { name } = c.req.query()230 return c.text(`name is ${name}`)231 })232 233+ 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+ })237+238 app.get('/add-header', (c) => {239 const { 'x-foo': bar } = c.req.header()240 return c.text(`foo is ${bar}`)241 })21 unmodified lines263 expect(res.status).toBe(200)264 expect(await res.text()).toBe('name is sam')265 })266 267+ 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+ })272+273+ 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+ })278+279 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 linesmodified:src/hono.ts+4 added
15 unmodified lines16 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 linesmodified:README.md+13 added-5 removed
135 unmodified lines136 ## Methods137 138 An instance of `Hono` has these methods.139 140-- 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)149 150 ## Routing151 152 ### Basic200 unmodified lines353 const { q, limit, offset } = c.req.query()354 ...355 })356 357+// Multiple query values358+app.get('/search', (c) => {359+ const queries = c.req.queries('q')360+ // ---> GET search?q=foo&q=bar361+ // queries[0] => foo, queries[1] => bar362+ ...363+})364+365 // Captured params366 app.get('/entry/:id', (c) => {367 const id = c.req.param('id')368 ...319 unmodified lines