-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclientInputValidation.ts
More file actions
170 lines (139 loc) · 8.7 KB
/
clientInputValidation.ts
File metadata and controls
170 lines (139 loc) · 8.7 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {
validateAttributes,
validateEvent,
validateEventValue,
validateEventProperties,
validateKey,
validateSplit,
validateSplits,
validateTrafficType,
validateIfNotDestroyed,
validateIfReadyFromCache,
validateEvaluationOptions
} from '../utils/inputValidation';
import { startsWith } from '../utils/lang';
import { GET_TREATMENT, GET_TREATMENTS, GET_TREATMENTS_BY_FLAG_SET, GET_TREATMENTS_BY_FLAG_SETS, GET_TREATMENTS_WITH_CONFIG, GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET, GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS, GET_TREATMENT_WITH_CONFIG, TRACK_FN_LABEL } from '../utils/constants';
import { IReadinessManager } from '../readiness/types';
import { MaybeThenable } from '../dtos/types';
import { ISettings } from '../types';
import SplitIO from '../../types/splitio';
import { isConsumerMode } from '../utils/settingsValidation/mode';
import { validateFlagSets } from '../utils/settingsValidation/splitFilters';
import { IFallbackTreatmentsCalculator } from '../evaluator/fallbackTreatmentsCalculator';
/**
* Decorator that validates the input before actually executing the client methods.
* We should "guard" the client here, while not polluting the "real" implementation of those methods.
*/
export function clientInputValidationDecorator<TClient extends SplitIO.IClient | SplitIO.IAsyncClient>(settings: ISettings, client: TClient, readinessManager: IReadinessManager, fallbackTreatmentsCalculator: IFallbackTreatmentsCalculator): TClient {
const { log, mode } = settings;
const isAsync = isConsumerMode(mode);
/**
* Avoid repeating this validations code
*/
function validateEvaluationParams(methodName: string, maybeKey: SplitIO.SplitKey, maybeNameOrNames: string | string[], maybeAttributes?: SplitIO.Attributes, maybeOptions?: SplitIO.EvaluationOptions) {
const key = validateKey(log, maybeKey, methodName);
const nameOrNames = methodName.indexOf('ByFlagSet') > -1 ?
validateFlagSets(log, methodName, maybeNameOrNames as string[], settings.sync.__splitFiltersValidation.groupedFilters.bySet) :
startsWith(methodName, GET_TREATMENTS) ?
validateSplits(log, maybeNameOrNames, methodName) :
validateSplit(log, maybeNameOrNames, methodName);
const attributes = validateAttributes(log, maybeAttributes, methodName);
const isNotDestroyed = validateIfNotDestroyed(log, readinessManager, methodName);
const options = validateEvaluationOptions(log, maybeOptions, methodName);
validateIfReadyFromCache(log, readinessManager, methodName);
const valid = isNotDestroyed && key && nameOrNames && attributes !== false;
return {
valid,
key,
nameOrNames,
attributes,
options
};
}
function evaluateFallBackTreatment(featureFlagName: string | false | string[], withConfig: boolean) {
if (Array.isArray(featureFlagName)) {
const res: SplitIO.Treatments = {};
featureFlagName.forEach((split: string) => res[split] = evaluateFallBackTreatment(split, withConfig) as SplitIO.Treatment);
return res;
}
const { treatment, config } = fallbackTreatmentsCalculator(featureFlagName as string);
return withConfig ?
{
treatment,
config
} : treatment;
}
function wrapResult<T>(value: T): MaybeThenable<T> {
return isAsync ? Promise.resolve(value) : value;
}
function getTreatment(maybeKey: SplitIO.SplitKey, maybeFeatureFlagName: string, maybeAttributes?: SplitIO.Attributes, maybeOptions?: SplitIO.EvaluationOptions) {
const params = validateEvaluationParams(GET_TREATMENT, maybeKey, maybeFeatureFlagName, maybeAttributes, maybeOptions);
return params.valid ?
client.getTreatment(params.key as SplitIO.SplitKey, params.nameOrNames as string, params.attributes as SplitIO.Attributes | undefined, params.options) :
wrapResult(evaluateFallBackTreatment(params.nameOrNames, false));
}
function getTreatmentWithConfig(maybeKey: SplitIO.SplitKey, maybeFeatureFlagName: string, maybeAttributes?: SplitIO.Attributes, maybeOptions?: SplitIO.EvaluationOptions) {
const params = validateEvaluationParams(GET_TREATMENT_WITH_CONFIG, maybeKey, maybeFeatureFlagName, maybeAttributes, maybeOptions);
return params.valid ?
client.getTreatmentWithConfig(params.key as SplitIO.SplitKey, params.nameOrNames as string, params.attributes as SplitIO.Attributes | undefined, params.options) :
wrapResult(evaluateFallBackTreatment(params.nameOrNames, true));
}
function getTreatments(maybeKey: SplitIO.SplitKey, maybeFeatureFlagNames: string[], maybeAttributes?: SplitIO.Attributes, maybeOptions?: SplitIO.EvaluationOptions) {
const params = validateEvaluationParams(GET_TREATMENTS, maybeKey, maybeFeatureFlagNames, maybeAttributes, maybeOptions);
return params.valid ?
client.getTreatments(params.key as SplitIO.SplitKey, params.nameOrNames as string[], params.attributes as SplitIO.Attributes | undefined, params.options) :
wrapResult(evaluateFallBackTreatment(params.nameOrNames || [], false));
}
function getTreatmentsWithConfig(maybeKey: SplitIO.SplitKey, maybeFeatureFlagNames: string[], maybeAttributes?: SplitIO.Attributes, maybeOptions?: SplitIO.EvaluationOptions) {
const params = validateEvaluationParams(GET_TREATMENTS_WITH_CONFIG, maybeKey, maybeFeatureFlagNames, maybeAttributes, maybeOptions);
return params.valid ?
client.getTreatmentsWithConfig(params.key as SplitIO.SplitKey, params.nameOrNames as string[], params.attributes as SplitIO.Attributes | undefined, params.options) :
wrapResult(evaluateFallBackTreatment(params.nameOrNames || [], true));
}
function getTreatmentsByFlagSets(maybeKey: SplitIO.SplitKey, maybeFlagSets: string[], maybeAttributes?: SplitIO.Attributes, maybeOptions?: SplitIO.EvaluationOptions) {
const params = validateEvaluationParams(GET_TREATMENTS_BY_FLAG_SETS, maybeKey, maybeFlagSets, maybeAttributes, maybeOptions);
return params.valid ?
client.getTreatmentsByFlagSets(params.key as SplitIO.SplitKey, params.nameOrNames as string[], params.attributes as SplitIO.Attributes | undefined, params.options) :
wrapResult({});
}
function getTreatmentsWithConfigByFlagSets(maybeKey: SplitIO.SplitKey, maybeFlagSets: string[], maybeAttributes?: SplitIO.Attributes, maybeOptions?: SplitIO.EvaluationOptions) {
const params = validateEvaluationParams(GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SETS, maybeKey, maybeFlagSets, maybeAttributes, maybeOptions);
return params.valid ?
client.getTreatmentsWithConfigByFlagSets(params.key as SplitIO.SplitKey, params.nameOrNames as string[], params.attributes as SplitIO.Attributes | undefined, params.options) :
wrapResult({});
}
function getTreatmentsByFlagSet(maybeKey: SplitIO.SplitKey, maybeFlagSet: string, maybeAttributes?: SplitIO.Attributes, maybeOptions?: SplitIO.EvaluationOptions) {
const params = validateEvaluationParams(GET_TREATMENTS_BY_FLAG_SET, maybeKey, [maybeFlagSet], maybeAttributes, maybeOptions);
return params.valid ?
client.getTreatmentsByFlagSet(params.key as SplitIO.SplitKey, (params.nameOrNames as string[])[0], params.attributes as SplitIO.Attributes | undefined, params.options) :
wrapResult({});
}
function getTreatmentsWithConfigByFlagSet(maybeKey: SplitIO.SplitKey, maybeFlagSet: string, maybeAttributes?: SplitIO.Attributes, maybeOptions?: SplitIO.EvaluationOptions) {
const params = validateEvaluationParams(GET_TREATMENTS_WITH_CONFIG_BY_FLAG_SET, maybeKey, [maybeFlagSet], maybeAttributes, maybeOptions);
return params.valid ?
client.getTreatmentsWithConfigByFlagSet(params.key as SplitIO.SplitKey, (params.nameOrNames as string[])[0], params.attributes as SplitIO.Attributes | undefined, params.options) :
wrapResult({});
}
function track(maybeKey: SplitIO.SplitKey, maybeTT: string, maybeEvent: string, maybeEventValue?: number, maybeProperties?: SplitIO.Properties) {
const key = validateKey(log, maybeKey, TRACK_FN_LABEL);
const tt = validateTrafficType(log, maybeTT, TRACK_FN_LABEL);
const event = validateEvent(log, maybeEvent, TRACK_FN_LABEL);
const eventValue = validateEventValue(log, maybeEventValue, TRACK_FN_LABEL);
const { properties, size } = validateEventProperties(log, maybeProperties, TRACK_FN_LABEL);
const isNotDestroyed = validateIfNotDestroyed(log, readinessManager, TRACK_FN_LABEL);
return isNotDestroyed && key && tt && event && eventValue !== false && properties !== false ? // @ts-expect-error
client.track(key, tt, event, eventValue, properties, size) :
wrapResult(false);
}
return {
getTreatment,
getTreatmentWithConfig,
getTreatments,
getTreatmentsWithConfig,
getTreatmentsByFlagSets,
getTreatmentsWithConfigByFlagSets,
getTreatmentsByFlagSet,
getTreatmentsWithConfigByFlagSet,
track
} as TClient;
}