From 25c93068e0c42efca158a4c97e8b036f053ce48e Mon Sep 17 00:00:00 2001 From: Griko Nibras Date: Wed, 31 Jan 2024 06:40:54 +0700 Subject: [PATCH] fix: prioritize mintscan explorer links [FRE-585] --- src/pages/api/explorer/[chainId].ts | 72 ++++++++++++++--------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/src/pages/api/explorer/[chainId].ts b/src/pages/api/explorer/[chainId].ts index 2b95f0ad..a59a5928 100644 --- a/src/pages/api/explorer/[chainId].ts +++ b/src/pages/api/explorer/[chainId].ts @@ -3,50 +3,50 @@ import { NextRequest } from "next/server"; import { explorersRecord } from "@/chains/explorers"; import { ExplorerResponse } from "@/schemas/api"; +import { raise } from "@/utils/assert"; export const config: PageConfig = { runtime: "edge", }; export default async function handler(req: NextRequest) { - let baseUrl: string | undefined; - const chainId = req.nextUrl.searchParams.get("chainId") || ""; - - const parsedIntId = parseInt(chainId); - const isEvmChain = typeof parsedIntId === "number" && !isNaN(parsedIntId); - - if (isEvmChain) { - const { EVM_CHAINS } = await import("@/constants/wagmi"); - const chain = EVM_CHAINS.find((chain) => chain.id === parseInt(chainId)); - if (chain?.blockExplorers?.default.url) { - baseUrl = chain.blockExplorers!.default.url; + try { + let baseUrl: string | undefined; + const chainId = req.nextUrl.searchParams.get("chainId") || raise("/api/explorer error: 'chainId' is required"); + + const parsedIntId = parseInt(chainId); + const isEvmChain = typeof parsedIntId === "number" && !isNaN(parsedIntId); + + if (isEvmChain) { + const { EVM_CHAINS } = await import("@/constants/wagmi"); + const chain = EVM_CHAINS.find((chain) => chain.id === parseInt(chainId)); + if (chain?.blockExplorers?.default.url) { + baseUrl = chain.blockExplorers!.default.url; + } + } else { + const explorers = explorersRecord[chainId] || []; + + baseUrl ||= explorers.find((explorer) => explorer.kind === "mintscan")?.tx_page; + baseUrl ||= explorers[0]?.tx_page; } - } - const explorers = explorersRecord[chainId] || []; - - const mintscan = explorers.find((explorer) => explorer.kind === "mintscan"); - if (mintscan && mintscan.tx_page) { - baseUrl = mintscan.tx_page; - } - - if (explorers[0]?.tx_page) { - baseUrl = explorers[0].tx_page; - } + if (!baseUrl) { + return new Response(null, { status: 404 }); // Not Found + } - if (!baseUrl) { - return new Response(null, { status: 404 }); // Not Found + const payload: ExplorerResponse = { + evm: isEvmChain, + explorer: baseUrl, + }; + + return new Response(JSON.stringify(payload), { + headers: { + "cache-control": "public, max-age=604800, immutable", // 1 week + "content-type": "application/json", + }, + }); + } catch (error) { + console.error(error); + return new Response(null, { status: 500 }); // Internal Server Error } - - const payload: ExplorerResponse = { - evm: isEvmChain, - explorer: baseUrl, - }; - - return new Response(JSON.stringify(payload), { - headers: { - "cache-control": "public, max-age=604800, immutable", // 1 week - "content-type": "application/json", - }, - }); }