Skip to content

devraselmahmud/TypeScript-Converter

Repository files navigation

DTO to TypeScript Converter

A robust and flexible tool designed to convert Java and C# Data Transfer Objects (DTOs) into TypeScript interfaces. This project bridges the gap between backend models and frontend type safety, offering both deterministic regex-based conversion and AI-powered fallback using Ollama.

🚀 Features

  • Dual-Mode Conversion:
    • Deterministic: Fast and accurate regex-based parsing for standard Java/C# syntax.
    • AI-Powered: Optional fallback to Ollama (LLM) for complex or non-standard code structures.
  • Multi-Language Support:
    • Java: Handles classes, records, interfaces, and common types (List, Map, etc.).
    • C#: Supports classes, properties, and nullable types.
  • Smart Type Mapping: Automatically maps backend types (e.g., BigDecimal, LocalDateTime, Dictionary) to their TypeScript equivalents.
  • Nullability Handling: Respects @NotNull, [Required], and nullable markers to generate accurate optional/required fields.
  • Multiple Interfaces:
    • Web UI: A user-friendly Flask-based web interface for quick conversions.
    • CLI: A command-line tool for file-based processing and automation.

📋 Prerequisites

  • Python: Version 3.8 or higher.
  • Ollama (Optional): Required only if you intend to use the AI fallback feature.
    • Install Ollama
    • Pull a model (default is deepseek-coder): ollama pull deepseek-coder

🛠️ Installation

  1. Clone the repository:

    git clone <repository-url>
    cd TypeScript-Converter
  2. Create a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies:

    pip install -r requirements.txt

💻 Usage

Web Interface

Start the Flask application to use the graphical interface.

  1. Run the app:
    python app.py
  2. Open your browser and navigate to http://localhost:5000.
  3. Paste your Java or C# code into the input box.
  4. (Optional) Check "Use Ollama Fallback" for AI-assisted conversion.
  5. Click Convert to see the TypeScript result.

Command Line Interface (CLI)

Use the CLI tool to convert files directly from your terminal.

python cli.py <input_file> [OPTIONS]

Arguments:

  • input_file: Path to the Java or C# file to convert.

Options:

  • --ollama: Enable Ollama LLM fallback for generation/cleaning.

Example:

python cli.py models/UserDto.java --ollama

⚙️ Configuration

You can configure the Ollama integration using environment variables:

  • OLLAMA_HOST: The URL of your Ollama instance.
    • Default (Local): http://localhost:11434
    • Default (Docker): http://ollama:11434
  • FLASK_HOST: The interface to bind the web server to.
    • Default (Local): 127.0.0.1
    • Default (Docker): 0.0.0.0
  • OLLAMA_MODEL: The model to use for conversion (default: deepseek-coder).

Example:

export OLLAMA_MODEL=llama3
python app.py

📂 Project Structure

  • app.py: Flask web application entry point.
  • cli.py: Command-line interface entry point.
  • converter.py: Core logic for regex-based parsing and type conversion.
  • ollama_client.py: Client for interacting with the Ollama API.
  • templates/: HTML templates for the web UI.
  • static/: Static assets (CSS, JS).

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

Convert Java/CSharp Entity/Dto to TypeScript Interface

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published