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/enrich with 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 internal tick to 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

EndpointCallerMethod
/api/auth/loginuseAuthPOST
/api/configFooterGET
/api/kundkort/searchuseSearchGET
/api/kundkort/search/advanceduseSearchGET
/api/kundkort/export?format=csvSearchPage.tsx:58GET
/api/kundkort/:orgNruseKundkortGET
/api/kundkort/:orgNr/enrichuseKundkort.enrich, triggerEcoEnrichmentPOST
/api/enrichment/errors?resolved=false&limit=…ErrorPanelGET

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.

See also