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>
44 lines
1.2 KiB
TypeScript
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'
|
|
}
|
|
});
|
|
};
|