2025-04-17 15:45:43 +02:00

2.0 KiB

G1 API Generator

A command-line tool for generating TypeScript API clients from OpenAPI schemas.

Features

  • Downloads OpenAPI schema from a specified URL
  • Generates TypeScript interfaces and API client code
  • Post-processes generated code to fix common issues
  • Supports HTTPS with option to skip TLS verification
  • Detects and warns about duplicate DTOs in the schema

Installation

# Install from G1 package registry
npm install @g1/api-generator --save-dev

# Or with pnpm
pnpm add @g1/api-generator -D

Usage

Command Line

# Basic usage
g1-api-generator https://your-api-server/openapi/schema.json ./src/lib/api

# Skip TLS verification (useful for local development with self-signed certificates)
g1-api-generator https://localhost:7205/openapi/schema.json ./src/lib/api --skip-tls-verify

In package.json scripts

{
  "scripts": {
    "api:generate": "g1-api-generator https://localhost:7205/openapi/schema.json src/lib/api"
  }
}

Programmatic Usage

import { generateApiClient } from '@g1/api-generator';

async function generateApi() {
  await generateApiClient({
    schemaUrl: 'https://your-api-server/openapi/schema.json',
    outputDir: './src/lib/api',
    skipTlsVerify: true, // Optional, default: false
    runPostProcessing: true // Optional, default: true
  });
}

generateApi().catch(console.error);

Generated Code Structure

The generated code is organized as follows:

  • models/ - TypeScript interfaces for API models
  • services/ - API client services for making requests
  • core/ - Core functionality for the API client
  • open-api.json - The downloaded and processed OpenAPI schema

Post-Processing

The tool automatically applies the following post-processing to the generated code:

  1. Removes import statements for void type
  2. Replaces usages of void as a type with any
  3. Detects and replaces self-referencing DTOs with any
  4. Adds auto-generated comments to files

License

UNLICENSED - Private package for Generation One use only.