Skip to content

Commit bc5a706

Browse files
authored
Merge pull request #1 from aabidsofi19/master
Create codegen
2 parents beb3533 + 6192939 commit bc5a706

File tree

7 files changed

+1909
-3
lines changed

7 files changed

+1909
-3
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@
1616

1717
# IDEs
1818
.vscode/*
19+
20+
# Node.js
21+
node_modules/
22+

README.md

Lines changed: 118 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,120 @@
1-
# layer5-repo-template
2-
This repository is used as the boilerplate for consistency across all Layer5 repos.
1+
# OpenAPI API Client Generation and Compilation
2+
3+
Generate an API client from an OpenAPI schema in YAML format and create a rtk-query api client. Thes package streamlines this process and ensures a smooth workflow.
4+
5+
## Dependencies
6+
7+
1. **Node.js and npm:** Ensure you have Node.js and npm installed on your system. You can download them from [nodejs.org](https://nodejs.org/).
8+
9+
## Installation
10+
11+
```bash
12+
npm install @layer5/rtk-query-codegen
13+
```
14+
15+
## Usage
16+
17+
Run the script using the following command:
18+
19+
```bash
20+
rtk-query-codegen -i /path/to/schema.yml -o /path/to/generated-api.js -c /path/to/codegen-config.json
21+
```
22+
23+
## Config File Options
24+
25+
### Simple usage
26+
27+
```
28+
{
29+
apiFile: string
30+
schemaFile: string
31+
apiImport?: string
32+
exportName?: string
33+
argSuffix?: string
34+
responseSuffix?: string
35+
hooks?:
36+
| boolean
37+
| { queries: boolean; lazyQueries: boolean; mutations: boolean }
38+
tag?: boolean
39+
outputFile: string
40+
filterEndpoints?:
41+
| string
42+
| RegExp
43+
| EndpointMatcherFunction
44+
| Array<string | RegExp | EndpointMatcherFunction>
45+
endpointOverrides?: EndpointOverrides[]
46+
flattenArg?: boolean
47+
}
48+
```
49+
50+
### Filtering endpoints
51+
52+
If you only want to include a few endpoints, you can use the `filterEndpoints` config option to filter your endpoints.
53+
54+
```
55+
{
56+
// ...
57+
// should only have endpoints loginUser, placeOrder, getOrderById, deleteOrder
58+
filterEndpoints: ['loginUser', /Order/],
59+
}
60+
```
61+
62+
### Endpoint overrides
63+
64+
If an endpoint is generated as a mutation instead of a query or the other way round, you can override that.
65+
66+
```
67+
{
68+
// ...
69+
"endpointOverrides": [
70+
{
71+
"pattern": "loginUser",
72+
"type": "mutation"
73+
}
74+
]
75+
}
76+
```
77+
78+
### Generating hooks
79+
80+
Setting `hooks: true` will generate `useQuery` and `useMutation` hook exports. If you also want `useLazyQuery` hooks generated or more granular control, you can also pass an object in the shape of: `{ queries: boolean; lazyQueries: boolean; mutations: boolean }`.
81+
82+
### Multiple output files
83+
84+
```
85+
86+
schemaFile: 'https://petstore3.swagger.io/api/v3/openapi.json',
87+
apiFile: './src/store/emptyApi.ts',
88+
outputFiles: {
89+
'./src/store/user.ts': {
90+
filterEndpoints: [/user/i],
91+
},
92+
'./src/store/order.ts': {
93+
filterEndpoints: [/order/i],
94+
},
95+
'./src/store/pet.ts': {
96+
filterEndpoints: [/pet/i],
97+
},
98+
},
99+
}
100+
```
101+
102+
## The Api.js file
103+
104+
The api.js file contains the generated api endpoints , it injects them into the base rtk client . And then exports all the hooks to use them .
105+
If we need to override an api endpoint we can injectEnpoints in a separate file .
106+
107+
## Troubleshooting
108+
109+
- If any of the steps fail, the script will exit with a non-zero status code, indicating a failure. Review the error messages to diagnose and resolve any issues.
110+
111+
- Ensure that the Bash script is executable by running `chmod +x generate-api.sh`.
112+
113+
## Important Notes
114+
115+
- Make sure the OpenAPI schema (`schema.yml`) is updated with latest changes and doesnt contain any breaking changes .
116+
117+
- Always validate and test the generated API client to ensure it functions as expected.
3118

4119
<div>&nbsp;</div>
5120

@@ -17,6 +132,7 @@ Become a <b>Meshtee</b> today!</p>
17132

18133
Find out more on the <a href="https://layer5.io/community">Layer5 community</a>. <br />
19134
<br /><br /><br /><br />
135+
20136
</p>
21137

22138
<div>&nbsp;</div>
@@ -30,7 +146,6 @@ Find out more on the <a href="https://layer5.io/community">Layer5 community</a>.
30146
</picture>
31147
</a>
32148

33-
34149
<a href="https://meshery.io/community"><img alt="Layer5 Community" src=".github/readme/images//community.svg" style="margin-right:8px;padding-top:5px;" width="140px" align="left" /></a>
35150

36151
<p>

0 commit comments

Comments
 (0)