Purpose
How the React app calls the backend. Three hooks plus a service module wrap every backend call.
Hooks
useKundkort(orgNr, token)
File: frontend/kundkort/hooks/useKundkort.ts. Returns { data, loading, error, statusCode, refetch, enrich }.
GET /api/kundkort/:orgNr(URL-encoded) →KundkortResponse(useKundkort.ts:71-95)enrich()→POST /api/kundkort/:orgNr/enrichwith body{ bypass_cache: true }(useKundkort.ts:24-55). Auto-refetches the detail after success.- Error mapping: 401 → “Ej autentiserad”; 404 → “Företaget hittades inte”; ≥500 → “Serverfel”; thrown → “Nätverksfel” (
useKundkort.ts:78-105). refetch()bumps an internaltickto re-trigger the effect.
useSearch(query, token, filters)
File: frontend/kundkort/hooks/useSearch.ts. Returns { results, loading }.
- 300 ms debounce
- Minimum 2 chars unless filters set
- Endpoints:
/api/kundkort/search?q=…for plain,/api/kundkort/search/advanced?…when any filter present (useSearch.ts:46-49) - Filter params:
sni,city,legal_form,active=false - Cancels in-flight timer on dependency change
useEcoApi(orgNr, token)
See EcoAPI Integration. Wraps the secondary financial-insights backend.
useAuth()
See Auth Flow.
Endpoints used by the SPA
| Endpoint | Caller | Method |
|---|---|---|
/api/auth/login | useAuth | POST |
/api/config | Footer | GET |
/api/kundkort/search | useSearch | GET |
/api/kundkort/search/advanced | useSearch | GET |
/api/kundkort/export?format=csv | SearchPage.tsx:58 | GET |
/api/kundkort/:orgNr | useKundkort | GET |
/api/kundkort/:orgNr/enrich | useKundkort.enrich, triggerEcoEnrichment | POST |
/api/enrichment/errors?resolved=false&limit=… | ErrorPanel | GET |
Backend route definitions: src/api/kundkort.ts (handlers, ~1063 lines) and src/api/index.ts:744-776 (regex routing table).
Auth
Every call sends Authorization: Bearer ${token}. /api/config is the lone exception — it is intentionally public, used by the footer DEVMODE badge (src/api/index.ts:114).
See also
Auth Flow, EcoAPI Integration, Kundkort Page, Search Page.