Skip to main content

Check TypeScript types against backend models

Project description

TS Backend Check logo

rtd pr_ci python_package_ci issues python pypi pypistatus license coc matrix

Check TypeScript types against backend models

ts-backend-check is a Python package used to check TypeScript types against their corresponding backend models to assure that all fields have been accounted for.

Developed by the activist community, this package is meant to help synchronize the work between frontend and backend development teams. Currently the process supports Django based backends.

Contents

Usage

Command Options

The CLI provides a simple interface to check TypeScript types against backend models:

# Show help and available commands:
ts-backend-check --help
ts-backend-check -gcf  # generate a configuration file
ts-backend-check -gtp  # generate a test project for experimenting with the CLI

# Check a TypeScript type against a backend model:
ts-backend-check -m <model-identifier-from-config-file>
ts-backend-check -a  # run all models

Outputs

Example success and error outputs for the CLI are:

ts-backend-check -m user
✅ Success: All backend models are synced with their corresponding TypeScript interfaces for the provided files.
ts-backend-check -m user

❌ ts-backend-check error: There are inconsistencies between the provided backend models and TypeScript interfaces. Please see the output below for details.

Field 'user_name' (camelCase: 'userName') from model 'UserModel' is missing in the TypeScript interfaces.
Expected to find this field in the frontend interface: User
To ignore this field, add the following comment to the TypeScript interface: '// ts-backend-check: ignore field userName'

Please fix the 1 error above to have the backend models of backend/models/user.py synced with the TypeScript interfaces of frontend/types/user.ts.

Installation

ts-backend-check is available for installation via uv (recommended) or pip.

For Users

# Using uv (recommended - fast, Rust-based installer):
uv pip install ts-backend-check

# Or using pip:
pip install ts-backend-check

For Development Build

git clone https://github.com/activist-org/ts-backend-check.git  # or ideally your fork
cd ts-backend-check

# With uv (recommended):
uv sync --all-extras  # install all dependencies
source .venv/bin/activate  # activate venv (macOS/Linux)
# .venv\Scripts\activate  # activate venv (Windows)

# Or with pip:
python -m venv .venv  # create virtual environment
source .venv/bin/activate  # activate venv (macOS/Linux)
# .venv\Scripts\activate  # activate venv (Windows)
pip install -e .

Configuration

ts-backend-check is configured via a .ts-backend.check.yaml (or .yml) configuration file, with an example being the configuration file for this repository that we use in testing. The following describes the structure of an entry in this file:

model_identifier: # an identifier you define that you want to pass to the CLI
  backend_model_path: path/to/a/models.py
  ts_interface_path: path/to/the/corresponding/model_interfaces.ts
  check_blank_model_fields: true # whether to assert that fields that can be blank must also be optional
  backend_to_ts_model_name_conversions: # used if the frontend name is not the backend name
    EventModel: [CommunityEvent]

pre-commit

The following is an example pre-commit hook:

- repo: local
  hooks:
    - id: run-ts-backend-check
      name: run ts-backend-check key-value checks
      files: ^src-dir/
      entry: ts-backend-check -a
      language: python
      pass_filenames: false
      additional_dependencies:
        - ts-backend-check

GitHub Action

The following is an example YAML file for a GitHub Action to check your backend models and TypeScript interface files on PRs and commits:

name: pr_ci_ts_backend_check
on:
  workflow_dispatch:
  pull_request:
    branches:
      - main
    types:
      - opened
      - reopened
      - synchronize
  push:
    branches:
      - main

jobs:
  ts_backend_check:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Project
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install uv
        uses: astral-sh/setup-uv@v7

      - name: Install Dependencies
        run: uv sync --frozen --all-extras

      - name: Execute All ts-backend-check Key-Value Checks
        run: |
          uv run ts-backend-check -a

Contributing

Public Matrix Chat

activist uses Matrix for internal communication. You're more than welcome to join us in our public chat rooms to share ideas, ask questions or just say hi to the team :) We'd suggest that you use the Element client and Element X for a mobile app.

Please see the contribution guide if you are interested in contributing. Work that is in progress or could be implemented is tracked in the issues and projects.

[!NOTE] Just because an issue is assigned on GitHub doesn't mean the team isn't open to your contribution! Feel free to write in the issues and we can potentially reassign it to you.

Also check the -next release- and -priority- labels in the issues for those that are most important, as well as those marked good first issue that are tailored for first-time contributors. For those new to coding or our tech stack, we've collected links to helpful documentation pages in the contribution guide.

We would be happy to discuss granting you further rights as a contributor after your first pull requests, with a maintainer role then being possible after continued interest in the project. activist seeks to be an inclusive, diverse and supportive organization. We'd love to have you on the team! Please see the mentorship and growth section of the contribution guide for further information.

How you can help

Environment setup

  1. First and foremost, please see the suggested IDE setup in the dropdown below to make sure that your editor is ready for development.

[!IMPORTANT]

Suggested IDE setup

VS Code

Install the following extensions:

  1. Fork the ts-backend-check repo, clone your fork, and configure the remotes:

[!NOTE]

Consider using SSH

Alternatively to using HTTPS as in the instructions below, consider SSH to interact with GitHub from the terminal. SSH allows you to connect without a user-pass authentication flow.

To run git commands with SSH, remember then to substitute the HTTPS URL, https://github.com/..., with the SSH one, git@github.com:....

  • e.g. Cloning now becomes git clone git@github.com:<your-username>/ts-backend-check.git

GitHub also has their documentation on how to Generate a new SSH key 🔑

# Clone your fork of the repo into the current directory.
git clone https://github.com/<your-username>/ts-backend-check.git
# Navigate to the newly cloned directory.
cd ts-backend-check
# Assign the original repo to a remote called "upstream".
git remote add upstream https://github.com/activist-org/ts-backend-check.git
  • Now, if you run git remote -v you should see two remote repositories named:
    • origin (forked repository)
    • upstream (ts-backend-check repository)
  1. Install uv if you don't already have it by following the official installation guide.

  2. Create a virtual environment for ts-backend-check (Python >=3.12), activate it and install dependencies:

    uv sync --all-extras  # create .venv and install all dependencies from uv.lock
    
    # Unix or macOS:
    source .venv/bin/activate
    
    # Windows:
    .venv\Scripts\activate.bat  # .venv\Scripts\activate.ps1 (PowerShell)
    

[!NOTE] If you change dependencies in pyproject.toml, regenerate the lock file with the following command:

uv lock  # refresh uv.lock for reproducible installs

After activating the virtual environment, set up pre-commit by running:

pre-commit install
# uv run pre-commit run --all-files  # lint and fix common problems in the codebase

You're now ready to work on ts-backend-check!

[!NOTE] Feel free to contact the team in the Development room on Matrix if you're having problems getting your environment setup!

Contributors

Thanks to all our amazing contributors! ❤️

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

ts_backend_check-1.3.0.tar.gz (102.4 kB view details)

Uploaded Source

Built Distribution

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

ts_backend_check-1.3.0-py3-none-any.whl (35.8 kB view details)

Uploaded Python 3

File details

Details for the file ts_backend_check-1.3.0.tar.gz.

File metadata

  • Download URL: ts_backend_check-1.3.0.tar.gz
  • Upload date:
  • Size: 102.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ts_backend_check-1.3.0.tar.gz
Algorithm Hash digest
SHA256 e237883044c0b0414e2d284d19d72f49b137122da027c843d40435485aeb0594
MD5 eee415d9cf658925f989f9787c52f774
BLAKE2b-256 369195e16b2fb674a5acad59f4e7cf59e5ff292463e12bc4d6e36890cbce9057

See more details on using hashes here.

File details

Details for the file ts_backend_check-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: ts_backend_check-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 35.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ts_backend_check-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b0768b9e1f3e17b866a177d277f197052df207abfde0abf00fa5743b36b79550
MD5 ef3bad11bc4787931147b3ce6827cbf1
BLAKE2b-256 7cced20cc23d192a27359fedccaa77f1406d7919f12394c781fd4feee800cffe

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