Add shared API types and type the smaller frontend pages

Expand src/types with User, Event, EventOption, Registration, Payment,
Ticket, paginated results and dashboard stats, modelled on the Prisma
schema. apiFetch now defaults to unknown instead of any. Dashboard
loaders are wrapped in useCallback so their effects can list them as
dependencies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 11:10:01 +02:00
co-authored by Claude Opus 5.5
parent 5024ab7241
commit dc4d2fff41
32 changed files with 375 additions and 175 deletions
+16 -12
View File
@@ -1,7 +1,9 @@
import type { Paginated, Payment, User } from "@/types";
export type ApiOptions = {
method?: string;
headers?: Record<string, string>;
body?: any;
body?: unknown;
authToken?: string | null;
nextOptions?: RequestInit;
};
@@ -83,7 +85,7 @@ export class ApiError extends Error {
}
}
export async function apiFetch<T = any>(path: string, options: ApiOptions = {}): Promise<T> {
export async function apiFetch<T = unknown>(path: string, options: ApiOptions = {}): Promise<T> {
const { method = "GET", headers = {}, body, authToken, nextOptions } = options;
const url = path.startsWith("http") ? path : `${API_BASE}${path}`;
@@ -163,10 +165,10 @@ export async function apiFetchListForPrerender<T>(path: string, options: ApiOpti
// Several list endpoints (/api/users, /api/payments) are paginated and cap
// `limit` at 200 server-side, so a single request can silently miss records
// beyond the first page. This walks every page and returns the full list.
async function fetchAllPages(path: string, token: string, params: Record<string, string> = {}): Promise<any[]> {
async function fetchAllPages<T>(path: string, token: string, params: Record<string, string> = {}): Promise<T[]> {
const query = new URLSearchParams({ ...params, limit: "200", page: "1" });
const first = await apiFetch<any>(`${path}?${query.toString()}`, { authToken: token });
const items: any[] = Array.isArray(first?.data) ? first.data : [];
const first = await apiFetch<Paginated<T>>(`${path}?${query.toString()}`, { authToken: token });
const items: T[] = Array.isArray(first?.data) ? first.data : [];
const pages = first?.pages || 1;
if (pages > 1) {
@@ -177,7 +179,7 @@ async function fetchAllPages(path: string, token: string, params: Record<string,
Array.from({ length: pages - 1 }, (_, i) => {
const page = i + 2;
const pageQuery = new URLSearchParams({ ...params, limit: "200", page: String(page) });
return apiFetch<any>(`${path}?${pageQuery.toString()}`, { authToken: token });
return apiFetch<Paginated<T>>(`${path}?${pageQuery.toString()}`, { authToken: token });
})
);
for (const res of remaining) {
@@ -188,19 +190,21 @@ async function fetchAllPages(path: string, token: string, params: Record<string,
return items;
}
export function fetchAllUsers(token: string, params: Record<string, string> = {}): Promise<any[]> {
return fetchAllPages("/api/users", token, params);
export function fetchAllUsers(token: string, params: Record<string, string> = {}): Promise<User[]> {
return fetchAllPages<User>("/api/users", token, params);
}
export function fetchAllPayments(token: string, params: Record<string, string> = {}): Promise<any[]> {
return fetchAllPages("/api/payments", token, params);
export function fetchAllPayments(token: string, params: Record<string, string> = {}): Promise<Payment[]> {
return fetchAllPages<Payment>("/api/payments", token, params);
}
// Creates a Yoco checkout for a registration's full remaining outstanding balance.
// Omitting `amount` makes the backend charge the full remainder rather than a partial amount —
// partial-amount checkouts are only ever created from the supervisor payments dashboard.
export function createFullPaymentCheckout(token: string, registrationId: string): Promise<{ redirectUrl?: string; priceUpdated?: boolean; newTotal?: number; message?: string }> {
return apiFetch("/api/payments/yoco-checkout", {
export type CheckoutResponse = { redirectUrl?: string; priceUpdated?: boolean; newTotal?: number; message?: string };
export function createFullPaymentCheckout(token: string, registrationId: string): Promise<CheckoutResponse> {
return apiFetch<CheckoutResponse>("/api/payments/yoco-checkout", {
method: "POST",
authToken: token,
body: {
+1 -1
View File
@@ -30,7 +30,7 @@ export type TwoFactorChallenge = {
enrolling?: boolean;
};
export function isTwoFactorChallenge(res: unknown): res is TwoFactorChallenge & { twoFactorRequired: true } {
export function isTwoFactorChallenge(res: unknown): res is TwoFactorChallenge {
return !!res && typeof res === "object" && (res as TwoFactorChallenge).twoFactorRequired === true;
}
+2 -2
View File
@@ -1,4 +1,4 @@
export type TableRow = Record<string, any>;
export type TableRow = Record<string, unknown>;
export function downloadCsv(filename: string, rows: TableRow[], columns?: { key: string; label?: string }[]) {
const cols = columns && columns.length > 0 ? columns : Object.keys(rows[0] || {}).map(k => ({ key: k, label: k }));
@@ -24,7 +24,7 @@ function escapeCsv(v: string) {
return v;
}
function valueToString(v: any): string {
function valueToString(v: unknown): string {
if (v == null) return '';
if (v instanceof Date) return v.toISOString();
if (typeof v === 'object') return JSON.stringify(v);
+3 -1
View File
@@ -15,7 +15,9 @@ export function editDistance(a: string, b: string): number {
}
/** Return a score 0–1 for how well a user matches a search query */
export function scoreUser(u: any, raw: string): number {
export type MatchableUser = { name?: string | null; email?: string | null; phoneNumber?: string | null };
export function scoreUser(u: MatchableUser, raw: string): number {
const q = raw.toLowerCase().trim();
if (!q || q.length < 2) return 0;