Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

## 0.3.0

- Migrated from `poetry` to `uv`.
- Refactored the template to be amenable to packages that monkey-patch the template engine.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const URLS = {
1. Install:

```bash
poetry add --dev django-typescript-routes
uv add django-typescript-routes
```

1. Add `django-typescript-routes` to your `INSTALLED_APPS` setting:
Expand Down
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
scripts/test
170 changes: 0 additions & 170 deletions poetry.lock

This file was deleted.

36 changes: 21 additions & 15 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
[tool.poetry]
authors = ["Justin Duke <justin@buttondown.email>"]
description = "Generate typescript routes from a Django URLconf"
license = "MIT"
[project]
name = "django-typescript-routes"
packages = [{ include = "typescript_routes" }]
version = "0.3.0"
description = "Generate typescript routes from a Django URLconf"
authors = [{ name = "Justin Duke", email = "justin@buttondown.email" }]
requires-python = "~=3.11"
readme = "README.md"
repository = "https://github.com/buttondown-email/django-typescript-routes"
version = "0.2.0"
license = "MIT"
dependencies = ["Django>=5,<6"]

[project.urls]
Repository = "https://github.com/buttondown-email/django-typescript-routes"

[dependency-groups]
dev = [
"pytest>=7.4.0,<8",
"ruff>=0.0.284,<0.0.285",
]

[tool.poetry.dependencies]
Django = "^5"
python = "^3.11"
[tool.hatch.build.targets.sdist]
include = ["typescript_routes"]

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.0"
ruff = "^0.0.284"
[tool.hatch.build.targets.wheel]
include = ["typescript_routes"]

[build-system]
build-backend = "poetry.core.masonry.api"
requires = ["poetry-core"]
requires = ["hatchling"]
build-backend = "hatchling.build"
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[pytest]
python_files = *--test.py
python_files = *--test.py *--test-monkeypatching.py
2 changes: 1 addition & 1 deletion scripts/test
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh -e
export DJANGO_SETTINGS_MODULE=tests.settings
poetry run pytest "$@"
uv run pytest "$@"
14 changes: 14 additions & 0 deletions tests/logic--test-monkeypatching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import re

import django
from typescript_routes.lib.logic import generate_routes

def test_handles_monkeypatching() -> None:
# See https://github.com/wrabit/django-cotton/issues/299.

from django.template import base
base.tag_re = re.compile(base.tag_re.pattern, re.DOTALL)

django.setup()

assert generate_routes("tests.urls_basic", []) == open("tests/fixtures/basic.ts").read()
5 changes: 4 additions & 1 deletion typescript_routes/lib/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,7 @@ def extract_routes(resolver: URLResolver, denylist: list[str]) -> Iterable[Route
def generate_routes(urlconf: str, denylist: list[str]) -> str:
resolver = get_resolver(urlconf)
routes = extract_routes(resolver, denylist)
return render_to_string("urls.ts.template", {"routes": routes})
rendered_string = render_to_string("urls.ts.template", {"routes": routes})
# Remove all empty lines injected via the template.
rendered_string = "\n".join(line for line in rendered_string.split("\n") if line.strip())
return rendered_string.strip() + "\n"
6 changes: 4 additions & 2 deletions typescript_routes/templates/urls.ts.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const URLS = {{% for route in routes %}
'{{ route.name }}': ({% for param in route.params %}{{ param.name }}: {{ param.typescript_type}}{% if not forloop.last%}, {% endif %}{% endfor %}) => `/{{ route.template }}`,{% endfor %}
const URLS = {
{% for route in routes %}
'{{ route.name }}': ({% for param in route.params %}{{ param.name }}: {{ param.typescript_type}}{% if not forloop.last%}, {% endif %}{% endfor %}) => `/{{ route.template }}`,
{% endfor %}
};
export default URLS;
Loading