nextjs-api-decorators
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

nextjs-api-decorators

GitHub npm CI workflows

❓ About

This package is an addon to next.js that allows you to use decorators in a class setting to structure your Next.JS API. This was created due to the age old problem in Next.JS of having to do

export default APIHandlingFunction(req, res) {
  if(req.method === "GET") {
    ...
  } else if (req.method === "POST") {
    ...
  }
}

in every API file. This leads to clutter, or you having to duplicate a fix using the map solution implemented in this library across all your files.

📥 Installation

You can install this package from NPM

  • npm install nextjs-api-decorators
  • yarn add nextjs-api-decorators

⚡ Usage

Demo file based on a template project generated with next-create-app
pages/api/items/[itemId].tsx

import { NextApiRequest, NextApiResponse } from "next";
import { http, genAPIRoute } from "nextjs-api-decorators";

// You can name the class whatever you want, doesn't matter. It's only there for us to be able to use decorators
class ItemRoutes {
    @http("ItemRoutes")
    GET(req: NextApiRequest, res: NextApiResponse) {
        return res.status(200).json({ success: true });
    }
}

export default genAPIRoute("ItemRoutes");

✋ Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

Please ensure any and all commits pass our linting and build steps as described in the root package.json.

⚖️ LICENSE

Licensed under the MIT License

Dependencies (0)

    Dev Dependencies (14)

    Package Sidebar

    Install

    npm i nextjs-api-decorators

    Weekly Downloads

    0

    Version

    1.0.3

    License

    MIT

    Unpacked Size

    6.51 kB

    Total Files

    9

    Last publish

    Collaborators

    • nico.03727