Generate typescript code from python
Project description
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_writes_ts-1.0.0.tar.gz.
File metadata
- Download URL: py_writes_ts-1.0.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
789b4a92acd65f0e07b16c6be2da4c1eef32eb824f8770873a559854e9860018
|
|
| MD5 |
84e28be2c8614fcb113196b5ea3f178e
|
|
| BLAKE2b-256 |
c94a9b204db5315b4313b673b07d8d94b63b934188394e82b1fdaa1299b1591f
|
File details
Details for the file py_writes_ts-1.0.0-py3-none-any.whl.
File metadata
- Download URL: py_writes_ts-1.0.0-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51e044941ba862715a479e621a0fa15ea416a99c6acd566fd9f5f31e8ebbc844
|
|
| MD5 |
148b01a6e45baf35bc7673327aee681d
|
|
| BLAKE2b-256 |
9ad95f46cfd9af918d98f2ec00a88a506232a079e84ac4414249471924774813
|