0.0.6 • Published 1 year ago

@hono/hc v0.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

hc - TypeSafe HTTP Client for Hono

Version Bundle Size Bundle Size

hc is HTTP Client based on Fetch API. It's Type-Safe. It's for Hono. You can access to the Hono application safety and ultra-easily.

import { hc } from '@hono/hc'
import type { AppType } from './server'

const client = hc<AppType>('http://localhost:8787/api')

const res = await client.post('/posts').json({
  id: 123,
  title: 'Hello Hono!',
  published: true,
})

const data = await res.json()
console.log(`${data.message}`)

Features

  • Small about 1.5kb
  • TypeSafe
  • Compatible with Fetch API
  • Optimized for Hono v3.x

Demo

Demo

Install

npm i @hono/hc

Or

yarn add @hono/hc

Examples

Server-side with Zod.

import { Hono } from 'hono'
import { validator } from 'hono/validator'
import { z } from 'zod'

const api = new Hono()

const schema = z.object({
  id: z.number(),
  title: z.string(),
  published: z.boolean(),
})

const route = api
  .post(
    '/posts',
    validator('json', (value, c) => {
      const result = schema.safeParse(value)
      if (!result.success) {
        return c.text('Invalid!', 400)
      }
      return result.data
    }),
    (c) => {
      const { title, published } = c.req.valid()
      return c.jsonT({
        success: true,
        message: `"${title}" is ${published ? 'published' : 'not published'}`,
      })
    }
  )
  .build()

export type AppType = typeof route

Author

Yusuke Wada https://github.com/yusukebe

License

MIT