Convert your python types to typescript
Project description
Petit ts
A Library to easly convert your python types to typescript types.
It's a part of the petite_stack.
Example
from petit_ts import T
store = TSTypeStore()
class Jeb(Enum):
A = 'R'
UserType = NamedLiteral('admin', 'user')
TestUnion = NamedUnion(str, int)
class CreateUserDto(BaseModel):
username: str
password: Union[str, Jeb]
role: UserType
jeb: TestUnion
store.add_type(CreateUserDto)
res = store.get_repr(CreateUserDto)
print(res)
# >>> "CreateUserDto"
# here we need to do this in order, to get all the required deps into our ts file
not_inlined = store.get_all_not_inlined()
print(not_inlined)
# >>> "type CreateUserDto = {
# username: string;
# password: string | Jeb;
# role: UserType;
# jeb: TestUnion;
# };
# export enum Jeb {
# A = "R",
# };
# type UserType = "admin" | "user";
# type TestUnion = string | number /*int*/"
with open('res.ts', 'w') as f :
# write what you need to the file
final = f'export a = function (a:any): {store.get_repr(CreateUserDto)};'
final += store.get_all_not_inlined()
f.write(final)
Supported types:
- None
- bool
- str
- int
- float
- Dict[K, V]
- List[T]
- List, list
- Dict, dict
- @dataclass, generic @dataclass
- Optional[T]
- Union[A, B, ...], Named(Union[A, B, ...])
- Literal[values], Named(Literal[1, 2, '3']) with values = Union[int, str]
Add support for a custom type
from typing import Tuple, Optional, Dict, Any, get_type_hints
from petit_ts import ClassHandler
from pydantic import BaseModel
class BaseModelHandler(ClassHandler):
@staticmethod
def is_mapping() -> bool:
return True
@staticmethod
def should_handle(cls, store, origin, args) -> bool:
return issubclass(cls, BaseModel)
@staticmethod
def build(cls: BaseModel, store, origin, args) -> Tuple[Optional[str], Dict[str, Any]]:
name = cls.__name__
fields = get_type_hints(cls)
return name, fields
store.add_class_handler(BaseModelHandler)
You have to implement for the ClassHandler
:
- is_mapping
- should_handle
- build
If this is a mapping, you should return the fields, a Dict[str, Any]
else you should return a string
For the BasicHandler :
- build
- should_handle
If you have any problem, don't hesitate to open an issue !
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
petit_ts-0.1.4.tar.gz
(11.9 kB
view details)
Built Distribution
File details
Details for the file petit_ts-0.1.4.tar.gz
.
File metadata
- Download URL: petit_ts-0.1.4.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3825bbd3900130522cbbed3ae5ab36c4da4ac8a886a1951badfeabd541a8dd99 |
|
MD5 | 8e12bab8687c134073715557a15860f5 |
|
BLAKE2b-256 | b8a5aa5506282e78655b73909a816a116e005c9e9db0dfd9852d1502b15d18bf |
File details
Details for the file petit_ts-0.1.4-py2.py3-none-any.whl
.
File metadata
- Download URL: petit_ts-0.1.4-py2.py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18a9f683257d2ed8862eac98c0cfd344aed3dc3c7c2c051f201724e54788201c |
|
MD5 | 135ebf84766dc8032d97e0cf10ca8677 |
|
BLAKE2b-256 | 24a498686aef901da3472d38c200f9ecb8179c2eaea02e8bc22880c1edf02bc1 |