https://github.com/justpowerful/hono-autoload
A package to autoload your hono routes and middleware easier 🔥✨
https://github.com/justpowerful/hono-autoload
auto-load autoloader bun hono honojs nodejs typescript
Last synced: 6 months ago
JSON representation
A package to autoload your hono routes and middleware easier 🔥✨
- Host: GitHub
- URL: https://github.com/justpowerful/hono-autoload
- Owner: JustPowerful
- License: mit
- Created: 2024-10-20T15:49:43.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-23T08:04:47.000Z (about 1 year ago)
- Last Synced: 2025-04-29T11:57:21.456Z (6 months ago)
- Topics: auto-load, autoloader, bun, hono, honojs, nodejs, typescript
- Language: TypeScript
- Homepage:
- Size: 153 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hono-autoload
An auto-loader for hono to simplify the routing process, and to void boilerplate code.
## Installation
for npm:
```bash
npm i hono-autoload
```
for bun:
```bash
bun i hono-autoload
```
## Usage
```ts
// index.ts
import { Hono } from 'hono';
import { createAutoloader, createAutoloaderMiddleware } from 'hono-autoload';
import { join } from 'path';
const app = new Hono();
async function runLoader() {
// NOTE: put your middleware loader before the route loader
await createAutoloaderMiddleware(app, join(__dirname, 'middleware'));
await createAutoloader(app, join(__dirname, 'routes'));
}
runLoader().then(() => {
console.log('Loaded all routes and middleware');
});
// ... listen for your server here
```
## Creating a route module
create a `routes` directory inside your project and put your route modules in there.
A route module should export a `path` and `handler` property like this:
```ts
import type { AutoLoadRoute } from "hono-autoload/types";
const routeModule: AutoLoadRoute = {
path: "/api",
handler: app,
};
export default routeModule; // don't forget to export default the route module
```
## Creating a middleware module
create a `middleware` directory inside your project and put your middleware modules in there.
A middleware module should export a `handler` and `matcher` property like this:
```ts
import type { AutoLoadMiddleware } from "hono-autoload/types";
const middlewareModule: AutoLoadMiddleware = {
handler: app,
matcher: "/api", // NOTE: not defining a matcher means the middleware works on all routes
};
export default middlewareModule; // don't forget to export default the middleware module
```
## License
MIT
## Contributing
You can contribute to this project on [GitHub](https://github.com/honojs/hono-autoload).
Contact me if you have any questions or suggestions.
E-mail: ahmedaminedoudech@gmail.com