Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe('compose', () => {

const a = async (c: Context, next: Next) => {
c.set('log', 'log')
c.set('routeIndex-before-next', c.req.routeIndex)
await next()
c.set('routeIndex-after-next', c.req.routeIndex)
}

const b = async (c: Context, next: Next) => {
Expand Down Expand Up @@ -47,6 +49,8 @@ describe('compose', () => {
expect(context.get('log')).not.toBeNull()
expect(context.get('log')).toBe('log message')
expect(context.get('xxx')).toBe('yyy')
expect(context.get('routeIndex-before-next')).toBe(0)
expect(context.get('routeIndex-after-next')).toBe(0)
})
it('Response', async () => {
const composed = compose(middleware)
Expand Down Expand Up @@ -244,7 +248,9 @@ describe('compose with Context - 500 error', () => {
}

const mHandler = async (_c: Context, next: Next) => {
_c.set('routeIndex-before-next', _c.req.routeIndex)
await next()
_c.set('routeIndex-after-next', _c.req.routeIndex)
}

middleware.push(buildMiddlewareTuple(mHandler))
Expand All @@ -259,6 +265,8 @@ describe('compose with Context - 500 error', () => {
expect(context.res.status).toBe(500)
expect(await context.res.text()).toBe('onError')
expect(context.finalized).toBe(true)
expect(context.get('routeIndex-before-next')).toBe(0)
expect(context.get('routeIndex-after-next')).toBe(0)
})

it('Error on handler - async', async () => {
Expand Down
5 changes: 4 additions & 1 deletion src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export const compose = <E extends Env = Env>(

if (handler) {
try {
res = await handler(context, () => dispatch(i + 1))
res = await handler(context, () => {
const routeIndex = context.req.routeIndex
return dispatch(i + 1).finally(() => (context.req.routeIndex = routeIndex))
})
} catch (err) {
if (err instanceof Error && onError) {
context.error = err
Expand Down
Loading