Skip to content

Commit

Permalink
Merge pull request #1 from aabidsofi19/master
Browse files Browse the repository at this point in the history
Create codegen
  • Loading branch information
leecalcote authored Dec 18, 2023
2 parents beb3533 + 6192939 commit bc5a706
Show file tree
Hide file tree
Showing 7 changed files with 1,909 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@

# IDEs
.vscode/*

# Node.js
node_modules/

121 changes: 118 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,120 @@
# layer5-repo-template
This repository is used as the boilerplate for consistency across all Layer5 repos.
# OpenAPI API Client Generation and Compilation

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.

## Dependencies

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

## Installation

```bash
npm install @layer5/rtk-query-codegen
```

## Usage

Run the script using the following command:

```bash
rtk-query-codegen -i /path/to/schema.yml -o /path/to/generated-api.js -c /path/to/codegen-config.json
```

## Config File Options

### Simple usage

```
{
apiFile: string
schemaFile: string
apiImport?: string
exportName?: string
argSuffix?: string
responseSuffix?: string
hooks?:
| boolean
| { queries: boolean; lazyQueries: boolean; mutations: boolean }
tag?: boolean
outputFile: string
filterEndpoints?:
| string
| RegExp
| EndpointMatcherFunction
| Array<string | RegExp | EndpointMatcherFunction>
endpointOverrides?: EndpointOverrides[]
flattenArg?: boolean
}
```

### Filtering endpoints

If you only want to include a few endpoints, you can use the `filterEndpoints` config option to filter your endpoints.

```
{
// ...
// should only have endpoints loginUser, placeOrder, getOrderById, deleteOrder
filterEndpoints: ['loginUser', /Order/],
}
```

### Endpoint overrides

If an endpoint is generated as a mutation instead of a query or the other way round, you can override that.

```
{
// ...
"endpointOverrides": [
{
"pattern": "loginUser",
"type": "mutation"
}
]
}
```

### Generating hooks

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 }`.

### Multiple output files

```
schemaFile: 'https://petstore3.swagger.io/api/v3/openapi.json',
apiFile: './src/store/emptyApi.ts',
outputFiles: {
'./src/store/user.ts': {
filterEndpoints: [/user/i],
},
'./src/store/order.ts': {
filterEndpoints: [/order/i],
},
'./src/store/pet.ts': {
filterEndpoints: [/pet/i],
},
},
}
```

## The Api.js file

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 .
If we need to override an api endpoint we can injectEnpoints in a separate file .

## Troubleshooting

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

- Ensure that the Bash script is executable by running `chmod +x generate-api.sh`.

## Important Notes

- Make sure the OpenAPI schema (`schema.yml`) is updated with latest changes and doesnt contain any breaking changes .

- Always validate and test the generated API client to ensure it functions as expected.

<div>&nbsp;</div>

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

Find out more on the <a href="https://layer5.io/community">Layer5 community</a>. <br />
<br /><br /><br /><br />

</p>

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


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

<p>
Expand Down
Loading

0 comments on commit bc5a706

Please sign in to comment.