import type { GetUidOptions, IdentityClientInfo, IdentityConfig, IdentityInfoResponse, IdentityObject, IdentityResponse, IdentityServerInfo, VisitHistory, VisitHistoryResponse } from '../types/types'; import { IdentityClient } from '../client/index'; import { Thumbmark } from '@thumbmarkjs/thumbmarkjs'; import { API_BASE_URL } from '../constants'; import detectIncognito from 'detectincognitojs'; export class QQuikIdentity { private cfg: IdentityConfig; private client: IdentityClient; private thumbmark: Thumbmark; constructor(cfg?: IdentityConfig) { const { apiKey = '', baseUrl = API_BASE_URL } = cfg || {}; this.cfg = { apiKey, baseUrl }; this.client = IdentityClient.getInstance(this.cfg); this.thumbmark = new Thumbmark({ performance: true, exclude: ['permissions'] }); } async getFingerprint(): Promise { // Call your feature util or API endpoint const fp = await this.thumbmark.get().then((fp) => fp.thumbmark); return fp; } async getDeviceInfo(): Promise<{components: Record[], thumbmark: string, info?: Record}> { const deviceInfo = await this.thumbmark.get({api_key: '760b4633da6c18016ef811725c00befb'}).then((fp) => ({components: fp.components, thumbmark: fp.thumbmark, info: fp?.info})); return deviceInfo; } async getIdentity(): Promise { const fp = await this.getFingerprint(); const res = await this.client.post(`/fingerprints/unique`, { browser_fp: fp, canvas_fp: fp }); return res.data.data; } async getUid(options?: GetUidOptions): Promise { const { collectDeviceInfo = false } = options || {}; let collectedDeviceInfo: string | null = null if(collectDeviceInfo){ try { const {components, thumbmark, info} = await this.getDeviceInfo(); const str = JSON.stringify({components, thumbmark, info}); collectedDeviceInfo = btoa(str); } catch (error) { console.error("Identity error: ", error); } } const fp = await this.getFingerprint(); const res = await this.client.post(`/fingerprints/unique`, { browser_fp: fp, canvas_fp: fp, data: collectedDeviceInfo }); return res.data.data.uid; } async isIncognito(): Promise { const isIncognito = await detectIncognito(); return isIncognito.isPrivate; } async getInfo(): Promise { const info = await this.thumbmark.get(); return info; } async getIdentityInfo(): Promise<{client: IdentityClientInfo, server: IdentityServerInfo}> { const info = await this.getInfo(); const res = await this.client.post(`/fingerprints/v2/unique`, { fingerprint: info.thumbmark, thumbmark: info.components }); return { client: info, server: res?.data?.data,}; } async getVisitHistory(): Promise<{total: number; visits: VisitHistory[]}> { const res = await this.client.get(`/fingerprints/v2/visits`); return res.data.data; } }