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. 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 --conf projectname.urls > assets/urls.ts

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.1.0.tar.gz (6.6 kB view hashes)

Uploaded Source

Built Distribution

django_typescript_routes-0.1.0-py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page