Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/atomic-state/next-rest-controller
A REST helper for Next.js APIs
https://github.com/atomic-state/next-rest-controller
next-api nextjs rest-api restful-next serverless
Last synced: about 1 month ago
JSON representation
A REST helper for Next.js APIs
- Host: GitHub
- URL: https://github.com/atomic-state/next-rest-controller
- Owner: atomic-state
- License: mit
- Created: 2022-06-15T01:12:08.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-06-15T05:20:46.000Z (over 2 years ago)
- Last Synced: 2024-04-16T21:06:06.439Z (9 months ago)
- Topics: next-api, nextjs, rest-api, restful-next, serverless
- Language: JavaScript
- Homepage:
- Size: 14.6 KB
- Stars: 5
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## REST controller for Next.js
#### How does it work?
Create restful controllers in Next.js
#### Example:
Inside `/pages/api/auth/[...handler]`
(filename must be a rest operator if you want customized URLs, but you can also use normal api handlers filenames, though they have to share the same URL in your controller)
```ts
// inside /pages/api/auth/[...handler]import { Controller } from "next-rest-controller"
// The first argument is the 'base' segment that will be used to map the correct url.
const AuthHandler = Controller("/auth", {
async "GET /auth"(req, res) {
res.status(401)
res.send("Forbidden")
},
async "GET /[id]/info"(req, res) {
res.send("Info for " + req.query.id)
},
// Like Express
async "GET /:id/info/:slug"(req, res) {
res.send("Info for " + req.query.id)
}
})
```#### Explanation
When adding a handler/method, it should start with an HTTP verb, followed by a space, and a url to handle (with or without query params using square brackets, or like Express, by placing `:` before).Sending, for example, a POST request that would be handled by a GET handler, will send a `405` status code