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

18h

v3.1.3

Published

A Next.js style dynamic API router for Koa-based APIs.

Downloads

291

Readme

npm install 18h
mkdir routes
import { join } from "path";
import { router } from "18h";

router({
  routesFolder: join(__dirname, "routes"),
  port: 8000,
  hostname: "localhost",
});
const logRequest = async (context, next) => {
  console.log(context);
  await next();
};

router({
  // ...
  middleware: [logRequest],
});

Example

Assuming you provided a folder called routes as the routesFolder when creating your router object, creating a file at routes/index.ts it will allow consumers to interact with that endpoint at the http://localhost/ URL.

Creating a file called routes/example.ts will allow consumers to interact with that endpoint at the http://localhost/example URL.

Creating a file called routes/example/index.ts will produce the same result as mentioned above.

Note

Placing square brackets [] around the entire name of a folder or file in the routes folder will allow for route parameters to be accepted through that endpoint.

/a/[b]/c would become the endpoint path /a/:b/c.

The following file structure would generate the corresponding API endpoint structure.

package.json
package-lock.json
node_modules/
src/
├── index.ts
└── routes/
    ├── index.ts
    ├── feed/
    │   └── index.ts
    ├── user/
    │   ├── delete.ts
    │   ├── index.ts
    │   └── settings/
    │       ├── private.ts
    │       └── name.ts
    ├── users/
    │   └── [userId]/
    │       ├── block.ts
    │       ├── index.ts
    │       └── follow.ts
    └── posts/
        ├── create.ts
        ├── delete.ts
        ├── index.ts
        ├── like.ts
        └── share.ts
tsconfig.json
/
/feed
/user/
/user/delete
/user/settings
/user/settings/private
/user/settings/name
/users/:userId
/users/:userId/block
/users/:userId/follow
/posts
/posts/create
/posts/delete
/posts/like
/posts/share
// src/routes/users/[userId]/block

import { route, method, validation } from "18h";

export default route<{ userId: string }>({
  get: method({
    /** If you are accepting a body, you must
     * define whether it can be `"form"`,
     * `"json"`, or both. */
    // accepts: ["json", "form"],
    /** Validation, request, and response schema
     * definition is done in one swoop. Uses "zod"
     * library under the hood. */
    schema: {
      request: validation.null(),
      response: validation.object({
        userId: validation.string(),
      }),
    },
    /** Optional middleware, `pre` will occur
     * before the hanler, while `post` will happen
     * after. */
    middleware: {
      pre: [],
      post: [],
    },
    async handler(context) {
      console.log(context.params.userId); // :userId sourced from URL.
      console.log(context.request.body); // null

      return {
        status: 200,
        headers: {
          "x-custom-header": "true",
        },
        body: {
          userId: "some_id",
        },
      };
    },
  }),
});

We can create a simple endpoint that just responds with the node package version of the current project we're in. The endpoint will work on all HTTP methods, not just GET, but we could change it to do that by changing all occurances of all to get.

import { route, method, validation } from "18h";
const { npm_package_version: version } = process.env;

export default route({
  all: method({
    schema: {
      request: validation.null(),
      response: validation.object({
        version: validation.string().optional(),
      }),
    },
    async handler() {
      return { body: { version } };
    },
  }),
});