Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: next() without await #1685

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions deno_dist/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type Env = {
Variables?: Variables
}

export type Next = () => Promise<void>
export type Next = () => void | Promise<void>

export type Input = {
in?: Partial<ValidationTargets>
Expand All @@ -46,7 +46,7 @@ export type MiddlewareHandler<
E extends Env = any,
P extends string = string,
I extends Input = {}
> = (c: Context<E, P, I>, next: Next) => Promise<Response | void>
> = (c: Context<E, P, I>, next: Next) => Response | void | Promise<Response | void>

export type H<
E extends Env = any,
Expand Down
49 changes: 49 additions & 0 deletions src/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,52 @@ describe('Compose', function () {
expect(ctx.next).toEqual(1)
})
})

describe('compose without `await`', () => {
const middleware: MiddlewareTuple[] = []

const a = async (c: C, next: Function) => {
c.req['log'] = 'log'
next()
}

const b = async (c: C, next: Function) => {
next()
c.res['headers'] = 'custom-header'
}

const c = async (c: C, next: Function) => {
c.req['xxx'] = 'yyy'
next()
c.res['zzz'] = c.req['xxx']
}

const handler = async (c: C, next: Function) => {
c.req['log'] = `${c.req.log} message`
next()
c.res = { message: 'new response' }
}

middleware.push(buildMiddlewareTuple(a))
middleware.push(buildMiddlewareTuple(b))
middleware.push(buildMiddlewareTuple(c))
middleware.push(buildMiddlewareTuple(handler))

it('Request', async () => {
const c: C = { req: {}, res: {}, finalized: false }
const composed = compose<C>(middleware)
const context = await composed(c)
expect(context.req['log']).not.toBeNull()
expect(context.req['log']).toBe('log message')
expect(context.req['xxx']).toBe('yyy')
})
it('Response', async () => {
const c: C = { req: {}, res: {}, finalized: false }
const composed = compose<C>(middleware)
const context = await composed(c)
expect(context.res['headers']).not.toBeNull()
expect(context.res['headers']).toBe('custom-header')
expect(context.res['message']).toBe('new response')
expect(context.res['zzz']).toBe('yyy')
})
})
97 changes: 97 additions & 0 deletions src/hono.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,103 @@ describe('Middleware', () => {
})
})

describe('Without `await`', () => {
const app = new Hono()

// Custom Logger
app.use('*', (c, next) => {
console.log(`${c.req.method} : ${c.req.url}`)
next()
})

// Append Custom Header
app.use('*', (c, next) => {
next()
c.res.headers.append('x-custom', 'root')
})

app.use('/hello', async (c, next) => {
next()
c.res.headers.append('x-message', 'custom-header')
})

app.use('/hello/*', async (c, next) => {
next()
c.res.headers.append('x-message-2', 'custom-header-2')
})

app.get('/hello', (c) => {
return c.text('hello')
})

app.use('/json/*', async (c, next) => {
c.res.headers.append('foo', 'bar')
await next()
})

app.get('/json', (c) => {
// With a raw response
return new Response(
JSON.stringify({
message: 'hello',
}),
{
headers: {
'content-type': 'application/json',
},
}
)
})

app.get('/hello/:message', (c) => {
const message = c.req.param('message')
return c.text(`${message}`)
})

app.get('/error', () => {
throw new Error('Error!')
})

app.notFound((c) => {
return c.text('Not Found Foo', 404)
})

it('logging and custom header', async () => {
const res = await app.request('http://localhost/hello')
expect(res.status).toBe(200)
expect(await res.text()).toBe('hello')
expect(res.headers.get('x-custom')).toBe('root')
expect(res.headers.get('x-message')).toBe('custom-header')
expect(res.headers.get('x-message-2')).toBe('custom-header-2')
})

it('logging and custom header with named param', async () => {
const res = await app.request('http://localhost/hello/message')
expect(res.status).toBe(200)
expect(await res.text()).toBe('message')
expect(res.headers.get('x-custom')).toBe('root')
expect(res.headers.get('x-message-2')).toBe('custom-header-2')
})

it('should return correct the content-type header', async () => {
const res = await app.request('http://localhost/json')
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toMatch(/^application\/json/)
})

it('not found', async () => {
const res = await app.request('http://localhost/foo')
expect(res.status).toBe(404)
expect(await res.text()).toBe('Not Found Foo')
})

it('internal server error', async () => {
const res = await app.request('http://localhost/error')
expect(res.status).toBe(500)
console.log(await res.text())
})
})

describe('Chained route', () => {
const app = new Hono()
app
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type Env = {
Variables?: Variables
}

export type Next = () => Promise<void>
export type Next = () => void | Promise<void>

export type Input = {
in?: Partial<ValidationTargets>
Expand All @@ -46,7 +46,7 @@ export type MiddlewareHandler<
E extends Env = any,
P extends string = string,
I extends Input = {}
> = (c: Context<E, P, I>, next: Next) => Promise<Response | void>
> = (c: Context<E, P, I>, next: Next) => Response | void | Promise<Response | void>

export type H<
E extends Env = any,
Expand Down