CLI Swagger Generator - sebamar88/bytekit GitHub Wiki

📜 CLI Swagger Generator

🇺🇸 English: Generate all TypeScript DTOs from an OpenAPI/Swagger specification in a single command. Supports OpenAPI 3.x and Swagger 2.x, with automatic spec discovery from documentation pages. 🇪🇸 Español: Genera todos los DTOs TypeScript a partir de una especificación OpenAPI/Swagger en un solo comando. Soporta OpenAPI 3.x y Swagger 2.x, con descubrimiento automático del spec desde páginas de documentación.


¿Qué hace? / What Does It Do?

ES: El modo --swagger del CLI de Bytekit descarga un spec OpenAPI/Swagger, lee todos los schemas definidos y genera un archivo TypeScript con interfaces y tipos para cada uno. Si le pasas una URL de documentación (Swagger UI), intentará encontrar el JSON del spec automáticamente.

EN: The --swagger mode of the Bytekit CLI downloads an OpenAPI/Swagger spec, reads all defined schemas, and generates a TypeScript file with interfaces and types for each one. If you pass a documentation URL (Swagger UI), it will try to find the spec JSON automatically.

bytekit --swagger https://petstore.swagger.io/v2/swagger.json
# ✅ Successfully generated 6 types!
# 📝 Output: src/types/api-docs.ts

Uso desde la terminal / Terminal Usage

bytekit --swagger <url>

ES: La URL puede ser:

  • Un enlace directo al JSON del spec (/swagger.json, /openapi.json)
  • Una URL de Swagger UI / documentación (el CLI intentará descubrir el spec)

EN: The URL can be:

  • A direct link to the spec JSON (/swagger.json, /openapi.json)
  • A Swagger UI / documentation URL (the CLI will try to discover the spec)

⚠️ v3 — HTTPS enforcement / Forzado de HTTPS

EN: Since v3, the CLI rejects remote http:// URLs. Only https:// is allowed for remote hosts. http://localhost and loopback addresses (127.0.0.1, ::1) remain allowed for local development.

ES: Desde v3, el CLI rechaza URLs remotas con http://. Solo se permite https:// para hosts remotos. http://localhost y direcciones de loopback (127.0.0.1, ::1) siguen permitidas para desarrollo local.


Ejemplos / Examples

Spec directo / Direct Spec

# Swagger 2.x
bytekit --swagger https://petstore.swagger.io/v2/swagger.json

# OpenAPI 3.x
bytekit --swagger https://api.example.com/openapi.json

Desde una página de documentación / From a Documentation Page

# Swagger UI page — auto-discovers the JSON spec
bytekit --swagger https://petstore.swagger.io

# Docs page with /docs suffix
bytekit --swagger https://api.example.com/docs

ES: Cuando la respuesta es HTML, el CLI prueba automáticamente estas rutas comunes:

EN: When the response is HTML, the CLI automatically tries these common paths:

/openapi.json
/swagger.json
/v2/api-docs
/api-docs

API interna / Internal API

ES: Internamente, el CLI delega en la función generateFromSwagger del módulo src/cli/swagger-generator.ts.

EN: Internally, the CLI delegates to the generateFromSwagger function from the src/cli/swagger-generator.ts module.

generateFromSwagger(options: SwaggerOptions)

interface SwaggerOptions {
  url: string | URL;   // Swagger/OpenAPI spec URL
  output?: string;     // Default: 'src/types/api-docs.ts'
}

Algoritmo de generación / Generation Algorithm

ES: Paso a paso de lo que sucede cuando ejecutas bytekit --swagger:

EN: Step-by-step breakdown of what happens when you run bytekit --swagger:

1. Hace fetch() a la URL proporcionada
   Performs fetch() to the provided URL

2. Inspecciona el Content-Type de la respuesta
   Inspects the response Content-Type

3. Si es HTML (Swagger UI) → intenta descubrir el spec JSON
   If HTML (Swagger UI) → tries to auto-discover the spec JSON
   Prueba: /openapi.json, /swagger.json, /v2/api-docs, /api-docs
   Tries: /openapi.json, /swagger.json, /v2/api-docs, /api-docs

4. Lee los schemas desde:
   Reads schemas from:
   • spec.components.schemas  (OpenAPI 3.x)
   • spec.definitions         (Swagger 2.x)

5. Para cada schema, genera la interfaz/tipo TypeScript
   For each schema, generates the TypeScript interface/type

6. Escribe todo en un solo archivo de salida
   Writes everything to a single output file

Mapeo de tipos / Type Mapping

ES: Cada tipo del spec OpenAPI se transforma a su equivalente TypeScript. A continuación se muestra la tabla completa de transformaciones.

EN: Each OpenAPI spec type is transformed to its TypeScript equivalent. Below is the complete transformation table.

Tipos básicos / Basic Types

OpenAPI Type Format TypeScript Output
string string
string date string | Date
string date-time string | Date
integer number
number number
boolean boolean
array T[] (element type resolved)
object Record<string, any> (if no properties)

Estructuras complejas / Complex Structures

OpenAPI Construct TypeScript Output Ejemplo / Example
properties + required export interface with optional/required fields See below
enum Union type literal 'active' | 'inactive' | 'pending'
$ref Resolved type name Category
oneOf / anyOf Union type (Cat | Dog)
allOf Intersection type (BaseModel & UserFields)
additionalProperties (no props) Record<string, T> Record<string, string>
additionalProperties (with props) extends Record<string, T> interface Foo extends Record<...>

Ejemplos de transformación / Transformation Examples

Interface con campos requeridos y opcionales / Interface with Required and Optional Fields

OpenAPI Schema:

{
  "Pet": {
    "type": "object",
    "required": ["id", "name"],
    "properties": {
      "id": { "type": "integer" },
      "name": { "type": "string" },
      "status": {
        "type": "string",
        "enum": ["available", "pending", "sold"]
      },
      "tag": { "type": "string" }
    }
  }
}

TypeScript generado / Generated TypeScript:

export interface Pet {
  id: number;
  name: string;
  status?: 'available' | 'pending' | 'sold';
  tag?: string;
}

Enum

OpenAPI Schema:

{
  "OrderStatus": {
    "type": "string",
    "enum": ["placed", "approved", "delivered"]
  }
}

TypeScript:

export type OrderStatus = 'placed' | 'approved' | 'delivered';

$ref — Referencias / References

OpenAPI Schema:

{
  "Order": {
    "type": "object",
    "properties": {
      "id": { "type": "integer" },
      "pet": { "$ref": "#/components/schemas/Pet" },
      "status": { "$ref": "#/components/schemas/OrderStatus" }
    }
  }
}

TypeScript:

export interface Order {
  id?: number;
  pet?: Pet;
  status?: OrderStatus;
}

oneOf / anyOf — Tipos unión / Union Types

OpenAPI Schema:

{
  "Response": {
    "oneOf": [
      { "$ref": "#/components/schemas/SuccessResult" },
      { "$ref": "#/components/schemas/ErrorResult" }
    ]
  }
}

TypeScript:

export type Response = (SuccessResult | ErrorResult);

allOf — Tipos intersección / Intersection Types

OpenAPI Schema:

{
  "AdminUser": {
    "allOf": [
      { "$ref": "#/components/schemas/BaseUser" },
      {
        "type": "object",
        "properties": {
          "permissions": {
            "type": "array",
            "items": { "type": "string" }
          }
        }
      }
    ]
  }
}

TypeScript:

export type AdminUser = (BaseUser & Record<string, any>);

Record types — additionalProperties

OpenAPI Schema:

{
  "Metadata": {
    "type": "object",
    "additionalProperties": { "type": "string" }
  }
}

TypeScript:

export type Metadata = Record<string, string>;

additionalProperties con properties propias / With Own Properties

OpenAPI Schema:

{
  "ExtendedUser": {
    "type": "object",
    "properties": {
      "id": { "type": "integer" },
      "name": { "type": "string" }
    },
    "additionalProperties": { "type": "string" }
  }
}

TypeScript:

export interface ExtendedUser extends Record<string, string> {
  id?: number;
  name?: string;
}

Sanitización de nombres / Name Sanitization

ES: Los nombres de los schemas se sanitizan eliminando todos los caracteres que no sean alfanuméricos. Esto asegura que los identificadores TypeScript generados sean válidos.

EN: Schema names are sanitized by removing all non-alphanumeric characters. This ensures the generated TypeScript identifiers are valid.

Original Name Sanitized Name
Pet Pet
api-response apiresponse
User.Profile UserProfile
data[0] data0

Uso programático / Programmatic Usage

ES: También puedes usar la función directamente en código:

EN: You can also use the function directly in code:

import { generateFromSwagger } from "bytekit/cli/swagger-generator";

await generateFromSwagger({
  url: "https://petstore.swagger.io/v2/swagger.json",
  output: "src/types/petstore.ts",
});

Archivo de salida / Output File

ES: El archivo generado incluye un comentario con la fecha de generación y todas las interfaces/tipos:

EN: The generated file includes a timestamp comment and all interfaces/types:

/**
 * Auto-generated types from Swagger/OpenAPI
 * Generated at 2026-03-27T12:00:00.000Z
 */

export interface Category {
  id?: number;
  name?: string;
}

export interface Tag {
  id?: number;
  name?: string;
}

export interface Pet {
  id: number;
  name: string;
  category?: Category;
  photoUrls: string[];
  tags?: Tag[];
  status?: 'available' | 'pending' | 'sold';
}

export type OrderStatus = 'placed' | 'approved' | 'delivered';

export interface Order {
  id?: number;
  petId?: number;
  quantity?: number;
  shipDate?: string | Date;
  status?: OrderStatus;
  complete?: boolean;
}

Errores comunes / Common Errors

Error Cause (EN) Causa (ES)
Could not automatically find the OpenAPI/Swagger JSON Passed a Swagger UI URL but the CLI couldn't discover the spec at common paths Se pasó una URL de Swagger UI pero el CLI no pudo encontrar el spec en las rutas comunes
No schemas or definitions found The spec has no components.schemas (v3) or definitions (v2) El spec no tiene components.schemas (v3) ni definitions (v2)
HTTP 404: Not Found The spec URL is incorrect or the server is down La URL del spec es incorrecta o el servidor está caído
Unexpected token < in JSON The URL returned an HTML page that the auto-discovery also couldn't resolve La URL devolvió HTML y el descubrimiento automático tampoco pudo resolverlo

OpenAPI 3.x vs Swagger 2.x

ES: El generador soporta ambas versiones. La diferencia principal es la ubicación de los schemas:

EN: The generator supports both versions. The main difference is where schemas live:

Feature OpenAPI 3.x Swagger 2.x
Schemas location components.schemas definitions
Spec file openapi.json swagger.json
Common discovery path /openapi.json /swagger.json, /v2/api-docs

ES: El CLI detecta automáticamente la versión y lee los schemas de la ubicación correcta.

EN: The CLI automatically detects the version and reads schemas from the correct location.


Véase También / See Also

  • CLI Overview — Vista general de todos los modos del CLI / Overview of all CLI modes
  • CLI Type Generator — Generar tipos desde respuestas de API individuales / Generate types from individual API responses
  • ApiClient — Cliente HTTP de Bytekit / Bytekit HTTP client
  • SchemaAdapter — Adaptador y validación de esquemas / Schema adapter and validation
⚠️ **GitHub.com Fallback** ⚠️