Skip to main content

Package for generating TypeScript types from DRF serializers

Project description

DRF Typescript generator

This package allows you to generate typescript types / interfaces for Django REST framework serializers, which can be then simply used in frontend applications.

Setup

Install the package with your preferred dependency management tool:

$ poetry add drf-typescript-generator

Add drf_typescript_generator to INSTALLED_APPS in your django settings.py file:

INSTALLED_APPS = [
    ...
    'drf_typescript_generator',
    ...
]

Usage

To generate types run django management command generate_types with the names of django apps you want the script to look for serializers in:

$ python manage.py generate_types my_app

Example serializer found in my_app:

class MySerializer(serializers.Serializer):
    some_string = serializers.CharField(max_length=100)
    some_number = serializers.IntegerField()
    some_boolean = serializers.BooleanField()
    choice = serializers.ChoiceField(
        choices=[1, 2, 3],
        allow_null=True
    )
    multichoice = serializers.MultipleChoiceField(
        choices=[2, 3, 5]
    )

Generated typescript type:

export type MySerializer = {
  choice: 1 | 2 | 3 | null
  multichoice: (2 | 3 | 5)[]
  someBoolean: boolean
  someNumber: number
  someString: string
}

The script looks for DRF routers in project urls.py file as well as urls.py files in given apps and extracts serializers based on viewsets defined in those routers.

Arguments

The generate_types command supports following arguments:

Argument Value type Description Default value
--format "type" | "interface" Whether to output typescript types or interfaces "type"
--semicolons boolean If the argument is present semicolons will be added in output False
--spaces int Output indentation will use given number of spaces (mutually exclusive with --tabs). Spaces are used if neither --spaces nor --tabs argument is present. 2
--tabs int Output indentation will use given number of tabs (mutually exclusive with --spaces) None

Features

The package currently supports following features that are correctly transformed to typescript syntax:

  • Basic serializers
  • Model serializers
  • Nested serializers
  • Method fields (typed with correct type if python type hints are used)
  • Required / optional fields
  • List fields
  • Choice and multiple choice fields (results in composite typescript type)
  • allow_blank, allow_null (results in composite typescript type)

More features are planned to add later on:

  • One to many and many to many fields correct typing
  • Differentiate between read / write only fields while generating type / interface
  • Integration with tools like drf_yasg to allow downloading the generated type from the documentation of the API
  • Accept custom mappings

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

drf-typescript-generator-0.1.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

drf_typescript_generator-0.1.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file drf-typescript-generator-0.1.1.tar.gz.

File metadata

  • Download URL: drf-typescript-generator-0.1.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.4 Darwin/19.6.0

File hashes

Hashes for drf-typescript-generator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 04981a9789da9b59a8ae43e33520fa7e26a48b47e5f162a246e31db746ab77f6
MD5 69b6d2ebe99fc694a3cb3d2bde39066e
BLAKE2b-256 17ed8872257fa460267fdc1e8171deee1d2a5cefd1100bf7d4965100f0f47b5c

See more details on using hashes here.

File details

Details for the file drf_typescript_generator-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_typescript_generator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1ff7e387fa060538f832064f71279786ff5849de78910319e3c882b67363053f
MD5 ed2ace006265c01992f8595e56104d1c
BLAKE2b-256 42f2c33b3b4c2450d1a92294d1c4832d45dc8b0e257156268f2daf678db89ffc

See more details on using hashes here.

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