https://github.com/lazuee/hono-emitter
Event emitter-based route handler for HonoJS.
https://github.com/lazuee/hono-emitter
emitter event-emitter hono route-handler
Last synced: 11 months ago
JSON representation
Event emitter-based route handler for HonoJS.
- Host: GitHub
- URL: https://github.com/lazuee/hono-emitter
- Owner: lazuee
- License: mit
- Created: 2025-06-07T10:11:33.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-09T09:05:56.000Z (about 1 year ago)
- Last Synced: 2025-07-20T23:19:47.169Z (11 months ago)
- Topics: emitter, event-emitter, hono, route-handler
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@lazuee/hono-emitter
- Size: 63.5 KB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
## hono-emitter
> `hono-emitter` is an Event emitter-based route handler for HonoJS.
### Installation
```bash
pnpm add -D @lazuee/hono-emitter
```
### Usage
```ts
import { argv } from "node:process";
import { fileURLToPath } from "node:url";
import { serve } from "@hono/node-server";
import { HonoEmitter } from "@lazuee/hono-emitter";
import { Hono } from "hono";
const isCLI = fileURLToPath(import.meta.url) === argv[1];
const app = new Hono().basePath("/api").get("/ping", (ctx) => ctx.text("pong"));
export const emitter = new HonoEmitter(app, "http://localhost:3000")
.on("get:/world", (ctx) => {
console.log("hello world");
return ctx.text("hello world");
})
.once("get:/world", async (_ctx, next) => {
console.log("middleware start");
await next();
console.log("middleware end");
});
emitter.on("get:/hello", (ctx) => ctx.text("hello")); // not included in typings (not chained)
if (isCLI) {
// If running directly, start the server
serve({ ...app, port: 3000 }, () => {
console.log("server is running...");
});
}
```
#### Client types
```ts
import { hc } from "hono/client";
import { emitter } from ".";
const log =
(name: S) =>
>(x: T) =>
x.text().then((y: string) => console.log(`${name}:`, y));
const client = hc("http://localhost:3000");
await client.api.ping.$get().then(log("client[/api/ping]"));
await client.api.world.$get().then(log("client[/api/world]"));
//@ts-expect-error - not available in types (not chained)
await client.api.hello.$get().then(log("client[/api/hello]"));
await emitter.emit("get:/api/ping").then(log("emitter[/api/ping]"));
await emitter.emit("get:/api/world").then(log("emitter[/api/world]"));
//@ts-expect-error - not available in types (not chained)
await emitter.emit("get:/api/hello").then(log("emitter[/api/hello]"));
```
For a usage example, check the [app/](https://github.com/lazuee/hono-emitter/tree/main/app) directory.
## License
This project is licensed under the MIT License - see the [LICENSE.md](https://github.com/lazuee/hono-emitter/blob/main/LICENSE.md) file for details
Copyright © `2025` `lazuee`