npm hono 3.0.0
v3.0.0

latest releases: 4.6.9, 4.6.8, 4.6.7...
20 months ago

Do Everything, Run Anywhere, But Small, And Faster.

Hono v3.0.0 has been released now! Introduce the new features.

Note

There are some breaking changes. See the migration guide.

HonoRequest

Previously, c.req was an extended Request object. But, the functions and types vary by runtime. So it was causing inconvenience.
Hence, HonoRequest is born.

HonoRequest has Hono-specific functions such as req.param() and req.query, then will wrap the Request object. You can access the original Response object via req.raw. Most functions and properties are bypassed, so you can still use c.req as before.

// c.req is HonoRequest
const id = c.req.param('id')
const query = c.req.query('q')
const data = await c.req.json()

// originalRequest is Request
const originalRequest = c.req.raw

Performance improvement

It is getting 2% - 11% faster. So, Hono is fast.

SS

RegExpRouter becomes the fastest router

RegExpRouter become the fastest router in the JavaScript world which includes such "find-my-way" used in Fastify.

SS

You can find the benchmark scripts.

Good-bye StaticRouter

Thanks to the faster RegExpRouter, StaticRouter is no longer needed. Obsolete. Good-bye StaticRouter!

New Validator

The previous Validator Middleware is obsolete and you can use the new validator. Its validator is very thin and we recommend using it with a third-party validator.

If you want to use Zod, now, we have "Zod Validator Middleware".

import { zValidator } from '@hono/zod-validator'
import { Hono } from 'hono'
import { z } from 'zod'

const app = new Hono()

app.post(
  '/posts',
  zValidator(
    'json',
    z.object({
      id: z.number(),
      title: z.string(),
    })
  ),
  (c) => {
    const { id, title } = c.req.valid('json')
    return c.text(`${id} is ${title}!`)
  }
)

RPC

Then, we'll show you the magic.


output.mov

We've made the HTTP client which is called hc. API specs can be shared as types between the API server and the client. It is like tRPC, but easier to handle due to its being integrated.

To enable RPC mode, simply change json() to jsonT() and share the endpoint type.

// server.ts
const route = app.post(
  '/posts',
  zValidator(
    'json',
    z.object({
      id: z.number(),
      title: z.string(),
    })
  ),
  (c) => {
    const { id, title } = c.req.valid('json')
    // ...
    return c.jsonT(
      {
        success: true,
        message: 'created!',
      },
      201
    )
  }
)

export type AppType = typeof route

On the client side, import the type and pass it to hc() as a generics.

import { hc } from 'hono/client'
import type { AppType } from './index'

const client = hc<AppType>('/api')

Now it is ready for use. See it.

SC

Thanks

This RPC feature is based on @cleaton's comment.

#582 (comment)

Thanks for the ultra-cool idea.

Adapter

Hono is also suitable for running in functions of Cloudflare Pages or in Edge Functions of Next.js. To make them easier, you can use the "Adapter".

// pages/api/[...route].ts
import { Hono } from 'hono'
import { handle } from 'hono/nextjs'

export const config = {
  runtime: 'edge',
}

const app = new Hono()

app.get('/hello', (c) => {
  return c.json({
    message: 'Hello from Hono!',
  })
})

export default handle(app, '/api')

The combination of this adapter and RPC is powerful. You can write the REST API on the Edge Functions and write the React applications with hc.

Also, ServeStatic Middleware has been obsolete and the feature of serving static files is now provided via an Adapter.

import { Hono } from 'hono'
import { serveStatic } from 'hono/cloudflare-workers'

const app = new Hono()

app.get('/static/*', serveStatic({ root: './' }))
app.get('/favicon.ico', serveStatic({ path: './favicon.ico' }))

HTTPException

The new HTTPException makes your application safer.

Multi-runtime CI support

Hono works on most JavaScript runtimes. It becomes to be tested on the CI for the following five runtimes.

SS

Support WinterCG Runtime Keys

Hono is following WinterCG and will be "de fact standard for Web Standard". Now, c.runtime() supports WinterCG's Runtime Keys.

app.get('/', (c) => {
  if (c.runtime === 'workerd') {
    return c.text('You are on Cloduflare')
  } else if (c.runtime === 'bun') {
    return c.text('You are on Bun')
  }
  ...
})

Available runtime keys are below.

  • node
  • deno
  • bun
  • workerd - Cloudflare Workers
  • fastly
  • edge-light - Vercel Edge Functions
  • lagon
  • other

create-hono

You can create the Hono application with one command and launch the application in 30 seconds!

npm create hono@latest my-app

SS

@honojs to @hono

@honojs namespace in the Npm registry is deprecated, but we can use @hono! So each Middleware is now renamed as follows.

  • @hono/graphql-server
  • @hono/sentry
  • @hono/firebase-auth

And Node.js adapter becomes @hono/node-server.

New middleware

Adding them, we can use these new middleware.

  • @hono/trpc-server - tRPC Server Middleware
  • @hono/qwik-city - Qwik City Middleware
  • @hono/zod-validator - Zod Validator Middleware

New website

Finally, we have to tell you about it. We have the new website with an ultra cool domain name.

hono.dev!!

Not only the domain but has been changed from Hugo-based to VitePress-based, making it easier to contribute.

Wrap-up

Many new features. Enjoy and share!

All Changes

New Contributors

Full Changelog: v2.7.3...v3.0.0

Don't miss a new hono release

NewReleases is sending notifications on new releases.