All files / src/profiles profileLookup.ts

81.82% Statements 9/11
100% Branches 2/2
100% Functions 3/3
81.82% Lines 9/11

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 30 31 32 33 34 35 36 37  1x 1x                     1x           2x 2x 2x   3x   5x       5x              
 
import { resolveZoneFileToProfile } from './profileZoneFiles'
import { config } from '../config'
 
/**
 * Look up a user profile by blockstack ID
 *
 * @param {string} username - The Blockstack ID of the profile to look up
 * @param {string} [zoneFileLookupURL=null] - The URL
 * to use for zonefile lookup. If falsey, lookupProfile will use the
 * blockstack.js [[getNameInfo]] function.
 * @returns {Promise} that resolves to a profile object
 */
export function lookupProfile(username: string, zoneFileLookupURL?: string): Promise<any> {
  if (!username) {
    return Promise.reject()
  }
  let lookupPromise
  if (zoneFileLookupURL) {
    const url = `${zoneFileLookupURL.replace(/\/$/, '')}/${username}`
    lookupPromise = fetch(url)
      .then(response => response.json())
  } else {
    lookupPromise = config.network.getNameInfo(username)
  }
  return lookupPromise
    .then((responseJSON) => {
      if (responseJSON.hasOwnProperty('zonefile')
          && responseJSON.hasOwnProperty('address')) {
        return resolveZoneFileToProfile(responseJSON.zonefile, responseJSON.address)
      } else {
        throw new Error('Invalid zonefile lookup response: did not contain `address`'
                        + ' or `zonefile` field')
      }
    })
}