https://github.com/buttondown/django-typescript-routes
Generate Typescript routes from a Django URLconf
https://github.com/buttondown/django-typescript-routes
django python typescript
Last synced: 2 months ago
JSON representation
Generate Typescript routes from a Django URLconf
- Host: GitHub
- URL: https://github.com/buttondown/django-typescript-routes
- Owner: buttondown
- License: mit
- Created: 2023-08-17T19:27:17.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-15T18:06:12.000Z (6 months ago)
- Last Synced: 2025-02-07T17:53:42.464Z (2 months ago)
- Topics: django, python, typescript
- Language: Python
- Homepage:
- Size: 23.4 KB
- Stars: 29
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- stars - buttondown/django-typescript-routes - Generate Typescript routes from a Django URLconf (Python)
- stars - buttondown/django-typescript-routes - Generate Typescript routes from a Django URLconf (Python)
README
# django-typescript-routes
Meant as a spiritual successor to [django-js-reverse](https://pypi.org/project/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:
```python
urls = [
path(
r"about",
about,
name="about",
),
path(
r"/",
subscribe,
name="subscribe",
),
path(
r"//subscribers//success",
subscription_success,
name="subscription-success",
),
]
```into:
```typescript
const URLS = {
about: () => `/`,
subscribe: (username: string) => `/${username}`,
"subscription-success": (username: string, pk: string) =>
`/${username}/subscribers/${pk}/success`,
};
```## Quick start
1. Install:
```bash
poetry add --dev django-typescript-routes
```1. Add `django-typescript-routes` to your `INSTALLED_APPS` setting:
```python
INSTALLED_APPS = [
...,
"typescript_routes",
...
]
```2. Run the management command to print out the typescript file:
```bash
python manage.py generate_typescript_routes --urlconf projectname.urls > assets/urls.ts
```## Contributing
### Running the test suite
Simply:
```
./scripts/test
```