Skip to content

Commit 7cceaa1

Browse files
refactor: simplify API fetch error handling in aa-chains route
1 parent 0e26a88 commit 7cceaa1

File tree

2 files changed

+35
-63
lines changed

2 files changed

+35
-63
lines changed

apps/portal/src/app/api/aa-chains/route.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

apps/portal/src/components/Document/AAChainList.tsx

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
11
/* eslint-disable @next/next/no-img-element */
22
import { cn } from "@/lib/utils";
33
import type { ChainMetadata } from "thirdweb/chains";
4-
import { getBaseUrl } from "../../lib/getBaseUrl";
4+
5+
type ApiResponseType = {
6+
data: Record<string, { service: string; enabled: boolean }[]>;
7+
};
58

69
async function getChains(): Promise<ChainMetadata[]> {
710
try {
8-
const chains = await fetch(`${getBaseUrl()}/api/aa-chains`);
9-
if (!chains.ok) {
10-
return [];
11-
}
12-
const result = (await chains.json()) as { data: ChainMetadata[] };
13-
return result.data;
11+
const [chainsWithServices, allChains] = await Promise.all([
12+
fetch("https://api.thirdweb.com/v1/chains/services", {
13+
headers: {
14+
"Content-Type": "application/json",
15+
},
16+
}).then((res) => res.json() as Promise<ApiResponseType>),
17+
fetch("https://api.thirdweb.com/v1/chains", {
18+
headers: {
19+
"Content-Type": "application/json",
20+
},
21+
}).then((res) => res.json() as Promise<{ data: ChainMetadata[] }>),
22+
]);
23+
24+
const aaChains = Object.entries(chainsWithServices.data)
25+
.filter(([, services]) =>
26+
services.some(
27+
(service) =>
28+
service.service === "account-abstraction" && service.enabled,
29+
),
30+
)
31+
.map(([chainId]) => Number(chainId));
32+
33+
const intersectedChains = allChains.data
34+
.filter((chain) =>
35+
aaChains.some((aaChainId) => aaChainId === chain.chainId),
36+
)
37+
.filter((c) => c.name)
38+
.sort((a, b) => a.name.localeCompare(b.name));
39+
return intersectedChains;
1440
} catch (error) {
15-
console.error(error);
16-
return [];
41+
console.error("Failed to fetch chains", error);
42+
throw error;
1743
}
1844
}
1945

0 commit comments

Comments
 (0)