コンテンツにスキップする

RPC のルートをグループ化

複数の app の型推論を正しく有効にしたい場合、以下の方法で app.route() を使用できます。

app.get()app.post() などのメソッドから返される値を app.route() の第 2 引数に渡します。

ts
import { Hono } from 'hono'
import { hc } from 'hono/client'

const authorsApp = new Hono()
  .get('/', (c) => c.json({ result: 'list authors' }))
  .post('/', (c) => c.json({ result: 'create an author' }, 201))
  .get('/:id', (c) => c.json({ result: `get ${c.req.param('id')}` }))

const booksApp = new Hono()
  .get('/', (c) => c.json({ result: 'list books' }))
  .post('/', (c) => c.json({ result: 'create a book' }, 201))
  .get('/:id', (c) => c.json({ result: `get ${c.req.param('id')}` }))

const app = new Hono()
  .route('/authors', authorsApp)
  .route('/books', booksApp)

type AppType = typeof app

また次の項目も参照してください

MIT ライセンスに基づいてリリースされています。