-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
115 lines (97 loc) · 3.56 KB
/
index.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
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
import { EdgeFeatureHubConfig } from './edge_featurehub_config';
import { FeatureHubPollingClient } from './polling_sdk';
import { FeatureHubConfig, fhLog } from './feature_hub_config';
import { FeatureStateHolder } from './feature_state';
import { ClientContext } from './client_context';
export * from './feature_state';
export * from './feature_hub_config';
export * from './edge_service';
export * from './client_feature_repository';
export * from './models/models';
export * from './analytics';
export * from './test_sdk';
export * from './polling_sdk';
export * from './middleware';
export * from './baggage_header';
export * from './interceptors';
export * from './client_context';
export * from './internal_feature_repository';
export * from './context_impl';
export * from './featurehub_repository';
export * from './edge_featurehub_config';
export * from './featurehub_eventsource';
export * from './local_context';
export class FeatureHub {
private static inBrowser: boolean = (typeof window !== 'undefined' && typeof document !== 'undefined');
public static feature<T = any>(key: string): FeatureStateHolder<T> { return this.context.feature(key); }
public static set(config: FeatureHubConfig, context: ClientContext) {
window['fhConfig'] = config;
window['fhContext'] = context;
}
public static get context(): ClientContext {
if (this.inBrowser) {
const fhContext = window['fhContext'];
if (fhContext) {
return fhContext;
}
}
throw new Error('No FeatureHub context defined');
}
public static get config(): FeatureHubConfig {
if (this.inBrowser) {
const fhConfig = window['fhConfig'];
if (fhConfig) {
return fhConfig;
}
}
throw new Error('No FeatureHub config defined');
}
public static _initialize() {
if (this.inBrowser) {
// check for a meta tag with the featurehub API key and url
const metaTags = document.getElementsByTagName('meta');
const apiKeys: Array<string> = [];
let pollInterval: string | undefined;
let url: string | undefined;
const params: Array<Array<string>> = [];
for (let count = 0; count < metaTags.length; count++) {
const name = metaTags[count].getAttribute('name');
const content = metaTags[count].content;
if (name === 'featurehub-url') {
url = content;
} else if (name === 'featurehub-apiKey') {
apiKeys.push(content);
} else if (name === 'featurehub-interval') {
pollInterval = content;
} else if (name?.startsWith('featurehub-')) {
params.push([name.substring(11), content]);
}
}
if (apiKeys.length > 0) {
if (pollInterval) {
const _interval = pollInterval;
fhLog.trace('setting polling interval to', pollInterval);
EdgeFeatureHubConfig.defaultEdgeServiceSupplier =
(repo, config) =>
new FeatureHubPollingClient(repo, config, parseInt(_interval));
}
const config = EdgeFeatureHubConfig.config(url || 'https://app.featurehub.io/vanilla', apiKeys[0]);
if (apiKeys.length > 1) {
for (let count = 1; count < apiKeys.length; count++) {
config.apiKey(apiKeys[count]);
}
}
const context = config.newContext();
for (let count = 0; count < params.length; count++) {
context.attributeValue(params[count][0], params[count][1]);
}
context.build();
this.set(config, context);
}
}
}
static close() {
this.config.close();
}
}
FeatureHub._initialize();