|
| 1 | +"""SPA React + TanStack Router example - shared "Library" backend + React SPA frontend. |
| 2 | +
|
| 3 | +All examples in this repository expose the same backend: |
| 4 | +- `/api/summary` - overview + featured book |
| 5 | +- `/api/books` - list of books |
| 6 | +- `/api/books/{book_id}` - single book |
| 7 | +
|
| 8 | +This example uses TanStack Router for file-based routing. The TypeGenConfig |
| 9 | +includes extra_commands to run `tsr generate` so that `routeTree.gen.ts` |
| 10 | +is always up-to-date during `litestar assets generate-types` and `build`. |
| 11 | +""" |
| 12 | + |
| 13 | +import os |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +from litestar import Controller, Litestar, get |
| 17 | +from litestar.exceptions import NotFoundException |
| 18 | +from msgspec import Struct |
| 19 | + |
| 20 | +from litestar_vite import PathConfig, RuntimeConfig, TypeGenConfig, ViteConfig, VitePlugin |
| 21 | + |
| 22 | +here = Path(__file__).parent |
| 23 | +DEV_MODE = os.getenv("VITE_DEV_MODE", "true").lower() in {"true", "1", "yes"} |
| 24 | + |
| 25 | + |
| 26 | +class Book(Struct): |
| 27 | + id: int |
| 28 | + title: str |
| 29 | + author: str |
| 30 | + year: int |
| 31 | + tags: list[str] |
| 32 | + |
| 33 | + |
| 34 | +class Summary(Struct): |
| 35 | + app: str |
| 36 | + headline: str |
| 37 | + total_books: int |
| 38 | + featured: Book |
| 39 | + |
| 40 | + |
| 41 | +BOOKS: list[Book] = [ |
| 42 | + Book(id=1, title="Async Python", author="C. Developer", year=2024, tags=["python", "async"]), |
| 43 | + Book(id=2, title="Type-Safe Web", author="J. Dev", year=2025, tags=["typescript", "api"]), |
| 44 | + Book(id=3, title="Frontend Patterns", author="A. Designer", year=2023, tags=["frontend", "ux"]), |
| 45 | +] |
| 46 | + |
| 47 | + |
| 48 | +def _get_book(book_id: int) -> Book: |
| 49 | + for book in BOOKS: |
| 50 | + if book.id == book_id: |
| 51 | + return book |
| 52 | + raise NotFoundException(detail=f"Book {book_id} not found") |
| 53 | + |
| 54 | + |
| 55 | +def _get_summary() -> Summary: |
| 56 | + """Build summary data. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + The summary data. |
| 60 | + """ |
| 61 | + return Summary( |
| 62 | + app="litestar-vite library", headline="One backend, many frontends", total_books=len(BOOKS), featured=BOOKS[0] |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +class LibraryController(Controller): |
| 67 | + """Library API controller.""" |
| 68 | + |
| 69 | + @get("/api/summary") |
| 70 | + async def summary(self) -> Summary: |
| 71 | + """Overview endpoint used across all examples. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + The result. |
| 75 | + """ |
| 76 | + return _get_summary() |
| 77 | + |
| 78 | + @get("/api/books") |
| 79 | + async def books(self) -> list[Book]: |
| 80 | + """Return all books. |
| 81 | +
|
| 82 | + Returns: |
| 83 | + The result. |
| 84 | + """ |
| 85 | + return BOOKS |
| 86 | + |
| 87 | + @get("/api/books/{book_id:int}") |
| 88 | + async def book_detail(self, book_id: int) -> Book: |
| 89 | + """Return a single book by id. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + The result. |
| 93 | + """ |
| 94 | + return _get_book(book_id) |
| 95 | + |
| 96 | + |
| 97 | +# [docs-start:tanstack-vite-config] |
| 98 | +# TanStack Router uses a Vite plugin that generates routeTree.gen.ts. |
| 99 | +# extra_commands ensures `tsr generate` runs during `generate-types` and `build`, |
| 100 | +# so `tsc --noEmit` passes without needing a full Vite dev/build cycle first. |
| 101 | +vite = VitePlugin( |
| 102 | + config=ViteConfig( |
| 103 | + mode="spa", |
| 104 | + dev_mode=DEV_MODE, |
| 105 | + paths=PathConfig(root=here), |
| 106 | + types=TypeGenConfig(extra_commands=[["tsr", "generate"]]), |
| 107 | + runtime=RuntimeConfig(port=5005), |
| 108 | + ) |
| 109 | +) |
| 110 | + |
| 111 | +app = Litestar(route_handlers=[LibraryController], plugins=[vite], debug=True) |
| 112 | +# [docs-end:tanstack-vite-config] |
0 commit comments