-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathFirstMatchStrategy.ts
39 lines (37 loc) · 1.27 KB
/
FirstMatchStrategy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {
BaseEvaluationStrategy,
FinalResult,
ProviderResolutionResult,
StrategyPerProviderContext,
} from './BaseEvaluationStrategy';
import { ErrorCode, EvaluationContext, FlagValue, OpenFeatureError, ResolutionDetails } from '@openfeature/server-sdk';
/**
* Return the first result that did not indicate "flag not found".
* If any provider in the course of evaluation returns or throws an error, throw that error
*/
export class FirstMatchStrategy extends BaseEvaluationStrategy {
override shouldEvaluateNextProvider<T extends FlagValue>(
strategyContext: StrategyPerProviderContext,
context: EvaluationContext,
result: ProviderResolutionResult<T>,
): boolean {
if (this.hasErrorWithCode(result, ErrorCode.FLAG_NOT_FOUND)) {
return true;
}
if (this.hasError(result)) {
return false;
}
return false;
}
override determineFinalResult<T extends FlagValue>(
strategyContext: StrategyPerProviderContext,
context: EvaluationContext,
resolutions: ProviderResolutionResult<T>[],
): FinalResult<T> {
const finalResolution = resolutions[resolutions.length - 1];
if (this.hasError(finalResolution)) {
return this.collectProviderErrors(resolutions);
}
return this.resolutionToFinalResult(finalResolution);
}
}