From 07cda8cff69c89930be5f710eada82273fdc36a4 Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Mon, 24 Jul 2023 06:04:22 -0700 Subject: [PATCH] chore: add ability to search for everything --- api/search.ts | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/api/search.ts b/api/search.ts index 2927ea3f..1e048f1d 100644 --- a/api/search.ts +++ b/api/search.ts @@ -20,7 +20,7 @@ const postFuse = new Fuse( includeScore: true, ignoreFieldNorm: true, }, - postIndex + postIndex, ); const collectionFuse = new Fuse( @@ -31,20 +31,38 @@ const collectionFuse = new Fuse( includeScore: true, ignoreFieldNorm: true, }, - collectionIndex + collectionIndex, ); export default async (req: VercelRequest, res: VercelResponse) => { // TODO: `pickdeep` only required fields const searchStr = req?.query?.query as string; - if (!searchStr) return []; - if (Array.isArray(searchStr)) return []; - const posts = postFuse.search(searchStr).map((item) => item.item); - const collections = collectionFuse.search(searchStr).map((item) => item.item); + if (!searchStr) { + res.send({ + posts: [], + totalPosts: 0, + collections: [], + totalCollections: 0, + }); + return; + } + if (searchStr === "*") { + res.send({ + posts, + totalPosts: posts.length, + collections, + totalCollections: collections.length, + }); + return; + } + const searchedPosts = postFuse.search(searchStr).map((item) => item.item); + const searchedCollections = collectionFuse + .search(searchStr) + .map((item) => item.item); res.send({ - posts, - totalPosts: posts.length, - collections, - totalCollections: collections.length, + posts: searchedPosts, + totalPosts: searchedPosts.length, + collections: searchedCollections, + totalCollections: searchedCollections.length, }); };