web3.news/app/routes/api.search.ts

27 lines
622 B
TypeScript
Raw Normal View History

2023-12-23 10:51:12 +00:00
import { type LoaderFunctionArgs } from "@remix-run/node";
import search from "@/lib/search.js";
export async function loader({ request }: LoaderFunctionArgs) {
const searchParams = new URL(request.url).searchParams;
const query = searchParams.get("q");
if (!query || !query.length) {
throw new Response("Invalid query", {
status: 400,
});
}
const results = await search.index("articles").search(query);
return results.hits.map((item) => {
return {
id: item.id,
timestamp: item.createdAt,
title: item.title,
description: "",
slug: item.slug,
};
});
}