Socket
Socket
Sign inDemoInstall

next-api-compose

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-api-compose

Compose middleware chain in Next.js API Routes


Version published
Weekly downloads
1.2K
increased by130.56%
Maintainers
1
Weekly downloads
 
Created
Source

Next.js API Compose · version codecov CodeFactor npm bundle size

Introduction

This library provides simple yet complete higher order function
with responsibility of composing multiple middleware functions into one Next.js API route handler.

The library does not contain routing utilities. I believe mechanism built in
Next.js itself or next-connect library are sufficient solutions.

Features

  • 😇 Simple and powerful API
  • 🥷 TypeScript support
  • 🧬 Maintaining order of middleware chain
  • 🔧 Compatible with Express/Connect middleware
  • 💢 Error handling
  • 📦 No dependencies
  • 💯 100% Test coverage

Installing

npm i next-api-compose -S
# or
yarn add next-api-compose

Basic usage:

import { compose } from 'next-api-compose'

export default compose([withBar, withFoo], (request, response) => {
  const { foo, bar } = request
  response.status(200).json({ foo, bar })
})

the withBar middleware will append bar property to request object, then withFoo will do accordingly the same but with foo property

Using Express or Connect middleware

If you want to use next-api-compose along with Connect middleware that is widely used eg. in Express framework, there is special utility function for it.

import { compose, convert } from 'next-api-compose'
import helmet from 'helmet'

const withHelmet = convert(helmet())

export default compose([withBar, withFoo, withHelmet], (request, response) => {
  const { foo, bar } = request
  response.status(200).json({ foo, bar })
})

in this example, popular middleware helmet is converted using utility function from next-api-compose and passed as one element in middleware chain

Examples

You can find more examples here:

the example/ directory contains simple Next.js application implementing next-api-compose . To fully explore examples implemented in it by yourself - simply do cd examples && npm i && npm run dev then navigate to http://localhost:3000/

Caveats

  1. You may need to add

    export const config = {
      api: {
        externalResolver: true
      }
    }
    

    to your Next.js API route configuration in order to dismiss false positive about stalled API requests.
    Discussion about this can be found on the Next.js GitHub repository page.

  2. If you are using TypeScript and strict types (no any at all), you may want to use Partial

    type NextApiRequestWithFoo = NextApiRequest & Partial<{ foo: string }>
    

    when extending API Route parameters' objects to avoid type errors during usage of compose.

License

This project is licensed under the MIT license.
All contributions are welcome.

Keywords

FAQs

Package last updated on 24 Sep 2021

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc