All files / src/storage lookup-profile.ts

84.61% Statements 11/13
50% Branches 4/8
100% Functions 1/1
91.66% Lines 11/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 303x 3x   3x             3x     3x 3x 3x 3x 3x 3x 3x                    
import { DEFAULT_ZONEFILE_LOOKUP_URL } from './common/constants';
import { resolveZoneFileToProfile } from './get-file/zone-file-to-profile';
import type { ProfileLookupOptions } from './common/types';
import { fetchPrivate } from 'micro-stacks/common';
 
/**
 * Look up a user profile by BNS name
 *
 * @returns {Promise} that resolves to a profile object
 */
export async function lookupProfile(
  options: ProfileLookupOptions
): Promise<Record<string, any> | null> {
  const { username, zoneFileLookupURL = DEFAULT_ZONEFILE_LOOKUP_URL } = options;
  Iif (!username) return Promise.reject();
  const url = `${zoneFileLookupURL.replace(/\/$/, '')}/${options.username}`;
  const res = await fetchPrivate(url);
  const payload: { zonefile: string; address: string } = await res.json();
  if (payload.hasOwnProperty('zonefile') && payload.hasOwnProperty('address')) {
    return (await resolveZoneFileToProfile(
      payload.zonefile,
      options.verify ? payload.address : undefined
    )) as Record<string, any> | null;
  } else E{
    throw new Error(
      'Invalid zonefile lookup response: did not contain `address`' + ' or `zonefile` field'
    );
  }
}