diff --git a/packages/sdk/src/FlagResolverClient.test.ts b/packages/sdk/src/FlagResolverClient.test.ts index ee8d75f..4d96d35 100644 --- a/packages/sdk/src/FlagResolverClient.test.ts +++ b/packages/sdk/src/FlagResolverClient.test.ts @@ -8,7 +8,7 @@ import { } from './FlagResolverClient'; import { setMaxListeners } from 'node:events'; import { SdkId } from './generated/confidence/flags/resolver/v1/types'; -import { abortableSleep } from './fetch-util'; +import { abortableSleep, FetchBuilder } from './fetch-util'; import { ApplyFlagsRequest, ResolveFlagsRequest } from './generated/confidence/flags/resolver/v1/api'; import { FlagResolution } from './FlagResolution'; import { Telemetry } from './Telemetry'; @@ -274,7 +274,7 @@ describe('Backend environment Evaluation', () => { describe('intercept', () => { const fetchMock = jest.fn, [Request]>(); - + const fetchBuilder = new FetchBuilder(); let underTest: typeof fetch; beforeEach(() => { @@ -291,7 +291,7 @@ describe('intercept', () => { const telemetryMock = jest.mocked(new Telemetry({ disabled: false, logger: { warn: jest.fn() } })); beforeEach(() => { - underTest = withTelemetryData(fetchMock, telemetryMock); + underTest = withTelemetryData(fetchBuilder, telemetryMock).build(fetchMock); telemetryMock.getSnapshot = jest.fn().mockReturnValue({ libraryTraces: [ { diff --git a/packages/sdk/src/FlagResolverClient.ts b/packages/sdk/src/FlagResolverClient.ts index f41c445..f4d5d09 100644 --- a/packages/sdk/src/FlagResolverClient.ts +++ b/packages/sdk/src/FlagResolverClient.ts @@ -120,11 +120,10 @@ export class FetchingFlagResolverClient implements FlagResolverClient { version: sdk.version, id: LibraryTraces_TraceId.TRACE_ID_RESOLVE_LATENCY, }); + const fetchBuilderWithTelemetry = withTelemetryData(new FetchBuilder(), telemetry); // TODO think about both resolve and apply request logic for backends - this.fetchImplementation = - environment === 'backend' - ? withTelemetryData(fetchImplementation, telemetry) - : withRequestLogic(withTelemetryData(fetchImplementation, telemetry)); + const fetchWithTelemetry = fetchBuilderWithTelemetry.build(fetchImplementation); + this.fetchImplementation = environment === 'backend' ? fetchWithTelemetry : withRequestLogic(fetchWithTelemetry); this.clientSecret = clientSecret; this.sdk = sdk; @@ -297,23 +296,18 @@ export class CachingFlagResolverClient implements FlagResolverClient { } } -export function withTelemetryData( - fetchImplementation: (request: Request) => Promise, - telemetry: Telemetry, -): typeof fetch { - return new FetchBuilder() - .modifyRequest(async request => { - const monitoring = telemetry.getSnapshot(); - if (monitoring.libraryTraces.length > 0) { - const headers = new Headers(request.headers); - const base64Message = btoa(String.fromCharCode(...Monitoring.encode(monitoring).finish())); +export function withTelemetryData(fetchBuilder: FetchBuilder, telemetry: Telemetry): FetchBuilder { + return fetchBuilder.modifyRequest(async request => { + const monitoring = telemetry.getSnapshot(); + if (monitoring.libraryTraces.length > 0) { + const headers = new Headers(request.headers); + const base64Message = btoa(String.fromCharCode(...Monitoring.encode(monitoring).finish())); - headers.set('X-CONFIDENCE-TELEMETRY', base64Message); - return new Request(request, { headers }); - } - return request; - }) - .build(fetchImplementation); + headers.set('X-CONFIDENCE-TELEMETRY', base64Message); + return new Request(request, { headers }); + } + return request; + }); } export function withRequestLogic(fetchImplementation: (request: Request) => Promise): typeof fetch {