Skip to main content

Generate TypeScript clients from Django APIs

Project description

Django TypeScript API Generator

A zero-dependency Django plugin that automatically generates TypeScript interfaces and documentation from your Django models.

Features

  • Zero Dependencies: Uses only Django and Python standard libraries
  • Type-Safe Interfaces: Generate TypeScript interfaces from Django models
  • Comprehensive Documentation: Automatic JSDoc generation with field descriptions and validation rules
  • Validation Constraints: Extracts min/max length, value constraints, regex patterns, and choices
  • Highly Configurable: Customize through Django settings or command-line options
  • Scalable Architecture: Built with design patterns for future extensibility

Installation

Add to your INSTALLED_APPS:

# settings.py
INSTALLED_APPS = [
    # ...
    'django_typescript_api',
]

Configure (optional):

# settings.py
DJANGO_TYPESCRIPT_API = {
    'OUTPUT_PATH': 'frontend/src/api/generated.ts',
    'INCLUDE_DOCS': True,
    'INCLUDE_VALIDATORS': True,
    'TYPE_MAPPINGS': {
        'DecimalField': 'string',  # Override default mapping
    }
}

Usage

# Generate TypeScript interfaces
python manage.py generate_ts_client

# Specific output file
python manage.py generate_ts_client --output frontend/src/api/models.ts

# Only specific apps
python manage.py generate_ts_client --apps blog users

# Skip documentation
python manage.py generate_ts_client --no-docs

# Skip validation constraints
python manage.py generate_ts_client --no-validators

# Verbose output with details
python manage.py generate_ts_client --verbose

# Dry run (preview without writing)
python manage.py generate_ts_client --dry-run

Example Output

// Generated by django-typescript-api
// DO NOT EDIT THIS FILE DIRECTLY

/**
 * Blog post
 * @interface Post
 */
export interface Post {
  /**
   * Post title
   * @minLength 5
   * @maxLength 200
   */
  title: string;
  
  /**
   * Publication status
   * @enum {'draft', 'published', 'archived'}
   */
  status: 'draft' | 'published' | 'archived';
  
  /**
   * When the post was created
   */
  created_at: string;
}

export interface User {
  // ... with similar documentation
}

Supported Field Types

  • String fields: CharField, TextField, EmailField, SlugField, etc.
  • Numeric fields: IntegerField, FloatField, DecimalField, etc.
  • Boolean fields: BooleanField
  • Date/time fields: DateField, DateTimeField, TimeField
  • Relations: ForeignKey, OneToOneField, ManyToManyField
  • Special fields: UUIDField, JSONField, FileField

Configuration Options

Setting Default Description
OUTPUT_PATH frontend/src/api/generated.ts Output file path
INCLUDE_DOCS True Generate JSDoc documentation
INCLUDE_VALIDATORS True Include validation constraints
TYPE_MAPPINGS {} Custom Django-to-TypeScript type mappings
CUSTOM_HEADERS Predefined Custom file header comments

Roadmap

  • Phase 2: API endpoint generation from DRF viewsets
  • Phase 3: Query parameter typing and advanced features
  • Phase 4: Performance optimizations and extensibility hooks

Contributing

This is currently in active development. The architecture is designed with scalability in mind using Builder, Visitor, and Strategy patterns.

License

MIT License – feel free to use and contribute!


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_typescript_api-0.1.0rc1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_typescript_api-0.1.0rc1-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file django_typescript_api-0.1.0rc1.tar.gz.

File metadata

  • Download URL: django_typescript_api-0.1.0rc1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_typescript_api-0.1.0rc1.tar.gz
Algorithm Hash digest
SHA256 45d5df7204075fc105a32e83d0e94635c92cb8bbf085c5bc9bba1efb7bd7c6ae
MD5 0e496365d7efc7bb0bc78bd2d0100a76
BLAKE2b-256 d3e1ab0fbcc9758147c6de36d120dd2703e57ea4a15ca4df6f73abf4d24bc532

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_typescript_api-0.1.0rc1.tar.gz:

Publisher: pypi_publish.yml on pyenvstudio/django-typescript-api

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_typescript_api-0.1.0rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_typescript_api-0.1.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 45908eda9cf143f8fb7a1adf5d892f418c053928b515748253b1fc3b5a8b0e1d
MD5 994a75cbca5ac7c903d5ba5ff54683f4
BLAKE2b-256 9677696aa0ad7339610ee451b682f163496ba94cd14862bc75a281a215649b90

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_typescript_api-0.1.0rc1-py3-none-any.whl:

Publisher: pypi_publish.yml on pyenvstudio/django-typescript-api

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page