Skip to content

Commit c8fad2d

Browse files
chore: add custom agent loader support
1 parent 2bffeff commit c8fad2d

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

src/components/fingerprint-provider.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PropsWithChildren, useCallback, useEffect, useMemo, useRef } from 'react'
22
import { FingerprintContext } from '../fingerprint-context'
3-
import { Agent, GetOptions, start, StartOptions } from '@fingerprint/agent'
3+
import { Agent, GetOptions, start, StartOptions, default as Loader } from '@fingerprint/agent'
44
import * as packageInfo from '../../package.json'
55
import { isSSR } from '../ssr'
66
import { WithEnvironment } from './with-environment'
@@ -49,6 +49,18 @@ interface ProviderWithEnvProps extends FingerprintProviderOptions {
4949
getOptions?: GetOptions
5050
}
5151

52+
function isLoader(value: unknown): value is Pick<typeof Loader, 'start'> {
53+
return typeof value === 'object' && value !== null && 'start' in value && typeof value.start === 'function'
54+
}
55+
56+
function getCustomLoader(props: Record<string, unknown>) {
57+
if ('customAgent' in props && isLoader(props.customAgent)) {
58+
return props.customAgent
59+
}
60+
61+
return undefined
62+
}
63+
5264
function ProviderWithEnv({
5365
children,
5466
forceRebuild,
@@ -57,6 +69,8 @@ function ProviderWithEnv({
5769
...agentOptions
5870
}: PropsWithChildren<ProviderWithEnvProps>) {
5971
const createClient = useCallback(() => {
72+
const customLoader = getCustomLoader(agentOptions)
73+
6074
let integrationInfo = `react-sdk/${packageInfo.version}`
6175

6276
if (env) {
@@ -67,10 +81,12 @@ function ProviderWithEnv({
6781

6882
const mergedIntegrationInfo = [...(agentOptions.integrationInfo || []), integrationInfo]
6983

70-
return start({
84+
const startParams = {
7185
...agentOptions,
7286
integrationInfo: mergedIntegrationInfo,
73-
})
87+
}
88+
89+
return customLoader ? customLoader.start(startParams) : start(startParams)
7490
}, [agentOptions, env])
7591

7692
const clientRef = useRef<Agent>()

0 commit comments

Comments
 (0)