feat: integrate typesense
This commit is contained in:
31
routes/api/resources.ts
Normal file
31
routes/api/resources.ts
Normal 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);
|
||||
},
|
||||
};
|
Reference in New Issue
Block a user