All files FingerprintJsProAgent.ts

77.77% Statements 21/27
78.94% Branches 15/19
100% Functions 3/3
77.77% Lines 21/27

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122                            2x                       30x                       30x 30x                   30x                           4x 4x 4x 3x     1x                                       11x 11x   11x 4x   7x   9x   8x               8x       8x   3x 3x              
import { NativeModules } from 'react-native'
import { UnknownError } from './errors'
import type {
  FingerprintJsProAgentParams,
  NativeVisitorData,
  ProAgent,
  RequestOptions,
  Tags,
  VisitorData,
  VisitorId,
} from './types'
import { unwrapError } from './unwrapError'
import { isDefined, isTruthy } from './utils'
 
const packageVersion = '__VERSION__'
 
/**
 *
 * @group API Client approach
 */
export class FingerprintJsProAgent implements ProAgent {
  /**
   * Initialises FingerprintJS Pro Agent with certain settings
   *
   * @param params
   */
  private requestOptions: RequestOptions = {}
 
  constructor({
    apiKey,
    region,
    endpointUrl,
    fallbackEndpointUrls = [],
    extendedResponseFormat = false,
    requestOptions = {},
    allowUseOfLocationData = false,
    locationTimeoutMillisAndroid = 5000,
  }: FingerprintJsProAgentParams) {
    try {
      NativeModules.RNFingerprintjsPro.configure(
        apiKey,
        region,
        endpointUrl,
        fallbackEndpointUrls,
        extendedResponseFormat,
        packageVersion,
        allowUseOfLocationData,
        locationTimeoutMillisAndroid
      )
      this.requestOptions = requestOptions
    } catch (e) {
      console.error('RNFingerprintjsPro configure 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}
   * @param options is used to configure requests with different settings
   */
  public async getVisitorId(tags?: Tags, linkedId?: string, options?: RequestOptions): Promise<VisitorId> {
    try {
      const timeout = options?.timeout ?? this.requestOptions.timeout
      if (isDefined(timeout)) {
        return await NativeModules.RNFingerprintjsPro.getVisitorIdWithTimeout(tags, linkedId, timeout)
      }
 
      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}
   * @param options is used to configure requests with different settings
   */
  public async getVisitorData(tags?: Tags, linkedId?: string, options?: RequestOptions): Promise<VisitorData> {
    try {
      const timeout = options?.timeout ?? this.requestOptions.timeout
      let visitorData: NativeVisitorData | null
      if (isDefined(timeout)) {
        visitorData = await NativeModules.RNFingerprintjsPro.getVisitorDataWithTimeout(tags, linkedId, timeout)
      } else {
        visitorData = await NativeModules.RNFingerprintjsPro.getVisitorData(tags, linkedId)
      }
      const [requestId, confidenceScore, visitorDataJsonString, sealedResult] = visitorData
      // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
      const result = {
        ...JSON.parse(visitorDataJsonString),
        requestId,
        confidence: {
          score: confidenceScore,
        },
      } as VisitorData
 
      Iif (isTruthy(sealedResult)) {
        result['sealedResult'] = sealedResult
      }
 
      return result
    } catch (error) {
      if (error instanceof Error) {
        throw unwrapError(error)
      } else E{
        throw new UnknownError(String(error))
      }
    }
  }
}