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

@weyvern/next-router

v1.3.0

Published

Utility library to create a router object to help exporting HTTP verb methods on NextJS 14 within the app router

Downloads

3

Readme

NextRouter

A small utility to create an Express-like router object that allows you to pipe middlewares and router handlers for api routes in the app directory Next.JS version 14.1

Context

As to Next.JS version 14.1, you need to generate a route.ts or route.js file and export a method named after the HTTP verbs you want to support:

// In route.ts file

export const GET: NextCustomMiddleware = () => {};
export const PUT: NextCustomMiddleware = () => {};
export const DELETE: NextCustomMiddleware = () => {};

// or

const GET: NextCustomMiddleware = () => {};
const PUT: NextCustomMiddleware = () => {};
const DELETE: NextCustomMiddleware = () => {};

export { GET, PUT, DELETE };

Since we need to run the same logic across different routes and methods, we wanted something that would resemble the Router object in ExpressJS.

Usage

It’s a custom class that declares use, get, put, post and delete methods that take a list of middlewares / route handlers (NextCustomMiddleware) and allows us to create an Express-like router:

// In route.ts file
import NextRouter from '@weyvern/next-router';
import type {
  NextCustomMiddleware,
  NextHandler,
  NextRequest,
  NextRouteParams
} from '@weyvern/next-router/dist/types';
const router = new NextRouter();

router.use(async (request: NextRequest, routes: NextRouteParams, next: NextHandler) => {
  next();
}); // Middleware that runs on all methods

router
  .get(
    async (request: NextRequest, routes: NextRouteParams, next: NextHandler) => {
      // Another middleware
      next();
    },
    async (request: NextRequest, routes: NextRouteParams) => {
      // you can user request params from routes.params
      Response.json({}, { status: 200, statusText: 'Success' });
    }
  )
  .put(async (request: NextRequest) => {
    Response.json({}, { status: 200, statusText: 'Updated' });
  })
  .delete(async (request: NextRequest) => {
    Response.json({}, { status: 200, statusText: 'Deleted' });
  }); // Every router method returns `this`, that's why it's chainable

export const { GET, POST, DELETE } = router.handlers();

Internally, all methods exposed by the class passed the route handlers to an utility called nextAPIHandler that's exported by name from the package, i.e. can be imported like:

import { nextAPIHandler } from '@weyvern/next-router';

It takes NextCustomMiddleware[] as parameter and enables us to use next() within our middlewares! The way it works is quite simple, we iterate over the middlewares / route handlers passed and declare a nextInvoked variable set to false , then simply pass a next function that can update this value because of the power of ✨closures✨

  • ⚠️ Please be mindful that NextJS router handlers are NOT Express router handlers:
    • The first parameters is of type NextRequest (exported from next/server)
    • The second parameter is where you can get route params.
    • The third parameter is the custom next function passed by the nextAPIhandler
    • You must return a Response explicitly, this is the native object https://nextjs.org/docs/app/building-your-application/routing/route-handlers#extended-nextrequest-and-nextresponse-apis
    • NextJS caches GET method that DO NOT use the request object!