npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nextjs-api-router

v1.0.0

Published

Lightweight tool to build clean restful API routes for your Next.js application.

Downloads

18,864

Readme

nextjs-api-router

Lightweight router class to build spotless (restful) API route handlers for your Next.js server application.

Installation

# npm
$ npm install next-js-api-router

# yarn
$ yarn add nextjs-api-router

Usage

First create a controller object that contains all handlers for the HTTP methods you want to handle on your specific route.

// API controller: controllers/example/index.ts

import type { Controller } from "nextjs-api-router";

const controller: Controller = {
  GET: (req, res) => {
    res.send("Hello world from nextjs-api-router!");
  },
  POST: (req, res) => {
    res.send(req.body);
  },
};

export default controller;

Import the controller in a new router class in the API handler file in /pages/api/%your-route%. Export the router.handle function by default to make your API work.

// API handler: pages/api/example.ts

import { Router } from "nextjs-api-router";
import controller from "../controllers/example";

const router = new Router(controller);
export default router.handle();

API

Router

The Router class manages the controller object inside the class itself. By instantiating it can optionally be given a controller object in the constructor.

The controller can be modifiied all time with the router.controller property of the class. For example:

router.controller.GET = (req, res) => {
  res.send("");
};

The router.handle function returns a function that handles the configured HTTP method handlers when sending a request to the server.

By passing handlers as arguments to the router.use method It's also possible to create middleware functions on the Router class. This middleware handler will be executed on every request to the route.

const router = new Router(controller);

const logger = (req, res, next) => {
  console.log(req.method, Date.now()
  next();
};

router.use(logger);

Controller

A controller object can be passed to the Router class constructor. It contains all the handlers for the HTTP methods used in the router. These handlers can be assigned singular or as an array (like middleware).

import type { Controller, Handler } from "nextjs-api-router";

const verify: Handler = (req, res, next) => {
    // do some verification here
    next();
}

const controller: Controller {
  // Just single handler functions
  GET: (req, res) => {res.send("Hello world!")},
  DELETE: (req, res) => {res.send("Goodbye world...")},

  // Multiple handlers
  POST: [verify, (req, res) => {
    res.send("You're verified now!")
  }],
}

Handler

The handler function is a function called inside the router. It has access to the request and the response of the server, and can be used as middleware, using the next parameter, to execute the next handler in series.

import { Router } from 'nextjs-api-router';
import type { Controller, Handler } from "nextjs-api-router";


const logger: Handler = (req, res, next) => {
  console.log(req.method, Date.now()
  next();
};

const reply: Handler = (req, res, next) => {
  res.send("You got this!")
};

const controller: Controller {
    GET: [logger, reply]
}

const router = new Router();

// Controller can also be passed in as parameter of the handle function
export default router.handle(controller);