@cubocicloide/stack-react-fastapi 13.1.0 → 13.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/templates/base/CLAUDE.md.hbs +14 -1
- package/templates/base/README.md.hbs +2 -2
- package/templates/base/backend/app/core/api_docs.py.hbs +182 -0
- package/templates/base/backend/app/main.py.hbs +22 -3
- package/templates/base/backend/app/routers/health.py +10 -2
- package/templates/base/backend/pyproject.toml.hbs +4 -0
- package/templates/base/docs/docs/dude.md.hbs +2 -2
- package/templates/base/docs/docs/index.md.hbs +2 -1
- package/templates/base/frontend/src/openapi/utils/openapi.yaml.hbs +6 -4
- package/templates/postgres/backend/app/routers/users.py +10 -2
- package/templates/postgres/backend/app/routers/users__id.py +10 -2
package/package.json
CHANGED
|
@@ -134,7 +134,20 @@ using the `react-fastapi` stack:
|
|
|
134
134
|
| `dude api review` | Validate `frontend/src/openapi/` against the saved spec |
|
|
135
135
|
|
|
136
136
|
**Workflow**: always run `dude api sync` after changing backend routes, then commit
|
|
137
|
-
the updated `frontend/src/openapi/` alongside the route change.
|
|
137
|
+
the updated `frontend/src/openapi/` alongside the route change. The reference is
|
|
138
|
+
served at http://localhost:8000/api/docs (Scalar).
|
|
139
|
+
|
|
140
|
+
**Documenting a route** — the schema only carries what the code declares:
|
|
141
|
+
|
|
142
|
+
- Every route gets a `tags` entry (on the `APIRouter`), an imperative `summary`
|
|
143
|
+
(≤ 6 words) and a `description` that says something the schema does not already
|
|
144
|
+
show — preconditions, side effects, who may call it, which error a wrong call
|
|
145
|
+
returns.
|
|
146
|
+
- Every tag used must exist in `API_TAGS` in `backend/app/core/api_docs.py`, or the
|
|
147
|
+
operation lands ungrouped at the bottom of the sidebar. `API_TAGS` order **is**
|
|
148
|
+
the sidebar order.
|
|
149
|
+
- Cross-cutting prose (auth header, conventions, the domain lifecycle) belongs in
|
|
150
|
+
`API_DESCRIPTION` in the same file, not repeated per route.
|
|
138
151
|
|
|
139
152
|
### Testing
|
|
140
153
|
|
|
@@ -71,8 +71,8 @@ Once the containers are up, open the URLs below.
|
|
|
71
71
|
|---------|-----|-------|
|
|
72
72
|
| **Frontend** | http://localhost:5173 | React + Vite — HMR active |
|
|
73
73
|
| **API** | http://localhost:8000 | FastAPI |
|
|
74
|
-
| **
|
|
75
|
-
| **
|
|
74
|
+
| **API reference** | http://localhost:8000/api/docs | Interactive reference (Scalar) — narrative in `backend/app/core/api_docs.py` |
|
|
75
|
+
| **OpenAPI schema** | http://localhost:8000/api/openapi.json | Raw schema (JSON) |
|
|
76
76
|
| **Health** | http://localhost:8000/api/health | JSON health check |
|
|
77
77
|
{{#if withCelery}}
|
|
78
78
|
| **Flower** | http://localhost:5555 | Celery task monitor |
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"""Narrative, sidebar structure and the reference page for the public API.
|
|
2
|
+
|
|
3
|
+
``API_DESCRIPTION`` becomes ``info.description`` in the OpenAPI schema and renders
|
|
4
|
+
as the landing page of the reference at ``/api/docs``. ``API_TAGS`` is the
|
|
5
|
+
sidebar: its order is the sidebar order, and every tag used by a router must
|
|
6
|
+
appear here or the operation lands ungrouped at the bottom. ``scalar_html``
|
|
7
|
+
renders the reference page itself.
|
|
8
|
+
|
|
9
|
+
All three are wired up in ``app/main.py``.
|
|
10
|
+
|
|
11
|
+
This file is where the reference stops being generated and starts being written.
|
|
12
|
+
Edit it as the API grows — the sections marked **TODO** describe your product,
|
|
13
|
+
not the scaffold.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
API_DESCRIPTION = """
|
|
17
|
+
The **{{projectName}}** HTTP API. It is consumed by the {{projectName}} web frontend and by any
|
|
18
|
+
service you authorise. Every response is JSON; every path below is relative to
|
|
19
|
+
`/api`.
|
|
20
|
+
|
|
21
|
+
<!-- TODO: replace this paragraph with what the API actually is — the domain in
|
|
22
|
+
plain words, who calls it, and what a caller can accomplish with it. If it is
|
|
23
|
+
internal-only, say so here rather than making integrators discover it from a 401. -->
|
|
24
|
+
|
|
25
|
+
## Authentication
|
|
26
|
+
|
|
27
|
+
**The scaffold ships no authentication.** Every endpoint below is open to anyone
|
|
28
|
+
who can reach the process. That is fine behind `docker compose` on your laptop and
|
|
29
|
+
unacceptable anywhere else.
|
|
30
|
+
|
|
31
|
+
<!-- TODO: add a scheme before this API leaves your machine. Declare a FastAPI
|
|
32
|
+
security dependency (e.g. `fastapi.security.HTTPBearer`), apply it to the routers
|
|
33
|
+
in `app/main.py`, then document the exact header here, verbatim:
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
Authorization: Bearer <access-token>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Spell out how a caller obtains the credential, how long it lives, and how it is
|
|
40
|
+
refreshed. Prefix mistakes are the most common integration failure — write the
|
|
41
|
+
header out, do not paraphrase it. Then uncomment `preferredSecurityScheme` in
|
|
42
|
+
`scalar_html` below and point it at the matching key from
|
|
43
|
+
`components.securitySchemes`. -->
|
|
44
|
+
|
|
45
|
+
## Base URLs
|
|
46
|
+
|
|
47
|
+
| Environment | Base URL |
|
|
48
|
+
| --- | --- |
|
|
49
|
+
| Local development | `http://localhost:8000` |
|
|
50
|
+
|
|
51
|
+
<!-- TODO: add the deployed environments and mirror them in the `servers` list in
|
|
52
|
+
`app/main.py` so the **Test Request** panel can target them. -->
|
|
53
|
+
|
|
54
|
+
## Conventions
|
|
55
|
+
|
|
56
|
+
- **No trailing slashes.** Paths are registered as `/api/users`, not `/api/users/`.
|
|
57
|
+
A trailing slash returns `307` and clients that drop the body on redirect fail
|
|
58
|
+
confusingly.
|
|
59
|
+
- **List endpoints return bare arrays**, not a pagination envelope, and they are
|
|
60
|
+
unbounded — every row comes back in one response. Add pagination before any
|
|
61
|
+
collection can grow.
|
|
62
|
+
- **Timestamps** — ISO-8601 with an offset, always UTC (`2026-01-31T09:15:00Z`).
|
|
63
|
+
- **IDs** — sequential integers, not UUIDs. Do not assume they are unguessable.
|
|
64
|
+
- **Errors** — two distinct body shapes:
|
|
65
|
+
- raised failures (`HTTPException`, e.g. 404): `{"detail": "User not found"}`
|
|
66
|
+
- request validation failures (**422**, produced by FastAPI itself before your
|
|
67
|
+
code runs): `{"detail": [{"loc": ["path", "id"], "msg": "…", "type": "…"}]}`.
|
|
68
|
+
Note the status code — FastAPI answers malformed input with `422`, not `400`.
|
|
69
|
+
|
|
70
|
+
## Lifecycle
|
|
71
|
+
|
|
72
|
+
<!-- TODO: this section carries the most weight in the whole reference, and the
|
|
73
|
+
scaffold cannot write it for you. Describe the state machine a caller must
|
|
74
|
+
respect: which state an object starts in, what each transition requires as a
|
|
75
|
+
precondition, what locks afterwards, and which status code a premature call
|
|
76
|
+
gets. Per-endpoint docs cannot express this; leaving it out is what makes
|
|
77
|
+
integrators guess. Delete this section only if the domain genuinely has no
|
|
78
|
+
stateful objects. -->
|
|
79
|
+
|
|
80
|
+
## Status
|
|
81
|
+
|
|
82
|
+
- The API is **unversioned**. Breaking changes ship without a path bump — pin
|
|
83
|
+
nothing, read the changelog.
|
|
84
|
+
- `GET /api/health` is a liveness probe, not a status page: it reports that the
|
|
85
|
+
process is up, not that the database is.
|
|
86
|
+
- No authentication, no pagination, no rate limiting, and no webhooks yet.
|
|
87
|
+
""".strip()
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
API_TAGS = [
|
|
91
|
+
{
|
|
92
|
+
"name": "Health",
|
|
93
|
+
"description": "Liveness probe. Used by container health checks and the frontend boot sequence.",
|
|
94
|
+
},
|
|
95
|
+
{{#if withPostgres}}
|
|
96
|
+
{
|
|
97
|
+
"name": "Users",
|
|
98
|
+
"description": "The accounts this API knows about. Read-only in the scaffold — writes belong in `app/queries/users.py`.",
|
|
99
|
+
},
|
|
100
|
+
{{/if}}
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
# Plain placeholders (not an f-string / str.format) so the CSS and JS braces below
|
|
105
|
+
# stay readable and unescaped.
|
|
106
|
+
_SCALAR_PAGE = """<!doctype html>
|
|
107
|
+
<html lang="en">
|
|
108
|
+
<head>
|
|
109
|
+
<meta charset="utf-8" />
|
|
110
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
111
|
+
<title>__TITLE__ API reference</title>
|
|
112
|
+
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 48 48'%3E%3Crect width='48' height='48' rx='12' fill='%231677ff'/%3E%3C/svg%3E" />
|
|
113
|
+
<style>
|
|
114
|
+
html, body { margin: 0; }
|
|
115
|
+
:root {
|
|
116
|
+
--scalar-font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
117
|
+
--scalar-font-code: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
118
|
+
}
|
|
119
|
+
/* Brand accent — matches the frontend's antd primary colour. */
|
|
120
|
+
.light-mode { --scalar-color-accent: #1677ff; }
|
|
121
|
+
.dark-mode { --scalar-color-accent: #4096ff; }
|
|
122
|
+
|
|
123
|
+
/* Brand bar. Delete this block and the <header> below together if you'd
|
|
124
|
+
rather have Scalar's own header on its own. */
|
|
125
|
+
.brandbar {
|
|
126
|
+
position: sticky; top: 0; z-index: 1000;
|
|
127
|
+
display: flex; align-items: center; gap: 0.75rem;
|
|
128
|
+
height: 56px; padding: 0 1.25rem;
|
|
129
|
+
background: #1677ff;
|
|
130
|
+
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.18);
|
|
131
|
+
}
|
|
132
|
+
.brandbar .name { color: #fff; font: 600 1rem/1 system-ui, -apple-system, sans-serif; }
|
|
133
|
+
.brandbar .spacer { flex: 1; }
|
|
134
|
+
.brandbar .tag {
|
|
135
|
+
color: #fff; opacity: 0.92;
|
|
136
|
+
font: 600 0.78rem/1 system-ui, -apple-system, sans-serif;
|
|
137
|
+
letter-spacing: 0.06em; text-transform: uppercase;
|
|
138
|
+
}
|
|
139
|
+
</style>
|
|
140
|
+
</head>
|
|
141
|
+
<body>
|
|
142
|
+
<header class="brandbar">
|
|
143
|
+
<span class="name">__TITLE__</span>
|
|
144
|
+
<span class="spacer"></span>
|
|
145
|
+
<span class="tag">API reference</span>
|
|
146
|
+
</header>
|
|
147
|
+
<div id="app"></div>
|
|
148
|
+
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
|
|
149
|
+
<script>
|
|
150
|
+
Scalar.createApiReference('#app', {
|
|
151
|
+
url: '__OPENAPI_URL__',
|
|
152
|
+
theme: 'default',
|
|
153
|
+
layout: 'modern',
|
|
154
|
+
hideDownloadButton: true,
|
|
155
|
+
showDeveloperTools: 'never',
|
|
156
|
+
persistAuth: true,
|
|
157
|
+
withDefaultFonts: false,
|
|
158
|
+
metaData: {
|
|
159
|
+
title: '__TITLE__ API reference',
|
|
160
|
+
description: 'HTTP API reference for __TITLE__.',
|
|
161
|
+
},
|
|
162
|
+
// Once you add a security scheme, name it here so the Authentication
|
|
163
|
+
// panel opens on it instead of "No authentication selected". The value
|
|
164
|
+
// must match a key under components.securitySchemes in the schema.
|
|
165
|
+
// authentication: { preferredSecurityScheme: 'HTTPBearer' },
|
|
166
|
+
customCss: `
|
|
167
|
+
:root {
|
|
168
|
+
/* Room for the brand bar so Scalar still fits the viewport. Drop
|
|
169
|
+
this line together with the <header> if you remove the bar. */
|
|
170
|
+
--scalar-custom-header-height: 56px;
|
|
171
|
+
}
|
|
172
|
+
a[href*="scalar.com"] { display: none !important; }
|
|
173
|
+
`,
|
|
174
|
+
})
|
|
175
|
+
</script>
|
|
176
|
+
</body>
|
|
177
|
+
</html>"""
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def scalar_html(title: str, openapi_url: str) -> str:
|
|
181
|
+
"""Render the API reference page (Scalar, loaded from a CDN)."""
|
|
182
|
+
return _SCALAR_PAGE.replace("__TITLE__", title).replace("__OPENAPI_URL__", openapi_url)
|
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
from fastapi import APIRouter, FastAPI
|
|
4
4
|
from fastapi.middleware.cors import CORSMiddleware
|
|
5
|
+
from fastapi.responses import HTMLResponse
|
|
5
6
|
|
|
7
|
+
from app.core.api_docs import API_DESCRIPTION, API_TAGS, scalar_html
|
|
8
|
+
from app.core.config import settings
|
|
6
9
|
from app.routers import (
|
|
7
10
|
health,
|
|
8
11
|
{{#if withPostgres}}
|
|
@@ -15,10 +18,19 @@ from app.routers import (
|
|
|
15
18
|
# the ALB Ingress (which routes /api → backend, everything else → frontend). This
|
|
16
19
|
# keeps a single backend prefix and works identically in local dev (the Vite
|
|
17
20
|
# proxy forwards /api) and in the cluster.
|
|
21
|
+
#
|
|
22
|
+
# The built-in Swagger UI and ReDoc are off: /api/docs is the one reference, and
|
|
23
|
+
# it renders the narrative from app/core/api_docs.py.
|
|
18
24
|
app = FastAPI(
|
|
19
|
-
title="{{projectName}}
|
|
20
|
-
|
|
21
|
-
|
|
25
|
+
title="{{projectName}} API",
|
|
26
|
+
description=API_DESCRIPTION,
|
|
27
|
+
version="1.0.0",
|
|
28
|
+
openapi_tags=API_TAGS,
|
|
29
|
+
# Add deployed environments here so the reference's "Test Request" panel can
|
|
30
|
+
# target them.
|
|
31
|
+
servers=[{"url": "http://localhost:8000", "description": "Local development"}],
|
|
32
|
+
docs_url=None,
|
|
33
|
+
redoc_url=None,
|
|
22
34
|
openapi_url="/api/openapi.json",
|
|
23
35
|
)
|
|
24
36
|
|
|
@@ -30,6 +42,13 @@ app.add_middleware(
|
|
|
30
42
|
allow_headers=["*"],
|
|
31
43
|
)
|
|
32
44
|
|
|
45
|
+
|
|
46
|
+
@app.get("/api/docs", include_in_schema=False)
|
|
47
|
+
async def get_api_docs() -> HTMLResponse:
|
|
48
|
+
"""Serve the API reference (Scalar)."""
|
|
49
|
+
return HTMLResponse(scalar_html(settings.APP_TITLE, "/api/openapi.json"))
|
|
50
|
+
|
|
51
|
+
|
|
33
52
|
api_router = APIRouter(prefix="/api")
|
|
34
53
|
|
|
35
54
|
api_router.include_router(health.router)
|
|
@@ -4,10 +4,18 @@ from fastapi import APIRouter
|
|
|
4
4
|
|
|
5
5
|
from app.schemas.health import HealthResponse
|
|
6
6
|
|
|
7
|
-
router = APIRouter()
|
|
7
|
+
router = APIRouter(tags=["Health"])
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
@router.get(
|
|
10
|
+
@router.get(
|
|
11
|
+
"/health",
|
|
12
|
+
summary="Check service health",
|
|
13
|
+
description=(
|
|
14
|
+
'Returns `{"status": "ok"}` while the process is accepting requests. This '
|
|
15
|
+
"is a liveness probe, not a status page — it does not test the database, "
|
|
16
|
+
"the cache or any downstream service."
|
|
17
|
+
),
|
|
18
|
+
)
|
|
11
19
|
async def get_health() -> HealthResponse:
|
|
12
20
|
"""Health-check endpoint."""
|
|
13
21
|
return HealthResponse(status="ok")
|
|
@@ -36,5 +36,9 @@ line-length = 100
|
|
|
36
36
|
[tool.ruff.lint]
|
|
37
37
|
select = ["E", "F", "I", "N", "UP", "B", "S", "RUF"]
|
|
38
38
|
|
|
39
|
+
[tool.ruff.lint.per-file-ignores]
|
|
40
|
+
# Prose and an embedded HTML document — line length is not a useful rule there.
|
|
41
|
+
"app/core/api_docs.py" = ["E501"]
|
|
42
|
+
|
|
39
43
|
[tool.mypy]
|
|
40
44
|
strict = true
|
|
@@ -61,8 +61,8 @@ Once the stack is up, open these URLs:
|
|
|
61
61
|
|---------|-----|-------|
|
|
62
62
|
| **Frontend** | http://localhost:5173 | React + Vite — HMR active |
|
|
63
63
|
| **API** | http://localhost:8000 | FastAPI |
|
|
64
|
-
| **
|
|
65
|
-
| **
|
|
64
|
+
| **API reference** | http://localhost:8000/api/docs | Interactive reference (Scalar) |
|
|
65
|
+
| **OpenAPI schema** | http://localhost:8000/api/openapi.json | Raw schema (JSON) |
|
|
66
66
|
| **Health** | http://localhost:8000/api/health | JSON health check |
|
|
67
67
|
{{#if withCelery}}| **Flower** | http://localhost:5555 | Celery task monitor |
|
|
68
68
|
{{/if}}
|
|
@@ -73,7 +73,8 @@ dude docs # → http://localhost:8001
|
|
|
73
73
|
|----------|-----|
|
|
74
74
|
| Frontend (dev) | <http://localhost:5173> |
|
|
75
75
|
| Backend API | <http://localhost:8000> |
|
|
76
|
-
|
|
|
76
|
+
| API reference (Scalar) | <http://localhost:8000/api/docs> |
|
|
77
|
+
| OpenAPI schema | <http://localhost:8000/api/openapi.json> |
|
|
77
78
|
| API health | <http://localhost:8000/api/health> |
|
|
78
79
|
{{#if withCelery}}
|
|
79
80
|
| Flower | <http://localhost:5555> |
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
openapi: 3.1.0
|
|
2
2
|
info:
|
|
3
|
-
title: {{projectName}}
|
|
4
|
-
version:
|
|
3
|
+
title: {{projectName}} API
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
paths:
|
|
6
6
|
/api/health:
|
|
7
7
|
get:
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
tags:
|
|
9
|
+
- Health
|
|
10
|
+
summary: Check service health
|
|
11
|
+
description: 'Returns `{"status": "ok"}` while the process is accepting requests.'
|
|
10
12
|
operationId: get_health_api_health_get
|
|
11
13
|
responses:
|
|
12
14
|
"200":
|
|
@@ -9,11 +9,19 @@ from app.core.database import get_db
|
|
|
9
9
|
from app.models.user import User
|
|
10
10
|
from app.queries.users import UsersQueries
|
|
11
11
|
|
|
12
|
-
router = APIRouter(tags=["
|
|
12
|
+
router = APIRouter(tags=["Users"])
|
|
13
13
|
_users = UsersQueries()
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
@router.get(
|
|
16
|
+
@router.get(
|
|
17
|
+
"/users",
|
|
18
|
+
response_model=list[User],
|
|
19
|
+
summary="List users",
|
|
20
|
+
description=(
|
|
21
|
+
"Returns every user as a bare array — there is no pagination envelope and "
|
|
22
|
+
"no result cap. Add pagination before this collection can grow."
|
|
23
|
+
),
|
|
24
|
+
)
|
|
17
25
|
def get_users(db: Session = Depends(get_db)) -> Sequence[User]:
|
|
18
26
|
"""Return all users."""
|
|
19
27
|
return _users.get_all(db)
|
|
@@ -7,11 +7,19 @@ from app.core.database import get_db
|
|
|
7
7
|
from app.models.user import User
|
|
8
8
|
from app.queries.users import UsersQueries
|
|
9
9
|
|
|
10
|
-
router = APIRouter(tags=["
|
|
10
|
+
router = APIRouter(tags=["Users"])
|
|
11
11
|
_users = UsersQueries()
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
@router.get(
|
|
14
|
+
@router.get(
|
|
15
|
+
"/users/{id}",
|
|
16
|
+
response_model=User,
|
|
17
|
+
summary="Retrieve a user",
|
|
18
|
+
description=(
|
|
19
|
+
'An unknown id returns `404` with `{"detail": "User not found"}`. A '
|
|
20
|
+
"non-integer id never reaches this handler — FastAPI rejects it with `422`."
|
|
21
|
+
),
|
|
22
|
+
)
|
|
15
23
|
def get_users__id(id: int, db: Session = Depends(get_db)) -> User:
|
|
16
24
|
"""Return a single user by ID."""
|
|
17
25
|
user = _users.get_by_id(db, id)
|