next-json-api
TypeScript icon, indicating that this package has built-in type declarations

2.2.0 • Public • Published

next-json-api

small function to simplify nextjs api routes

import { JsonHandler, ApiError } 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 = Awaited<ReturnType<typeof handler>>;

/next-json-api/

    Package Sidebar

    Install

    npm i next-json-api

    Weekly Downloads

    0

    Version

    2.2.0

    License

    MIT

    Unpacked Size

    52.3 kB

    Total Files

    16

    Last publish

    Collaborators

    • nwylynko