Skip to content
Myr

Tweaks return values of functions for request parameters. (#234)

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

* Tweaks return values of functions for request parameters. If no arguments are specified, returns an object containing all parameters. * Add tests for app.req.param and app.req.query. * Enable to get all cookies by `c.req.cookie()`.

authored by Taku Amano · committed by GitHub · parent b314348 · Browse files · 5 files+211 added−106 removed
This commit

State

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

Diff stats

5 files+211 added−106 removed

  • tests:+151 added−85 removed
  • other:+60 added−21 removed

5 files+211 added−106 removed

modified:src/middleware/cookie/index.test.ts+78 added-47 removed
modified:src/middleware/cookie/index.ts+12 added-5 removed
modified:src/context.ts+25 added-8 removed
5 unmodified lines
6type Data = string | ArrayBuffer | ReadableStream78export type Env = Record<string, any>910export class Context<RequestParamKeyType = string, E = Env> {10export class Context<RequestParamKeyType extends string = string, E = Env> {11 req: Request<RequestParamKeyType>12 res: Response = undefined13 env: E14 event: FetchEvent
18 unmodified lines
33 this._map = {}34 Object.assign(this, opts)35 }3637 private initRequest<T>(req: Request<T>): Request<T> {38 req.header = (name: string): string => {39 return req.headers.get(name)40 }41 req.query = (key: string): string => {37 private initRequest<T extends string>(req: Request<T>): Request<T> {38 req.header = ((name?: string): string | Record<string, string> => {39 if (name) {40 return req.headers.get(name)41 } else {42 const result: Record<string, string> = {}43 for (const [key, value] of req.headers) {44 result[key] = value45 }46 return result47 }48 }) as typeof req.header4950 req.query = ((key?: string): string | Record<string, string> => {51 const url = new URL(req.url)52 if (key) {53 return url.searchParams.get(key)54 } else {55 const result: Record<string, string> = {}56 for (const [key, value] of url.searchParams) {57 result[key] = value58 }43 return url.searchParams.get(key)44 }59 return result60 }61 }) as typeof req.query62 return req63 }6465 header(name: string, value: string): void {
84 unmodified lines
modified:src/hono.test.ts+73 added-38 removed
179 unmodified lines
180 })181})182183describe('param and query', () => {184 const apps: Record<string, Hono> = {}185 apps['get by name'] = (() => {184 const app = new Hono()186 const app = new Hono()187186 app.get('/entry/:id', (c) => {187 const id = c.req.param('id')188 return c.text(`id is ${id}`)189 })188 app.get('/entry/:id', (c) => {189 const id = c.req.param('id')190 return c.text(`id is ${id}`)191 })192191 app.get('/date/:date{[0-9]+}', (c) => {192 const date = c.req.param('date')193 return c.text(`date is ${date}`)194 })193 app.get('/date/:date{[0-9]+}', (c) => {194 const date = c.req.param('date')195 return c.text(`date is ${date}`)196 })197196 app.get('/search', (c) => {197 const name = c.req.query('name')198 return c.text(`name is ${name}`)199 })198 app.get('/search', (c) => {199 const name = c.req.query('name')200 return c.text(`name is ${name}`)201 })202201 app.get('/add-header', (c) => {202 const bar = c.req.header('X-Foo')203 return c.text(`foo is ${bar}`)204 })203 app.get('/add-header', (c) => {204 const bar = c.req.header('X-Foo')205 return c.text(`foo is ${bar}`)206 })207206 it('param of /entry/:id is found', async () => {207 const res = await app.request('http://localhost/entry/123')208 expect(res.status).toBe(200)209 expect(await res.text()).toBe('id is 123')210 })208 return app209 })()210212 it('param of /date/:date is found', async () => {213 const res = await app.request('http://localhost/date/0401')214 expect(res.status).toBe(200)215 expect(await res.text()).toBe('date is 0401')216 })211 apps['get all as an object'] = (() => {212 const app = new Hono()213218 it('query of /search?name=sam is found', async () => {219 const res = await app.request('http://localhost/search?name=sam')220 expect(res.status).toBe(200)221 expect(await res.text()).toBe('name is sam')222 })214 app.get('/entry/:id', (c) => {215 const { id } = c.req.param()216 return c.text(`id is ${id}`)217 })218219 app.get('/date/:date{[0-9]+}', (c) => {220 const { date } = c.req.param()221 return c.text(`date is ${date}`)222 })223224 app.get('/search', (c) => {225 const { name } = c.req.query()226 return c.text(`name is ${name}`)227 })228229 app.get('/add-header', (c) => {230 const { 'x-foo': bar } = c.req.header()231 return c.text(`foo is ${bar}`)232 })233234 return app235 })()236237 describe.each(Object.keys(apps))('%s', (name) => {238 const app = apps[name]239240 it('param of /entry/:id is found', async () => {241 const res = await app.request('http://localhost/entry/123')242 expect(res.status).toBe(200)243 expect(await res.text()).toBe('id is 123')244 })245246 it('param of /date/:date is found', async () => {247 const res = await app.request('http://localhost/date/0401')248 expect(res.status).toBe(200)249 expect(await res.text()).toBe('date is 0401')250 })251252 it('query of /search?name=sam is found', async () => {253 const res = await app.request('http://localhost/search?name=sam')254 expect(res.status).toBe(200)255 expect(await res.text()).toBe('name is sam')256 })257224 it('/add-header header - X-Foo is Bar', async () => {225 const req = new Request('http://localhost/add-header')226 req.headers.append('X-Foo', 'Bar')227 const res = await app.request(req)228 expect(res.status).toBe(200)229 expect(await res.text()).toBe('foo is Bar')258 it('/add-header header - X-Foo is Bar', async () => {259 const req = new Request('http://localhost/add-header')260 req.headers.append('X-Foo', 'Bar')261 const res = await app.request(req)262 expect(res.status).toBe(200)263 expect(await res.text()).toBe('foo is Bar')264 })265 })266})267268describe('Middleware', () => {
461 unmodified lines
modified:src/hono.ts+23 added-8 removed
6 unmodified lines
7import { TrieRouter } from './router/trie-router' // Default Router8import { getPathFromURL, mergePath } from './utils/url'910declare global {11 interface Request<ParamKeyType = string> {12 param: (key: ParamKeyType) => string13 query: (key: string) => string14 header: (name: string) => string11 interface Request<ParamKeyType extends string = string> {12 param: {13 (key: ParamKeyType): string14 (): Record<ParamKeyType, string>15 }16 query: {17 (key: string): string18 (): Record<string, string>19 }20 header: {21 (name: string): string22 (): Record<string, string>23 }24 }25}2618export type Handler<RequestParamKeyType = string, E = Env> = (27export type Handler<RequestParamKeyType extends string = string, E = Env> = (28 c: Context<RequestParamKeyType, E>,29 next: Next30) => Response | Promise<Response> | void | Promise<void>31export type NotFoundHandler<E = Env> = (c: Context<string, E>) => Response
180 unmodified lines
212 const path = getPathFromURL(request.url, { strict: this.strict })213 const method = request.method214215 const result = await this.matchRoute(method, path)207 request.param = (key: string): string => {208 if (result) return result.params[key]209 }216 request.param = ((key?: string): string | Record<string, string> => {217 if (result) {218 if (key) {219 return result.params[key]220 } else {221 return result.params222 }223 }224 }) as typeof request.param225 const handlers = result ? result.handlers : [this.notFoundHandler]226227 const c = new Context<string, E>(request, { env: env, event: event, res: undefined })228 c.notFound = () => this.notFoundHandler(c)
35 unmodified lines