32 lines
894 B
TypeScript
32 lines
894 B
TypeScript
|
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);
|
||
|
},
|
||
|
};
|