Generates typescript code to access your api based on the openapi (swagger) json files. The generated files are completely typesafe. This library supports only openapi schemas with the version 3.
With npm
$ npm install --global openapi-typegen`
With yarn
$ yarn global add openapi-typegen
$ typegen --src <path-or-url-to-schema> --out <output-directory>
For example
$ typegen --src http://localhost:8080/v2/api-docs --out ./generated
import { createApi } from './generated/create-api';
const api = createApi({
baseUrl: 'http://localhost:8000/api' // Override the base url the default is the url from the first server in the schema
});
// Now you can use the api as the following. The function names will be generated
// based on the operationIds
api.findPetsByTags({
query: {
tags: ['available']
}
})
.then(response => console.log(`Found pets ${response.data}`))
.catch(error => console.error('Error while searching pets'));
For an example of the generated files please take a look at the pet-store example.