github honojs/hono v2.4.0

latest releases: v4.6.2, v4.6.1, v4.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

New Contributors

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

Don't miss a new hono release

NewReleases is sending notifications on new releases.