# 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](https://github.com/ellmetha/django-js-routes). Big thanks to the original author of this package.
## Table of Contents
- [Installation](#installation)
- [Usage](#usage)
- [Settings](#settings)
- [Advanced features](#advanced-features)
- [License](#license)
## Installation
To install django-ts-routes, please use the pip command as follows:
```shell
$ 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:
```python
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](https://github.com/ellmetha/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:
```python
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`:
```shell
$ 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.
```typescript
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.
Raw data
{
"_id": null,
"home_page": null,
"name": "django-ts-routes",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "django, export, reverse, typescript, urls",
"author": "Sven Groot",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/45/bc/81acc8d7b445a2308016ccc8b77b6179cd2ed6a9a4300ef5637946bb2988/django_ts_routes-1.0.0.tar.gz",
"platform": null,
"description": "# django-ts-routes\n\n**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](https://github.com/ellmetha/django-js-routes). Big thanks to the original author of this package.\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [Settings](#settings)\n- [Advanced features](#advanced-features)\n- [License](#license)\n\n## Installation\n\nTo install django-ts-routes, please use the pip command as follows:\n\n```shell\n$ pip install django-ts-routes\n```\n\nOnce the package is installed, you'll have to add the application to `INSTALLED_APPS` in your project's settings module:\n\n```python\nINSTALLED_APPS = (\n # all other apps...\n 'ts_routes',\n)\n```\n\nYou 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](https://github.com/ellmetha/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:\n\n```python\nTS_ROUTES_INCLUSION_LIST = [\n 'home',\n 'catalog:product_list',\n 'catalog:product_detail',\n]\n```\n\nNote 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.\n\nOnce the list of URLs to expose is configured, you can dump the routes with the management command `dump_routes_resolver`:\n\n```shell\n$ python manage.py dump_routes_resolver --output-dir=static/src/routes\n```\n\n## Usage\n\nThe 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.\n\n```typescript\nimport reverseUrl from \"@/routes\";\n\nreverseUrl(\"home\");\nreverseUrl(\"catalog:product_list\");\nreverseUrl(\"catalog:product_detail\", { pk: productId });\n```\n\n## Settings\n\n### TS_ROUTES_INCLUSION_LIST\n\nDefault: `[]`\n\nThe `TS_ROUTES_INCLUSION_LIST` setting allows to define the URL patterns and URL namespaces that should be exposed.\n\n## License\n\nMIT. See `LICENSE` for more details.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Expose and perform reverse lookups of Django URLs in the TypeScript world.",
"version": "1.0.0",
"project_urls": {
"homepage": "https://github.com/svengt/django-ts-routes",
"repository": "https://github.com/svengt/django-ts-routes"
},
"split_keywords": [
"django",
" export",
" reverse",
" typescript",
" urls"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9d23ee9b7e83a59ceb25558c1c86699c2cb1d10e6a1a61fd47747794659006a4",
"md5": "662d16478001cb48ce814c1002d4620c",
"sha256": "717f89cbfc02295f3acac75ce3387b0f90a1020cb449d5015dc6842973d0d4f5"
},
"downloads": -1,
"filename": "django_ts_routes-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "662d16478001cb48ce814c1002d4620c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 8028,
"upload_time": "2024-12-14T22:07:27",
"upload_time_iso_8601": "2024-12-14T22:07:27.761490Z",
"url": "https://files.pythonhosted.org/packages/9d/23/ee9b7e83a59ceb25558c1c86699c2cb1d10e6a1a61fd47747794659006a4/django_ts_routes-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "45bc81acc8d7b445a2308016ccc8b77b6179cd2ed6a9a4300ef5637946bb2988",
"md5": "0737363804f33a3d6f3bd176ef74d436",
"sha256": "79bd42fa7e3aebb51c3dc392522b5d2ba2b4c71a1897e8b2316330fd735e1b4e"
},
"downloads": -1,
"filename": "django_ts_routes-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "0737363804f33a3d6f3bd176ef74d436",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 24571,
"upload_time": "2024-12-14T22:07:29",
"upload_time_iso_8601": "2024-12-14T22:07:29.991759Z",
"url": "https://files.pythonhosted.org/packages/45/bc/81acc8d7b445a2308016ccc8b77b6179cd2ed6a9a4300ef5637946bb2988/django_ts_routes-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-14 22:07:29",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "svengt",
"github_project": "django-ts-routes",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "django-ts-routes"
}