-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex-ff-wrapper.ts
More file actions
74 lines (63 loc) · 2.88 KB
/
index-ff-wrapper.ts
File metadata and controls
74 lines (63 loc) · 2.88 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
import { ISdkFactoryParams } from '../sdkFactory/types';
import { sdkFactory } from '../sdkFactory/index';
import SplitIO from '../../types/splitio';
import { objectAssign } from '../utils/lang/objectAssign';
import { validateTarget } from '../utils/inputValidation/target';
import { GET_CONFIG } from '../utils/constants';
import { ISettings } from '../types';
function parseConfig(treatmentWithConfig: SplitIO.TreatmentWithConfig): SplitIO.Config {
return {
variant: treatmentWithConfig.treatment,
value: treatmentWithConfig.config ? JSON.parse(treatmentWithConfig.config) : {},
};
}
/**
* Configs SDK Client factory implemented as a wrapper over the FF SDK.
* Exposes getConfig and track at the root level instead of requiring a client() call.
* getConfig delegates to getTreatmentWithConfig and wraps the parsed JSON config in a Config object.
*/
export function configsClientFactory(params: ISdkFactoryParams): SplitIO.ConfigsClient {
const ffSdk = sdkFactory({ ...params, lazyInit: true }) as (SplitIO.ISDK | SplitIO.IAsyncSDK) & { init(): void };
const ffClient = ffSdk.client() as SplitIO.IClient & { init(): void; flush(): Promise<void> };
const ffManager = ffSdk.manager();
const log = (ffSdk.settings as ISettings).log;
return objectAssign(
// Inherit status interface (EventEmitter, Event, getStatus, ready, whenReady, whenReadyFromCache) from ffClient
Object.create(ffClient) as SplitIO.IStatusInterface,
{
settings: ffSdk.settings,
Logger: ffSdk.Logger,
init() {
ffSdk.init();
},
flush(): Promise<void> {
return ffClient.flush();
},
destroy(): Promise<void> {
return ffSdk.destroy();
},
getConfig(name: string, target?: SplitIO.Target): SplitIO.Config {
if (target) {
// Serve config with target
if (validateTarget(log, target, GET_CONFIG)) {
const result = ffClient.getTreatmentWithConfig(target.key, name, target.attributes) as SplitIO.TreatmentWithConfig;
return parseConfig(result);
} else {
log.error('Invalid target for getConfig.');
}
}
// Serve config without target
const config = ffManager.split(name) as SplitIO.SplitView;
if (!config) {
log.error('Provided config name does not exist. Serving empty config object.');
return parseConfig({ treatment: 'control', config: null });
}
log.info('Serving default config variant, ' + config.defaultTreatment + ' for config ' + name);
return parseConfig({ treatment: config.defaultTreatment, config: config.configs[config.defaultTreatment] });
},
track(key: SplitIO.SplitKey, trafficType: string, eventType: string, value?: number, properties?: SplitIO.Properties): boolean {
return ffClient.track(key, trafficType, eventType, value, properties) as boolean;
}
}
);
}