All files FingerprintJsProAgent.ts

50% Statements 7/14
50% Branches 3/6
66.66% Functions 2/3
50% Lines 7/14

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                                8x 8x                                                                               4x 4x       2x               2x 2x              
import { NativeModules } from 'react-native'
import { UnknownError, unwrapError } from './errors'
import type { FingerprintJsProAgentParams, Tags, VisitorData } from './types'
import * as packageInfo from '../package.json'
 
type VisitorId = string
 
/**
 *
 * @group API Client approach
 */
export class FingerprintJsProAgent {
  /**
   * Initialises FingerprintJS Pro Agent with certain settings
   *
   * @param params
   */
  constructor({
    apiKey,
    region,
    endpointUrl,
    fallbackEndpointUrls = [],
    extendedResponseFormat = false,
  }: FingerprintJsProAgentParams) {
    try {
      NativeModules.RNFingerprintjsPro.init(
        apiKey,
        region,
        endpointUrl,
        fallbackEndpointUrls,
        extendedResponseFormat,
        packageInfo.version
      )
    } catch (e) {
      console.error('RNFingerprintjsPro init error: ', e)
    }
  }
 
  /**
   * Returns visitor identifier based on the request options {@link https://dev.fingerprint.com/docs/native-android-integration#get-the-visitor-identifier | more info in the documentation page}
   *
   * @param tags is a customer-provided value or an object that will be saved together with the analysis event and will be returned back to you in a webhook message or when you search for the visit in the server API. {@link https://dev.fingerprint.com/docs/js-agent#tag | more info in the documentation page}
   * @param linkedId  is a way of linking current analysis event with a custom identifier. This will allow you to filter visit information when using the Server API {@link https://dev.fingerprint.com/docs/js-agent#linkedid | more info in the documentation page}
   */
  public async getVisitorId(tags?: Tags, linkedId?: string): Promise<VisitorId> {
    try {
      return await NativeModules.RNFingerprintjsPro.getVisitorId(tags, linkedId)
    } catch (error) {
      if (error instanceof Error) {
        throw unwrapError(error)
      } else {
        throw new UnknownError(String(error))
      }
    }
  }
 
  /**
   * Returns visitor identification data based on the request options {@link https://dev.fingerprint.com/docs/native-android-integration#get-the-visitor-identifier | more info in the documentation page}
   *
   * Provide `extendedResponseFormat` option in the {@link constructor} to get response in the {@link https://dev.fingerprint.com/docs/native-android-integration#response-format | extended format}
   *
   * @param tags is a customer-provided value or an object that will be saved together with the analysis event and will be returned back to you in a webhook message or when you search for the visit in the server API. {@link https://dev.fingerprint.com/docs/js-agent#tag | more info in the documentation page}
   * @param linkedId  is a way of linking current analysis event with a custom identifier. This will allow you to filter visit information when using the Server API {@link https://dev.fingerprint.com/docs/js-agent#linkedid | more info in the documentation page}
   */
  public async getVisitorData(tags?: Tags, linkedId?: string): Promise<VisitorData> {
    try {
      const [requestId, confidenceScore, visitorDataJsonString] = await NativeModules.RNFingerprintjsPro.getVisitorData(
        tags,
        linkedId
      )
      return {
        ...JSON.parse(visitorDataJsonString),
        requestId,
        confidence: {
          score: confidenceScore,
        },
      }
    } catch (error) {
      if (error instanceof Error) {
        throw unwrapError(error)
      } else E{
        throw new UnknownError(String(error))
      }
    }
  }
}