|
| 1 | +# Open API Typescript Client Generator |
| 2 | + |
| 3 | +Generates Open API client for TypeScript. Extremely configurable. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +1. Generates TypeScript models for all the schemas in the Open API document in a form of interfaces and type aliases. |
| 8 | +2. Generates TypeScript services for all the operations in the Open API document. |
| 9 | +3. Generates a client class that combines all the services. |
| 10 | +4. Uses `fetch` API for making HTTP requests by default, but can be configured to use any other HTTP client. |
| 11 | +5. May generate validation for the API responses if configured. |
| 12 | + |
| 13 | +## Setup |
| 14 | + |
| 15 | +Install using npm |
| 16 | + |
| 17 | +```shell |
| 18 | +npm add --save-dev api-typescript-generator |
| 19 | +``` |
| 20 | + |
| 21 | +Or using yarn |
| 22 | + |
| 23 | +```shell |
| 24 | +yarn add --dev api-typescript-generator |
| 25 | +``` |
| 26 | + |
| 27 | +## Configuring |
| 28 | + |
| 29 | +Create a `api-typescript-generator.config.ts` file in the root of your project. |
| 30 | + |
| 31 | +```ts |
| 32 | +import path from 'path'; |
| 33 | +import {ApiTypescriptGeneratorConfig} from 'api-typescript-generator'; |
| 34 | + |
| 35 | +const configuration: ApiTypescriptGeneratorConfig = { |
| 36 | + generates: [ |
| 37 | + { |
| 38 | + type: 'openapiClient', |
| 39 | + document: { |
| 40 | + // The source of the Open API document. |
| 41 | + // Can also be { type: 'file', path: 'path/to/file.yaml' } |
| 42 | + // Both YAML and JSON formats are supported. |
| 43 | + source: { |
| 44 | + type: 'url', |
| 45 | + url: 'https://raw.githubusercontent.com/readmeio/oas-examples/main/3.1/yaml/petstore.yaml' |
| 46 | + } |
| 47 | + }, |
| 48 | + // The output directory for the generated client. |
| 49 | + outputDirPath: path.join(__dirname, 'petstore-api-client'), |
| 50 | + client: { |
| 51 | + // The name of the generated client class. |
| 52 | + name: 'PetStoreApiClient', |
| 53 | + // Overrides the default base URL for the API. |
| 54 | + baseUrl: 'https://petstore.swagger.io/v2' |
| 55 | + } |
| 56 | + } |
| 57 | + ] |
| 58 | +}; |
| 59 | + |
| 60 | +export default configuration; |
| 61 | +``` |
| 62 | + |
| 63 | +Add the following script to your `package.json`: |
| 64 | + |
| 65 | +```json |
| 66 | +{ |
| 67 | + // ... |
| 68 | + "scripts": { |
| 69 | + // ... |
| 70 | + "openapi-codegen": "api-typescript-generator generate api-typescript-generator.config.ts" |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +Run the script: |
| 76 | + |
| 77 | +```shell |
| 78 | +npm run openapi-codegen |
| 79 | +``` |
| 80 | + |
| 81 | +Or using yarn: |
| 82 | + |
| 83 | +```shell |
| 84 | +yarn openapi-codegen |
| 85 | +``` |
| 86 | + |
| 87 | +By default it generates: |
| 88 | + |
| 89 | +1. Models for all the schemas in the Open API document. Stored at `models` directory by default. |
| 90 | +2. Services for all the operations in the Open API document. Stored at `services` directory by default. |
| 91 | +3. A client class that combines all the services. Stored at the root of the output directory by default. |
| 92 | +4. Core classes for handling HTTP requests and responses. Stored at `core` directory by default. |
| 93 | + |
| 94 | +## Usage |
| 95 | + |
| 96 | +The generated client can be used as follows: |
| 97 | + |
| 98 | +```ts |
| 99 | +import {PetStoreApiClient} from './petstore-api-client'; |
| 100 | + |
| 101 | +async function testApiClient() { |
| 102 | + const apiClient = new PetStoreApiClient(); |
| 103 | + console.log(await client.store.getInventory()); |
| 104 | +} |
| 105 | + |
| 106 | +testApiClient().catch(console.error); |
| 107 | +``` |
| 108 | + |
| 109 | +## What is configurable? |
| 110 | + |
| 111 | +1. Validation of the API responses. See [validation](docs/interfaces/openapi_client.OpenApiClientGeneratorConfig.md#validation). |
| 112 | +2. Default base URL for the API. See [client.baseUrl](docs/interfaces/openapi_client.OpenApiClientGeneratorConfigClient.md#baseUrl). |
| 113 | +3. Leading and trailing comments for the files. See [comments](docs/interfaces/openapi_client.OpenApiClientGeneratorConfigComments.md). |
| 114 | +4. File naming conventions. I.e. [models.filenameFormat](docs/interfaces/openapi_client.OpenApiClientGeneratorConfigModels.md#filenameFormat). |
| 115 | +5. Output directory structure. I.e. [models.relativeDirPath](docs/interfaces/openapi_client.OpenApiClientGeneratorConfigModels.md#relativeDirPath). |
| 116 | +6. JSDoc generation. I.e. [models.generateJsDoc](docs/interfaces/openapi_client.OpenApiClientGeneratorConfigModels.md#generateJsDoc). |
| 117 | +7. Many more. See the documentation below. |
| 118 | + |
| 119 | +## Documentation |
| 120 | + |
| 121 | +Types are exported as part of three modules, depending on the area of application: |
| 122 | + |
| 123 | +1. [`api-typescript-generator`](docs/modules/index.md) - The main module that exports the common API Generator Configuration types. |
| 124 | +2. [`api-typescript-generator/openapi-client`](docs/modules/openapi_client.md) - The module that exports the Open API Client Generator Configuration types. |
| 125 | +3. [`api-typescript-generator/openapi`](docs/modules/openapi.md) - The module that exports the Open API Document types. |
| 126 | + |
| 127 | +At the moment only types are exported to be used with CLI. Callable API is planned for the future. |
| 128 | + |
| 129 | +## References |
| 130 | + |
| 131 | +1. [Open API Specification](https://swagger.io/specification/) |
| 132 | +2. [JSON Schema](https://json-schema.org/) |
| 133 | + |
| 134 | + |
0 commit comments