https://github.com/zaida04/nextjs-api-decorators
✨ Create your Next.JS API using TypeScript decorators
https://github.com/zaida04/nextjs-api-decorators
Last synced: about 1 year ago
JSON representation
✨ Create your Next.JS API using TypeScript decorators
- Host: GitHub
- URL: https://github.com/zaida04/nextjs-api-decorators
- Owner: zaida04
- License: mit
- Created: 2022-06-02T00:31:46.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-06-02T02:44:39.000Z (about 4 years ago)
- Last Synced: 2025-02-06T07:32:38.482Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# `nextjs-api-decorators`
[](https://github.com/zaida04/nextjs-api-decorators/blob/main/LICENSE)
[](https://www.npmjs.com/package/nextjs-api-decorators)
[](https://github.com/zaida04/nextjs-api-decorators/actions/workflows/ci.yml)
## ❓ 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
```typescript
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](https://www.npmjs.com/package/nextjs-api-decorators)
- `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`
```typescript
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](https://github.com/zaida04/nextjs-api-decorators/blob/main/LICENSE)