0.1.17 • Published 2 years ago

next-controllers v0.1.17

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Next-Controllers

A library for create api routes for NextJS.

Installation

Install with npm

npm i next-controllers

Or yarn

yarn add next-controllers

Setup

  1. Enable decorators in your tsconfig.ts:

    {
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true
    }
  2. Add or modify a .babelrc file with the following content:

    {
      "presets": ["next/babel"],
      "plugins": [["@babel/plugin-proposal-decorators", { "legacy": true }], "@babel/plugin-proposal-class-properties"]
    }
  3. Install the babel dependencies

    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.

// 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:

curl http://localhost:3000/api
> Hello World!
curl http://localhost:3000/api/Alexandra
> Hello Alexandra!
0.1.17

2 years ago

0.1.16

2 years ago

0.1.15

2 years ago

0.1.14

2 years ago

0.1.13

2 years ago

0.1.12

2 years ago

0.1.11

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.1

2 years ago