Create Endpoint Factory

For creating NextJS API routes easily

A utility for creating API route handlers for NextJS apps using the pages router.

import { createEndpointFactory } from "next-create-endpoint-factory";

export const createEndpoint = createEndpointFactory();
import { createEndpoint } from "./api";

const endpoint = createEndpoint({
  methods: (method) => ({
    get: method<string>({
      handler: () => {
        return "foo";
      },
    }),
    post: method<void>({
      handler: ({ body }, { failWithCode }) => {
        console.log(body);
        if (!body) {
          throw failWithCode(400, "no body");
        }
      },
    }),
  }),
});

export default endpoint.handler;
Back to all packages