From 5e5b16022122b71760634ac90e3fd962aa831c74 Mon Sep 17 00:00:00 2001 From: Michael Beemer Date: Wed, 11 Dec 2024 15:09:12 -0500 Subject: [PATCH] refactor: improve track interface for providers (#1100) ## This PR - updates the context and trackingEventDetails on the tracking interface to not be optional since they're always supplied by the SDK. ### Notes This is a small QoL improvement for provider devs implementing the tracking interface. ### How to test The SDK still compiles, and the tests don't need to be modified. --------- Signed-off-by: Michael Beemer Signed-off-by: Todd Baert Co-authored-by: Todd Baert --- .../src/client/internal/open-feature-client.ts | 2 +- packages/server/test/client.spec.ts | 12 ++++++++++++ packages/shared/src/provider/provider.ts | 2 +- .../web/src/client/internal/open-feature-client.ts | 2 +- packages/web/test/client.spec.ts | 12 ++++++++++++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/server/src/client/internal/open-feature-client.ts b/packages/server/src/client/internal/open-feature-client.ts index 663e1bb3f..bd9a4e5a4 100644 --- a/packages/server/src/client/internal/open-feature-client.ts +++ b/packages/server/src/client/internal/open-feature-client.ts @@ -226,7 +226,7 @@ export class OpenFeatureClient implements Client { return this.evaluate(flagKey, this._provider.resolveObjectEvaluation, defaultValue, 'object', context, options); } - track(occurrenceKey: string, context: EvaluationContext, occurrenceDetails: TrackingEventDetails): void { + track(occurrenceKey: string, context: EvaluationContext = {}, occurrenceDetails: TrackingEventDetails = {}): void { try { this.shortCircuitIfNotReady(); diff --git a/packages/server/test/client.spec.ts b/packages/server/test/client.spec.ts index 69dc41fa5..b1db27412 100644 --- a/packages/server/test/client.spec.ts +++ b/packages/server/test/client.spec.ts @@ -859,6 +859,18 @@ describe('OpenFeatureClient', () => { }).not.toThrow(); }); + it('provide empty tracking details to provider if not supplied in call', async () => { + await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER }); + const client = OpenFeature.getClient(); + client.track(eventName); + + expect(MOCK_PROVIDER.track).toHaveBeenCalledWith( + eventName, + expect.any(Object), + expect.any(Object), + ); + }); + it('should call provider with correct context', async () => { await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER }); OpenFeature.setContext({ [globalContextKey]: globalContextValue }); diff --git a/packages/shared/src/provider/provider.ts b/packages/shared/src/provider/provider.ts index e964f1b8e..74ae8606f 100644 --- a/packages/shared/src/provider/provider.ts +++ b/packages/shared/src/provider/provider.ts @@ -133,5 +133,5 @@ export interface CommonProvider(flagKey, this._provider.resolveObjectEvaluation, defaultValue, 'object', options); } - track(occurrenceKey: string, occurrenceDetails: TrackingEventDetails): void { + track(occurrenceKey: string, occurrenceDetails: TrackingEventDetails = {}): void { try { this.shortCircuitIfNotReady(); diff --git a/packages/web/test/client.spec.ts b/packages/web/test/client.spec.ts index 381dd9b25..c67b593ab 100644 --- a/packages/web/test/client.spec.ts +++ b/packages/web/test/client.spec.ts @@ -655,6 +655,18 @@ describe('OpenFeatureClient', () => { }).not.toThrow(); }); + it('provide empty tracking details to provider if not supplied in call', async () => { + await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER }); + const client = OpenFeature.getClient(); + client.track(eventName); + + expect(MOCK_PROVIDER.track).toHaveBeenCalledWith( + eventName, + expect.any(Object), + expect.any(Object), + ); + }); + it('should no-op and not throw if provider throws', async () => { await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER,