# All files / src/profiles profileLookup.ts

83.33% Statements 10/12
100% Branches 2/2
100% Functions 3/3
83.33% Lines 10/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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    1x
    1x
    1x

    1x

    2x
    2x
    2x

    3x

    5x

    5x

    import { resolveZoneFileToProfile } from './profileZoneFiles'
    import { config } from '../config'
    import { fetchPrivate } from '../fetchUtil'

    /**
     * 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 = fetchPrivate(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')
          }
        })
    }