django-ts-routes is a Django application allowing to expose and perform reverse lookups of Django named URL patterns in a TypeScript code base. This codebase is based on django-js-routes. Big thanks to the original author of this package.
To install django-ts-routes, please use the pip command as follows:
$ pip install django-ts-routes
Once the package is installed, you'll have to add the application to INSTALLED_APPS
in your project's settings module:
INSTALLED_APPS = (
# all other apps...
'ts_routes',
)
You can then define which URL patterns or URL namespaces you want to expose by setting the TS_ROUTES_INCLUSION_LIST
setting (for compatibility with django-js-routes you can also use JS_ROUTES_INCLUSION_LIST
). This setting allows to define which URLs should be serialized and made available to the client side through the generated and / or exported TypeScript helper. This list should contain only URL pattern names or namespaces. Here is an example:
TS_ROUTES_INCLUSION_LIST = [
'home',
'catalog:product_list',
'catalog:product_detail',
]
Note that if a namespace is included in this list, all the underlying URLs will be made available to the client side through the generated TypeScript helper. Django-ts-routes is safe by design in the sense that only the URLs that you configure in this inclusion list will be publicly exposed on the client side.
Once the list of URLs to expose is configured, you can dump the routes with the management command dump_routes_resolver
:
$ python manage.py dump_routes_resolver --output-dir=static/src/routes
The URL patterns you configured through the TS_ROUTES_INCLUSION_LIST
setting will be exported to TypeScript files in the output-dir
directory. This directory will contain a index.ts
with routes of the default language, other supported languages will be in separate files. The files export the reverseUrl
function that can be used to generate URLs in your TypeScript code.
import reverseUrl from "@/routes";
reverseUrl("home");
reverseUrl("catalog:product_list");
reverseUrl("catalog:product_detail", { pk: productId });
Default: []
The TS_ROUTES_INCLUSION_LIST
setting allows to define the URL patterns and URL namespaces that should be exposed.
MIT. See LICENSE
for more details.