npm hono 2.4.0
v2.4.0

latest releases: 4.6.2, 4.6.1, 4.6.0...
22 months ago

Summary

This release includes the fix for the ESM support, v.queries() feature in Validator middleware, and CustomHandler that is exported as Handler interface.

Fixing ESM support

Previously, if we use Hono package in Node.js directly, the error would throw. This release has enabled proper support for both ESM and CommonJS (However, this isn't easy to test and may not work well in your environment. If there is a problem, please let me know. We will fix it).

"Handler" interface

If we want to declare a handler outside of app.get(), we write it like this:

const handler: Handler = (c) => {
  return c.text('Hi!')
}

Then, in this handler, how can we make c.env or c.get('foo') have a type? Pass string as the first argument of the Generics for Handler and write the following:

type Env = {
  Bindings: { TOKEN: string }
  Variables: { post: Post }
}

const handler: Handler<string, Env> = (c) => { // <---
  const post = c.get('post')
  return c.json(post)
}

This is redundant to write string on purpose. So, in this release, we can write just like this:

const handler: Handler<Env> = (c) => {
  const post = c.get('post')
  return c.json(post)
}

Of course, it keeps to have the types:

SS

Actually, we can do something similar for Validator schema:

const schema = (v: Validator) => ({
  query: v.query('q'),
})

type Schema = ReturnType<typeof schema>

const handler: Handler<Schema> = (c) => {
  const { query } = c.req.valid()
  return c.json({ query: query })
}

SS

What's Changed

  • fix(build): fix ESM support in Node.js / use esbuild by @yusukebe in #631
  • test(bun): JWT middleware works on Bun v0.2.2 by @yusukebe in #633
  • fix(typo) package.cjs.json target to type by @taishinaritomi in #634
  • fix(validator): enable handling "Bad Request" in validator by @yusukebe in #635
  • feat(types): introduce CustomHandler interface by @yusukebe in #637
  • feat(validator): support v.queries by @yusukebe in #636
  • fix(types): improve custom handler by @yusukebe in #641
  • test(routing): add test cases for route grouping order by @yujong-lee in #640

New Contributors

Full Changelog: v2.3.2...v2.4.0

Don't miss a new hono release

NewReleases is sending notifications on new releases.