Skip to content
Myr

feat(app): pass `env` to `getPath()` (#1345)

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

* feat(app): pass `env` to `getPath()` * denoify

authored by Yusuke Wada · committed by GitHub · parent 4a02747 · Browse files · 3 files+32 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+32 added−5 removed

  • tests:+28 added−1 removed
  • other:+4 added−4 removed

3 files+32 added−5 removed

modified:deno_dist/hono-base.ts+2 added-2 removed
64 unmodified lines
65 This class is like an abstract class and does not have a router.66 To use it, inherit the class and implement router in the constructor.67 */68 router!: Router<H>69 readonly getPath: (request: Request) => string69 readonly getPath: (request: Request, options?: { env?: E['Bindings'] }) => string70 private _basePath: string = '/'71 private path: string = '/'7273 routes: RouterRoute[] = []
203 unmodified lines
277 executionCtx: ExecutionContext | FetchEventLike | undefined,278 env: E['Bindings'],279 method: string280 ): Response | Promise<Response> {281 const path = this.getPath(request)281 const path = this.getPath(request, { env })282283 // Handle HEAD method284 if (method === 'HEAD') {285 return (async () =>
103 unmodified lines
modified:src/hono-base.ts+2 added-2 removed
64 unmodified lines
65 This class is like an abstract class and does not have a router.66 To use it, inherit the class and implement router in the constructor.67 */68 router!: Router<H>69 readonly getPath: (request: Request) => string69 readonly getPath: (request: Request, options?: { env?: E['Bindings'] }) => string70 private _basePath: string = '/'71 private path: string = '/'7273 routes: RouterRoute[] = []
203 unmodified lines
277 executionCtx: ExecutionContext | FetchEventLike | undefined,278 env: E['Bindings'],279 method: string280 ): Response | Promise<Response> {281 const path = this.getPath(request)281 const path = this.getPath(request, { env })282283 // Handle HEAD method284 if (method === 'HEAD') {285 return (async () =>
103 unmodified lines
modified:src/hono.test.ts+28 added-1 removed
8 unmodified lines
9import { RegExpRouter } from './router/reg-exp-router'10import { TrieRouter } from './router/trie-router'11import type { Handler, Next } from './types'12import type { Expect, Equal } from './utils/types'13import { getPath, getPathNoStrict } from './utils/url'13import { getPath } from './utils/url'1415// https://stackoverflow.com/a/6566640216function throwExpression(errorMessage: string): never {17 throw new Error(errorMessage)
498 unmodified lines
516 expect(await res.text()).toBe('hello sub foo')517 expect(res.status).toBe(200)518 })519520 describe('routing with the bindings value', () => {521 const app = new Hono<{ Bindings: { host: string } }>({522 getPath: (req, options) => {523 const url = new URL(req.url)524 const host = options?.env?.host525 const prefix = url.host === host ? '/FOO' : ''526 return url.pathname === '/' ? prefix : `${prefix}${url.pathname}`527 },528 })529530 app.get('/about', (c) => c.text('About root'))531 app.get('/FOO/about', (c) => c.text('About FOO'))532533 it('Should return 200 without specifying a hostname', async () => {534 const res = await app.request('/about')535 expect(res.status).toBe(200)536 expect(await res.text()).toBe('About root')537 })538539 it('Should return 200 with specifying the hostname in env', async () => {540 const req = new Request('http://foo.localhost/about')541 const res = await app.fetch(req, { host: 'foo.localhost' })542 expect(res.status).toBe(200)543 expect(await res.text()).toBe('About FOO')544 })545 })546547 describe('Chained route', () => {548 const app = new Hono()549550 app
1808 unmodified lines