npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hono-router

v1.0.4

Published

hotloading script to generate file-based routing config for Hono

Downloads

43

Readme

hono-router

hono-router is a script that generates a file-based router for Hono, a small, simple, and ultrafast web framework for the Edges. It automatically creates routes based on your file structure and exports, making it easier to organize and maintain your Hono application.

Features

  • Automatically generates routes based on your file structure
  • Supports dynamic routes (e.g., [id].ts becomes :id in the route)
  • Allows co-location of component files with routes
  • Watches for file changes and regenerates the router
  • Sorts routes to prioritize static paths over dynamic paths
  • Supports multiple HTTP methods (GET, PUT, POST, DELETE, PATCH)
  • Can be run with Bun for fast execution

Installation

npm install hono-router

Usage

  1. Create a directory structure for your routes and components. For example:
src/
  routes/
    index.ts
    users/
      index.ts
      [id].ts
      UserList.tsx
    posts/
      index.ts
      [id].ts
      PostEditor.tsx
  1. In your route files, export functions for the HTTP methods you want to handle. For example, in src/routes/users/[id].ts:
import { Context } from 'hono';

export const onRequestGet = (c: Context) => {
	const id = c.req.param('id');
	return c.json({ message: `Get user ${id}` });
};

export const onRequestPut = (c: Context) => {
	const id = c.req.param('id');
	return c.json({ message: `Update user ${id}` });
};
  1. Run the router generator script:
bunx hono-router src/routes router.ts

or

npx hono-router src/routes router.ts

This will generate a router.ts file with all your routes.

  1. In your main Hono app file, import and use the generated router:
import { Hono } from 'hono';
import { loadRoutes } from './router';

const app = new Hono();
loadRoutes(app);

export default app;

Component Integration

hono-router allows you to co-locate component files with your routes. Any TypeScript or TypeScript JSX (.tsx) files that start with a capital letter are ignored by the router generation process. This enables you to keep your components close to the routes that use them without affecting the routing logic.

For example:

  • src/routes/users/UserList.tsx will be ignored by the router
  • src/routes/users/index.ts will be processed for route generation

This feature helps in maintaining a clean and organized project structure where components and their associated routes are kept together.

Watch Mode

The script automatically watches for changes in your routes directory and regenerates the router file when changes are detected.

Supported HTTP Methods

The generator supports the following HTTP methods:

  • GET
  • PUT
  • POST
  • DELETE
  • PATCH

To use these methods, export functions with the corresponding names in your route files:

  • onRequestGet
  • onRequestPut
  • onRequestPost
  • onRequestDelete
  • onRequestPatch

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the ISC License.