https://github.com/nexys-system/openapi-client-generator
https://github.com/nexys-system/openapi-client-generator
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/nexys-system/openapi-client-generator
- Owner: nexys-system
- Created: 2024-08-25T23:16:17.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-08-25T23:43:37.000Z (10 months ago)
- Last Synced: 2025-01-24T02:59:40.292Z (5 months ago)
- Language: TypeScript
- Homepage: https://nexys-system.github.io/openapi-client-generator/
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# openapi-client-generator
[](https://github.com/nexys-system/openapi-client-generator/actions/workflows/test.yml)
A TypeScript-based library to generate client code from an OpenAPI specification. This library parses an OpenAPI JSON file and produces TypeScript enums, interfaces, and client functions that can be used to interact with an API.
## Features
- **Load OpenAPI Specification**: Load and parse an OpenAPI JSON file.
- **Generate Enums**: Automatically generate TypeScript enums for `string` types with defined `enum` values in the OpenAPI spec.
- **Generate Types**: Create TypeScript interfaces for API request and response objects based on the OpenAPI schema.
- **Generate Client Functions**: Generate reusable, typed client functions to interact with the API.## Installation
Install the package via npm:
```bash
npm install openapi-client-generator
```## Usage
### 1. Load the OpenAPI Specification
Start by loading your OpenAPI JSON file using the `loadOpenAPISpec` function:
```typescript
import { loadOpenAPISpec } from "openapi-client-generator";const specs = loadOpenAPISpec("./openapi.json");
```### 2. Generate TypeScript Client Code
You can generate the entire client code (enums, types, and functions) by passing the loaded specification to the `generateClientCode` function:
```typescript
import { generateClientCode } from "openapi-client-generator";const clientCode = generateClientCode(specs);
console.log(clientCode);
```### 3. Generate Enums Only
If you only need to generate TypeScript enums for your OpenAPI spec, use the `generateEnums` function:
```typescript
import { generateEnums } from "openapi-client-generator";const enums = generateEnums(specs.components?.schemas || {});
console.log(enums);
```### 4. Generate Types Only
To generate TypeScript interfaces for your API objects, use the `generateTypes` function:
```typescript
import { generateTypes } from "openapi-client-generator";const types = generateTypes(specs.components?.schemas || {});
console.log(types);
```### 5. Generate Client Functions Only
To generate client functions for interacting with the API, use the `generateClientFunctions` function:
```typescript
import { generateClientFunctions } from "openapi-client-generator";const functions = generateClientFunctions(specs.paths);
console.log(functions);
```## Example Output
Given an OpenAPI specification, the output will include:
1. **Enums** for any `string` schemas with `enum` values.
2. **Interfaces** for all object schemas in the OpenAPI spec.
3. **Typed client functions** for each path and operation in the OpenAPI spec.For example, the following OpenAPI spec:
```json
{
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"id": { "type": "string" },
"name": { "type": "string" },
"age": { "type": "integer" }
},
"required": ["id", "name"]
},
"Role": {
"type": "string",
"enum": ["Admin", "User", "Guest"]
}
}
},
"paths": {
"/users": {
"get": {
"operationId": "getUsers",
"responses": {
"200": {
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/User" }
}
}
}
}
}
}
}
}
```Would generate:
```typescript
export enum Role {
Admin = "Admin",
User = "User",
Guest = "Guest",
}export interface User {
id: string;
name: string;
age?: number;
}export const getUsers = async (): Promise => {
const url = host + "/users";
return genericJSONRequest(url, "GET");
};
```## Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue to discuss improvements or new features.
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.