feat: integrate typesense

This commit is contained in:
2023-08-05 21:52:43 +02:00
parent f35a63fcee
commit 46cd823b6c
9 changed files with 241 additions and 26 deletions

31
routes/api/resources.ts Normal file
View File

@ -0,0 +1,31 @@
import { Handlers } from "$fresh/server.ts";
import { BadRequestError } from "@lib/errors.ts";
import { getTypeSenseClient } from "@lib/typesense.ts";
import { json } from "@lib/helpers.ts";
export const handler: Handlers = {
async GET(req, _ctx) {
const url = new URL(req.url);
const query = url.searchParams.get("q");
if (!query) {
throw new BadRequestError('Query parameter "q" is required.');
}
const query_by = url.searchParams.get("query_by") || "name,description";
const typesenseClient = await getTypeSenseClient();
if (!typesenseClient) {
throw new Error("Query not available");
}
// Perform the Typesense search
const searchResults = await typesenseClient.collections("resources")
.documents().search({
q: query,
query_by,
limit_hits: 100,
});
return json(searchResults.hits);
},
};