chore: add ability to search for everything

This commit is contained in:
Corbin Crutchley
2023-07-24 06:04:22 -07:00
parent b0c04f6220
commit 07cda8cff6

View File

@@ -20,7 +20,7 @@ const postFuse = new Fuse<ExtendedPostInfo>(
includeScore: true, includeScore: true,
ignoreFieldNorm: true, ignoreFieldNorm: true,
}, },
postIndex postIndex,
); );
const collectionFuse = new Fuse( const collectionFuse = new Fuse(
@@ -31,20 +31,38 @@ const collectionFuse = new Fuse(
includeScore: true, includeScore: true,
ignoreFieldNorm: true, ignoreFieldNorm: true,
}, },
collectionIndex collectionIndex,
); );
export default async (req: VercelRequest, res: VercelResponse) => { export default async (req: VercelRequest, res: VercelResponse) => {
// TODO: `pickdeep` only required fields // TODO: `pickdeep` only required fields
const searchStr = req?.query?.query as string; const searchStr = req?.query?.query as string;
if (!searchStr) return []; if (!searchStr) {
if (Array.isArray(searchStr)) return []; res.send({
const posts = postFuse.search(searchStr).map((item) => item.item); posts: [],
const collections = collectionFuse.search(searchStr).map((item) => item.item); totalPosts: 0,
collections: [],
totalCollections: 0,
});
return;
}
if (searchStr === "*") {
res.send({ res.send({
posts, posts,
totalPosts: posts.length, totalPosts: posts.length,
collections, collections,
totalCollections: collections.length, 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: searchedPosts,
totalPosts: searchedPosts.length,
collections: searchedCollections,
totalCollections: searchedCollections.length,
});
}; };