django-typomatic


Namedjango-typomatic JSON
Version 1.1.0 PyPI version JSON
download
home_pagehttps://github.com/adenh93/django-typomatic
SummaryA simple solution for generating Typescript interfaces from your Django Rest Framework Serializers.
upload_time2020-10-18 06:14:54
maintainer
docs_urlNone
authorAden Herold
requires_python
license
keywords django django rest framework drf typescript python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # django-typomatic ![pypi badge](https://img.shields.io/pypi/v/django-typomatic)

_A simple solution for generating Typescript interfaces from your [Django Rest Framework Serializers](https://www.django-rest-framework.org/api-guide/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](https://github.com/adenh93/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_

```python
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_

```typescript
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_

```python
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_

```typescript
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_

```python
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_

```python
...
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_

```typescript
export interface Foo {
  foo: string
}

export interface Bar {
  bar: string
}
```

_external.ts_

```typescript
export interface FooBar {
  foo_bar: string
}
```



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/adenh93/django-typomatic",
    "name": "django-typomatic",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "Django,Django Rest Framework,DRF,Typescript,Python",
    "author": "Aden Herold",
    "author_email": "aden.herold1@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/d1/66/6f197f438eac377f4bd6a5dbec9a71da9c9c643f59b4b6251d3d29fd05b7/django-typomatic-1.1.0.tar.gz",
    "platform": "any",
    "description": "# django-typomatic ![pypi badge](https://img.shields.io/pypi/v/django-typomatic)\n\n_A simple solution for generating Typescript interfaces from your [Django Rest Framework Serializers](https://www.django-rest-framework.org/api-guide/serializers/)._\n\nSince I now require a simple package to generate Typescript interfaces for Django Rest Framework serializers, I've decided to port over my [Typemallow](https://github.com/adenh93/typemallow/) package for use with DRF serializers!\n\n### Usage:\n\n_Using django-typomatic is just as simple!_\n\nFirst, install the package\n`pip install django-typomatic`\n\nNext, 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.\n\nAll 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.\n\n_main.py_\n\n```python\nfrom django_typomatic import ts_interface, generate_ts\nfrom rest_framework import serializers\n\n\n@ts_interface()\nclass Foo(serializers.Serializer):\n    some_field = serializers.CharField()\n    another_field = serializers.DateTimeField()\n\n\ngenerate_ts('./output.ts')\n```\n\n_output.ts_\n\n```typescript\nexport interface Foo {\n  some_field: string\n  another_field: date\n}\n```\n\n_django-typomatic_ supports nested serializers, as well as list fields and other fields that act as lists (any field with many=True)\n\n_main.py_\n\n```python\nfrom django_typomatic import ts_interface, generate_ts\nfrom rest_framework import serializers\n\n\n@ts_interface()\nclass Foo(serializers.Serializer):\n    some_field = serializers.ListField(child=serializers.IntegerField())\n    another_field = serializers.CharField()\n\n\n@ts_interface()\nclass Bar(serializers.Serializer):\n    foo = Foo()\n    foos = Foo(many=True)\n    bar_field = serializers.CharField()\n```\n\n_output.ts_\n\n```typescript\nexport interface Foo {\n  some_field: number[]\n  another_field: string\n}\n\nexport interface Bar {\n  foo: Foo\n  foos: Foo[]\n  bar_field: string\n}\n```\n\nAlternatively, you can call `get_ts()`, which will return the generated interfaces as a raw string, rather than writing the results to a file:\n\n_main.py_\n\n```python\nfrom django_typomatic import ts_interface, get_ts\nfrom rest_framework import serializers\n\n\n@ts_interface()\nclass Foo(serializers.Serializer):\n    some_field = serializers.ListField(child=serializers.IntegerField())\n    another_field = serializers.CharField()\n\nprint(get_ts())\n```\n\nwhich outputs the following string:\n\n`export interface Foo {\\n some_field: number[];\\n another_field: string;\\n}\\n\\n`\n\n### Extended Usage:\n\nThe `@ts_interface()` decorator function accepts an optional parameter, _context_, which defaults to... well... 'default'.\n\n\"_Why is this the case?_\"\n\nWhen 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.\n\nThis 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.\n\nThis is useful if you wish to output different contexts to different files, e.g.\n\n_main.py_\n\n```python\n...\nfrom django_typomatic import ts_interface, generate_ts\nfrom rest_framework import serializers\n\n\n@ts_interface(context='internal')\nclass Foo(serializers.Serializer):\n    foo = serializers.CharField()\n\n\n@ts_interface(context='internal')\nclass Bar(serializers.Serializer):\n    bar = serializers.CharField()\n\n\n@ts_interface(context='external')\nclass FooBar(serializers.Serializer):\n    foo_bar = serializers.CharField()\n\n\n'''\nwe're telling django-typomatic that we only want to generate interfaces from serializers with\nan 'internal' context to './internal.ts'\n'''\ngenerate_ts('./internal.ts', context='internal')\n\n'''\nonly generate interfaces from serializers with an 'external' context to './external.ts'\n'''\ngenerate_ts('./external.ts', context='external')\n```\n\n_internal.ts_\n\n```typescript\nexport interface Foo {\n  foo: string\n}\n\nexport interface Bar {\n  bar: string\n}\n```\n\n_external.ts_\n\n```typescript\nexport interface FooBar {\n  foo_bar: string\n}\n```\n\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A simple solution for generating Typescript interfaces from your Django Rest Framework Serializers.",
    "version": "1.1.0",
    "split_keywords": [
        "django",
        "django rest framework",
        "drf",
        "typescript",
        "python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "8161f4a508d6266882cb9075d7f959de",
                "sha256": "b54557bb4fccc39ede27e9058f5e7b7f11f954ee36b38cc8ee6f11ce1b868dba"
            },
            "downloads": -1,
            "filename": "django_typomatic-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8161f4a508d6266882cb9075d7f959de",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 17544,
            "upload_time": "2020-10-18T06:14:52",
            "upload_time_iso_8601": "2020-10-18T06:14:52.659928Z",
            "url": "https://files.pythonhosted.org/packages/ae/ba/c59fb0b2172d9f7bf27f16f3a1f414693ffb31b9ea14795aacdb55cb1315/django_typomatic-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "536b258172377a9a051ec2356cb7f415",
                "sha256": "e3dbe0f5f5a53b7a36c39f656a63bce2c6c27b733715d9092ab586feac33f981"
            },
            "downloads": -1,
            "filename": "django-typomatic-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "536b258172377a9a051ec2356cb7f415",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4819,
            "upload_time": "2020-10-18T06:14:54",
            "upload_time_iso_8601": "2020-10-18T06:14:54.089114Z",
            "url": "https://files.pythonhosted.org/packages/d1/66/6f197f438eac377f4bd6a5dbec9a71da9c9c643f59b4b6251d3d29fd05b7/django-typomatic-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2020-10-18 06:14:54",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": null,
    "github_project": "adenh93",
    "error": "Could not fetch GitHub repository",
    "lcname": "django-typomatic"
}
        
Elapsed time: 0.15303s