# next-controllers

> An api for create routing controllers in nextjs

Latest version **0.1.17** (published 2022-01-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install next-controllers
pnpm add next-controllers
yarn add next-controllers
bun add next-controllers
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.17 |
| Published | 2022-01-17 |
| First published | 2021-12-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 127.2 KB |
| Known vulnerabilities | 0 (+16 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | neociber94 |
| Keywords | nextjs, api, router, routing, controllers, typescript |

## Links

- npm: https://www.npmjs.com/package/next-controllers
- Repository: https://github.com/Neo-Ciber94/next-controllers
- Homepage: https://github.com/Neo-Ciber94/next-controllers#readme
- Issues: https://github.com/Neo-Ciber94/next-controllers/issues
- npm.io page: https://npm.io/package/next-controllers

## Dependencies (4)

- [next](https://npm.io/package/next.md) ^12.0.7
- [regexparam](https://npm.io/package/regexparam.md) ^2.0.0
- [body-parser](https://npm.io/package/body-parser.md) ^1.19.1
- [stacktrace-parser](https://npm.io/package/stacktrace-parser.md) ^0.1.10

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 0.1.17 (latest) — 2022-01-17
- 0.1.16 — 2022-01-17
- 0.1.15 — 2022-01-17
- 0.1.14 — 2022-01-05
- 0.1.13 — 2022-01-02
- 0.1.12 — 2021-12-27
- 0.1.11 — 2021-12-23
- 0.1.10 — 2021-12-23
- 0.1.9 — 2021-12-18
- 0.1.8 — 2021-12-18
- 0.1.7 — 2021-12-05
- 0.1.6 — 2021-12-05
- 0.1.5 — 2021-12-05
- 0.1.4 — 2021-12-05
- 0.1.3 — 2021-12-04
- … 4 more at https://npm.io/package/next-controllers/versions

## README

# Next-Controllers

A library for create api routes for `NextJS`.

## Installation

Install with `npm`

```codecopy
npm i next-controllers
```

Or `yarn`

```codecopy
yarn add next-controllers
```

## Setup

1. Enable decorators in your `tsconfig.ts`:

   ```json
   {
     "experimentalDecorators": true,
     "emitDecoratorMetadata": true
   }
   ```

2. Add or modify a `.babelrc` file with the following content:

   ```json
   {
     "presets": ["next/babel"],
     "plugins": [["@babel/plugin-proposal-decorators", { "legacy": true }], "@babel/plugin-proposal-class-properties"]
   }
   ```

3. Install the `babel` dependencies

   ```codecopy
   npm i -D @babel/plugin-proposal-class-properties @babel/plugin-proposal-decorators
   ```

## Usage

Create a file under `pages/api/` with the pattern: `[[...params]]`
to allow catch all the request.

Define the controller on the route.

```ts
// pages/api/[...hello].ts

import { Get, NextApiContext, withController } from 'next-controllers';

class HelloController {
  @Get()
  sayHello() {
    return 'Hello World!';
  }

  @Get('/:name')
  sayHelloTo(context: NextApiContext) {
    return `Hello ${context.request.params.name}!`;
  }
}

export default withController(HelloController);
```

**Request results:**

```bash
curl http://localhost:3000/api
> Hello World!
```

```bash
curl http://localhost:3000/api/Alexandra
> Hello Alexandra!
```

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