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-common-middleware

v1.0.1

Published

A collection of commonly used Next.js API middleware patterns

Downloads

11

Readme

Docs   •   Issues   •   NPM   •   Examples (coming some time)

What this is about

This package exports some common Next.js API middleware patterns that you might need across different applications. It aims to provide useful and mostly flexible drop-in functions.

What is included

  • Authorization: Basic, Bearer (JWT), Custom
  • Route Guarding: Make sure certain fields are present in the request
  • RESTify Your Routes: A simple function allowing you to map different handlers to http methods
  • Error Catching: Wrap your handlers with a convenient error handling middleware

If you have something in mind that is generally help- or useful and is not included in this list, please feel free to open an issue.

Getting Started

Installation

Yarn

yarn add nextjs-api-common-middleware

NPM

npm install --save nextjs-api-common-middleware

Configuration

While generally not required, it is recommended that you re-export the middleware collection with your own default configuration.

Create a file called middleware.js/middleware.ts somewhere that suits you well, the contents of the file should look something like this:

import { createExport } from 'nextjs-api-common-middleware';

const m = createExport({
	catch: (_req, res, err) => {
		console.error(err);
		res.status(500).send('An unknown error occurred');
	},
	auth: {
		strategy: 'custom',
		custom: (authHeaderValue, _req) => {
			if (authHeaderValue && authHeaderValue === 'test') {
				return {
					uid: 123,
					user: {
						firstname: 'Test',
						lastname: 'User',
					},
				};
			}
			return null;
		},
	},
});

export default m;

Usage

Basic Example

// src/pages/api/hello.js
import m from '../../middleware'; // or 'nextjs-api-common-middleware'

async function handler(req, res) {
	res.json({ hello: 'world' });
}

export default m.auth(handler); // second argument could be additional options

Chaining Middleware

// src/pages/api/hello.js
import m from '../../middleware'; // or 'nextjs-api-common-middleware'

async function handler(req, res) {
	res.json({ hello: 'world' });
}

export default m._.chain([m.auth, m.guard], handler, {
	// auth options are still remembered from the initial configuration
	guard: {
		required: ['foo'],
	},
});