Skip to content

Commit

Permalink
fix: response
Browse files Browse the repository at this point in the history
  • Loading branch information
guanbinrui committed Aug 3, 2024
1 parent af33bb7 commit c527879
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/services/getPostLinksKV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ export async function readPostLinks(request: Request): Promise<PostLinks | null>
if (source && postId) {
return await kv.hgetall(`post-links:${source}:${postId}`);
}

return null;
}

export async function readPostLinksAll(request: Request): Promise<Record<string, PostLinks[]> | null> {
export async function readPostLinksAll(request: Request): Promise<Record<string, PostLinks> | null> {
const { searchParams } = new URL(request.url);

const parsed = PostIndicator.safeParse({
Expand All @@ -36,10 +35,14 @@ export async function readPostLinksAll(request: Request): Promise<Record<string,

if (source && postIds?.length) {
const allSettled = await Promise.allSettled(
postIds.map((postId) => kv.hgetall(`post-links:${source}:${postId}`)),
postIds.map(async (postId) => {
const links = await kv.hgetall(`post-links:${source}:${postId}`);
if (links) return [postId, links] as [string, PostLinks];
return null;
}),
);
return Object.fromEntries(
allSettled.map((x) => (x.status === 'fulfilled' && x.value ? [x.value.postId, x.value] : [])),
allSettled.map((x) => (x.status === 'fulfilled' && x.value ? x.value : null)).filter((y) => y !== null),
);
}
return null;
Expand Down

0 comments on commit c527879

Please sign in to comment.