diff --git a/src/routes/api/v1/libs/+server.ts b/src/routes/api/v1/libs/+server.ts index 2ef09bd..7ae5484 100644 --- a/src/routes/api/v1/libs/+server.ts +++ b/src/routes/api/v1/libs/+server.ts @@ -4,11 +4,60 @@ */ import { json } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; +import type { Repository } from '$lib/types'; import { getClient } from '$lib/server/db/client'; import { RepositoryService } from '$lib/server/services/repository.service'; import { getQueue } from '$lib/server/pipeline/startup'; import { handleServiceError } from '$lib/server/utils/validation'; +// --------------------------------------------------------------------------- +// Row mapper — better-sqlite3 returns snake_case column names from SELECT *; +// this converts them to the camelCase shape expected by the Repository type +// and by client components (e.g. RepositoryCard reads repo.trustScore). +// --------------------------------------------------------------------------- + +interface RawRepoRow { + id: string; + title: string; + description: string | null; + source: string; + source_url: string; + branch: string | null; + state: string; + total_snippets: number; + total_tokens: number; + trust_score: number; + benchmark_score: number; + stars: number | null; + github_token: string | null; + last_indexed_at: number | null; + created_at: number; + updated_at: number; +} + +function mapRepo(raw: Repository): Repository { + const r = raw as unknown as RawRepoRow; + return { + id: r.id, + title: r.title, + description: r.description ?? null, + source: r.source as Repository['source'], + sourceUrl: r.source_url, + branch: r.branch ?? null, + state: r.state as Repository['state'], + totalSnippets: r.total_snippets ?? 0, + totalTokens: r.total_tokens ?? 0, + trustScore: r.trust_score ?? 0, + benchmarkScore: r.benchmark_score ?? 0, + stars: r.stars ?? null, + githubToken: r.github_token ?? null, + lastIndexedAt: + r.last_indexed_at != null ? new Date(r.last_indexed_at * 1000) : null, + createdAt: new Date(r.created_at * 1000), + updatedAt: new Date(r.updated_at * 1000) + }; +} + function getService() { return new RepositoryService(getClient()); } @@ -28,11 +77,14 @@ export const GET: RequestHandler = ({ url }) => { const libraries = service.list({ state: state ?? undefined, limit, offset }); const total = service.count(state ?? undefined); - // Augment each library with its versions array; strip sensitive fields. - const enriched = libraries.map(({ githubToken: _token, ...repo }) => ({ - ...repo, - versions: service.getVersions(repo.id) - })); + // Map raw snake_case rows to camelCase, augment with versions, strip sensitive fields. + const enriched = libraries.map((rawRepo) => { + const { githubToken: _token, ...repo } = mapRepo(rawRepo); + return { + ...repo, + versions: service.getVersions(repo.id) + }; + }); return json({ libraries: enriched, total, limit, offset }); } catch (err) {