Skip to content
/ 18h Public

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

License

Notifications You must be signed in to change notification settings

ridafkih/18h

Repository files navigation

18h

18h is a wrapper for Koa which allows you to create Next.js-style, serverful dynamic API routing sourced from a directory of your choosing.

Installation

npm i 18h

Usage

/** /src/index.ts */

createRouter({
  routesFolder: join(__dirname, "routes"),
  port: 4000,
});
/** /src/routes/users/[userId]/delete.ts */

import { Route } from "18h";
import { deleteUserById } from "@/actions/users";
import { postRequestCleanupAction } from "@/example/ambiguous";

const handler: Route<{}, { success: boolean }, { userId: string }> = {
  async handler(context) {
    const success = await deleteUserById(context.params.userId);

    return {
      headers: {
        "X-Custom-Header": "true",
      },
      body: { success },
      code: success ? 200 : 500,
    };
  },
  middleware: {
    post: [postRequestCleanupAction],
  },
};

export default handler;

Example Consumption Based on Example Above

import axios from "axios";

(async () => {
  const success = await axios
    .post("http://localhost:4000/users/123/delete")
    .then(({ data }) => data.success);

  console.log(success); // true;
})();

More details & documentation coming soon! πŸ‘Š