Skip to content

Commit

Permalink
fix: fix API response
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Nov 21, 2023
1 parent 7f68d3e commit 286143d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
35 changes: 17 additions & 18 deletions src/addressResolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as ensResolver from './ens';
import * as lensResolver from './lens';
import * as unstoppableDomainResolver from './unstoppableDomains';
import cache from './cache';
import { Address, Handle } from './utils';
import { Address, Handle, withoutEmptyValues } from './utils';
import { version } from 'os';

Check warning on line 7 in src/addressResolvers/index.ts

View workflow job for this annotation

GitHub Actions / lint / Lint

'version' is defined but never used

const RESOLVERS = [ensResolver, unstoppableDomainResolver, lensResolver];
const MAX_LOOKUP_ADDRESSES = 250;
Expand All @@ -23,23 +24,21 @@ export async function lookupAddresses(addresses: Address[]) {
return Promise.reject({ error: 'params contains invalid address', code: 400 });
}

return Object.fromEntries(
Object.entries(
await cache(normalizedAddresses, async (addresses: Address[]) => {
const results = await Promise.all(RESOLVERS.map(r => r.lookupAddresses(addresses)));

return Object.fromEntries(
addresses.map(address => [
address,
results.map(r => r[address]).filter(handle => !!handle)[0] || ''
])
);
})
).filter(([, handle]) => handle)
);
const results = await cache(normalizedAddresses, async (addresses: Address[]) => {
const results = await Promise.all(RESOLVERS.map(r => r.lookupAddresses(addresses)));

return Object.fromEntries(
addresses.map(address => [
address,
results.map(r => r[address]).filter(handle => !!handle)[0] || ''
])
);
});

return withoutEmptyValues(results);
}

export async function resolveName(handle: Handle): Promise<Address | undefined> {
export async function resolveName(handle: Handle): Promise<Record<Handle, Address>> {
const results = await cache([handle], async (h: Handle[]) => {
let address: Address | undefined;
const _handle = h[0];
Expand All @@ -52,8 +51,8 @@ export async function resolveName(handle: Handle): Promise<Address | undefined>
address = await unstoppableDomainResolver.resolveName(_handle);
}

return { [handle]: address };
return { [handle]: address || '' };
});

return results[handle];
return withoutEmptyValues(results);
}
4 changes: 4 additions & 0 deletions src/addressResolvers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ const broviderUrl = process.env.BROVIDER_URL || 'https://rpc.brovider.xyz';
export function provider(network: string) {
return snapshot.utils.getProvider(network, { broviderUrl });
}

export function withoutEmptyValues(obj: Record<string, any>) {
return Object.fromEntries(Object.entries(obj).filter(([, value]) => value));
}
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ router.post('/', async (req, res) => {
else if (method === 'resolve_name') result = await resolveName(params);
else return rpcError(res, 400, 'invalid method', id);

if (result.error) return rpcError(res, result.code || 500, result.error, id);
return rpcSuccess(res, result, id);
if (result?.error) return rpcError(res, result.code || 500, result.error, id);
return rpcSuccess(res, { address: result[params] }, id);
} catch (e) {
capture(e);
return rpcError(res, 500, e, id);
Expand Down

0 comments on commit 286143d

Please sign in to comment.