Skip to main content

Generate typescript routes from a Django URLconf

Project description

django-typescript-routes

Meant as a spiritual successor to django-js-reverse, django-typescript-routes is meant to answer to the following question:

I've got a Typescript-based SPA that is powered by a Django-based API. How do I safely make requests to Django without messing up the routes or parameters?

django-typescript-routes is how! At a high level, it turns:

urls = [
    path(
        r"about",
        about,
        name="about",
    ),
    path(
        r"/<str:username>",
        subscribe,
        name="subscribe",
    ),
    path(
        r"/<str:username>/subscribers/<pk:uuid>/success",
        subscription_success,
        name="subscription-success",
    ),
]

into:

const URLS = {
  about: () => `/`,
  subscribe: (username: string) => `/${username}`,
  "subscription-success": (username: string, pk: string) =>
    `/${username}/subscribers/${pk}/success`,
};

Quick start

  1. Install:
uv add django-typescript-routes
  1. Add django-typescript-routes to your INSTALLED_APPS setting:
INSTALLED_APPS = [
    ...,
    "typescript_routes",
    ...
]
  1. Run the management command to print out the typescript file:
python manage.py generate_typescript_routes --urlconf projectname.urls > assets/urls.ts

Contributing

Running the test suite

Simply:

./scripts/test

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

django_typescript_routes-0.4.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

django_typescript_routes-0.4.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file django_typescript_routes-0.4.0.tar.gz.

File metadata

File hashes

Hashes for django_typescript_routes-0.4.0.tar.gz
Algorithm Hash digest
SHA256 db66b123289e4baf6aa689bf4e3bbacf595a5b933e00d5246f8876e189419b89
MD5 75e271940f3bbbdd5b9f180ca6824acb
BLAKE2b-256 ff9eb42fbcff435698ab3daafb89cc8d8f56a1c40dfd4534c9961ab7091b73af

See more details on using hashes here.

File details

Details for the file django_typescript_routes-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_typescript_routes-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5c3a34ea3122076969f3415772f52bce423b67e976b9cbf10d5627f349e75673
MD5 9680f03bb5d4f94417580a3a16038146
BLAKE2b-256 d1f0f5f6b5daa419fdd1677161e7d9fe5032e506e5320acdbd2038812b146c2f

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