fix(UI-0001): map snake_case repo columns to camelCase in libs API response
better-sqlite3 returns raw snake_case column names from SELECT *, so trust_score, total_snippets etc. were not matching the camelCase keys (trustScore, totalSnippets) that RepositoryCard.svelte reads. Added a mapRepo() helper in the GET /api/v1/libs handler to normalise the shape before JSON serialisation, fixing the trust score and snippet count display on repository cards. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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 }) => ({
|
||||
// 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) {
|
||||
|
||||
Reference in New Issue
Block a user