Skip to content

Commit 87fb1f8

Browse files
needimANDREYDEN
andauthored
chore: documentation (#108)
* chore: documentation * Extend "Getting Started" section * Add warning about code re-generation * Add spacing before warning * refactor --------- Co-authored-by: ANDREYDEN <[email protected]>
1 parent 151d47a commit 87fb1f8

File tree

1 file changed

+72
-19
lines changed

1 file changed

+72
-19
lines changed

README.md

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,80 @@
1+
<br>
2+
<br>
3+
<br>
14
<div align="center" style="margin-bottom: 16px">
25
<img src="openapi-codegen-logo.svg" width="400px" />
36
</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)
430

5-
[![npm](https://img.shields.io/npm/v/@openapi-codegen/cli.svg?style=for-the-badge)](https://www.npmjs.com/package/@openapi-codegen/cli)
6-
[![License](https://img.shields.io/npm/l/@openapi-codegen/cli.svg?style=for-the-badge)](https://github.com/fabien0102/openapi-codegen/blob/main/LICENSE)
31+
## Getting started
732

8-
Tooling to give you full type-safety around OpenAPI specs.
33+
1. **Initialize the generator**
934

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">
1140

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..
1342

14-
**For backend: (in coming)**
43+
2. **Start Generation**
1544

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:
1749

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.
1954

20-
```bash
21-
$ npm i -D @openapi-codegen/{cli,typescript}
22-
$ npx openapi-codegen init
23-
```
55+
&nbsp;
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)
2462

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
2664

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.)
2866

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).
3078

3179
## Philosophy
3280

@@ -81,27 +129,32 @@ SignUpInput:
81129

82130
OpenAPI Codegen will be able to generate all the relevant validation (or at least give you the choice to do it).
83131

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!
85134

86135
### Frontend side
87136

88137
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.
89138

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)_
91140

92141
```tsx
93142
import { useListPets } from "./petStore/petStoreComponents"; // <- output from openapi-codegen
94143
95-
const App = () => {
144+
const Example = () => {
96145
const { data, loading, error } = useListPets();
97146
98147
// `data` is fully typed and have all documentation from OpenAPI
99148
};
100149
```
101150
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+
102154
And since this generated from the specs, everything is safe at build time!
103155
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 😅
105158
106159
## Configuration
107160

0 commit comments

Comments
 (0)