PY WRITES TS
A library of tools to aid in the generation of typescript interfaces and functions from python dataclasses.
Use this to write a script that generates a typescript sdk from your python backend!
Installation
pip install py-writes-ts
Usage
Class to Interface
from py_writes_ts.class_to_interface import generate_typescript_interfaces
from dataclasses import dataclass
@dataclass
class User:
id: int
name: str
age: int
code = generate_typescript_interfaces([User])
print(code)
Output:
export interface User {
id: number;
name: string;
age: number;
}
Function Generator
from py_writes_ts.function_generator import generate_typescript_function
from dataclasses import dataclass
@dataclass
class GetUserByIdRequest:
id: int
@dataclass
class GetUserByIdResponse:
id: int
name: str
age: int
code = generate_typescript_function(
function_name="getUserById",
parameters={
"params": GetUserByIdRequest
},
return_type=GetUserByIdResponse,
valid_refs=[GetUserByIdRequest, GetUserByIdResponse],
body="""
const response = await fetch(`/api/get_user_by_id`, {{
method: "POST",
headers: {{
"Content-Type": "application/json"
}},
body: JSON.stringify(params)
}});
const data = await response.json();
return data;
"""
)
print(code)
Output:
export async function getUserById(
params: GetUserByIdRequest
): Promise<GetUserByIdResponse> {
const response = await fetch(`/api/get_user_by_id`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(params)
});
if (!response.ok) {
throw new Error(`API call failed with status ${response.status}`);
}
const data: GetUserByIdResponse = await response.json();
return data;
}
More examples
Look at the tests for more examples.
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for py-writes-ts 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| py_writes_ts-1.0.1.tar.gz | 11.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_writes_ts-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.5 kB
Release files / py_writes_ts-1.0.1.tar.gz
| Download URL | py_writes_ts-1.0.1.tar.gz |
|---|---|
| Size | 11.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
56805b4c8c51242b45d26c9f43fc803002478d5ec445fa70ad1bc4d975d65d61
|
|
BLAKE2b-256 checksum How to use checksums |
70964392bc35f953606fddc98fd07d689b1377615fd40ea4c8b98eb9bdd08919
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Release files / py_writes_ts-1.0.1-py3-none-any.whl
| Download URL | py_writes_ts-1.0.1-py3-none-any.whl |
|---|---|
| Size | 9.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1c9d731991c87ffcf1e8de8db61e98b2f5f9299246c1676e59b4fbcd84cb12b7
|
|
BLAKE2b-256 checksum How to use checksums |
91ca9e39dba9ed8172a5e9ae159feeaced52a15acec56584c5d2fbb53979943c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|