Skip to content
Myr

feat(hono-base): Added `replaceRequest: false` option for `.mount` (#4113)

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

authored by Glen Maddern · committed by GitHub · parent 15a83b1 · Browse files · 2 files+26 added−3 removed
This commit

State

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

Diff stats

2 files+26 added−3 removed

  • tests:+19 added−0 removed
  • other:+7 added−3 removed

2 files+26 added−3 removed

modified:src/hono-base.ts+7 added-3 removed
90 unmodified lines
91type MountOptions =92 | MountOptionHandler93 | {94 optionHandler?: MountOptionHandler95 replaceRequest?: MountReplaceRequest95 replaceRequest?: MountReplaceRequest | false96 }9798class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> {99 get!: HandlerInterface<E, 'get', S, BasePath>
227 unmodified lines
327 if (typeof options === 'function') {328 optionHandler = options329 } else {330 optionHandler = options.optionHandler331 if (options.replaceRequest === false) {332 replaceRequest = (request) => request333 } else {331 replaceRequest = options.replaceRequest334 replaceRequest = options.replaceRequest335 }336 }337 }338339 // prepare handlers for request
19 unmodified lines
359 }360 })()361362 const handler: MiddlewareHandler = async (c, next) => {359 const res = await applicationHandler(replaceRequest!(c.req.raw), ...getOptions(c))363 const res = await applicationHandler(replaceRequest(c.req.raw), ...getOptions(c))364365 if (res) {366 return res367 }
152 unmodified lines
modified:src/hono.test.ts+19 added
2732 unmodified lines
2733 expect(res.status).toBe(200)2734 expect(await res.text()).toBe('/app')2735 })2736 })27372738 describe('With replaceRequest: false', () => {2739 const anotherApp = (req: Request) => {2740 const path = getPath(req)2741 if (path === '/app') {2742 return new Response(getPath(req))2743 }2744 return new Response(null, { status: 404 })2745 }27462747 const app = new Hono()2748 app.mount('/app', anotherApp, { replaceRequest: false })27492750 it('Should return 200 response with the correct path', async () => {2751 const res = await app.request('/app')2752 expect(res.status).toBe(200)2753 expect(await res.text()).toBe('/app')2754 })2755 })2756})27572758describe('HEAD method', () => {2759 const app = new Hono()
865 unmodified lines