Skip to main content

A simple solution for generating Typescript interfaces from your Django Rest Framework Serializers.

Project description

django-typomatic pypi badge

A simple solution for generating Typescript interfaces from your Django Rest Framework Serializers.

Since I now require a simple package to generate Typescript interfaces for Django Rest Framework serializers, I've decided to port over my Typemallow package for use with DRF serializers!

Usage:

Using django-typomatic is just as simple!

First, install the package pip install django-typomatic

Next, for your Django Rest Framework serializers that you wish to generate Typescript interfaces for, simply import ts_interface and generate_ts from the django_typomatic module, and add the @ts_interface() class decorator to your Django Rest Framework serializer class.

All that is required to generate your Typescript interfaces is to call the generate_ts() function, and provide a filepath as a parameter to output the result.

main.py

from django_typomatic import ts_interface, generate_ts
from rest_framework import serializers


@ts_interface()
class Foo(serializers.Serializer):
    some_field = serializers.CharField()
    another_field = serializers.DateTimeField()


generate_ts('./output.ts')

output.ts

export interface Foo {
  some_field: string
  another_field: date
}

django-typomatic supports nested serializers, as well as list fields and other fields that act as lists (any field with many=True)

main.py

from django_typomatic import ts_interface, generate_ts
from rest_framework import serializers


@ts_interface()
class Foo(serializers.Serializer):
    some_field = serializers.ListField(child=serializers.IntegerField())
    another_field = serializers.CharField()


@ts_interface()
class Bar(serializers.Serializer):
    foo = Foo()
    foos = Foo(many=True)
    bar_field = serializers.CharField()

output.ts

export interface Foo {
  some_field: number[]
  another_field: string
}

export interface Bar {
  foo: Foo
  foos: Foo[]
  bar_field: string
}

Alternatively, you can call get_ts(), which will return the generated interfaces as a raw string, rather than writing the results to a file:

main.py

from django_typomatic import ts_interface, get_ts
from rest_framework import serializers


@ts_interface()
class Foo(serializers.Serializer):
    some_field = serializers.ListField(child=serializers.IntegerField())
    another_field = serializers.CharField()

print(get_ts())

which outputs the following string:

export interface Foo {\n some_field: number[];\n another_field: string;\n}\n\n

Extended Usage:

The @ts_interface() decorator function accepts an optional parameter, context, which defaults to... well... 'default'.

"Why is this the case?"

When a Serializer is identified with with @ts_interface decorator, it is added to a list in a dictionary of serializers, with the dictionary key being the value provided to the context parameter. If you were to provide different contexts for each serializer, additional keys will be created if they do not exist, or the serializer will simply be appended to the list at the existing key.

This comes in handy, as the generate_ts() function also accepts an optional context parameter, which will filter only serializers in the dictionary at the specific key.

This is useful if you wish to output different contexts to different files, e.g.

main.py

...
from django_typomatic import ts_interface, generate_ts
from rest_framework import serializers


@ts_interface(context='internal')
class Foo(serializers.Serializer):
    foo = serializers.CharField()


@ts_interface(context='internal')
class Bar(serializers.Serializer):
    bar = serializers.CharField()


@ts_interface(context='external')
class FooBar(serializers.Serializer):
    foo_bar = serializers.CharField()


'''
we're telling django-typomatic that we only want to generate interfaces from serializers with
an 'internal' context to './internal.ts'
'''
generate_ts('./internal.ts', context='internal')

'''
only generate interfaces from serializers with an 'external' context to './external.ts'
'''
generate_ts('./external.ts', context='external')

internal.ts

export interface Foo {
  foo: string
}

export interface Bar {
  bar: string
}

external.ts

export interface FooBar {
  foo_bar: string
}

Camelize

You can use django dependencies that converts the response from snake_casing to camelCasing. The solution offered for this is camelize:

from django_typomatic import ts_interface, generate_ts
from rest_framework import serializers


@ts_interface()
class Foo(serializers.Serializer):
    some_field = serializers.CharField()


generate_ts('./output.ts', camelize=True)

Different from the main example. The interface attributes are now camel casing.

output.ts

export interface Foo {
  someField: string
}

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-typomatic-1.5.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

django_typomatic-1.5.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file django-typomatic-1.5.1.tar.gz.

File metadata

  • Download URL: django-typomatic-1.5.1.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

File hashes

Hashes for django-typomatic-1.5.1.tar.gz
Algorithm Hash digest
SHA256 9afae5df70871d9f45f13d1c0fddd7e7d6056229c230606b321982b5bbbbdc27
MD5 4cd15253f0955f164191668f3d40bd6b
BLAKE2b-256 f7654631cce77e3c31f266e27852bb789e3c747a6d1bcb4610ada49ad9459859

See more details on using hashes here.

File details

Details for the file django_typomatic-1.5.1-py3-none-any.whl.

File metadata

  • Download URL: django_typomatic-1.5.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

File hashes

Hashes for django_typomatic-1.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 97c0abfb2c2109a6a5eb1d88282c5313159ead0ba50bc2b3259b2804356aedf6
MD5 3f75dce8a7dcf01bd47551a0ec241154
BLAKE2b-256 2c13b3b500e01fc1c96e865ab12b36d20e531360813ebe1fd2d3e8dc310e6ce3

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