Skip to content

Commit

Permalink
create shopper session class; add identify
Browse files Browse the repository at this point in the history
  • Loading branch information
wsbrunson committed Apr 16, 2024
1 parent 4b3d77a commit 155e81b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
52 changes: 52 additions & 0 deletions src/shopper-insights/fingerprint.js
Original file line number Diff line number Diff line change
@@ -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<FingerprintResults>,
|};

export class Fingerprint {
fingerprinter: ?Fingerprinter;
results: ?FingerprintResults;

async load(): Promise<void> {
if (!this.fingerprinter) {
this.fingerprinter = await loadFingerprintJS({
apiKey: __FINGERPRINT_JS_API_KEY__,
});
}
}

async collect(): Promise<FingerprintResults> {
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<FingerprintResults> {
return this.collect();
}
}

// $FlowIssue flow is bad with classes
export const fingerprint = new Fingerprint();
2 changes: 2 additions & 0 deletions src/shopper-insights/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import type { LazyExport } from "../types";
import { callMemoizedRestAPI } from "../lib";

import { fingerprint } from "./fingerprint";
import {
ShopperSession,
type ShopperInsightsInterface,
Expand All @@ -40,6 +41,7 @@ const sessionState = {
export const ShopperInsights: LazyExport<ShopperInsightsInterface> = {
__get__: () => {
const shopperSession = new ShopperSession({
fingerprint,
logger: getLogger(),
// $FlowIssue ZalgoPromise vs Promise
request: callMemoizedRestAPI,
Expand Down
15 changes: 15 additions & 0 deletions src/shopper-insights/shopperSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
} from "../constants/api";
import { ValidationError } from "../lib";

import { Fingerprint, fingerprint } from "./fingerprint";

Check failure on line 16 in src/shopper-insights/shopperSession.js

View workflow job for this annotation

GitHub Actions / main

'fingerprint' is defined but never used

export type MerchantPayloadData = {|
email?: string,
phone?: {|
Expand Down Expand Up @@ -246,23 +248,27 @@ export interface ShopperInsightsInterface {
}

export class ShopperSession {
fingerprint: Fingerprint;
logger: LoggerType;
request: Request;
requestId: string = "";
sdkConfig: SdkConfig;
sessionState: Storage;

constructor({
fingerprint,

Check failure on line 259 in src/shopper-insights/shopperSession.js

View workflow job for this annotation

GitHub Actions / main

'fingerprint' is already declared in the upper scope on line 16 column 23
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 });
Expand Down Expand Up @@ -345,4 +351,13 @@ export class ShopperSession {
throw error;
}
}

async identify(): Promise<{||}> {
const { requestId } = await this.fingerprint.get();

this.requestId = requestId;

// $FlowIssue
return {};
}
}
6 changes: 6 additions & 0 deletions src/shopper-insights/shopperSession.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const defaultSdkConfig = {
};

const createShopperSession = ({
fingerprint = {
load: vi.fn(),
collect: vi.fn(),
get: vi.fn(),
},
sdkConfig = defaultSdkConfig,
logger = {
info: vi.fn(),
Expand All @@ -51,6 +56,7 @@ const createShopperSession = ({
request = mockFindEligiblePaymentsRequest(),
} = {}) =>
new ShopperSession({
fingerprint,
sdkConfig,
// $FlowIssue
logger,
Expand Down

0 comments on commit 155e81b

Please sign in to comment.