A typescript code generator for Django Rest Framework.
Project description
django-rest-tsg
A TypeScript code generator for Django Rest Framework, which saved your hand-working and guaranteed consistency between Python codes and modern frontend codes written in TypeScript.
Features
It generates TypeScript codes from 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_tsg
Put a tsgconfig.py file with build tasks into your django project’s root.
from django.conf import settings
from django_rest_tsg.build import build
BUILD_DIR = settings.BASE_DIR / "app/src/core"
BUILD_TASKS = [
build(Foo),
build(BarSerializer, {"alias": "Foobar"}),
]
Add django_rest_tsg to your INSTALLED_APPS.
INSTALLED_APPS = [
...
"django_rest_tsg"
]
Run 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
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 (TODO) |
All |
str | 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 Distributions
Built Distribution
Hashes for django_rest_tsg-0.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de77c1d80d3ad9f7141ec69a8ebf57851a7337f1528b3e477a1a7b913e5a0853 |
|
MD5 | b996fec110bc3286f541b66c53a884d4 |
|
BLAKE2b-256 | 484251bb96931d4808e39ef4e6b08cebcd8b2b8af682ad991796541a70d9b2cd |