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 <beeme1mr@users.noreply.github.com>
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
Co-authored-by: Todd Baert <todd.baert@dynatrace.com>
This commit is contained in:
Michael Beemer 2024-12-11 15:09:12 -05:00 committed by GitHub
parent ba8d1aeec8
commit 5e5b160221
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 27 additions and 3 deletions

View File

@ -226,7 +226,7 @@ export class OpenFeatureClient implements Client {
return this.evaluate<T>(flagKey, this._provider.resolveObjectEvaluation, defaultValue, 'object', context, options); return this.evaluate<T>(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 { try {
this.shortCircuitIfNotReady(); this.shortCircuitIfNotReady();

View File

@ -859,6 +859,18 @@ describe('OpenFeatureClient', () => {
}).not.toThrow(); }).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 () => { it('should call provider with correct context', async () => {
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER }); await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER });
OpenFeature.setContext({ [globalContextKey]: globalContextValue }); OpenFeature.setContext({ [globalContextKey]: globalContextValue });

View File

@ -133,5 +133,5 @@ export interface CommonProvider<S extends ClientProviderStatus | ServerProviderS
* @param context * @param context
* @param trackingEventDetails * @param trackingEventDetails
*/ */
track?(trackingEventName: string, context?: EvaluationContext, trackingEventDetails?: TrackingEventDetails): void; track?(trackingEventName: string, context: EvaluationContext, trackingEventDetails: TrackingEventDetails): void;
} }

View File

@ -183,7 +183,7 @@ export class OpenFeatureClient implements Client {
return this.evaluate<T>(flagKey, this._provider.resolveObjectEvaluation, defaultValue, 'object', options); return this.evaluate<T>(flagKey, this._provider.resolveObjectEvaluation, defaultValue, 'object', options);
} }
track(occurrenceKey: string, occurrenceDetails: TrackingEventDetails): void { track(occurrenceKey: string, occurrenceDetails: TrackingEventDetails = {}): void {
try { try {
this.shortCircuitIfNotReady(); this.shortCircuitIfNotReady();

View File

@ -655,6 +655,18 @@ describe('OpenFeatureClient', () => {
}).not.toThrow(); }).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 () => { it('should no-op and not throw if provider throws', async () => {
await OpenFeature.setProviderAndWait({ await OpenFeature.setProviderAndWait({
...MOCK_PROVIDER, ...MOCK_PROVIDER,