Skip to content

Commit 5e5b160

Browse files
beeme1mrtoddbaert
andauthored
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 <[email protected]> Signed-off-by: Todd Baert <[email protected]> Co-authored-by: Todd Baert <[email protected]>
1 parent ba8d1ae commit 5e5b160

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

packages/server/src/client/internal/open-feature-client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export class OpenFeatureClient implements Client {
226226
return this.evaluate<T>(flagKey, this._provider.resolveObjectEvaluation, defaultValue, 'object', context, options);
227227
}
228228

229-
track(occurrenceKey: string, context: EvaluationContext, occurrenceDetails: TrackingEventDetails): void {
229+
track(occurrenceKey: string, context: EvaluationContext = {}, occurrenceDetails: TrackingEventDetails = {}): void {
230230
try {
231231
this.shortCircuitIfNotReady();
232232

packages/server/test/client.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,18 @@ describe('OpenFeatureClient', () => {
859859
}).not.toThrow();
860860
});
861861

862+
it('provide empty tracking details to provider if not supplied in call', async () => {
863+
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER });
864+
const client = OpenFeature.getClient();
865+
client.track(eventName);
866+
867+
expect(MOCK_PROVIDER.track).toHaveBeenCalledWith(
868+
eventName,
869+
expect.any(Object),
870+
expect.any(Object),
871+
);
872+
});
873+
862874
it('should call provider with correct context', async () => {
863875
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER });
864876
OpenFeature.setContext({ [globalContextKey]: globalContextValue });

packages/shared/src/provider/provider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,5 @@ export interface CommonProvider<S extends ClientProviderStatus | ServerProviderS
133133
* @param context
134134
* @param trackingEventDetails
135135
*/
136-
track?(trackingEventName: string, context?: EvaluationContext, trackingEventDetails?: TrackingEventDetails): void;
136+
track?(trackingEventName: string, context: EvaluationContext, trackingEventDetails: TrackingEventDetails): void;
137137
}

packages/web/src/client/internal/open-feature-client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export class OpenFeatureClient implements Client {
183183
return this.evaluate<T>(flagKey, this._provider.resolveObjectEvaluation, defaultValue, 'object', options);
184184
}
185185

186-
track(occurrenceKey: string, occurrenceDetails: TrackingEventDetails): void {
186+
track(occurrenceKey: string, occurrenceDetails: TrackingEventDetails = {}): void {
187187
try {
188188
this.shortCircuitIfNotReady();
189189

packages/web/test/client.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,18 @@ describe('OpenFeatureClient', () => {
655655
}).not.toThrow();
656656
});
657657

658+
it('provide empty tracking details to provider if not supplied in call', async () => {
659+
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER });
660+
const client = OpenFeature.getClient();
661+
client.track(eventName);
662+
663+
expect(MOCK_PROVIDER.track).toHaveBeenCalledWith(
664+
eventName,
665+
expect.any(Object),
666+
expect.any(Object),
667+
);
668+
});
669+
658670
it('should no-op and not throw if provider throws', async () => {
659671
await OpenFeature.setProviderAndWait({
660672
...MOCK_PROVIDER,

0 commit comments

Comments
 (0)