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.3.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.3.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django_typescript_routes-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d2c10b6d84407bc9254215514936d23afff33358cc477ade8c5bcdaf96a39608
MD5 a2f1a58fc1e6253868abd4c289bc2ac6
BLAKE2b-256 8a247c3d649a50aa92e3bf33890bd140d5fc90aa04d7c3f7b963c8e44d9fbb8d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_typescript_routes-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d8ac8833587d2defe303207d67a008d65ab606a273124f64e5b645aa2deef103
MD5 c8ec0f5bbb6217e6d04a071ecb22c5f6
BLAKE2b-256 e85b6918668aae7a0211c5f3d48af1ad807eb681af0e518be90d46d416cfa562

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