
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
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 pretty enough 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
import { compose } from 'next-api-compose'
export default compose(
{ sharedErrorHandler, middlewareChain: [withBar, withFoo, withError] },
(request, response) => {
// This is unreachable because ^^^^^^^^^ will return 418 status.
const { foo, bar } = request
response.status(200).json({ foo, bar })
}
)
async function sharedErrorHandler(error, _request, response) {
response.status(418).json({ error: error.message })
}
function withFoo(handler) {
return async function (request, response) {
request.foo = 'foo'
return handler(request, response)
}
}
function withBar(handler) {
return async function (request, response) {
request.bar = 'bar'
return handler(request, response)
}
}
function withError() {
return async function () {
throw new Error('some error')
}
}
import type { NextApiRequest, NextApiResponse } from 'next'
import type {
ExtendedNextApiHandler,
NextApiComposeMiddlewares,
NextApiComposeOptions
} from 'next-api-compose'
import { compose } from 'next-api-compose'
type NextApiRequestWithFoo = NextApiRequest & Partial<{ foo: string }>
type NextApiRequestWithBar = NextApiRequest & Partial<{ bar: string }>
type NextApiRequestWithFooBar = NextApiRequestWithFoo & NextApiRequestWithBar
const mws: NextApiComposeMiddlewares<NextApiRequestWithFooBar> = [withFoo, withBar]
const options: NextApiComposeOptions<NextApiRequestWithFooBar> = {
sharedErrorHandler: handleErrors,
middlewareChain: mws
}
export default compose<NextApiRequestWithFooBar>(options, (request, response) => {
const { foo, bar } = request
response.status(200).json({ foo, bar })
})
function handleErrors(error: Error, _request: NextApiRequest, response: NextApiResponse) {
response.status(418).json({ error: error.message })
}
function withFoo(handler: ExtendedNextApiHandler<NextApiRequestWithFoo>) {
return async function (request: NextApiRequestWithFoo, response: NextApiResponse) {
request.foo = 'foo'
return handler(request, response)
}
}
function withBar(handler: ExtendedNextApiHandler<NextApiRequestWithBar>) {
return async function (request: NextApiRequestWithBar, response: NextApiResponse) {
request.bar = 'bar'
return handler(request, response)
}
}
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 435 weekly downloads. As such, next-api-compose popularity was classified as not popular.
We found that next-api-compose demonstrated a not healthy version release cadence and project activity because the last version was released 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.