feat(TRUEREF-0009-0010): implement indexing pipeline job queue and public REST API
- SQLite-backed job queue with sequential processing and startup recovery - Atomic snippet replacement in single transaction - context7-compatible GET /api/v1/libs/search and GET /api/v1/context - Token budget limiting and JSON/txt response format support - CORS headers on all API routes via SvelteKit handle hook - Library ID parser supporting /owner/repo and /owner/repo/version Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
64
src/routes/api/v1/libs/search/+server.ts
Normal file
64
src/routes/api/v1/libs/search/+server.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* GET /api/v1/libs/search
|
||||
*
|
||||
* Search libraries by name. Compatible with context7's
|
||||
* GET /api/v2/libs/search interface.
|
||||
*
|
||||
* Query parameters:
|
||||
* libraryName (required) — library name to search for
|
||||
* query (optional) — user's question for relevance ranking
|
||||
* limit (optional) — max results, default 10, max 50
|
||||
* type (optional) — "json" (default) or "txt"
|
||||
*/
|
||||
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { getClient } from '$lib/server/db/client';
|
||||
import { SearchService } from '$lib/server/search/search.service';
|
||||
import { formatLibrarySearchJson } from '$lib/server/api/formatters';
|
||||
import { CORS_HEADERS } from '$lib/server/api/formatters';
|
||||
|
||||
function getService(): SearchService {
|
||||
return new SearchService(getClient());
|
||||
}
|
||||
|
||||
export const GET: RequestHandler = ({ url }) => {
|
||||
const libraryName = url.searchParams.get('libraryName');
|
||||
|
||||
if (!libraryName || !libraryName.trim()) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: 'libraryName is required', code: 'MISSING_PARAMETER' }),
|
||||
{
|
||||
status: 400,
|
||||
headers: { 'Content-Type': 'application/json', ...CORS_HEADERS }
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const query = url.searchParams.get('query') ?? undefined;
|
||||
const limitRaw = parseInt(url.searchParams.get('limit') ?? '10', 10);
|
||||
const limit = Math.min(isNaN(limitRaw) || limitRaw < 1 ? 10 : limitRaw, 50);
|
||||
|
||||
try {
|
||||
const service = getService();
|
||||
const results = service.searchRepositories({ libraryName, query, limit });
|
||||
const body = formatLibrarySearchJson(results);
|
||||
|
||||
return json(body, {
|
||||
headers: CORS_HEADERS
|
||||
});
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Internal server error';
|
||||
return new Response(JSON.stringify({ error: message, code: 'INTERNAL_ERROR' }), {
|
||||
status: 500,
|
||||
headers: { 'Content-Type': 'application/json', ...CORS_HEADERS }
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const OPTIONS: RequestHandler = () => {
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
headers: CORS_HEADERS
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user