A lightweight Hono middleware for convenient content-type based response sending.
pnpm add hono-sweet-senderThis package has hono as a peer dependency, so make sure you also have it installed:
pnpm add honoimport { 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')
})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
}
}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>): ResponseShorthand for serving JavaScript content with Content-Type: application/javascript.
MIT