0.0.3 • Published 1 year ago

@hono/client v0.0.3

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

Hono Client

Version Bundle Size Bundle Size

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

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

const client = new Client<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/client

Or

yarn add @hono/client

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