UNPKG

@fingerprintjs/fingerprintjs-pro-spa

Version:

FingerprintJS Pro JavaScript agent for Single-Page Applications (SPA)

236 lines (224 loc) 8.44 kB
/** * FingerprintJS Pro SPA v1.3.3 - Copyright (c) FingerprintJS, Inc, 2025 (https://fingerprint.com) * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license. */ import * as FingerprintJS from '@fingerprintjs/fingerprintjs-pro'; import { GetOptions as GetOptions$1, LoadOptions as LoadOptions$1 } from '@fingerprintjs/fingerprintjs-pro'; export { FingerprintJS as FingerprintJSPro }; type VisitorData<TExtended extends boolean = false> = TExtended extends false ? FingerprintJS.GetResult : FingerprintJS.ExtendedGetResult; type FpjsSpaResponse<T> = T & { cacheHit: boolean; }; declare enum CacheLocation { Memory = "memory", LocalStorage = "localstorage", SessionStorage = "sessionstorage", NoCache = "nocache" } interface FpjsClientOptions { /** * Options for the FingerprintJS JS Agent load() method. */ loadOptions: FingerprintJS.LoadOptions; /** * Defines which built-in cache mechanism the client should use. */ cacheLocation?: CacheLocation; /** * Custom cache implementation. Takes precedence over the `cacheLocation` property. */ cache?: ICache; /** * Duration in seconds for which data is stored in cache. Cannot exceed 86_400 (24h) because caching data * for longer than 24 hours can negatively affect identification accuracy. */ cacheTimeInSeconds?: number; /** * Custom prefix for localStorage and sessionStorage cache keys. Will be ignored if `cache` is provided. */ cachePrefix?: string; } type CacheEntry<TExtended extends boolean = false> = VisitorData<TExtended>; type Cacheable<TExtended extends boolean = false> = WrappedCacheEntry<TExtended>; type WrappedCacheEntry<TExtended extends boolean = false> = { body: CacheEntry<TExtended>; expiresAt: number; }; declare class CacheKey<TExtended extends boolean> { tag: {} | null; linkedId: string | null; extendedResult: boolean | TExtended; constructor(options: GetOptions$1<TExtended>); /** * Converts this `CacheKey` instance into a string for use in a cache * @returns A string representation of the key */ toKey(): string; } type MaybePromise<T> = Promise<T> | T; interface ICache { set<T = WrappedCacheEntry>(key: string, entry: T): MaybePromise<void>; get<T = WrappedCacheEntry>(key: string): MaybePromise<T | undefined>; remove(key: string): MaybePromise<void>; allKeys(): MaybePromise<string[]>; } /** * Implementation of caching that uses local storage * */ declare class LocalStorageCache implements ICache { prefix: string; constructor(prefix?: string); set<T = Cacheable>(key: string, entry: T): void; get<T = Cacheable>(key: string): T | undefined; remove(key: string): void; allKeys(): string[]; } /** * Implementation of caching that uses session storage * */ declare class SessionStorageCache implements ICache { prefix: string; constructor(prefix?: string); /** * It takes a key and an entry, and sets the entry in the session storage with the key * @param {string} key - The key to store the entry under. * @param {Cacheable} entry - The value to be stored in the cache. */ set<T = Cacheable>(key: string, entry: T): void; /** * It gets the value of the key from the session storage, parses it as JSON, and returns it * @param {string} key - The key to store the data under. * @returns The value of the key in the sessionStorage. */ get<T = Cacheable>(key: string): T | undefined; /** * It removes the item from session storage with the given key * @param {string} key - The key to store the value under. */ remove(key: string): void; /** * It returns an array of all the keys in the session storage that start with the prefix * @returns An array of all the keys in the sessionStorage that start with the prefix. */ allKeys(): string[]; } /** * Implementation of caching that uses in-memory storage * */ declare class InMemoryCache { enclosedCache: ICache; } /** * Implementation of stub cache that is used when cache is disabled by user * */ declare class CacheStub implements ICache { set(): void; get(): undefined; remove(): void; allKeys(): never[]; } interface CustomAgent { load: (options: FingerprintJS.LoadOptions) => Promise<FingerprintJS.Agent>; } interface FpjsSpaOptions extends Omit<FpjsClientOptions, 'loadOptions'> { customAgent?: CustomAgent; loadOptions?: FpjsClientOptions['loadOptions']; } /** * FingerprintJS SDK for Single Page Applications */ declare class FpjsClient { private cacheManager; private readonly loadOptions; private agent; private agentPromise; private readonly customAgent; readonly cacheLocation?: CacheLocation; private inFlightRequests; constructor(options?: FpjsSpaOptions); /** * Loads FPJS JS agent with certain settings and stores the instance in memory * [https://dev.fingerprint.com/docs/js-agent#agent-initialization] * * @param passedLoadOptions Additional load options to be passed to the agent, they will be merged with load options provided in the constructor. */ init(passedLoadOptions?: Partial<LoadOptions$1>): Promise<FingerprintJS.Agent>; /** * Returns visitor identification data based on the request options * [https://dev.fingerprint.com/docs/js-agent#visitor-identification] * * @param options * @param ignoreCache if set to true a request to the API will be made even if the data is present in cache */ getVisitorData<TExtended extends boolean>(options?: GetOptions$1<TExtended>, ignoreCache?: boolean): Promise<FpjsSpaResponse<VisitorData<TExtended>>>; /** * Returns cached visitor data based on the request options, or undefined if the data is not present in cache * */ getVisitorDataFromCache<TExtended extends boolean>(options?: GetOptions$1<TExtended>): Promise<FpjsSpaResponse<VisitorData<TExtended>> | undefined>; /** * Checks if request matching given options is present in cache * */ isInCache(options?: GetOptions$1<boolean>): Promise<boolean>; /** * Clears visitor data from cache regardless of the cache implementation */ clearCache(): Promise<void>; /** * Makes a CacheKey object from GetOptions */ static makeCacheKey<TExtended extends boolean>(options: GetOptions$1<TExtended>): CacheKey<TExtended>; private _identify; } /** * @deprecated * * Use `FingerprintJSPro.defaultEndpoint` instead, this export will be removed in the next major version * */ declare const defaultEndpoint: { default: "endpoint"; }; /** * @deprecated * * Use `FingerprintJSPro.defaultTlsEndpoint` instead, this export will be removed in the next major version */ declare const defaultTlsEndpoint: { default: "tlsEndpoint"; }; /** * @deprecated * * Use `FingerprintJSPro.defaultScriptUrlPattern` instead, this export will be removed in the next major version */ declare const defaultScriptUrlPattern: string; /** * @deprecated * * Use `FingerprintJSPro.Agent` instead, this export will be removed in the next major version * */ type Agent = FingerprintJS.Agent; /** * @deprecated * * Use `FingerprintJSPro.GetOptions` instead, this export will be removed in the next major version */ type GetOptions<TExtended extends boolean, TIP = unknown> = FingerprintJS.GetOptions<TExtended, TIP>; /** * @deprecated * * Use `FingerprintJSPro.GetResult` instead, this export will be removed in the next major version */ type GetResult = FingerprintJS.GetResult; /** * @deprecated * * Use `FingerprintJSPro.LoadOptions` instead, this export will be removed in the next major version */ type LoadOptions = FingerprintJS.LoadOptions; /** * @deprecated * * Use `FingerprintJSPro.ExtendedGetResult` instead, this export will be removed in the next major version */ type ExtendedGetResult = FingerprintJS.ExtendedGetResult; export { type Agent, CacheLocation, CacheStub, type Cacheable, type CustomAgent, type ExtendedGetResult, FpjsClient, type FpjsClientOptions, type FpjsSpaOptions, type FpjsSpaResponse, type GetOptions, type GetResult, type ICache, InMemoryCache, type LoadOptions, LocalStorageCache, SessionStorageCache, type VisitorData, defaultEndpoint, defaultScriptUrlPattern, defaultTlsEndpoint };