Skip to content

RPC를 위한 라우트 그룹화

여러 app에 대해 타입 추론을 올바르게 활성화하려면 app.route()를 다음과 같이 사용한다.

app.get()이나 app.post() 같은 메서드에서 반환된 값을 app.route()의 두 번째 인자로 전달한다.

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

const authorsApp = new Hono()
  .get('/', (c) => c.json({ result: '작가 목록 조회' }))
  .post('/', (c) => c.json({ result: '작가 생성' }, 201))
  .get('/:id', (c) => c.json({ result: `${c.req.param('id')} 조회` }))

const booksApp = new Hono()
  .get('/', (c) => c.json({ result: '책 목록 조회' }))
  .post('/', (c) => c.json({ result: '책 생성' }, 201))
  .get('/:id', (c) => c.json({ result: `${c.req.param('id')} 조회` }))

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

type AppType = typeof app

참고 자료

Released under the MIT License.