### Install pydantic-to-typescript CLI Tool Source: https://github.com/phillipdupuis/pydantic-to-typescript/blob/master/README.md This command installs the `pydantic-to-typescript` command-line interface tool using pip. It is the standard way to get the utility set up in your Python environment. ```bash pip install pydantic-to-typescript ``` -------------------------------- ### Consume API with Generated TypeScript Interfaces Source: https://github.com/phillipdupuis/pydantic-to-typescript/blob/master/README.md This TypeScript example demonstrates how to use the generated interfaces (LoginCredentials, LoginResponseData) in a frontend application. It shows an asynchronous function for making a POST request to a login endpoint, handling both successful responses and errors with type-safe data. ```ts import { LoginCredentials, LoginResponseData } from "./apiTypes.ts"; async function login( credentials: LoginCredentials, resolve: (data: LoginResponseData) => void, reject: (error: string) => void ) { try { const response: Response = await fetch("/login/", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(credentials), }); const data: LoginResponseData = await response.json(); resolve(data); } catch (error) { reject(error.message); } } ``` -------------------------------- ### pydantic-to-typescript CLI Arguments Reference Source: https://github.com/phillipdupuis/pydantic-to-typescript/blob/master/README.md This section details the available command-line arguments for the `pydantic-to-typescript` tool. These options allow users to specify input modules, output file paths, models to exclude, and custom `json2ts` commands for conversion. ```APIDOC pydantic-to-typescript CLI: --module: Description: name or filepath of the python module you would like to convert. All the pydantic models within it will be converted to typescript interfaces. Discoverable submodules will also be checked. --output: Description: name of the file the typescript definitions should be written to. Ex: './frontend/apiTypes.ts' --exclude: Description: name of a pydantic model which should be omitted from the resulting typescript definitions. This option can be defined multiple times, ex: --exclude Foo --exclude Bar to exclude both the Foo and Bar models from the output. --json2ts-cmd: Description: optional, the command used to invoke json2ts. The default is 'json2ts'. Specify this if you have it installed locally (ex: 'yarn json2ts') or if the exact path to the executable is required (ex: /myproject/node_modules/bin/json2ts) ``` -------------------------------- ### Convert Pydantic Models to TypeScript Definitions Source: https://github.com/phillipdupuis/pydantic-to-typescript/blob/master/README.md This section provides various methods to convert defined Pydantic models into TypeScript definitions. It shows command-line usage with pydantic2ts by specifying the module or file path, as well as programmatic conversion using the generate_typescript_defs function in Python. ```bash $ pydantic2ts --module backend.api --output ./frontend/apiTypes.ts ``` ```bash $ pydantic2ts --module ./backend/api.py --output ./frontend/apiTypes.ts ``` ```python from pydantic2ts import generate_typescript_defs generate_typescript_defs("backend.api", "./frontend/apiTypes.ts") ``` -------------------------------- ### Upgrade pydantic-to-typescript for Pydantic V2 Compatibility Source: https://github.com/phillipdupuis/pydantic-to-typescript/blob/master/README.md Use this command to upgrade `pydantic-to-typescript` to version 2 or higher. This ensures compatibility and resolves potential issues when working with Pydantic versions greater than 2. ```bash pip install 'pydantic-to-typescript>2' ``` -------------------------------- ### Generated TypeScript Interfaces from Pydantic Models Source: https://github.com/phillipdupuis/pydantic-to-typescript/blob/master/README.md This TypeScript snippet displays the automatically generated interfaces from the Pydantic models. These interfaces provide type definitions for LoginCredentials, LoginResponseData, and Profile, ensuring type safety when interacting with the API on the frontend. ```ts /* tslint:disable */ /** /* This file was automatically generated from pydantic models by running pydantic2ts. /* Do not modify it by hand - just update the pydantic models and then re-run the script */ export interface LoginCredentials { username: string; password: string; } export interface LoginResponseData { token: string; profile: Profile; } export interface Profile { username: string; age?: number; hobbies: string[]; } ``` -------------------------------- ### Define Pydantic Models for FastAPI API Source: https://github.com/phillipdupuis/pydantic-to-typescript/blob/master/README.md This Python snippet demonstrates how to define Pydantic BaseModel classes for a FastAPI application. It includes models for login credentials, user profiles, and API response data, showcasing how to structure data for an API endpoint. ```python from fastapi import FastAPI from pydantic import BaseModel from typing import List, Optional api = FastAPI() class LoginCredentials(BaseModel): username: str password: str class Profile(BaseModel): username: str age: Optional[int] hobbies: List[str] class LoginResponseData(BaseModel): token: str profile: Profile @api.post('/login/', response_model=LoginResponseData) def login(body: LoginCredentials): profile = Profile(**body.dict(), age=72, hobbies=['cats']) return LoginResponseData(token='very-secure', profile=profile) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.