Macro

Reduce duplication in your registry files.

Macros are small compile-time modules that are transformed out when being installed.

This is useful for writing a component file once, and adopt to consumer's framework once installed.

Default Macros

Fuma CLI comes with a few pre-built macros.

$routeHandler()

Framework-agnostic HTTP request handler.

src/post-route.ts
import { $routeHandler } from "fuma-cli/macros/route-handler";

export const handler = $routeHandler(
  { methods: ["GET", "POST"], params: ["id"] },
  async (req, params) => {
    return Response.json({ id: params.id });
  },
);

The first paremeter must be analyzable as literals in the AST.

OptionDescription
methodsNon-empty array of HTTP method strings (e.g. "GET", "POST").
paramsA list of path parameters.
catchAllName for the catch-all / splat parameter.

Reserved Variables

Avoid naming the parameters args or ctx.

Add the route file to registry definition:

const registry = {
  components: [
    {
      name: "my-component",
      files: [
        {
          type: "route-handler",
          route: "api/posts/[id]", // see below for correct format
          path: "post-route.ts",
        },
      ],
    },
  ],
};

The route field defines the actual route to handle requests, its format is same as Next.js App Router (without the extra route.ts).

Route FormatExampleDescription
Static pathapi/postsMatches requests to /api/posts
Dynamic parameterapi/posts/[id]Matches requests to /api/posts/123, exposes params.id
Multiple parametersapi/posts/[postId]/[commentId]Matches /api/posts/1/2, exposes params.postId and params.commentId
Catch-all (splat) segmentapi/posts/[...slug]Matches /api/posts/a/b/c, exposes params.slug as array
Optional catch-all segmentapi/posts/[[...slug]]Matches /api/posts or /api/posts/a/b, exposes params.slug

On this page