1 Star 0 Fork 0

人间 / django-rest-tsg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT
https://codecov.io/gh/jinkanhq/django-rest-tsg/branch/main/graph/badge.svg?token=LX8E3QB541

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 All str | Path
enforce_uppercase Enum bool (False)
MIT License Copyright (c) 2021 Yinian Chin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A typescript code generator for Django Rest Framework. 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/jinkanhq/django-rest-tsg.git
git@gitee.com:jinkanhq/django-rest-tsg.git
jinkanhq
django-rest-tsg
django-rest-tsg
main

搜索帮助