@autofleet/settings 1.0.4 → 1.0.5-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +62 -0
- package/package.json +1 -1
- package/src/index.ts +70 -0
package/dist/index.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ declare class SettingsManager {
|
|
|
26
26
|
static readNetworkValue(networkValue: SettingValue, defaultValue: SettingValue): string | number | boolean;
|
|
27
27
|
constructor({ serviceUrl, ttl }?: SettingsClassOptions);
|
|
28
28
|
get(key: SettingValue, defaultValue?: SettingValue, labels?: LabelsArray, { timeout, rejectOnFail }?: GetSettingOption): Promise<unknown>;
|
|
29
|
+
getMultiple(keys: SettingValue[], defaultValues: SettingValue[], labels?: LabelsArray, { timeout, rejectOnFail }?: GetSettingOption): Promise<any>;
|
|
29
30
|
setLocal(key: string, labels: LabelsArray, value: SettingValue): void;
|
|
30
31
|
flush(): void;
|
|
31
32
|
}
|
package/dist/index.js
CHANGED
|
@@ -101,6 +101,68 @@ class SettingsManager {
|
|
|
101
101
|
return returnValue;
|
|
102
102
|
});
|
|
103
103
|
}
|
|
104
|
+
getMultiple(keys, defaultValues, labels = [], { timeout = 5000, rejectOnFail = false } = {}) {
|
|
105
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
106
|
+
if (keys.length > defaultValues.length) {
|
|
107
|
+
throw new Error('Got more keys then default values. A default value is required for each key');
|
|
108
|
+
}
|
|
109
|
+
if (process.env.NODE_ENV === 'test') {
|
|
110
|
+
return defaultValues;
|
|
111
|
+
}
|
|
112
|
+
// eslint-disable-next-line consistent-return
|
|
113
|
+
keys.map((key, index) => __awaiter(this, void 0, void 0, function* () {
|
|
114
|
+
const cacheKey = `${key}_${JSON.stringify(labels)}`;
|
|
115
|
+
const cacheValue = this.settingsCache.get(cacheKey);
|
|
116
|
+
if (cacheValue !== undefined) {
|
|
117
|
+
if (waitingToNetwork === cacheValue) {
|
|
118
|
+
yield nextTick();
|
|
119
|
+
return new Promise((resolve) => {
|
|
120
|
+
this.networkEvents.once(cacheKey, (networkValue) => {
|
|
121
|
+
resolve(SettingsManager.readNetworkValue(networkValue, defaultValues[index]));
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}));
|
|
127
|
+
keys.map((key) => __awaiter(this, void 0, void 0, function* () {
|
|
128
|
+
const cacheKey = `${key}_${JSON.stringify(labels)}`;
|
|
129
|
+
this.settingsCache.set(cacheKey, waitingToNetwork, this.ttl);
|
|
130
|
+
}));
|
|
131
|
+
let settings;
|
|
132
|
+
try {
|
|
133
|
+
const { data } = yield this.network.get('/api/v1/settings', {
|
|
134
|
+
params: {
|
|
135
|
+
keys,
|
|
136
|
+
labels,
|
|
137
|
+
},
|
|
138
|
+
timeout,
|
|
139
|
+
});
|
|
140
|
+
settings = data;
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
logger.error('Cant get setting from network');
|
|
144
|
+
if (rejectOnFail) {
|
|
145
|
+
throw new ConnotGetNetworkValueError();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
// eslint-disable-next-line array-callback-return
|
|
149
|
+
settings.map((setting, index) => {
|
|
150
|
+
const cacheKey = `${keys[index]}_${JSON.stringify(labels)}`;
|
|
151
|
+
this.networkEvents.emit(cacheKey, setting);
|
|
152
|
+
let returnValue;
|
|
153
|
+
try {
|
|
154
|
+
returnValue = SettingsManager.readNetworkValue(setting, defaultValues[index]);
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
this.settingsCache.del(cacheKey);
|
|
158
|
+
throw err;
|
|
159
|
+
}
|
|
160
|
+
this.settingsCache.set(cacheKey, returnValue, this.ttl);
|
|
161
|
+
return returnValue;
|
|
162
|
+
});
|
|
163
|
+
return settings;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
104
166
|
setLocal(key, labels, value) {
|
|
105
167
|
const cacheKey = `${key}_${JSON.stringify(labels)}`;
|
|
106
168
|
this.settingsCache.set(cacheKey, value, this.ttl);
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -135,6 +135,76 @@ class SettingsManager {
|
|
|
135
135
|
return returnValue;
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
+
async getMultiple(
|
|
139
|
+
keys: SettingValue[],
|
|
140
|
+
defaultValues: SettingValue[],
|
|
141
|
+
labels: LabelsArray = [],
|
|
142
|
+
{ timeout = 5000, rejectOnFail = false } : GetSettingOption = {},
|
|
143
|
+
): Promise<any> {
|
|
144
|
+
if (keys.length > defaultValues.length) {
|
|
145
|
+
throw new Error('Got more keys then default values. A default value is required for each key');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (process.env.NODE_ENV === 'test') {
|
|
149
|
+
return defaultValues;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// eslint-disable-next-line consistent-return
|
|
153
|
+
keys.map(async (key, index) => {
|
|
154
|
+
const cacheKey = `${key}_${JSON.stringify(labels)}`;
|
|
155
|
+
const cacheValue = this.settingsCache.get(cacheKey);
|
|
156
|
+
if (cacheValue !== undefined) {
|
|
157
|
+
if (waitingToNetwork === cacheValue) {
|
|
158
|
+
await nextTick();
|
|
159
|
+
return new Promise((resolve) => {
|
|
160
|
+
this.networkEvents.once(cacheKey, (networkValue) => {
|
|
161
|
+
resolve(SettingsManager.readNetworkValue(networkValue, defaultValues[index]));
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
keys.map(async (key) => {
|
|
169
|
+
const cacheKey = `${key}_${JSON.stringify(labels)}`;
|
|
170
|
+
this.settingsCache.set(cacheKey, waitingToNetwork, this.ttl);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
let settings;
|
|
174
|
+
try {
|
|
175
|
+
const { data } = await this.network.get('/api/v1/settings', {
|
|
176
|
+
params: {
|
|
177
|
+
keys,
|
|
178
|
+
labels,
|
|
179
|
+
},
|
|
180
|
+
timeout,
|
|
181
|
+
});
|
|
182
|
+
settings = data;
|
|
183
|
+
} catch (err) {
|
|
184
|
+
logger.error('Cant get setting from network');
|
|
185
|
+
if (rejectOnFail) {
|
|
186
|
+
throw new ConnotGetNetworkValueError();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// eslint-disable-next-line array-callback-return
|
|
191
|
+
settings.map((setting: any, index: number) => {
|
|
192
|
+
const cacheKey = `${keys[index]}_${JSON.stringify(labels)}`;
|
|
193
|
+
this.networkEvents.emit(cacheKey, setting);
|
|
194
|
+
let returnValue;
|
|
195
|
+
try {
|
|
196
|
+
returnValue = SettingsManager.readNetworkValue(setting, defaultValues[index]);
|
|
197
|
+
} catch (err) {
|
|
198
|
+
this.settingsCache.del(cacheKey);
|
|
199
|
+
throw err;
|
|
200
|
+
}
|
|
201
|
+
this.settingsCache.set(cacheKey, returnValue, this.ttl);
|
|
202
|
+
return returnValue;
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
return settings;
|
|
206
|
+
}
|
|
207
|
+
|
|
138
208
|
setLocal(key: string, labels: LabelsArray, value: SettingValue) {
|
|
139
209
|
const cacheKey = `${key}_${JSON.stringify(labels)}`;
|
|
140
210
|
this.settingsCache.set(cacheKey, value, this.ttl);
|