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,
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: searchedPosts,
totalPosts: searchedPosts.length,
collections: searchedCollections,
totalCollections: searchedCollections.length,
});
};