NWylynko / next-json-api

small function to simplify nextjs api routes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

next-json-api

small function to simplify nextjs api routes

import { JsonHandler, ApiError, type GetResponse } from "next-json-api";

// wrap your handler in JsonHandler
const handler = JsonHandler(async (req, res) => {
  if (Math.random() > 0.5) {

    // throw ApiError with the status code and message
    throw new ApiError(500, "Something went wrong");
  }

  // return the response
  return {
    hello: "world",
  };
});

export default handler;

// import this client side to get the same type
export type Response = GetResponse<typeof handler>;

About

small function to simplify nextjs api routes

License:MIT License


Languages

Language:TypeScript 100.0%