https://github.com/codefromanywhere/next-openapi-demo
Working schema-first with an openapi in a nextJS project
https://github.com/codefromanywhere/next-openapi-demo
Last synced: 10 months ago
JSON representation
Working schema-first with an openapi in a nextJS project
- Host: GitHub
- URL: https://github.com/codefromanywhere/next-openapi-demo
- Owner: CodeFromAnywhere
- Created: 2024-04-14T08:49:46.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-18T16:17:12.000Z (almost 2 years ago)
- Last Synced: 2024-11-28T21:05:05.521Z (about 1 year ago)
- Language: TypeScript
- Size: 70.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Motivation:
- Make desing-first approach for building OpenAPIs easy (following the [recommendation](https://learn.openapis.org/best-practices.html))
# Features:
This is a [Next.js](https://nextjs.org/) project based on [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). What it adds based on the standard configuration (typescript, pages router), is the following:
- `openapi.json` is your SSOT for your input/output
- `npm run dev` watches `openapi.json`
- Auto-generates types for your OpenAPI.
- Builds SDK Client for your OpenAPI.
- Makes it very easy to reuse your endpoints as regular functions or cli's as well.
# How to use
To work with this, change your functions and their IO in `openapi.json`, then run `npm run types` to re-generate your types.
Now, all you need to make sure to do, is to have a function for each endpoint you defined in your `api` folder, in this way:
```ts
import { Endpoint, makeEndpoint } from "@/api-util";
export const testEndpoint: Endpoint<"testEndpoint"> = async (context) => {
// Your function
};
export default makeEndpoint(testEndpoint);
```
In the above example, the test endpoint is automatically typescript validated and also it can validate its input automatically, all based on your openapi spec.
# Wishlist
- Watch your `/api` folder and detect api paths that are available and compare that to the ones that are defined in OpenAPI. With this info, we can warn the user whether or not an API exists and should exist.