Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_writes_ts-1.0.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_writes_ts-1.0.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file py_writes_ts-1.0.1.tar.gz.

File metadata

  • Download URL: py_writes_ts-1.0.1.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for py_writes_ts-1.0.1.tar.gz
Algorithm Hash digest
SHA256 56805b4c8c51242b45d26c9f43fc803002478d5ec445fa70ad1bc4d975d65d61
MD5 ead64a14c90064718eb49219bec76ddd
BLAKE2b-256 70964392bc35f953606fddc98fd07d689b1377615fd40ea4c8b98eb9bdd08919

See more details on using hashes here.

File details

Details for the file py_writes_ts-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: py_writes_ts-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for py_writes_ts-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1c9d731991c87ffcf1e8de8db61e98b2f5f9299246c1676e59b4fbcd84cb12b7
MD5 39eea424e700aaef90e5c0ec2df9186e
BLAKE2b-256 91ca9e39dba9ed8172a5e9ae159feeaced52a15acec56584c5d2fbb53979943c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page