# use-next-middleware

> Middleware for Next.js API Routes

Latest version **0.1.0** (published 2021-04-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-next-middleware
pnpm add use-next-middleware
yarn add use-next-middleware
bun add use-next-middleware
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2021-04-04 |
| First published | 2021-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=12 |
| Dependencies | 0 |
| Unpacked size | 194.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | An Pham |
| Maintainers | ancs21 |
| Keywords | react, middleware, next.js, next-api-routes, middleware-next.js |

## Links

- npm: https://www.npmjs.com/package/use-next-middleware
- Repository: https://github.com/ancs21/use-next-middleware
- Homepage: https://github.com/ancs21/use-next-middleware#readme
- Issues: https://github.com/ancs21/use-next-middleware/issues
- npm.io page: https://npm.io/package/use-next-middleware

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2021-04-04
- 0.0.3 — 2021-04-03
- 0.0.2 — 2021-04-03
- 0.0.1 — 2021-04-03

## README

# Middleware for Next.js API Routes

Next.js released version 9, which added API Routes. This enabled us to write APIs. Currently, Next.js release version 10, but, I think... still, something was missing: **Middleware**. Because the official Next.js docs recommend writing functions inside your [API route handler](https://nextjs.org/docs/api-routes/api-middlewares), there is no easy way to implement it.

So, I write `use-next-middleware`. It's very clean, minimal and composable middleware patterns.

## Installation

Use the package manager **yarn** or **npm** to install `use-next-middleware`.

```bash
yarn install use-next-middleware
```

## Quick Start

```js
import { useNextMiddleware } from 'next-api-middleware'
import { NextApiRequest } from 'next'

const handler = async (req, res) => {
  const { name } = req.user
  const { role } = req.permissions

  res.status(200)
  res.send(`Hello, ${name}, Role is ${role}`)
}

export default useNextMiddleware(
  async (req: NextApiRequest & { user: { name: string } }, res, next) => {
    console.log('Add user.name in request')
    req.user = {
      name: 'Snoop',
    }

    next()
  },

  async (
    req: NextApiRequest & { permissions: { role: string } },
    res,
    next
  ) => {
    console.log('Add permissions.role in request')

    req.permissions = {
      role: 'admin',
    }

    await next()
  }
)(handler)
```

## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

## License

MIT

---
_Source: https://npm.io/package/use-next-middleware · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
