commit
This commit is contained in:
60
.github/copilot-instructions.md
vendored
60
.github/copilot-instructions.md
vendored
@@ -40,6 +40,66 @@ This monorepo contains a Django backend and a Vite/React frontend, orchestrated
|
||||
- Use TypeScript strict mode (see `tsconfig.*.json`).
|
||||
- Linting: ESLint config in `eslint.config.js`.
|
||||
|
||||
### Frontend API Client (required)
|
||||
All frontend API calls must use the shared client at frontend/src/api/Client.ts.
|
||||
|
||||
- Client.public: no cookies, no Authorization header (for public Django endpoints).
|
||||
- Client.auth: sends cookies and includes Bearer token; auto-refreshes on 401 (retries up to 2x).
|
||||
- Centralized error handling: subscribe via Client.onError to show toasts/snackbars.
|
||||
- Tokens are stored in cookies by Client.setTokens and cleared by Client.clearTokens.
|
||||
|
||||
Example usage (TypeScript)
|
||||
```ts
|
||||
import Client from "@/api/Client";
|
||||
|
||||
// Public request (no credentials)
|
||||
async function listPublicItems() {
|
||||
const res = await Client.public.get("/api/public/items/");
|
||||
return res.data;
|
||||
}
|
||||
|
||||
// Login (obtain tokens and persist to cookies)
|
||||
async function login(username: string, password: string) {
|
||||
// Default SimpleJWT endpoint (adjust if your backend differs)
|
||||
const res = await Client.public.post("/api/token/", { username, password });
|
||||
const { access, refresh } = res.data;
|
||||
Client.setTokens(access, refresh);
|
||||
}
|
||||
|
||||
// Authenticated requests (auto Bearer + refresh on 401)
|
||||
async function fetchProfile() {
|
||||
const res = await Client.auth.get("/api/users/me/");
|
||||
return res.data;
|
||||
}
|
||||
|
||||
function logout() {
|
||||
Client.clearTokens();
|
||||
window.location.assign("/login");
|
||||
}
|
||||
|
||||
// Global error toasts
|
||||
import { useEffect } from "react";
|
||||
function useApiErrors(showToast: (msg: string) => void) {
|
||||
useEffect(() => {
|
||||
const unsubscribe = Client.onError((e) => {
|
||||
const { message, status } = e.detail;
|
||||
showToast(status ? `${status}: ${message}` : message);
|
||||
});
|
||||
return unsubscribe;
|
||||
}, [showToast]);
|
||||
}
|
||||
```
|
||||
|
||||
Vite env used by the client:
|
||||
- VITE_API_BASE_URL (default: http://localhost:8000)
|
||||
- VITE_API_REFRESH_URL (default: /api/token/refresh/)
|
||||
- VITE_LOGIN_PATH (default: /login)
|
||||
|
||||
Notes
|
||||
- Public client never sends cookies or Authorization.
|
||||
- Ensure Django CORS settings allow your frontend origin. See backend/vontor_cz/settings.py.
|
||||
- Use React Router layouts and guards as documented in frontend/src/routes/ROUTES.md and frontend/src/layouts/LAYOUTS.md.
|
||||
|
||||
## Integration Points
|
||||
- **Payments**: `thirdparty/` contains custom integrations for Stripe, GoPay, Trading212.
|
||||
- **Real-time**: Django Channels (ASGI, Redis) for websockets.
|
||||
|
||||
Reference in New Issue
Block a user