New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

nextjs-api-common-middleware

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nextjs-api-common-middleware

A collection of commonly used Next.js API middleware patterns

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

nextjs-api-common-middleware

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'],
	},
});

Keywords

next

FAQs

Package last updated on 16 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts