Skip to content

Repository files navigation

hono-sweet-sender

A lightweight Hono middleware for convenient content-type based response sending.

Install

pnpm add hono-sweet-sender

This package has hono as a peer dependency, so make sure you also have it installed:

pnpm add hono

Usage

Basic

import { Hono } from 'hono'
import { sweetSender } from 'hono-sweet-sender'

const app = new Hono()

app.use(sweetSender)

app.get('/script.js', (c) => {
  return c.js('console.log("hello")')
})

app.get('/custom', (c) => {
  return c.contentType('text/plain').send('hello world')
})

TypeScript Support

The middleware extends Hono's Context type automatically when imported. For the best IDE experience, you can also create a type declaration file in your project:

// types/hono.d.ts or any .d.ts file
import type { ContentTypeSender } from 'hono-sweet-sender'

declare module 'hono' {
  interface Context {
    contentType: (type: string) => ContentTypeSender
    js: (body: string) => Response
  }
}

API

c.contentType(type: string)

Creates a sender for a specific Content-Type.

Returns an object with a send() method:

send(body: BodyInit | string, status?: number, headers?: Record<string, string>): Response

c.js(body: string)

Shorthand for serving JavaScript content with Content-Type: application/javascript.

License

MIT

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages