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
Provenance
- Commit
07bc29e- Tree
1ceaec3- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
4a02747
3 files changed+32 added−5 removed
modified:deno_dist/hono-base.ts+2 added-2 removed
64 unmodified lines65 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 = '/'72 73 routes: RouterRoute[] = []203 unmodified lines277 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 })282 283 // Handle HEAD method284 if (method === 'HEAD') {285 return (async () =>103 unmodified linesmodified:src/hono-base.ts+2 added-2 removed
64 unmodified lines65 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 = '/'72 73 routes: RouterRoute[] = []203 unmodified lines277 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 })282 283 // Handle HEAD method284 if (method === 'HEAD') {285 return (async () =>103 unmodified linesmodified:src/hono.test.ts+28 added-1 removed
8 unmodified lines9 import { RegExpRouter } from './router/reg-exp-router'10 import { TrieRouter } from './router/trie-router'11 import type { Handler, Next } from './types'12 import type { Expect, Equal } from './utils/types'13-import { getPath, getPathNoStrict } from './utils/url'13+import { getPath } from './utils/url'14 15 // https://stackoverflow.com/a/6566640216 function throwExpression(errorMessage: string): never {17 throw new Error(errorMessage)498 unmodified lines516 expect(await res.text()).toBe('hello sub foo')517 expect(res.status).toBe(200)518 })519 520+ 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+ })529+530+ app.get('/about', (c) => c.text('About root'))531+ app.get('/FOO/about', (c) => c.text('About FOO'))532+533+ 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+ })538+539+ 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+ })546+547 describe('Chained route', () => {548 const app = new Hono()549 550 app1808 unmodified lines