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`,
};
- Install:
poetry add --dev django-typescript-routes
- Add
django-typescript-routes
to yourINSTALLED_APPS
setting:
INSTALLED_APPS = [
...,
"typescript_routes",
...
]
- Run the management command to print out the typescript file:
python manage.py generate_typescript_routes --urlconf projectname.urls > assets/urls.ts
Simply:
./scripts/test