All files / src serverApiClient.ts

97.91% Statements 47/48
94.44% Branches 17/18
100% Functions 6/6
97.91% Lines 47/48

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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 3228x 8x                     8x 8x 8x   8x                             15x 1x           14x   14x 14x   14x                                                                                             21x 1x     20x                                                                                             10x 1x     9x 1x     8x                                                                     9x 1x     8x                                                                                                                                   6x                   42x           42x 42x                 1x     41x 41x   41x 13x   13x 2x     11x         11x 11x   3x   8x       28x 28x   6x   22x 14x 2x   12x   8x      
import { AllowedMethod, getRequestPath, GetRequestPathOptions, SuccessJsonOrVoid } from './urlUtils'
import {
  Event,
  EventUpdate,
  FingerprintApi,
  GetEventOptions,
  Options,
  Region,
  SearchEventsFilter,
  SearchEventsResponse,
} from './types'
import { paths } from './generatedApiTypes'
import { RequestError, SdkError, TooManyRequestsError } from './errors/apiErrors'
import { isErrorResponse } from './errors/handleErrorResponse'
import { toError } from './errors/toError'
 
export class FingerprintServerApiClient implements FingerprintApi {
  public readonly region: Region
 
  public readonly apiKey: string
 
  protected readonly fetch: typeof fetch
 
  private readonly defaultHeaders: Record<string, string>
 
  /**
   * FingerprintJS server API client used to fetch data from FingerprintJS
   * @constructor
   * @param {Options} options - Options for FingerprintJS server API client
   */
  constructor(options: Readonly<Options>) {
    if (!options.apiKey) {
      throw Error('Api key is not set')
    }
 
    // These type assertions are safe because the Options type allows the
    // region or authentication mode to be specified as a string or an enum value.
    // The resulting JS from using the enum value or the string is identical.
    this.region = (options.region as Region) ?? Region.Global
 
    this.apiKey = options.apiKey
    this.fetch = options.fetch ?? fetch
 
    this.defaultHeaders = {
      Authorization: `Bearer ${this.apiKey}`,
      ...options.defaultHeaders,
    }
  }
 
  /**
   * Retrieves a specific identification event with the information from each activated product — Identification and all active [Smart signals](https://dev.fingerprint.com/docs/smart-signals-overview).
   *
   * @param eventId - identifier of the event
   * @param {object|undefined} options - Optional `getEvent` operation options
   * @param {string|undefined} options.ruleset_id - Optional ruleset ID to evaluate against the event
   *
   * @returns {Promise<Event>} - promise with event response. For more information, see the [Server API documentation](https://dev.fingerprint.com/reference/getevent).
   *
   * @example
   * ```javascript Handling an event
   * client
   *  .getEvent('<eventId>')
   *  .then((event) => console.log(event))
   *  .catch((error) => {
   *    if (error instanceof RequestError) {
   *       console.log(error.statusCode, error.message)
   *       // Access raw response in error
   *       console.log(error.response)
   *     }
   *   })
   * ```
   *
   * @example Handling an event with rule_action
   * ```javascript
   * client
   *  .getEvent('<eventId>', { ruleset_id: '<rulesetId>' })
   *  .then((event) => {
   *    const ruleAction = event.rule_action
   *    if (ruleAction?.type === 'block') {
   *      console.log('Blocked by rule:', ruleAction.rule_id, ruleAction.status_code)
   *    }
   *  })
   *  .catch((error) => {
   *    if (error instanceof RequestError) {
   *      console.log(error.statusCode, error.message)
   *    }
   *  })
   * ```
   * */
  public async getEvent(eventId: string, options?: GetEventOptions): Promise<Event> {
    if (!eventId) {
      throw new TypeError('eventId is not set')
    }
 
    return this.callApi({
      path: '/events/{event_id}',
      pathParams: [eventId],
      method: 'get',
      queryParams: options,
    })
  }
 
  /**
   * Update an event with a given event ID
   * @description Change information in existing events specified by `eventId` or *flag suspicious events*.
   *
   * When an event is created, it is assigned `linkedId` and `tag` submitted through the JS agent parameters. This information might not be available on the client so the Server API allows for updating the attributes after the fact.
   *
   * **Warning** It's not possible to update events older than one month.
   *
   * @param body - Data to update the event with.
   * @param eventId The unique event [identifier](https://docs.fingerprint.com/reference/js-agent-v4-get-function#event_id).
   *
   * @return {Promise<void>}
   *
   * @example
   * ```javascript
   * const body = {
   *  linked_id: 'linked_id',
   *  suspect: false,
   * }
   *
   * client
   *   .updateEvent(body, '<eventId>')
   *   .then(() => {
   *     // Event was successfully updated
   *   })
   *  .catch((error) => {
   *    if (error instanceof RequestError) {
   *       console.log(error.statusCode, error.message)
   *       // Access raw response in error
   *       console.log(error.response)
   *
   *       if(error.statusCode === 409) {
   *          // Event is not mutable yet, wait a couple of seconds and retry the update.
   *       }
   *     }
   *   })
   * ```
   */
  public async updateEvent(body: EventUpdate, eventId: string): Promise<void> {
    if (!body) {
      throw new TypeError('body is not set')
    }
 
    if (!eventId) {
      throw new TypeError('eventId is not set')
    }
 
    return this.callApi({
      path: '/events/{event_id}',
      pathParams: [eventId],
      method: 'patch',
      body: JSON.stringify(body),
    })
  }
 
  /**
   * Delete data by visitor ID
   * Request deleting all data associated with the specified visitor ID. This API is useful for compliance with privacy regulations. All delete requests are queued:
   * Recent data (10 days or newer) belonging to the specified visitor will be deleted within 24 hours. * Data from older (11 days or more) identification events  will be deleted after 90 days.
   * If you are interested in using this API, please [contact our support team](https://fingerprint.com/support/) to activate it for you. Otherwise, you will receive a 403.
   *
   * @param visitorId The [visitor ID](https://dev.fingerprint.com/docs/js-agent#visitorid) you want to delete.*
   *
   * @return {Promise<void>} Promise that resolves when the deletion request is successfully queued
   *
   * @example
   * ```javascript
   * client
   *   .deleteVisitorData('<visitorId>')
   *   .then(() => {
   *     // Data deletion request was successfully queued
   *   })
   *  .catch((error) => {
   *    if (error instanceof RequestError) {
   *       console.log(error.statusCode, error.message)
   *       // Access raw response in error
   *       console.log(error.response)
   *     }
   *   })
   * ```
   */
  public async deleteVisitorData(visitorId: string): Promise<void> {
    if (!visitorId) {
      throw new TypeError('visitorId is not set')
    }
 
    return this.callApi({
      path: '/visitors/{visitor_id}',
      pathParams: [visitorId],
      method: 'delete',
    })
  }
 
  /**
   * Search for identification events, including Smart Signals, using
   * multiple filtering criteria. If you don't provide `start` or `end`
   * parameters, the default search range is the last 7 days.
   *
   * Please note that events include mobile signals (e.g. `rootApps`) even if
   * the request originated from a non-mobile platform. We recommend you
   * **ignore** mobile signals for such requests.
   *
   * @param {SearchEventsFilter} filter - Events filter
   * @param {number} filter.limit - Limit the number of events returned. Must be greater than 0.
   * @param {string|undefined} filter.pagination_key - Use `pagination_key` to get the next page of results.
   * @param {string|undefined} filter.visitor_id - Unique [visitor identifier](https://dev.fingerprint.com/reference/get-function#visitorid) issued by Fingerprint Identification. Filter for events matching this `visitor_id`.
   * @param {string|undefined} filter.bot - Filter events by the bot detection result, specifically:
   *             - events where any kind of bot was detected.
   *             - events where a good bot was detected.
   *             - events where a bad bot was detected.
   *             - events where no bot was detected.
   *
   *             Allowed values: `all`, `good`, `bad`, `none`.
   * @param {string|undefined} filter.ip_address - Filter events by IP address range. The range can be as specific as a
   *             single IP (/32 for IPv4 or /128 for IPv6).
   *             All ip_address filters must use CIDR notation, for example,
   *             10.0.0.0/24, 192.168.0.1/32
   * @param {string|undefined} filter.linked_id - Filter events by your custom identifier.
   *             You can use [linked IDs](https://dev.fingerprint.com/reference/get-function#linkedid) to
   *             associate identification requests with your own identifier, for
   *             example, session ID, purchase ID, or transaction ID. You can then
   *             use this `linked_id` parameter to retrieve all events associated
   *             with your custom identifier.
   * @param {string|undefined} filter.url - Filter events by the URL (`url` property) associated with the event.
   * @param {string|undefined} filter.origin - Filter events by the origin field of the event. Origin could be the website domain or mobile app bundle ID (eg: com.foo.bar)
   * @param {number|undefined} filter.start - Filter events with a timestamp greater than the start time, in Unix time (milliseconds).
   * @param {number|undefined} filter.end - Filter events with a timestamp smaller than the end time, in Unix time (milliseconds).
   * @param {boolean|undefined} filter.reverse - Sort events in reverse timestamp order.
   * @param {boolean|undefined} filter.suspect - Filter events previously tagged as suspicious via the [Update API](https://dev.fingerprint.com/reference/updateevent).
   * @param {boolean|undefined} filter.vpn - Filter events by VPN Detection result.
   * @param {boolean|undefined} filter.virtual_machine - Filter events by Virtual Machine Detection result.
   * @param {boolean|undefined} filter.tampering - Filter events by Browser Tampering Detection result.
   * @param {boolean|undefined} filter.anti_detect_browser - Filter events by Anti-detect Browser Detection result.
   * @param {boolean|undefined} filter.incognito - Filter events by Browser Incognito Detection result.
   * @param {boolean|undefined} filter.privacy_settings - Filter events by Privacy Settings Detection result.
   * @param {boolean|undefined} filter.jailbroken - Filter events by Jailbroken Device Detection result.
   * @param {boolean|undefined} filter.frida - Filter events by Frida Detection result.
   * @param {boolean|undefined} filter.factory_reset - Filter events by Factory Reset Detection result.
   * @param {boolean|undefined} filter.cloned_app - Filter events by Cloned App Detection result.
   * @param {boolean|undefined} filter.emulator - Filter events by Android Emulator Detection result.
   * @param {boolean|undefined} filter.root_apps - Filter events by Rooted Device Detection result.
   * @param {'high'|'medium'|'low'|undefined} filter.vpn_confidence - Filter events by VPN Detection result confidence level.
   * @param {number|undefined} filter.min_suspect_score - Filter events with Suspect Score result above a provided minimum threshold.
   * @param {boolean|undefined} filter.developer_tools - Filter events by Developer Tools detection result.
   * @param {boolean|undefined} filter.location_spoofing - Filter events by Location Spoofing detection result.
   * @param {boolean|undefined} filter.mitm_attack - Filter events by MITM (Man-in-the-Middle) Attack detection result.
   * @param {boolean|undefined} filter.proxy - Filter events by Proxy detection result.
   * @param {string|undefined} filter.sdk_version - Filter events by a specific SDK version associated with the identification event (`sdk.version` property).
   * @param {string|undefined} filter.sdk_platform - Filter events by the SDK Platform associated with the identification event (`sdk.platform` property).
   * @param {string[]|undefined} filter.environment - Filter for events by providing one or more environment IDs (`environment_id` property).
   * */
  async searchEvents(filter: SearchEventsFilter): Promise<SearchEventsResponse> {
    return this.callApi({
      path: '/events',
      method: 'get',
      queryParams: filter,
    })
  }
 
  private async callApi<Path extends keyof paths, Method extends AllowedMethod<Path>>(
    options: GetRequestPathOptions<Path, Method> & { headers?: Record<string, string>; body?: BodyInit }
  ): Promise<SuccessJsonOrVoid<Path, Method>> {
    const url = getRequestPath({
      ...options,
      region: this.region,
    })
 
    let response: Response
    try {
      response = await this.fetch(url, {
        method: options.method.toUpperCase(),
        headers: {
          ...this.defaultHeaders,
          ...options.headers,
        },
        body: options.body,
      })
    } catch (e) {
      throw new SdkError('Network or fetch error', undefined, e as Error)
    }
 
    const contentType = response.headers.get('content-type') ?? ''
    const isJson = contentType.includes('application/json')
 
    if (response.ok) {
      const hasNoBody = response.status === 204 || response.headers.get('content-length') === '0'
 
      if (hasNoBody) {
        return undefined as SuccessJsonOrVoid<Path, Method>
      }
 
      Iif (!isJson) {
        throw new SdkError('Expected JSON response but received non-JSON content type', response)
      }
 
      let data
      try {
        data = await response.clone().json()
      } catch (e) {
        throw new SdkError('Failed to parse JSON response', response, toError(e))
      }
      return data as SuccessJsonOrVoid<Path, Method>
    }
 
    let errPayload
    try {
      errPayload = await response.clone().json()
    } catch (e) {
      throw new SdkError('Failed to parse JSON response', response, toError(e))
    }
    if (isErrorResponse(errPayload)) {
      if (response.status === 429) {
        throw new TooManyRequestsError(errPayload, response)
      }
      throw new RequestError(errPayload.error.message, errPayload, response.status, errPayload.error.code, response)
    }
    throw RequestError.unknown(response)
  }
}