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

next-route-handler-pipe

v2.0.0

Published

Pipe together functions to create reusable function pipelines for your Next.js route handlers.

Downloads

1,686

Readme

⚠️ This package is for projects using Next.js App Router. For projects using Pages Router see next-api-route-middleware.

Introduction

Piping functions allows us to abstract reusable code that runs before the route handler is invoked.

Example uses:

  • ✅ Validate body/query data
  • 🔒 Verifying signatures
  • 🪝 Catch errors
  • ✨ Add data to the req object

Getting started

1. Install'r

npm i next-route-handler-pipe

2. Create a pipe function

A pipe function is a function that accepts req, event, and next. It should return await next() when done, or return a NextResponse.

// PipeFunction accepts a generic when mutating req
export const withUser: PipeFunction<{ userId: string }> = async (req, event, next) => {
  const { userId } = await validateUserRequest();

  if (userId) {
    req.userId = userId;
    return await next();
  } else {
    return NextResponse.json({ message: 'Invalid auth token.' }, { status: 401 });
  }
};

3. Create a pipe

Import the pipe function and pass it pipe functions followed by a handler. If a pipe function adds data to the req you can compose an intersection type with NextRequest & the added data.

import { pipe } from 'next-route-handler-pipe';

async function handler(req: NextRequest & { userId: string }) {
  console.log(req.userId);
}

export const POST = pipe(withUser, handler);

Examples

validateBody

Combine with Zod to create an awesome validation pattern.

// validate-body.ts

// This is a pipe function that makes use of the factory pattern, meaning it returns a pipe function.
// This allows us to pass args into our pipe, like a zod schema.

export const validateBody = (zodSchema: z.ZodSchema): PipeFunction<{ data: any }> => {
  return async function (req, event, next) {
    // Get the body of the request and parse with Zod
    const body = await req.json();
    const validation = zodSchema.safeParse(typeof body === 'object' ? body : JSON.parse(body));

    // If validation fails we return early, preventing the handler from running.
    if (!validation.success) {
      console.error('Validation error from validateBody', validation.error);
      return NextResponse.json({ message: 'Validation error' }, { status: 400 });
    }
    // If validation passes we can add the parsed data to the req for easy access in our handler.
    else {
      req.data = validation.data;
      // Always return await next() in your pipe function.
      return await next();
    }
  };
};
// route.ts

// Create a Zod schema
const postSchema = z
  .object({
    postId: z.string(),
    postTitle: z.string(),
    authorName: z.string(),
  })

  // We can even add a Zod transform if we need
  .transform(({ postId, postTitle, authorName }) => ({
    post_id: postId,
    post_title: postTitle,
    author_first_name: authorName.split(' ')[0],
    author_last_name: authorName.split(' ')[1],
  }));

// Infer type from schema
type Post = z.infer<typeof postSchema>;

// Add intersection with `NextRequest` & your inferred type
async function handler(req: NextRequest & { data: Post }) {
  // We've validated that this data will always exist at this point
  const { post_id, post_title } = req.data;
}

// Notice we're calling the validateBody function - that's because it's a factory that will return a pipe function
export const POST = pipe(validateBody(postSchema), handler);

catchErrors

We can also perform actions with inner pipe functions. In this example we wrap the inner pipe functions in a try catch, allowing us to catch any errors that bubble up.

export const catchErrors: PipeFunction = async (req, event, next) => {
  try {
    return await next();
  } catch (error) {
    console.error(error);
    return NextResponse.json({ message: 'Server error!' }, { status: 500 });
  }
};