Files
trueref/src/routes/api/v1/libs/[id]/index/+server.ts
Giancarmine Salucci 3d1bef5003 feat(TRUEREF-0002): implement repository management service and REST API
Add RepositoryService with full CRUD, ID resolution helpers, input
validation, six SvelteKit API routes (GET/POST /api/v1/libs,
GET/PATCH/DELETE /api/v1/libs/:id, POST /api/v1/libs/:id/index), and
37 unit tests covering all service operations.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-22 17:43:06 +01:00

44 lines
1.2 KiB
TypeScript

/**
* POST /api/v1/libs/:id/index — trigger an indexing job for a repository.
*/
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { getClient } from '$lib/server/db/client';
import { RepositoryService } from '$lib/server/services/repository.service';
import { handleServiceError, NotFoundError } from '$lib/server/utils/validation';
export const POST: RequestHandler = async ({ params, request }) => {
try {
const service = new RepositoryService(getClient());
const id = decodeURIComponent(params.id);
const repo = service.get(id);
if (!repo) throw new NotFoundError(`Repository ${id} not found`);
let versionId: string | undefined;
try {
const body = await request.json();
versionId = body.version ?? undefined;
} catch {
// body is optional
}
const job = service.createIndexingJob(id, versionId);
return json({ job }, { status: 202 });
} catch (err) {
return handleServiceError(err);
}
};
export const OPTIONS: RequestHandler = () => {
return new Response(null, {
status: 204,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
}
});
};