Skip to main content

Check TypeScript interfaces against backend models

Project description

TS Backend Check logo

rtd ci_static_analysis ci_pytest issues python pypi pypistatus license coc matrix

Contents

About ts-backend-check

[!IMPORTANT] Before you contribute, read the contributing guide.

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

Developed by the activist community, this package helps keep frontend and backend development teams in sync.

The package supports Django-based backends.

Installation

Users

You can install ts-backend-check using uv (recommended) or pip.

uv

(Recommended - fast, Rust-based installer)

uv pip install ts-backend-check

pip

pip install ts-backend-check

Development Build

You can install the latest development build using uv, pip, or by cloning the repository.

Clone the Repository (Development Build)

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

uv (Development Build)

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

pip (Development Build)

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

Back to top.

How It Works

Commands

These are some example commands:

Show Help and Available Commands

# You can use either ts-backend-check or tsbc to access the CLI.
# ts-backend-check --help
tsbc --help

Generate a Configuration File

# ts-backend-check --generate-config-file
tsbc -gcf

Generate the Test Project

# ts-backend-check includes a test project for testing the functionalities.
# ts-backend-check --generate-test-project
tsbc -gtp

Check a TypeScript Interface Against a Backend Model

# ts-backend-check --identifier <model-interface-identifier-from-config-file>
tsbc -i <model-interface-identifier-from-config-file>

Check All Models and Interfaces

# ts-backend-check --all
tsbc -a

Example Outputs

These are some example outputs for passed and failed checks.

Passed Check

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

Failed Check

ts-backend-check -i invalid_model

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

Field 'description' (camelCase: 'description') from model 'EventModel' is missing in the TypeScript interfaces.
Expected to find this field in the frontend interfaces: Event, EventExtended
To ignore this field, add the following comment to the TypeScript file (in order based on the model fields): '// ts-backend-check: ignore description'

Field 'participants' (camelCase: 'participants') from model 'EventModel' doesn't match the TypeScript interfaces based on blank to optional agreement.
Please check 'src/ts_backend_check/test_project/backend/models.py' and the corresponding files in 'ts_interface_paths' to make sure that all 'blank=True' fields are optional (?) in the TypeScript interfaces file.

No matching TypeScript interface found for the model 'UserModel'.
Please name your TypeScript interfaces the same as the corresponding backend models.
You can also use the 'backend_to_ts_model_name_conversions' option within the configuration file.
The key is the backend model name and the value is a list of the corresponding interfaces.
This option is also how you can break larger backend models into multiple interfaces that extend one another.

Please fix the 3 errors above to continue the sync of the backend models of src/ts_backend_check/test_project/backend/models.py and the corresponding TypeScript interfaces.

Back to top.

Configuration

YAML File

You can configure ts-backend-check using the .ts-backend.check.yaml (or .yml) configuration file.

For an example, see the configuration file for this repository that we use in testing.

This example 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_paths:
    - path/to/the/corresponding/model_interfaces.ts
    - path/to/another/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
    BackendModel:
      - Interface
      - InterfaceExtended

pre-commit

This is an example of a prek or pre-commit hook:

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

GitHub Action

This is an example YAML file for a GitHub Action to check your backend models and TypeScript interface files on pull requests and commits:

name: 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@v6

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

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

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

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

Back to top.

Contributing

See the contribution guidelines before contributing. You can help by:

  • 🐞 Reporting bugs.
  • ✨ Working with us on new features.
  • 📝 Improving the documentation.

We track work that is in progress or may be implemented in the the issues and projects.

Contact the Team

Public Matrix Chat

activist uses Matrix for team communication. Join us in our public chat rooms to share ideas, ask questions or just say hi to the team.

We recommend using the Element client and Element X for a mobile app.

Contributors

Thanks to all our amazing contributors! ❤️

Back to top.

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.6.1.tar.gz (96.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.6.1-py3-none-any.whl (37.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ts_backend_check-1.6.1.tar.gz
  • Upload date:
  • Size: 96.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.6.1.tar.gz
Algorithm Hash digest
SHA256 ef1516261aac16000e94589044eda27d461f20f95c9dd80fb0560e3cd763f725
MD5 0e55168d913cdfd9ada32709c9d97b20
BLAKE2b-256 28d1b0626f8216baf1f2fa67b56ef1b9bfd9f8756ec47c6ab1c33e457d62129d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ts_backend_check-1.6.1-py3-none-any.whl
  • Upload date:
  • Size: 37.4 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.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fdd75b9cef79166c03b64a360122e50e639b4d8e262be95d27bf2aebc42602c9
MD5 bca0f8b7cb2bc60f1c7a2c92e01f6704
BLAKE2b-256 ff0ea771b851e789334a77828ff8c4368f0140c907fd25d0fe45fa7c64b5719c

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