@arisris/next-api-router
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

Next.js Api Router

A chainable router designed for Next.js api. inspired and regex based from itty-router

Features

  • [x] Tiny (~8xx bytes compressed) with zero dependencies.
  • [x] Strong typing with typescript also javascript with VSCode
  • [x] Anything is async/await handler
  • [x] Route params, with wildcards and optionals (e.g. /base/:collection/:id?)
  • [x] Middleware support
  • [x] By default error handler response is json
  • [x] Support anything of HTTP methods
  • [x] Chainable route declarations
  • [x] No dependencies

Installation

npm i @arisris/next-api-router

Example Usage

// file: pages/api/[...any].ts
import NextApiRoute from "@arisris/next-api-router";

export default NextApiRoute({
  key: "all", // a rest params default to "any"
  timeout: 5000, // timeout in ms default to 10000ms
  //onError: (error, req, res) => res.status(500).send("Internal Server Error") // By default handled with json response
})
  .all("*", async () => true) // middleware should return true
  .get("/hello", async (_, res) => res.send("Hello World")) // simple
  .get(
    "/wait",
    async () => await new Promise((resolve) => setTimeout(resolve, 3000)), // wait for 3000ms
    async (_, res) => res.json({ msg: "Done..." })
  )
  .get("/error", async () => {
    throw new Error("OMG!");
  })
  .get("/timeout", async () => {
    // No response is considered timeout
  })
  .get("/user/:name?", async (req, res) => res.json({ params: req.params }))
  // not found
  .all("*", async (_, res) => res.status(404).send("Not Found"))
  .handle; // Final handler of NextApiHandler

Todo better documentation

--

Links

My Website itty-router

Package Sidebar

Install

npm i @arisris/next-api-router

Weekly Downloads

1

Version

1.0.8

License

MIT

Unpacked Size

30.6 kB

Total Files

9

Last publish

Collaborators

  • arisris