Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
next-api-compose
Advanced tools
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.
npm i next-api-compose -S
# or
yarn add next-api-compose
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
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
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/
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.
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
.
This project is licensed under the MIT license.
All contributions are welcome.
FAQs
Compose middleware chain in Next.js API Routes. Supports Pages and App router
The npm package next-api-compose receives a total of 1,073 weekly downloads. As such, next-api-compose popularity was classified as popular.
We found that next-api-compose demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.