All files FingerprintJsProAgent.web.ts

0% Statements 0/26
0% Branches 0/15
0% Functions 0/3
0% Lines 0/26

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                                                                                                                                                                                                                                 
import { UnknownError } from './errors'
import type { FingerprintJsProAgentParams, ProAgent, RequestOptions, Tags, VisitorData, VisitorId } from './types'
import { FingerprintJSPro, FpjsClient } from '@fingerprintjs/fingerprintjs-pro-spa'
import { unwrapError } from './unwrapError'
 
const packageVersion = '__VERSION__'
 
/**
 *
 * @group API Client approach
 * @platform web
 */
export class FingerprintJsProAgent implements ProAgent {
  private readonly requestOptions: RequestOptions = {}
 
  private readonly extendedResponseFormat: boolean = false
 
  private readonly client: FpjsClient
 
  private readonly agentPromise: Promise<FingerprintJSPro.Agent>
 
  constructor({
    apiKey,
    region,
    endpointUrl,
    fallbackEndpointUrls = [],
    extendedResponseFormat = false,
    requestOptions = {},
    scriptUrlPattern,
    storageKey,
    urlHashing,
    remoteControlDetection,
    cache,
    cachePrefix,
    cacheLocation,
    cacheTimeInSeconds,
  }: FingerprintJsProAgentParams) {
    const endpoints: string[] = []
    if (endpointUrl) {
      endpoints.push(endpointUrl)
    }
    endpoints.push(...fallbackEndpointUrls)
 
    this.client = new FpjsClient({
      loadOptions: {
        apiKey,
        region,
        endpoint: endpoints.length > 0 ? endpoints : undefined,
        scriptUrlPattern,
        storageKey,
        urlHashing,
        remoteControlDetection,
        integrationInfo: [`fingerprint-pro-react-native/${packageVersion}/web`],
      },
      cache,
      cachePrefix,
      cacheLocation,
      cacheTimeInSeconds,
    })
    this.agentPromise = this.client.init()
    this.requestOptions = requestOptions
    this.extendedResponseFormat = extendedResponseFormat
  }
 
  /**
   * @inheritDoc
   * */
  public async getVisitorId(tags?: Tags, linkedId?: string, options?: RequestOptions): Promise<VisitorId> {
    try {
      const timeout = options?.timeout ?? this.requestOptions.timeout
 
      const agent = await this.agentPromise
      const result = await agent.get({
        extendedResult: this.extendedResponseFormat,
        tag: tags,
        linkedId,
        timeout,
      })
 
      return result.visitorId
    } catch (error) {
      if (error instanceof Error) {
        throw unwrapError(error)
      }
 
      throw new UnknownError(String(error))
    }
  }
 
  /**
   * @inheritDoc
   * */
  public async getVisitorData(tags?: Tags, linkedId?: string, options?: RequestOptions): Promise<VisitorData> {
    try {
      const timeout = options?.timeout ?? this.requestOptions.timeout
      const agent = await this.agentPromise
 
      return await agent.get({
        tag: tags,
        linkedId,
        extendedResult: this.extendedResponseFormat,
        timeout,
      })
    } catch (error) {
      if (error instanceof Error) {
        throw unwrapError(error)
      } else {
        throw new UnknownError(String(error))
      }
    }
  }
}