Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nwylynko/next-json-api
small function to simplify nextjs api routes
https://github.com/nwylynko/next-json-api
Last synced: 30 days ago
JSON representation
small function to simplify nextjs api routes
- Host: GitHub
- URL: https://github.com/nwylynko/next-json-api
- Owner: NWylynko
- License: mit
- Created: 2023-02-09T01:24:58.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-30T04:43:31.000Z (over 1 year ago)
- Last Synced: 2024-11-25T13:42:00.855Z (about 2 months ago)
- Language: TypeScript
- Size: 79.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# next-json-api
small function to simplify nextjs api routes```ts
import { JsonHandler, ApiError, type GetResponse } from "next-json-api";// wrap your handler in JsonHandler
const handler = JsonHandler(async (req, res) => {
if (Math.random() > 0.5) {// throw ApiError with the status code and message
throw new ApiError(500, "Something went wrong");
}// return the response
return {
hello: "world",
};
});export default handler;
// import this client side to get the same type
export type Response = GetResponse;
```