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 🙏

© 2026 – Pkg Stats / Ryan Hefner

nextjs-app-router-handler

v0.0.2

Published

This is a simple route handler implementation for Next.js

Readme

NextJs App Router Handler

DON'T USE THIS PACKAGE IN PRODUCTION. THIS PACKAGE IS IN DEVELOPMENT.

This is a simple route handler implementation for handling POST, GET, PATCH, PUT, and DELETE requests related to posts.

Installation

npm install nextjs-app-router-handler

Usage

Before using this handler, create handler function.

// lib/createHandler.ts
import { Handler } from 'nextjs-app-router-handler';

export function createHandler() {
  const handler = new Handler();
  return handler
    .useBeforeHandlers(context => {
      const now = Date.now();
      context.set('now', now);
    })
    .useAfterHandlers(context => {
      const now = context.get<number>('now')!;
      const after = Date.now();
      console.log(`Time: ${after - now}ms`);
    });
}

We created this function because we can add middlewares, guards, and other things to the handler. We can use this function in all requests.

GET request example


// app/api/route.ts

import { createHandler } from '@/lib/createHandler';
const GET = createHandler()
  .useMiddlewares(context => {
    const id = context.searchParams().get('id');
    if (id && Number(id) === 5) {
      return NextResponse.json({
        message: 'Post not found',
      });
    }
  })
  .handle(context => {
    // context.req is NextRequest. You can access request with this.
    
    return {
      message: 'Hello World',
    }
  });

export { GET };

We created a GET request handler. Now, we can open http://localhost:3000/api and see the result. If we open http://localhost:3000/api?id=5, we will see the result of the middleware.

POST request example


import { createHandler } from '@/lib/createHandler';
import { BadRequestException, HttpStatusCode } from 'nextjs-app-router-handler';

const POST = createHandler()
  .usePipes(async context => {
    // We can validate body data with pipes.

    // we can access request with context.req
    const json = await context.req.json();

    // We can check if the JSON is valid
    if (json && !json.name) {
      // The handle method will not be called if we throw an exception.
      throw new BadRequestException('name is required');
    }

    // We can set body data with the setBody method. We will use it in the handle method.
    context.setBody(json);
  })
  .status(HttpStatusCode.CREATED)
  .handle(context => {

    // We can get body data with the getBody method.
    const body = context.getBody<{
      name: string;
    }>()!;

    return {
      name: body.name,
    };
  });

export { POST };

We created a POST request handler. Now, we can open http://localhost:3000/api and see the result. If we open http://localhost:3000/api with POST method and send this JSON data:

{
  "name": "John Doe"
}

We will see the result of the handler. The response status will be 201 because we set it with the status method. If we send the following JSON data:

{
}

We will see a bad request error because we threw an exception in the pipe method.