InvokeAI/invokeai/frontend/web/src/services/api/README.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
3.0 KiB
Markdown
Raw Normal View History

# Generated axios API client
- [Generated axios API client](#generated-axios-api-client)
- [Generation](#generation)
- [Generate the API client from the nodes web server](#generate-the-api-client-from-the-nodes-web-server)
- [Generate the API client from JSON](#generate-the-api-client-from-json)
- [Getting the JSON from the nodes web server](#getting-the-json-from-the-nodes-web-server)
- [Getting the JSON with a python script](#getting-the-json-with-a-python-script)
- [Generate the API client](#generate-the-api-client)
- [The generated client](#the-generated-client)
- [Fix a small issue](#fix-a-small-issue)
This API client is generated by an [openapi code generator](https://github.com/ferdikoomen/openapi-typescript-codegen).
After generation, we will need to fix a small issue.
## Generation
The axios client may be generated by from the OpenAPI schema from the nodes web server, or from JSON.
### Generate the API client from the nodes web server
We need to start the nodes web server, which serves the OpenAPI schema to the generator.
1. Start the nodes web server.
```bash
# from the repo root
python scripts/invoke-new.py --web
```
2. Generate the API client.
```bash
# from invokeai/frontend/web/
yarn api:web
```
### Generate the API client from JSON
The JSON can be acquired from the nodes web server, or with a python script.
#### Getting the JSON from the nodes web server
Start the nodes web server as described above, then download the file.
```bash
# from invokeai/frontend/web/
curl http://localhost:9090/openapi.json -o openapi.json
```
#### Getting the JSON with a python script
Run this python script from the repo root, so it can access the nodes server modules.
The script will output `openapi.json` in the repo root. Then we need to move it to `invokeai/frontend/web/`.
```bash
# from the repo root
python invokeai/frontend/web/src/services/api/generate_openapi_json.py
mv openapi.json invokeai/frontend/web/
```
#### Generate the API client
Now we can generate the API client from the JSON.
```bash
# from invokeai/frontend/web/
yarn api:file
```
## The generated client
The client will be written to `invokeai/frontend/web/services/api/`:
- `axios` client
- TS types
- An easily parseable schema, which we can use to generate UI
## Fix a small issue
In `models/Graph.ts`, `edges` is not parsed correctly from the OpenAPI schema. The generator outputs:
```typescript
{
...
edges?: Array<Array<any>>;
...
}
```
This is incorrect. It should be:
```typescript
{
...
edges?: Array<[EdgeConnection, EdgeConnection]>;
...
}
```
That is, `edges` is an array of tuples, each consisting of two `EdgeConnections`, where the first `EdgeConnection` is the "from" node, and the second is the "to" node.
You will also need to import the `EdgeConnection` type:
```typescript
import type { EdgeConnection } from './EdgeConnection';
```
If you regenerate the client, you will need to manually fix this.
Hopefully we can fix the parsing of the schema in the future.