mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-09 12:57:45 +00:00
add unicorn data to search API response, fix type errors
This commit is contained in:
@@ -2,7 +2,8 @@ import type { VercelRequest, VercelResponse } from "@vercel/node";
|
||||
import Fuse from "fuse.js";
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
import type { ExtendedPostInfo } from "types/index";
|
||||
import { CollectionInfo, UnicornInfo, type PostInfo } from "types/index";
|
||||
import type { ServerReturnType } from "../src/views/search/types";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const searchIndex = require("./searchIndex.json");
|
||||
@@ -12,7 +13,7 @@ const collectionIndex = Fuse.parseIndex(searchIndex.collectionIndex);
|
||||
const posts = searchIndex.posts;
|
||||
const collections = searchIndex.collections;
|
||||
|
||||
const postFuse = new Fuse<ExtendedPostInfo>(
|
||||
const postFuse = new Fuse<PostInfo>(
|
||||
posts,
|
||||
{
|
||||
threshold: 0.3,
|
||||
@@ -23,7 +24,7 @@ const postFuse = new Fuse<ExtendedPostInfo>(
|
||||
postIndex,
|
||||
);
|
||||
|
||||
const collectionFuse = new Fuse(
|
||||
const collectionFuse = new Fuse<CollectionInfo>(
|
||||
collections,
|
||||
{
|
||||
threshold: 0.3,
|
||||
@@ -34,35 +35,53 @@ const collectionFuse = new Fuse(
|
||||
collectionIndex,
|
||||
);
|
||||
|
||||
export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
const unicorns: Record<string, UnicornInfo> = searchIndex.unicorns;
|
||||
|
||||
function runQuery(req: VercelRequest): ServerReturnType {
|
||||
// TODO: `pickdeep` only required fields
|
||||
const searchStr = req?.query?.query as string;
|
||||
if (!searchStr) {
|
||||
res.send({
|
||||
return {
|
||||
unicorns: {},
|
||||
posts: [],
|
||||
totalPosts: 0,
|
||||
collections: [],
|
||||
totalCollections: 0,
|
||||
});
|
||||
return;
|
||||
};
|
||||
}
|
||||
if (searchStr === "*") {
|
||||
res.send({
|
||||
return {
|
||||
unicorns,
|
||||
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({
|
||||
|
||||
const searchedUnicorns: Record<string, UnicornInfo> = {};
|
||||
for (const post of searchedPosts) {
|
||||
for (const id of post.authors) searchedUnicorns[id] = unicorns[id];
|
||||
}
|
||||
for (const collection of searchedCollections) {
|
||||
for (const id of collection.authors) searchedUnicorns[id] = unicorns[id];
|
||||
}
|
||||
|
||||
return {
|
||||
unicorns: searchedUnicorns,
|
||||
posts: searchedPosts,
|
||||
totalPosts: searchedPosts.length,
|
||||
collections: searchedCollections,
|
||||
totalCollections: searchedCollections.length,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
const response = runQuery(req);
|
||||
res.send(response);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user