Skip to content

Commit

Permalink
Limit string length on search route
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansive committed Dec 22, 2023
1 parent 9635011 commit f62c244
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/app/api/search/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export async function GET(request: NextRequest) {
const searchParams: string = decodeURIComponent(
request.nextUrl.searchParams.toString()
);
if (searchParams.length > 128) {
throw NextResponse.json(
{ message: "Search entry is too long" },
{ status: 403 }
);
}

const results = await xata.db.Books.search(searchParams, {
target: ["title"],
Expand Down
15 changes: 10 additions & 5 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ export default function Home() {

setStatus("loading");

setTimeout(async () => {
// const nextSearchResults = BOOKS.filter(({ title }) =>
// title.includes(searchTerm)
// );
try {
const response = await fetch("/api/search?query=" + searchTerm);
if (!response.ok) {
throw new Error();
}
const data = await response.json();
setSearchResults(data["records"]);
setStatus("success");
}, 1500);
console.log("success");
} catch (error) {
console.log("wtf");
setStatus("idle");
//setStatus("error");
}
};

return (
Expand Down

0 comments on commit f62c244

Please sign in to comment.