From 155e81bf30577641dad33b9ecc0015b6e929a3c3 Mon Sep 17 00:00:00 2001 From: Shane Brunson Date: Tue, 23 Jan 2024 16:04:14 -0600 Subject: [PATCH] create shopper session class; add identify --- package.json | 1 + src/shopper-insights/fingerprint.js | 52 +++++++++++++++++++++ src/shopper-insights/interface.js | 2 + src/shopper-insights/shopperSession.js | 15 ++++++ src/shopper-insights/shopperSession.test.js | 6 +++ 5 files changed, 76 insertions(+) create mode 100644 src/shopper-insights/fingerprint.js diff --git a/package.json b/package.json index d14c500ad3..cae9184ef8 100644 --- a/package.json +++ b/package.json @@ -103,6 +103,7 @@ "vitest": "^1.3.1" }, "dependencies": { + "@fingerprintjs/fingerprintjs-pro": "^3.8.6", "@krakenjs/beaver-logger": "^5.7.0", "@krakenjs/belter": "^2.0.0", "@krakenjs/cross-domain-utils": "^3.0.0", diff --git a/src/shopper-insights/fingerprint.js b/src/shopper-insights/fingerprint.js new file mode 100644 index 0000000000..c57207fae7 --- /dev/null +++ b/src/shopper-insights/fingerprint.js @@ -0,0 +1,52 @@ +/* @flow */ +/* eslint-disable eslint-comments/disable-enable-pair */ +/* eslint-disable no-restricted-globals, promise/no-native */ +import { load as loadFingerprintJS } from "@fingerprintjs/fingerprintjs-pro"; + +const __FINGERPRINT_JS_API_KEY__ = "Eh4QKkI51U0rVUUPeQT8"; + +type FingerprintResults = {| + requestId: string, +|}; + +type Fingerprinter = {| + get: () => Promise, +|}; + +export class Fingerprint { + fingerprinter: ?Fingerprinter; + results: ?FingerprintResults; + + async load(): Promise { + if (!this.fingerprinter) { + this.fingerprinter = await loadFingerprintJS({ + apiKey: __FINGERPRINT_JS_API_KEY__, + }); + } + } + + async collect(): Promise { + if (this.results && this.results.requestId) { + return this.results; + } + + if (!this.fingerprinter) { + await this.load(); + } + + if (!this.fingerprinter) { + throw new Error("fingerprint library failed to load"); + } + + this.results = await this.fingerprinter.get(); + + return this.results; + } + + get(): Promise { + return this.collect(); + } +} + +// $FlowIssue flow is bad with classes +export const fingerprint = new Fingerprint(); diff --git a/src/shopper-insights/interface.js b/src/shopper-insights/interface.js index fbeef43d87..93e83f03b4 100644 --- a/src/shopper-insights/interface.js +++ b/src/shopper-insights/interface.js @@ -15,6 +15,7 @@ import { import type { LazyExport } from "../types"; import { callMemoizedRestAPI } from "../lib"; +import { fingerprint } from "./fingerprint"; import { ShopperSession, type ShopperInsightsInterface, @@ -40,6 +41,7 @@ const sessionState = { export const ShopperInsights: LazyExport = { __get__: () => { const shopperSession = new ShopperSession({ + fingerprint, logger: getLogger(), // $FlowIssue ZalgoPromise vs Promise request: callMemoizedRestAPI, diff --git a/src/shopper-insights/shopperSession.js b/src/shopper-insights/shopperSession.js index a08e8ca000..7cc12da2f6 100644 --- a/src/shopper-insights/shopperSession.js +++ b/src/shopper-insights/shopperSession.js @@ -13,6 +13,8 @@ import { } from "../constants/api"; import { ValidationError } from "../lib"; +import { Fingerprint, fingerprint } from "./fingerprint"; + export type MerchantPayloadData = {| email?: string, phone?: {| @@ -246,6 +248,7 @@ export interface ShopperInsightsInterface { } export class ShopperSession { + fingerprint: Fingerprint; logger: LoggerType; request: Request; requestId: string = ""; @@ -253,16 +256,19 @@ export class ShopperSession { sessionState: Storage; constructor({ + fingerprint, logger, request, sdkConfig, sessionState, }: {| + fingerprint: Fingerprint, logger: LoggerType, request: Request, sdkConfig: SdkConfig, sessionState: Storage, |}) { + this.fingerprint = fingerprint; this.logger = logger; this.request = request; this.sdkConfig = parseSdkConfig({ sdkConfig, logger }); @@ -345,4 +351,13 @@ export class ShopperSession { throw error; } } + + async identify(): Promise<{||}> { + const { requestId } = await this.fingerprint.get(); + + this.requestId = requestId; + + // $FlowIssue + return {}; + } } diff --git a/src/shopper-insights/shopperSession.test.js b/src/shopper-insights/shopperSession.test.js index 06a563ab1c..94dc342f06 100644 --- a/src/shopper-insights/shopperSession.test.js +++ b/src/shopper-insights/shopperSession.test.js @@ -39,6 +39,11 @@ const defaultSdkConfig = { }; const createShopperSession = ({ + fingerprint = { + load: vi.fn(), + collect: vi.fn(), + get: vi.fn(), + }, sdkConfig = defaultSdkConfig, logger = { info: vi.fn(), @@ -51,6 +56,7 @@ const createShopperSession = ({ request = mockFindEligiblePaymentsRequest(), } = {}) => new ShopperSession({ + fingerprint, sdkConfig, // $FlowIssue logger,