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

@johno/router

v0.0.3

Published

A miniaturized (and fast af) router for the edge, and everywhere else.

Downloads

8

Readme

router

A miniaturized (and fast af) router for the edge, and everywhere else.

import { Router, text, json } from "@johno/router";

const router = new Router();

router
  .get("/", (req) => text("Hello world!"))
  .get("/hello/:name", (req, env, ctx, routeInfo) => {
    json({ name: routeInfo.params.name });
  });

router.handle(request, env, ctx);

Features

  • Simple, declarative API
  • Built for the edge
  • Standards-based (Request/Response/URL)
  • Zero dependencies
  • Tiny footprint
  • ESM-only
  • Written in TypeScript

Motivation

@johno/router is built for edge environments including Cloudflare Workers, Lambda, Netlify Functions, Next.js, etc. What makes @johno/router interesting is what it doesn't do.

@johno/router does not:

  • Monkey patch the global Request and Response objects
  • Route match with regex, it uses a custom parser
  • Implement a bespoke handler API
  • Use middleware, we recommend using lib functions
  • Minify or obfuscate code (readable and debuggable output)

Handler API

@johno/router uses a simple handler API that is compatible with the Request and Response objects. It augments edge function handlers with an additional argument, routeInfo. This means you can drop in @johno/router to any serverless function environment and you will work with their existing APIs.

Installation

Use your favorite package manager to install @johno/router:

npm install @johno/router

# pnpm add @johno/router
# yarn add @johno/router
# bun add @johno/router

Usage

If you've used a router before, @johno/router will feel familiar. To define a set of routes you:

  • Instantiate a router
  • Add routes for given paths and HTTP methods
  • Pass each route a handler function
  • Call handle with the request and its environment
import { Router, text } from "@johno/router";

const router = new Router();

router.get("/", (req, res) => {
  text("Hello world!");
});

router.get("/hello/:name", (req, res, routeInfo) => {
  text(`Hello ${routeInfo.params.name}!`);
});

router.handle(request, env, ctx);

Using with Cloudflare Workers

@johno/router is designed to work seamlessly with Cloudflare Workers. The handle method accepts the Request, Env, and Context objects which you can leverage in your handlers.

import { Router, text, json } from "@johno/router";

const router = new Router();

export default {
  async fetch(
    request: Request,
    env: Env,
    ctx: ExecutionContext
  ): Promise<Response> {
    return router
      .post("/hello", () => text("Hello world!"))
      .post("/world", () => json({ hello: "world" }))
      .handle(request, env, ctx);
  },
};

Code of Conduct

By participating in our community and interacting with this repository, you agree to abide by our code of conduct.

Development

To learn about how to contribute to this project, please read the contributing guide.

License

MIT


Built by johno