memorium/routes/api/resources.ts

38 lines
1017 B
TypeScript
Raw Normal View History

2023-08-05 21:52:43 +02:00
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";
2023-08-06 00:33:06 +02:00
let filter_by = "";
const type = url.searchParams.get("type");
if (type) {
filter_by = `type:=${type}`;
}
2023-08-05 21:52:43 +02:00
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,
2023-08-06 00:33:06 +02:00
filter_by,
2023-08-05 21:52:43 +02:00
});
return json(searchResults.hits);
},
};