Open API Typescript Client Generator
Generates Open API client for TypeScript. Extremely configurable.
Features
- Generates TypeScript models for all the schemas in the Open API document in a form of interfaces and type aliases.
- Generates TypeScript services for all the operations in the Open API document.
- Generates a client class that combines all the services.
- Uses
fetch
API for making HTTP requests by default, but can be configured to use any other HTTP client. - May generate validation for the API responses if configured.
Setup
Install using npm
npm add --save-dev api-typescript-generator
Or using yarn
yarn add --dev api-typescript-generator
Configuring
Create a api-typescript-generator.config.ts
file in the root of your project.
import path from 'path';
import {ApiTypescriptGeneratorConfig} from 'api-typescript-generator';
const configuration: ApiTypescriptGeneratorConfig = {
generates: [
{
type: 'openapiClient',
document: {
source: {
type: 'url',
url: 'https://raw.githubusercontent.com/readmeio/oas-examples/main/3.1/yaml/petstore.yaml'
}
},
outputDirPath: path.join(__dirname, 'petstore-api-client'),
client: {
name: 'PetStoreApiClient',
baseUrl: 'https://petstore.swagger.io/v2'
}
}
]
};
export default configuration;
Add the following script to your package.json
:
{
"scripts": {
"openapi-codegen": "api-typescript-generator generate api-typescript-generator.config.ts"
}
}
Run the script:
npm run openapi-codegen
Or using yarn:
yarn openapi-codegen
By default it generates:
- Models for all the schemas in the Open API document. Stored at
models
directory by default. - Services for all the operations in the Open API document. Stored at
services
directory by default. - A client class that combines all the services. Stored at the root of the output directory by default.
- Core classes for handling HTTP requests and responses. Stored at
core
directory by default.
Usage
The generated client can be used as follows:
import {PetStoreApiClient} from './petstore-api-client';
async function testApiClient() {
const apiClient = new PetStoreApiClient();
console.log(await client.store.getInventory());
}
testApiClient().catch(console.error);
What is configurable?
- Validation of the API responses. See validation.
- Default base URL for the API. See client.baseUrl.
- Leading and trailing comments for the files. See comments.
- File naming conventions. I.e. models.filenameFormat.
- Output directory structure. I.e. models.relativeDirPath.
- JSDoc generation. I.e. models.generateJsDoc.
- Many more. See the documentation below.
Documentation
The most important interface for now would be the OpenApiClientGeneratorConfig interface. It contains all the configuration options for the Open API Client Generator.
Types are exported as part of three modules, depending on the area of application:
api-typescript-generator
- The main module that exports the common API Generator Configuration types.api-typescript-generator/openapi-client
- The module that exports the Open API Client Generator Configuration types.api-typescript-generator/openapi
- The module that exports the Open API Document types.
At the moment only types are exported to be used with CLI. Callable API is planned for the future.
References
- Open API Specification
- JSON Schema