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-file-router

v0.0.5

Published

File-based router for easier route management for hono.

Downloads

17

Readme

Hono File Router 🔥

File-based router for easier route management for hono. Heavily inspired by SvelteKit's file-based routing. Future features of this router will be based on how SvelteKit handles files.

Install

Installation is simple. You may install it using any of the popular package managers out there that can use npmjs.

npm install hono-file-router

How It Works

Supposed you have a file structure that looks like this:

./
├- src/
│  ├- api/
│  │  ├- tasks/
│  │  │  ├- [id]/
│  │  │  │  └- GET.ts
│  │  │  ├- change
│  │  │  │  └- index.ts
│  │  │  ├- GET.ts
│  │  │  └- POST.ts
│  │  └- GET.ts
│  └- app.ts
└ package.json

Inside the app.ts you may import the folder router in your hono app initialization as shown:

// app.ts
import { Hono } from 'hono';
import { createFolderRoute } from 'hono-file-router';

const app = new Hono();

app.get('/hello', (c) => {
	return c.text('Hello Hono!');
});

// path is relative to root directory
app.route('/api', await createFolderRoute({ path: './src/api' }));

//... rest of your routes

During server start, createFolderRoute scans the provided path folder and imports all of the methods found inside. In the example provided above, all POST.ts and GET.ts scripts are mounted to the hono application. All HTTP request methods are supported.

Inside the method ts files, you can create the route by importing hono's factory createHandlers. However, I suggest you use the passthrough createHandler function exported by the file router as in the future we will improve it and implement type-safe routes.

// ./src/api/GET.ts
import { createHandler } from 'hono-file-router';

export default createHandler(async (context) => {
	return context.json({ hello: 'world!' });
});

You might have noticed that we have a file ./src/api/tasks/change/index.ts. If you want to keep your methods in a single file, you can do so by exporting your methods as variables.

import { createHandler } from 'hono-file-router';

export const GET = createHandler(async (context) => {
	return context.json({ hello: 'world!' });
});

export const POST = createHandler(async (context) => {
	return context.json({ hi: 'you are using POST method' });
});

Keep in mind that methods are case sensitive and must always be in uppercase.

To Do

Features that needs to be implemented. Feel free to contribute.

Must Have Features

  • [ ] Build (import() is not build-friendly, an intermediate compile step is needed)
  • [ ] Rest parameters (/path/[...to]/file will allow /path/this/is/a/long/file route)
  • [ ] 404 error file (error.ts maybe?)
  • [ ] Optional parameters (sveltekit uses [[optional]] parameters)
  • [ ] Route sorting (which ones go first if multiple matches are found)
  • [ ] Route Matcher
  • [ ] Encoding (unusable chars in the filesystem can be used as chars in routes)
  • [ ] RegEx Matcher (we can leverage the use of encoding when that feature is done)

Nice to Have Features

  • [ ] None for now