A typescript code generator for Django Rest Framework.
Project description
django-rest-typescript-generator
A TypeScript code generator for Django Rest Framework, which saves your hand-working and guarantees consistency between Python codes and modern frontend codes written in TypeScript.
Features
It generates TypeScript codes from the following Python types.
- Django REST Framework serializers: manual working on
Serializer
,ModelSerializer
derived from Django ORM models,DataclassSerializer
via djangorestframework-dataclasses. - Python dataclasses: Classes decorated by
dataclasses.dataclass
. - Python enums: Subclasses of
enum.Enum
.
It also supports nested types and composite types.
Requirements
- Python >=3.9
- Django >=3.0
- Django REST Framework >=3.12
Usage
Install using pip
.
$ pip install django-rest-typescript-generator
Put a tsgconfig.py file with build tasks into your Django project's root.
from django.conf import settings
from django_rest_typescript_generator.build import build
BUILD_DIR = settings.BASE_DIR / "app/src/core"
BUILD_TASKS = [
build(Foo),
build(BarSerializer, {"alias": "Foobar"}),
]
Add django_rest_typescript_generator to your INSTALLED_APPS.
INSTALLED_APPS = [
...
"django_rest_typescript_generator"
]
Run the buildtypescript command on manage.py.
$ python manage.py buildtypescript
Or you can switch to another place.
$ python manage.py buildtypescript --build-dir /somewhere/you/cannot/explain
Or you can switch to another place.
$ python manage.py buildtypescript --build-dir /somewhere/you/cannot/explain
Examples
Input: Serializer
class PathSerializer(serializers.Serializer):
name = serializers.CharField()
suffix = serializers.CharField()
suffixes = serializers.ListField(child=serializers.CharField())
stem = serializers.CharField()
is_directory = serializers.BooleanField(source="is_dir")
size = serializers.IntegerField(source="stat.st_size")
Output: Interface
export interface Path {
name: string;
suffix: string;
suffixes: string[];
stem: string;
isDirectory: boolean;
size: number;
}
There are more examples in test cases.
Build Options
All options are listed in the table below.
Name | Context | Value |
---|---|---|
alias | All | str |
build_dir | All | str or Path |
enforce_uppercase | Enum | bool (False) |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_rest_typescript_generator-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5d5c2dc8a31a0f85152ffc153a4f5c088ed2f5a33b706dbb6dcc69de710f6ea |
|
MD5 | f32af91003302bdbf4142014938ba31a |
|
BLAKE2b-256 | 782db8417a1dcc7e80f6b2b527018cfd314b1623d637edcf817a726ab2861723 |
Hashes for django_rest_typescript_generator-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78a7ff6bf991fe49c618c4aacc8b3c235e6172cc3468652b5b39dc5ad193d122 |
|
MD5 | 7309413149e79f226bd665d038bab4c0 |
|
BLAKE2b-256 | e932c9222f589f6045d8a5461b198c56ea4bd69011c1daeca429fcb6de60f3df |