# All files / src/profiles profileLookup.ts

86.67% Statements 13/15
75% Branches 6/8
100% Functions 3/3
86.67% Lines 13/15

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
    39
    1x
    1x
    1x

    1x

    8x

    8x
    4x
    4x
    4x

    4x

    8x

    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<Record<string, any>> {
      Iif (!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) => {
          Eif (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')
          }
        })
    }