Skip to main content

Expose and perform reverse lookups of Django URLs in the TypeScript world.

Project description

django-ts-routes

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.

Table of Contents

Installation

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

Usage

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 });

Settings

TS_ROUTES_INCLUSION_LIST

Default: []

The TS_ROUTES_INCLUSION_LIST setting allows to define the URL patterns and URL namespaces that should be exposed.

License

MIT. See LICENSE for more details.

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_ts_routes-1.0.0.tar.gz (24.6 kB view details)

Uploaded Source

Built Distribution

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

django_ts_routes-1.0.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file django_ts_routes-1.0.0.tar.gz.

File metadata

  • Download URL: django_ts_routes-1.0.0.tar.gz
  • Upload date:
  • Size: 24.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for django_ts_routes-1.0.0.tar.gz
Algorithm Hash digest
SHA256 79bd42fa7e3aebb51c3dc392522b5d2ba2b4c71a1897e8b2316330fd735e1b4e
MD5 0737363804f33a3d6f3bd176ef74d436
BLAKE2b-256 45bc81acc8d7b445a2308016ccc8b77b6179cd2ed6a9a4300ef5637946bb2988

See more details on using hashes here.

File details

Details for the file django_ts_routes-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_ts_routes-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 717f89cbfc02295f3acac75ce3387b0f90a1020cb449d5015dc6842973d0d4f5
MD5 662d16478001cb48ce814c1002d4620c
BLAKE2b-256 9d23ee9b7e83a59ceb25558c1c86699c2cb1d10e6a1a61fd47747794659006a4

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