fix: backend SSE uses fetch streaming (EventSource not available in Bun), bulk lookup fallback
This commit is contained in:
+28
-2
@@ -15,7 +15,8 @@ export async function getAddressInfo(address: string): Promise<any> {
|
||||
return res.json();
|
||||
}
|
||||
|
||||
/** Bulk address lookup — POST /address with { terms: [...] } */
|
||||
/** Bulk address lookup — POST /address with { terms: [...] }
|
||||
* Normalizes response to { address: info } map regardless of API format. */
|
||||
export async function getAddressInfoBulk(addresses: string[]): Promise<Record<string, any>> {
|
||||
if (addresses.length === 0) return {};
|
||||
const res = await fetch(`${API}/address`, {
|
||||
@@ -23,7 +24,32 @@ export async function getAddressInfoBulk(addresses: string[]): Promise<Record<st
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ terms: addresses }),
|
||||
});
|
||||
return res.json();
|
||||
const data = await res.json();
|
||||
|
||||
// Normalize: if response is already keyed by address, return as-is.
|
||||
// If it's an array, index by address field.
|
||||
if (Array.isArray(data)) {
|
||||
const map: Record<string, any> = {};
|
||||
for (const item of data) {
|
||||
if (item?.address) map[item.address] = item;
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
// Check if it's keyed by address — verify first value has address-like fields
|
||||
const firstKey = Object.keys(data)[0];
|
||||
if (firstKey && data[firstKey]?.address) return data;
|
||||
|
||||
// If keyed by index (0, 1, 2...), map back to addresses
|
||||
if (firstKey === "0" || firstKey === "1") {
|
||||
const map: Record<string, any> = {};
|
||||
for (let i = 0; i < addresses.length; i++) {
|
||||
if (data[i]) map[addresses[i]] = data[i];
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
export function getQrUrl(text: string): string {
|
||||
|
||||
Reference in New Issue
Block a user