-
Notifications
You must be signed in to change notification settings - Fork 10
/
ConfigCatClientOptions.ts
177 lines (132 loc) · 5.73 KB
/
ConfigCatClientOptions.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
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
171
172
173
174
175
176
177
import { ConfigCatConsoleLogger } from "./ConfigCatLogger";
import { IConfigCatLogger, IAutoPollOptions, ILazyLoadingOptions, IManualPollOptions, LogLevel, ICache } from "./index";
import { InMemoryCache } from "./Cache";
import COMMON_VERSION from "./Version";
/** Control the location of the config.json files containing your feature flags and settings within the ConfigCat CDN. */
export enum DataGovernance {
/** Select this if your feature flags are published to all global CDN nodes. */
Global = 0,
/** Select this if your feature flags are published to CDN nodes only in the EU. */
EuOnly = 1
}
export interface IOptions {
logger?: IConfigCatLogger | null;
requestTimeoutMs?: number | null;
baseUrl?: string | null;
/** You can set a base_url if you want to use a proxy server between your application and ConfigCat */
proxy?: string | null;
/** Default: Global. Set this parameter to be in sync with the Data Governance preference on the Dashboard:
* https://app.configcat.com/organization/data-governance (Only Organization Admins have access) */
dataGovernance?: DataGovernance | null;
/**
* ICache instance for cache the config.
*/
cache?: ICache | null;
}
export abstract class OptionsBase implements IOptions {
private configFileName = "config_v5";
public logger: IConfigCatLogger = new ConfigCatConsoleLogger(LogLevel.Warn);
public apiKey: string;
public clientVersion: string;
public requestTimeoutMs: number = 30000;
public baseUrl: string;
public baseUrlOverriden: boolean = false;
public proxy: string = "";
public dataGovernance: DataGovernance;
public cache: ICache;
constructor(apiKey: string, clientVersion: string, options?: IOptions | null, defaultCache?: ICache | null) {
if (!apiKey) {
throw new Error("Invalid 'apiKey' value");
}
if (!defaultCache){
defaultCache = new InMemoryCache();
}
this.apiKey = apiKey;
this.clientVersion = clientVersion;
this.dataGovernance = options?.dataGovernance ?? DataGovernance.Global;
this.cache = defaultCache;
switch (this.dataGovernance) {
case DataGovernance.EuOnly:
this.baseUrl = "https://cdn-eu.configcat.com";
break;
default:
this.baseUrl = "https://cdn-global.configcat.com";
break;
}
if (options) {
if (options.logger) {
this.logger = options.logger;
}
if (options.requestTimeoutMs) {
if (options.requestTimeoutMs < 0) {
throw new Error("Invalid 'requestTimeoutMs' value");
}
this.requestTimeoutMs = options.requestTimeoutMs;
}
if (options.baseUrl) {
this.baseUrl = options.baseUrl;
this.baseUrlOverriden = true;
}
if (options.proxy) {
this.proxy = options.proxy;
}
if (options.cache) {
this.cache = options.cache;
}
}
}
getUrl(): string {
return this.baseUrl + "/configuration-files/" + this.apiKey + "/" + this.configFileName + ".json";
}
getCacheKey(): string {
return "js_" + this.configFileName + "_" + this.apiKey;
}
}
export class AutoPollOptions extends OptionsBase implements IAutoPollOptions {
/** The client's poll interval in seconds. Default: 60 seconds. */
public pollIntervalSeconds: number = 60;
/** You can subscribe to configuration changes with this callback. */
public configChanged: () => void = () => { };
/** Maximum waiting time between the client initialization and the first config acquisition in seconds. */
public maxInitWaitTimeSeconds: number = 5;
constructor(apiKey: string, options?: IAutoPollOptions | null, defaultCache?: ICache | null) {
super(apiKey, "a-" + COMMON_VERSION, options, defaultCache);
if (options) {
if (options.pollIntervalSeconds !== undefined && options.pollIntervalSeconds !== null) {
this.pollIntervalSeconds = options.pollIntervalSeconds;
}
if (options.configChanged) {
this.configChanged = options.configChanged;
}
if (options.maxInitWaitTimeSeconds !== undefined && options.maxInitWaitTimeSeconds !== null) {
this.maxInitWaitTimeSeconds = options.maxInitWaitTimeSeconds;
}
}
if (this.pollIntervalSeconds < 1) {
throw new Error("Invalid 'pollIntervalSeconds' value");
}
if (this.maxInitWaitTimeSeconds < 0) {
throw new Error("Invalid 'maxInitWaitTimeSeconds' value");
}
}
}
export class ManualPollOptions extends OptionsBase implements IManualPollOptions {
constructor(apiKey: string, options?: IManualPollOptions | null, defaultCache?: ICache | null) {
super(apiKey, "m-" + COMMON_VERSION, options, defaultCache);
}
}
export class LazyLoadOptions extends OptionsBase implements ILazyLoadingOptions {
/** The cache TTL. */
public cacheTimeToLiveSeconds: number = 60;
constructor(apiKey: string, options?: ILazyLoadingOptions | null, defaultCache?: ICache | null) {
super(apiKey, "l-" + COMMON_VERSION, options, defaultCache);
if (options) {
if (options.cacheTimeToLiveSeconds) {
this.cacheTimeToLiveSeconds = options.cacheTimeToLiveSeconds;
}
}
if (!this.cacheTimeToLiveSeconds || this.cacheTimeToLiveSeconds < 1) {
throw new Error("Invalid 'cacheTimeToLiveSeconds' value. Value must be greater than zero.");
}
}
}