An open API service indexing awesome lists of open source software.

https://github.com/magnusopera/openapigen

TypeScript OpenApi Generator
https://github.com/magnusopera/openapigen

dotnet openapi tool typescript

Last synced: 20 days ago
JSON representation

TypeScript OpenApi Generator

Awesome Lists containing this project

README

          

# OpenApiGen

OpenApiGen is a .NET tool for generating OpenAPI-based TypeScript clients. It aims to produce simple, type-safe, and easily consumable clients for modern TypeScript applications, especially those using React Query and Axios.

[![Build Status](https://github.com/MagnusOpera/OpenApiGen/actions/workflows/build.yml/badge.svg)](https://github.com/MagnusOpera/OpenApiGen/actions)

[![NuGet Version](https://img.shields.io/nuget/v/MagnusOpera.OpenApiGen.svg)](https://www.nuget.org/packages/MagnusOpera.OpenApiGen)

## Why OpenApiGen?

Existing tools often generate overly complicated clients. OpenApiGen focuses on:

- **Obvious input/output messages:** 1 input message + query args, 1 output message. Everything inlined unless specified.
- **Simple functions for each operation:** Easy integration with React Query, no complicated classes or inheritance trees.

### Example Generated Output

Below is a sample of the TypeScript client code generated by OpenApiGen for a PATCH operation on `/User/{id}`. This output demonstrates the inlined request/response types and the simple function signature designed for easy integration with tools like React Query:

```typescript
// === patch /User/{id} ===
export type UserIdPatchRequest = {
firstName: null | string
lastName: null | string
}
export type UserIdPatch200Response = {
firstName: string
lastName: string
}
export type UserIdPatch400Response = ProblemDetails
export type UserIdPatch404Response = ProblemDetails
export async function patchUserIdAsync(axios: AxiosInstance, bearer: string, id: string, request: UserIdPatchRequest): Promise<[200, UserIdPatch200Response] | [400, UserIdPatch400Response] | [404, UserIdPatch404Response]> {
const resp = await axios.patch(`/User/${id}`, request, { validateStatus: () => true, headers: { Authorization: `Bearer ${bearer}` } })
switch (resp.status) {
case 200: return [200, resp.data as UserIdPatch200Response]
case 400: return [400, resp.data as UserIdPatch400Response]
case 404: return [404, resp.data as UserIdPatch404Response]
default: throw `Unexpected status ${resp.status}`
}
}
```

## Features

- Generates TypeScript clients from OpenAPI definitions
- Designed for easy integration with React Query
- Inlines types for clarity and simplicity
- Support for Bearer token (authorization header) and ApiKey (cookie)
- Minimal dependencies, no runtime bloat
- Supports OpenApi 3.0/3.1 Schema

## Getting Started

### Install via .NET Tool

You can install the latest released binary globally using the .NET CLI:

```bash
dotnet tool install --global MagnusOpera.OpenApiGen
```

Or update to the latest version:

```bash
dotnet tool update --global MagnusOpera.OpenApiGen
```

After installation, you can run the tool using:

```bash
openapigen --help
```

### Prerequisites

- [.NET 10.0 SDK](https://dotnet.microsoft.com/download)
- Node.js (for consuming generated TypeScript clients)
- [GNU Make](https://www.gnu.org/software/make/) (optional, for building with `make`)

### Build

Clone the repository and build the tool:

```bash
git clone https://github.com/MagnusOpera/OpenApiGen.git
cd OpenApiGen
make build
```

### Test
Install Node.js dependencies (for checking TypeScript output):

```bash
make install
```

To run the generator with the provided sample project:

```bash
make run
```

This will generate a TypeScript client from the sample API definition in `SampleApi/SampleApi.json`. Output is available in the `generated` folder.

### Usage

Run the generator with your OpenAPI definition:

```bash
dotnet run --project OpenApiGen/OpenApiGen.csproj --
```

With:
- : path to openapi definition file
- : path to generated client. WARNING: content is purged without notice.

#### Example

```bash
dotnet run --project OpenApiGen/OpenApiGen.csproj -- Examples/config.json Examples/api.json -o ./generated
```

## Project Structure

- `OpenApiGen/` - Main C# generator code
- `Examples/` - Example OpenAPI definitions and configuration files

## Contributing

Contributions are welcome! Please open issues or pull requests for bug fixes, features, or documentation improvements.

## License

This project is licensed under the MIT License. See [LICENSE.md](../LICENSE.md) for details.