Skip to main content

Generate strongly-typed Python helpers (TypedDict param classes + path/url builders) from a React Router v6+ route tree so Python code can build frontend URLs safely.

Project description

Release Notes Downloads GitHub CI Status License: MIT

Generate Typed Python URL Helpers from React Router Routes

Generate strongly-typed Python helpers (TypedDict param objects + overloads) from a React Router v6+ route tree. This is useful when a Python backend, worker, or test suite needs to construct URLs that stay in sync with a JavaScript/TypeScript frontend using React Router.

Take a look at this project for a live usage example.

How it works

Given a React Router project, the CLI either:

  • Auto-detects your package manager (bun, pnpm, or npm) and runs <package-manager> react-router routes --json (if you pass --directory), or
  • Reads a pre-generated JSON file (if you pass --json-file)

It walks the returned route objects and produces a Python module containing:

  • RoutePaths Literal of every concrete route pattern (e.g. /users/:userId?, /files/*).
  • Per-route TypedDict classes containing snake_case parameter keys.
  • Overloaded react_router_path() to build a relative path with validation + percent-encoding.
  • Overloaded react_router_url() to prepend a base URL (explicit argument or BASE_URL env var).
  • Optional url_params argument on both functions to append query string parameters.

Installation

Using uv (recommended):

uv add react-router-routes

Prerequisites

Your JS project must have react-router and a package manager with the react-router routes --json command available (React Router v6+ data APIs). The tool automatically detects your package manager (bun, pnpm, or npm) based on lockfiles and availability. The Python process must run inside (or have access to) that project directory so the CLI can execute the command.

CLI Usage

The script entry point is named react-router-routes (see pyproject.toml).

Two ways to supply routes:

  1. Have the tool invoke <package-manager> react-router routes --json by providing a directory (auto-detects bun, pnpm, or npm):
react-router-routes ./routes_typing.py --directory ./frontend
  1. Provide an existing JSON file (output of pnpm react-router routes --json):
react-router-routes ./routes_typing.py --json-file tests/react-router.json

Then import the generated module in Python code:

from routes_typing import react_router_path, react_router_url, RoutePaths

# Basic path generation
react_router_path('/users/:userId', {'user_id': 123})  # -> '/users/123'

# URL generation with base URL
react_router_url('/files/*', {'splat': 'docs/readme.md'}, base_url='https://example.com')
# -> 'https://example.com/files/docs/readme.md'

# Adding query parameters with url_params
react_router_path('/users/:userId', {'user_id': 123}, url_params={'tab': 'profile', 'edit': 'true'})
# -> '/users/123?tab=profile&edit=true'

react_router_url('/home', base_url='https://example.com', url_params={'page': '1', 'sort': 'name'})
# -> 'https://example.com/home?page=1&sort=name'

Environment Variables

  • BASE_URL (optional) – If set and you omit base_url when calling react_router_url, this value is prepended. If missing the function returns the path and logs a warning.
  • LOG_LEVEL (optional) – Standard Python logging level (INFO, DEBUG, etc.).

MIT License


This project was created from iloveitaly/python-package-template

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

react_router_routes-0.4.2.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

react_router_routes-0.4.2-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file react_router_routes-0.4.2.tar.gz.

File metadata

  • Download URL: react_router_routes-0.4.2.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for react_router_routes-0.4.2.tar.gz
Algorithm Hash digest
SHA256 71da7100ee7e2a6a0baedc20fa9e83a983e5eacf1b18469e2ac0fd60fc1bd234
MD5 9bc12bff870f33d344336514eaedeee6
BLAKE2b-256 033df70da3e647d1a15ff658d42e6c34119cbef8f04d89d65cbcf2970d0c5a30

See more details on using hashes here.

File details

Details for the file react_router_routes-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: react_router_routes-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for react_router_routes-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 86710bdedaa6c69aa560fd0845ad6b1869b9657042353ee9dbd80ec5d680152c
MD5 10461673a935afae6a3d6068f2ab248a
BLAKE2b-256 ab2ed668bfb1002c72848d3e5de3d86f4428a49d0911963de7c73c4fe2e136a6

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