All Projects → neg4n → next-api-compose

neg4n / next-api-compose

Licence: MIT license
🧬 Simple, dependency free, error aware and powerful utility to compose chain of multiple middleware into one Next.js API Route.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to next-api-compose

next-api-og-image
Easy way to generate open-graph images dynamically in HTML or React using Next.js API Routes. Suitable for serverless environment.
Stars: ✭ 179 (+713.64%)
Mutual labels:  next, nextjs-plugin, nextjs-api-routes
next-share
Social media share buttons for your next React apps.
Stars: ✭ 145 (+559.09%)
Mutual labels:  next, nextjs-plugin
next-saas-starter
⚡️ Free Next.js responsive landing page template for SaaS products made using JAMStack architecture.
Stars: ✭ 497 (+2159.09%)
Mutual labels:  next, nextjs-typescript
next-ts-starter
A Next.JS powered Typescript starter with styled components, styled-system, framer motion, jest and cypress
Stars: ✭ 27 (+22.73%)
Mutual labels:  next
react-redux-nextjs-bootstrap-starter
next + react + redux + bootstrap starter
Stars: ✭ 24 (+9.09%)
Mutual labels:  next
next-boilerplate
☶ The easiest way to create a Next app by running one command.
Stars: ✭ 65 (+195.45%)
Mutual labels:  next
next-global-css
A preset for nextjs allowing using 3d party libraries with global css
Stars: ✭ 74 (+236.36%)
Mutual labels:  nextjs-plugin
Next I18next
The easiest way to translate your NextJs apps.
Stars: ✭ 2,818 (+12709.09%)
Mutual labels:  next
dhan-gaadi
A complete online bus reservation system (Node, React, Mongo, NextJS, ReactNative)
Stars: ✭ 207 (+840.91%)
Mutual labels:  next
next-qrcode
React hooks for generating QRCode for your next React apps.
Stars: ✭ 87 (+295.45%)
Mutual labels:  nextjs-plugin
website
Fully responsive website built with NextJS, React and Fluent UI, with the aim of providing services and access to all groups of didactic courses and general purposes to students of the University of Milan.
Stars: ✭ 29 (+31.82%)
Mutual labels:  next
react-redux-nextjs-material-ui-pwa-starter
No description or website provided.
Stars: ✭ 33 (+50%)
Mutual labels:  next
next-all-in
🗃⚛️ Next starter for creating any type of site
Stars: ✭ 29 (+31.82%)
Mutual labels:  next
stacker.news
It's like Hacker News but we pay you Bitcoin.
Stars: ✭ 196 (+790.91%)
Mutual labels:  next
opendaylight-sample-apps
Sample applications for use with OpenDaylight (https://www.opendaylight.org/)
Stars: ✭ 56 (+154.55%)
Mutual labels:  next
nextjs-typescript-postgresql-graphql-realtime-chat
WunderGraph Realtime Chat Example using NextJS, TypeScript, PostgreSQL, GraphQL
Stars: ✭ 53 (+140.91%)
Mutual labels:  nextjs-typescript
React Awesome Slider
React content transition slider. Awesome Slider is a 60fps, light weight, performant component that renders an animated set of production ready UI general purpose sliders with fullpage transition support for NextJS and GatsbyJS. 🖥️ 📱
Stars: ✭ 2,343 (+10550%)
Mutual labels:  next
next-netlify-starter
A one-click starter project for Next and Netlify
Stars: ✭ 76 (+245.45%)
Mutual labels:  next
fullstack-nextjs-ecommerce
Fullstack Next.js E-Commerce made with NextAuth + Prisma, Docker + TypeScript + React Query + Stripe + Tailwind Sentry and much more 🛒
Stars: ✭ 524 (+2281.82%)
Mutual labels:  nextjs-typescript
React.ai
It recognize your speech and trained AI Bot will respond(i.e Customer Service, Personal Assistant) using Machine Learning API (DialogFlow, apiai), Speech Recognition, GraphQL, Next.js, React, redux
Stars: ✭ 38 (+72.73%)
Mutual labels:  next

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].