All files / src FingerprintClient.web.ts

0% Statements 0/8
0% Branches 0/8
0% Functions 0/3
0% Lines 0/8

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                                                                                                                                   
import * as Fingerprint from '@fingerprint/agent'
import type { FingerprintClient, FingerprintResponse, GetOptions, StartOptions } from './types'
import { unwrapError } from './unwrapError'
import { isEmptyArray, isTruthy } from './utils'
 
const packageVersion = '__VERSION__'
 
/**
 * Web implementation backed by `@fingerprint/agent`.
 *
 * @group API Client approach
 * @platform web
 */
class WebFingerprintClient implements FingerprintClient {
  private readonly agent: Fingerprint.Agent
 
  constructor({ apiKey, region, endpoints: endpointsParam, web }: StartOptions) {
    const endpoints =
      isTruthy(endpointsParam) && !isEmptyArray(endpointsParam)
        ? Fingerprint.withoutDefault(endpointsParam)
        : endpointsParam
 
    this.agent = Fingerprint.start({
      apiKey,
      region,
      endpoints,
      storageKeyPrefix: web?.storageKeyPrefix,
      urlHashing: web?.urlHashing,
      cache: web?.cache,
      integrationInfo: [`fingerprint-pro-react-native/${packageVersion}/web`],
    })
  }
 
  public async get(options?: GetOptions): Promise<FingerprintResponse> {
    try {
      const result = await this.agent.get({
        tags: options?.tags,
        linkedId: options?.linkedId,
        timeout: options?.timeout,
      })
 
      return {
        // `visitor_id` is only omitted by the backend in zeroTrust mode; default to '' to keep the
        // cross-platform shape stable.
        visitor_id: result.visitor_id ?? undefined,
        event_id: result.event_id,
        suspect_score: result.suspect_score,
        sealed_result: result.sealed_result ? result.sealed_result.base64() : null,
        cache_hit: result.cache_hit,
      }
    } catch (error) {
      throw unwrapError(error)
    }
  }
}
 
/**
 * Creates a Fingerprint client with the given options.
 *
 * @group API Client approach
 * @platform web
 */
export function start(options: StartOptions): FingerprintClient {
  return new WebFingerprintClient(options)
}