-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.ts
More file actions
57 lines (48 loc) · 1.83 KB
/
index.ts
File metadata and controls
57 lines (48 loc) · 1.83 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { FallbackTreatmentConfiguration, Treatment, TreatmentWithConfig } from '../../../types/splitio';
import { FallbacksSanitizer } from './fallbackSanitizer';
import { CONTROL } from '../../utils/constants';
import { isString } from '../../utils/lang';
import { ILogger } from '../../logger/types';
export type IFallbackTreatmentsCalculator = {
resolve(flagName: string, label: string): TreatmentWithConfig & { label: string };
}
export const FALLBACK_PREFIX = 'fallback - ';
export class FallbackTreatmentsCalculator implements IFallbackTreatmentsCalculator {
private readonly fallbacks: FallbackTreatmentConfiguration;
constructor(logger: ILogger, fallbacks?: FallbackTreatmentConfiguration) {
const sanitizedGlobal = fallbacks?.global ? FallbacksSanitizer.sanitizeGlobal(logger, fallbacks.global) : undefined;
const sanitizedByFlag = fallbacks?.byFlag ? FallbacksSanitizer.sanitizeByFlag(logger, fallbacks.byFlag) : {};
this.fallbacks = {
global: sanitizedGlobal,
byFlag: sanitizedByFlag
};
}
resolve(flagName: string, label: string): TreatmentWithConfig & { label: string } {
const treatment = this.fallbacks.byFlag?.[flagName];
if (treatment) {
return this.copyWithLabel(treatment, label);
}
if (this.fallbacks.global) {
return this.copyWithLabel(this.fallbacks.global, label);
}
return {
treatment: CONTROL,
config: null,
label,
};
}
private copyWithLabel(fallback: Treatment | TreatmentWithConfig, label: string): TreatmentWithConfig & { label: string } {
if (isString(fallback)) {
return {
treatment: fallback,
config: null,
label: `${FALLBACK_PREFIX}${label}`,
};
}
return {
treatment: fallback.treatment,
config: fallback.config,
label: `${FALLBACK_PREFIX}${label}`,
};
}
}