fatline

Hono

Middleware for Hono and edge runtimes

Hono is a fast web framework that runs on Cloudflare Workers, Deno, Bun, and Node.js. fatline provides middleware that adds a c.fatline() helper for serialized responses.

Setup

import { Hono } from 'hono'
import { fatlineMiddleware } from 'fatline/hono'

const app = new Hono()

app.use('*', fatlineMiddleware())

app.get('/user', (c) => {
  return c.fatline({
    user: {
      name: 'Alice',
      createdAt: new Date(),
      roles: new Set(['admin']),
    }
  })
})

The c.fatline() helper serializes your data and sets Content-Type: application/json.

Client-Side

import { deserialize } from 'fatline'

const response = await fetch('/user')
const json = await response.text()
const data = deserialize(json)

data.user.createdAt instanceof Date  // true
data.user.roles instanceof Set       // true

With Hono RPC

Hono's RPC client expects standard JSON. You'll need to handle serialization manually:

// Server
app.get('/user', (c) => {
  return c.fatline({ ... })
})

// Client
import { hc } from 'hono/client'
import { deserialize } from 'fatline'

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

// For fatline endpoints, use raw fetch:
const res = await fetch('/user')
const user = deserialize(await res.text())

For full type-safe RPC with automatic serialization, consider tRPC or oRPC.

Edge Runtimes

fatline works in all environments Hono supports:

Cloudflare Workers:

// wrangler.toml + src/index.ts
import { Hono } from 'hono'
import { fatlineMiddleware } from 'fatline/hono'

const app = new Hono()
app.use('*', fatlineMiddleware())

export default app

Deno:

import { Hono } from 'npm:hono'
import { fatlineMiddleware } from 'npm:fatline/hono'

const app = new Hono()
app.use('*', fatlineMiddleware())

Deno.serve(app.fetch)

Bun:

import { Hono } from 'hono'
import { fatlineMiddleware } from 'fatline/hono'

const app = new Hono()
app.use('*', fatlineMiddleware())

export default app

Middleware Options

fatlineMiddleware({
  // Custom content type (default: 'application/json')
  contentType: 'application/json',
})

TypeScript

Extend Hono's context type to get autocomplete for c.fatline():

import { Hono, Context } from 'hono'
import { fatlineMiddleware } from 'fatline/hono'

// Extend context type
declare module 'hono' {
  interface ContextVariableMap {
    fatline: <T>(data: T) => Response
  }
}

const app = new Hono()
app.use('*', fatlineMiddleware())

app.get('/user', (c) => {
  return c.var.fatline({ ... })  // ← typed
})

Next Steps

On this page