|
| 1 | +<br> |
| 2 | +<br> |
| 3 | +<br> |
1 | 4 | <div align="center" style="margin-bottom: 16px"> |
2 | 5 | <img src="openapi-codegen-logo.svg" width="400px" /> |
3 | 6 | </div> |
| 7 | +<br> |
| 8 | +<br> |
| 9 | + |
| 10 | + <div align="center"> |
| 11 | + <a href="https://www.npmjs.com/package/@openapi-codegen/cli"> |
| 12 | + <img alt="npm" src="https://img.shields.io/npm/v/@openapi-codegen/cli.svg?style=for-the-badge"> |
| 13 | + </a> |
| 14 | + <a href="https://github.com/fabien0102/openapi-codegen/blob/main/LICENCE.md"> |
| 15 | + <img alt="Read the documentation" src="https://img.shields.io/npm/l/@openapi-codegen/cli.svg?style=for-the-badge"> |
| 16 | + </a> |
| 17 | +<br> |
| 18 | +<br> |
| 19 | + Tooling to give you full type-safety around OpenAPI specs. |
| 20 | + |
| 21 | + </div> |
| 22 | + |
| 23 | +<br> |
| 24 | + |
| 25 | +### You can generate |
| 26 | + |
| 27 | +- TypeScript types |
| 28 | +- Type-safe Generic Fetchers |
| 29 | +- Type-safe React Query hooks (https://github.com/tanstack/query) |
4 | 30 |
|
5 | | -[](https://www.npmjs.com/package/@openapi-codegen/cli) |
6 | | -[](https://github.com/fabien0102/openapi-codegen/blob/main/LICENSE) |
| 31 | +## Getting started |
7 | 32 |
|
8 | | -Tooling to give you full type-safety around OpenAPI specs. |
| 33 | +1. **Initialize the generator** |
9 | 34 |
|
10 | | -**For frontend:** |
| 35 | + ```bash |
| 36 | + $ npx @openapi-codegen/cli init |
| 37 | + ``` |
| 38 | + |
| 39 | + <img style="max-width: 400px" src="https://user-images.githubusercontent.com/271912/194000679-5a4501b8-5fc0-430c-9217-028bf91a5dcd.gif"> |
11 | 40 |
|
12 | | -This will give you full auto-completion and type-safety of your APIs |
| 41 | + If you wish to change any of the selections made, you can do so in the generated `openapi-codegen.config.ts` file later.. |
13 | 42 |
|
14 | | -**For backend: (in coming)** |
| 43 | +2. **Start Generation** |
15 | 44 |
|
16 | | -This will generate everything you need to deliver a perfect API, spec driven. |
| 45 | + ```bash |
| 46 | + $ npx openapi-codegen gen {namespace} |
| 47 | + ``` |
| 48 | + After the code generation is done, you will notice the following files: |
17 | 49 |
|
18 | | -## Getting started |
| 50 | + - `{namespace}Fetcher.ts` - defines a function that will make requests to your API. |
| 51 | + - `{namespace}Context.tsx` - the context that provides `{namespace}Fetcher` to other components. |
| 52 | + - `{namespace}Components.tsx` - generated React Query components (if you selected React Query as part of initialization). |
| 53 | + - `{namespace}Schemas.ts` - the generated Typescript types from the provided Open API schemas. |
19 | 54 |
|
20 | | -```bash |
21 | | -$ npm i -D @openapi-codegen/{cli,typescript} |
22 | | -$ npx openapi-codegen init |
23 | | -``` |
| 55 | + |
| 56 | + |
| 57 | + > **Warning** |
| 58 | + > |
| 59 | + > If `{namespace}Fetcher.ts` or `{namespace}Context.tsx` already exist in the output folder, they will not be replaced. However, `{namespace}Components.tsx` and `{namespace}Schemas.ts` will be re-generated each time based on the Open API spec file provided. |
| 60 | +
|
| 61 | +3. **Configure the Fetcher** (optional) |
24 | 62 |
|
25 | | -Follow the steps, this will generate a configuration file for you (openapi-codegen.config.ts). |
| 63 | + After the first step you should see a file called `{namespace}Fetcher.ts` in your ouput directory. This file |
26 | 64 |
|
27 | | -You should have a bunch of types / components ready to be used. |
| 65 | + By default it uses the built-in Fetch API, you are free to change this to your fetching library of choice (Axios, Got etc.) |
28 | 66 |
|
29 | | -Note: The generated `{namespace}Fetcher.ts` assume a global `fetch`, if you want to use this in a nodejs environment, please update this file (this is just a template) |
| 67 | + If your Open API spec contains a configured server, then the base URL for all requests will default to that server's URL. If no such configuration exists, you'll need to specify the base URL value. |
| 68 | + |
| 69 | +4. **Install and Configure React Query** (optional) |
| 70 | + |
| 71 | + If during generator setup you picked `> React Query components`, then you will need to install and configure React Query in order for the generated React hooks to work properly: |
| 72 | + |
| 73 | + - Install the library |
| 74 | + ```bash |
| 75 | + npm i @tanstack/react-query |
| 76 | + ``` |
| 77 | + - Wire up the `QueryClient` as described [here](https://tanstack.com/query/v4/docs/adapters/react-query). |
30 | 78 |
|
31 | 79 | ## Philosophy |
32 | 80 |
|
@@ -81,27 +129,32 @@ SignUpInput: |
81 | 129 |
|
82 | 130 | OpenAPI Codegen will be able to generate all the relevant validation (or at least give you the choice to do it). |
83 | 131 |
|
84 | | -Note: You can also attach any custom logic by using the `x-*` tag, the possibilities are endless! |
| 132 | +> **Note** |
| 133 | +> You can also attach any custom logic by using the `x-*` tag, the possibilities are endless! |
85 | 134 |
|
86 | 135 | ### Frontend side |
87 | 136 |
|
88 | 137 | Having to reverse engineer a backend response is the least productive/fun task ever! However, given a nice OpenAPI specs, we can actually generate nicely typed code for you that lets you interact with your API in a safe manner. |
89 | 138 |
|
90 | | -Taking React as example, calling an API can be as simple as this: |
| 139 | +Taking React as example, calling an API can be as simple as this: _(this hooks are using **Tanstack Query** under the hood)_ |
91 | 140 |
|
92 | 141 | ```tsx |
93 | 142 | import { useListPets } from "./petStore/petStoreComponents"; // <- output from openapi-codegen |
94 | 143 |
|
95 | | -const App = () => { |
| 144 | +const Example = () => { |
96 | 145 | const { data, loading, error } = useListPets(); |
97 | 146 |
|
98 | 147 | // `data` is fully typed and have all documentation from OpenAPI |
99 | 148 | }; |
100 | 149 | ``` |
101 | 150 |
|
| 151 | +> **Note** |
| 152 | +> You can also check this blog post about using generated hooks in React https://xata.io/blog/openapi-typesafe-react-query-hooks |
| 153 | +
|
102 | 154 | And since this generated from the specs, everything is safe at build time! |
103 | 155 |
|
104 | | -Note: If you can’t trust your backend, some runtime validation can be useful to avoid surprises in production 😅 |
| 156 | +> **Note** |
| 157 | +> If you can’t trust your backend, some runtime validation can be useful to avoid surprises in production 😅 |
105 | 158 |
|
106 | 159 | ## Configuration |
107 | 160 |
|
|
0 commit comments