@autofleet/settings 1.0.5-beta-3 → 1.0.5
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.js +7 -9
- package/dist/index.test.js +181 -123
- package/package.json +1 -1
- package/src/index.test.ts +179 -112
- package/src/index.ts +7 -10
package/dist/index.js
CHANGED
|
@@ -131,7 +131,7 @@ class SettingsManager {
|
|
|
131
131
|
const cacheKey = `${obj.key}_${JSON.stringify(labels)}`;
|
|
132
132
|
this.settingsCache.set(cacheKey, waitingToNetwork, this.ttl);
|
|
133
133
|
});
|
|
134
|
-
let
|
|
134
|
+
let values;
|
|
135
135
|
try {
|
|
136
136
|
const { data } = yield this.network.get('/api/v1/settings', {
|
|
137
137
|
params: {
|
|
@@ -140,21 +140,20 @@ class SettingsManager {
|
|
|
140
140
|
},
|
|
141
141
|
timeout,
|
|
142
142
|
});
|
|
143
|
-
|
|
143
|
+
values = data.values;
|
|
144
144
|
}
|
|
145
145
|
catch (err) {
|
|
146
|
-
logger.error('Cant get setting from network');
|
|
146
|
+
logger.error('Cant get setting from network', { err });
|
|
147
147
|
if (rejectOnFail) {
|
|
148
148
|
throw new ConnotGetNetworkValueError();
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
this.networkEvents.emit(cacheKey, setting);
|
|
151
|
+
return settingsToGet.map((setting, index) => {
|
|
152
|
+
const cacheKey = `${setting.key}_${JSON.stringify(labels)}`;
|
|
153
|
+
this.networkEvents.emit(cacheKey, values[index]);
|
|
155
154
|
let returnValue;
|
|
156
155
|
try {
|
|
157
|
-
returnValue = SettingsManager.readNetworkValue(
|
|
156
|
+
returnValue = SettingsManager.readNetworkValue(values[index], setting.defaultValue);
|
|
158
157
|
}
|
|
159
158
|
catch (err) {
|
|
160
159
|
this.settingsCache.del(cacheKey);
|
|
@@ -163,7 +162,6 @@ class SettingsManager {
|
|
|
163
162
|
this.settingsCache.set(cacheKey, returnValue, this.ttl);
|
|
164
163
|
return returnValue;
|
|
165
164
|
});
|
|
166
|
-
return settings;
|
|
167
165
|
});
|
|
168
166
|
}
|
|
169
167
|
setLocal(key, labels, value) {
|
package/dist/index.test.js
CHANGED
|
@@ -23,129 +23,187 @@ const mockSetting = (key, value, labels = undefined, response = 200) => {
|
|
|
23
23
|
.query(labels ? { labels } : undefined);
|
|
24
24
|
return n.reply(response, { value });
|
|
25
25
|
};
|
|
26
|
+
const mockMultipleSettings = (keys, values, response = 200) => {
|
|
27
|
+
const n = nock(`http://${serviceUrl}`)
|
|
28
|
+
.get('/api/v1/settings')
|
|
29
|
+
.query({
|
|
30
|
+
keys,
|
|
31
|
+
});
|
|
32
|
+
return n.reply(response, { values });
|
|
33
|
+
};
|
|
26
34
|
describe('Settings', () => {
|
|
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
|
-
const settings = new index_1.default({
|
|
58
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
59
|
-
});
|
|
60
|
-
const value = yield settings.get('key1', 'dv');
|
|
61
|
-
expect(value).toEqual('value1');
|
|
62
|
-
expect(m.isDone()).toBeTruthy();
|
|
63
|
-
}));
|
|
64
|
-
it('Cache from server', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
65
|
-
const m = mockSetting('key1', 'value1');
|
|
66
|
-
const settings = new index_1.default({
|
|
67
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
68
|
-
});
|
|
69
|
-
const value = yield settings.get('key1', 'dv');
|
|
70
|
-
expect(value).toEqual('value1');
|
|
71
|
-
expect(m.isDone()).toBeTruthy();
|
|
72
|
-
const value2 = yield settings.get('key1', 'dv');
|
|
73
|
-
expect(value2).toEqual('value1');
|
|
74
|
-
}));
|
|
75
|
-
it('Use only one request for similar keys', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
76
|
-
const m = mockSetting('key1EM', 'value1EM');
|
|
77
|
-
const settings = new index_1.default({
|
|
78
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
79
|
-
});
|
|
80
|
-
const [value, value2] = yield Promise.all([
|
|
81
|
-
settings.get('key1EM', 'dv'),
|
|
82
|
-
settings.get('key1EM', 'dv'),
|
|
83
|
-
]);
|
|
84
|
-
expect(value).toEqual('value1EM');
|
|
85
|
-
expect(m.isDone()).toBeTruthy();
|
|
86
|
-
expect(value2).toEqual('value1EM');
|
|
87
|
-
}));
|
|
88
|
-
it('Finds with labels', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
89
|
-
const labels = [{ fleetId: 'uuid' }];
|
|
90
|
-
const m = mockSetting('key1', 'value1', labels);
|
|
91
|
-
const settings = new index_1.default({
|
|
92
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
93
|
-
});
|
|
94
|
-
const value = yield settings.get('key1', 'dv', labels);
|
|
95
|
-
expect(value).toEqual('value1');
|
|
96
|
-
expect(m.isDone()).toBeTruthy();
|
|
97
|
-
}));
|
|
98
|
-
it('Returns default value when network error', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
99
|
-
const settings = new index_1.default({
|
|
100
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
101
|
-
});
|
|
102
|
-
const value = yield settings.get('key1', 'dv', [{ fleetId: 'uuid' }]);
|
|
103
|
-
expect(value).toEqual('dv');
|
|
104
|
-
}));
|
|
105
|
-
it('Throws an error if network error and no default value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
106
|
-
const settings = new index_1.default({
|
|
107
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
108
|
-
});
|
|
109
|
-
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE, [{ fleetId: 'uuid' }]);
|
|
110
|
-
expect(f())
|
|
111
|
-
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
112
|
-
}));
|
|
113
|
-
it('Throws an error if network error and no default value and success the next time', (done) => __awaiter(void 0, void 0, void 0, function* () {
|
|
114
|
-
const settings = new index_1.default({
|
|
115
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
116
|
-
});
|
|
117
|
-
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE);
|
|
118
|
-
expect(f())
|
|
119
|
-
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
120
|
-
setTimeout(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
+
describe('get', () => {
|
|
36
|
+
it('Throws exeption if there is no defualt value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
37
|
+
const settings = new index_1.default({
|
|
38
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
39
|
+
});
|
|
40
|
+
expect(settings.get('key1'))
|
|
41
|
+
.rejects.toEqual(new Error('Can\'t get a key without defaultValue'));
|
|
42
|
+
}));
|
|
43
|
+
it('Can get 0 as default value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
44
|
+
const settings = new index_1.default({
|
|
45
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
46
|
+
});
|
|
47
|
+
expect(yield settings.get('key1', 0)).toBe(0);
|
|
48
|
+
}));
|
|
49
|
+
it('Can get false as default value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
+
const settings = new index_1.default({
|
|
51
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
52
|
+
});
|
|
53
|
+
expect(yield settings.get('key1', false)).toBe(false);
|
|
54
|
+
}));
|
|
55
|
+
it('Uses env.SETTING_MS_SERVICE_HOST as service host if no one defined', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
56
|
+
const m = mockSetting('key1', 'value1');
|
|
57
|
+
process.env.SETTING_MS_SERVICE_HOST = serviceUrl;
|
|
58
|
+
const settings = new index_1.default();
|
|
59
|
+
const value = yield settings.get('key1', 'dv');
|
|
60
|
+
expect(value).toEqual('value1');
|
|
61
|
+
expect(m.isDone()).toBeTruthy();
|
|
62
|
+
process.env.SETTING_MS_SERVICE_HOST = '';
|
|
63
|
+
}));
|
|
64
|
+
it('Get a key from server', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
121
65
|
const m = mockSetting('key1', 'value1');
|
|
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
|
-
|
|
66
|
+
const settings = new index_1.default({
|
|
67
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
68
|
+
});
|
|
69
|
+
const value = yield settings.get('key1', 'dv');
|
|
70
|
+
expect(value).toEqual('value1');
|
|
71
|
+
expect(m.isDone()).toBeTruthy();
|
|
72
|
+
}));
|
|
73
|
+
it('Cache from server', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
74
|
+
const m = mockSetting('key1', 'value1');
|
|
75
|
+
const settings = new index_1.default({
|
|
76
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
77
|
+
});
|
|
78
|
+
const value = yield settings.get('key1', 'dv');
|
|
79
|
+
expect(value).toEqual('value1');
|
|
80
|
+
expect(m.isDone()).toBeTruthy();
|
|
81
|
+
const value2 = yield settings.get('key1', 'dv');
|
|
82
|
+
expect(value2).toEqual('value1');
|
|
83
|
+
}));
|
|
84
|
+
it('Use only one request for similar keys', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
85
|
+
const m = mockSetting('key1EM', 'value1EM');
|
|
86
|
+
const settings = new index_1.default({
|
|
87
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
88
|
+
});
|
|
89
|
+
const [value, value2] = yield Promise.all([
|
|
90
|
+
settings.get('key1EM', 'dv'),
|
|
91
|
+
settings.get('key1EM', 'dv'),
|
|
92
|
+
]);
|
|
93
|
+
expect(value).toEqual('value1EM');
|
|
94
|
+
expect(m.isDone()).toBeTruthy();
|
|
95
|
+
expect(value2).toEqual('value1EM');
|
|
96
|
+
}));
|
|
97
|
+
it('Finds with labels', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
98
|
+
const labels = [{ fleetId: 'uuid' }];
|
|
99
|
+
const m = mockSetting('key1', 'value1', labels);
|
|
100
|
+
const settings = new index_1.default({
|
|
101
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
102
|
+
});
|
|
103
|
+
const value = yield settings.get('key1', 'dv', labels);
|
|
104
|
+
expect(value).toEqual('value1');
|
|
105
|
+
expect(m.isDone()).toBeTruthy();
|
|
106
|
+
}));
|
|
107
|
+
it('Returns default value when network error', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
108
|
+
const settings = new index_1.default({
|
|
109
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
110
|
+
});
|
|
111
|
+
const value = yield settings.get('key1', 'dv', [{ fleetId: 'uuid' }]);
|
|
112
|
+
expect(value).toEqual('dv');
|
|
113
|
+
}));
|
|
114
|
+
it('Throws an error if network error and no default value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
115
|
+
const settings = new index_1.default({
|
|
116
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
117
|
+
});
|
|
118
|
+
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE, [{ fleetId: 'uuid' }]);
|
|
119
|
+
expect(f())
|
|
120
|
+
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
121
|
+
}));
|
|
122
|
+
it('Throws an error if network error and no default value and success the next time', (done) => __awaiter(void 0, void 0, void 0, function* () {
|
|
123
|
+
const settings = new index_1.default({
|
|
124
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
125
|
+
});
|
|
126
|
+
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE);
|
|
127
|
+
expect(f())
|
|
128
|
+
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
129
|
+
setTimeout(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
130
|
+
const m = mockSetting('key1', 'value1');
|
|
131
|
+
expect(yield settings.get('key1', settings.NEVER_DEFAULT_VALUE)).toEqual('value1');
|
|
132
|
+
expect(m.isDone());
|
|
133
|
+
done();
|
|
134
|
+
}), 100);
|
|
135
|
+
}));
|
|
136
|
+
it('Values can be flushed', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
137
|
+
const m1 = mockSetting('key1', 'value1');
|
|
138
|
+
const settings = new index_1.default({
|
|
139
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
140
|
+
});
|
|
141
|
+
settings.flush();
|
|
142
|
+
const value = yield settings.get('key1', 'dv');
|
|
143
|
+
expect(value).toEqual('value1');
|
|
144
|
+
expect(m1.isDone()).toBeTruthy();
|
|
145
|
+
settings.flush();
|
|
146
|
+
const m2 = mockSetting('key1', 'value2');
|
|
147
|
+
const value2 = yield settings.get('key1', 'dv');
|
|
148
|
+
expect(value2).toEqual('value2');
|
|
149
|
+
expect(m2.isDone()).toBeTruthy();
|
|
150
|
+
}));
|
|
151
|
+
it('when NODE_ENV === test return the default value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
152
|
+
process.env.NODE_ENV = 'test';
|
|
153
|
+
const settings = new index_1.default({
|
|
154
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
155
|
+
});
|
|
156
|
+
const value = yield settings.get('key1', 'dv');
|
|
157
|
+
expect(value).toEqual('dv');
|
|
158
|
+
process.env.NODE_ENV = 'node-common-test';
|
|
159
|
+
}));
|
|
160
|
+
});
|
|
161
|
+
describe('getMultiple', () => {
|
|
162
|
+
it('when NODE_ENV === test return the default values', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
163
|
+
process.env.NODE_ENV = 'test';
|
|
164
|
+
const settings = new index_1.default({
|
|
165
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
166
|
+
});
|
|
167
|
+
const values = yield settings.getMultiple([
|
|
168
|
+
{
|
|
169
|
+
key: 'key0',
|
|
170
|
+
defaultValue: 'defaultValue0',
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
key: 'key1',
|
|
174
|
+
defaultValue: 'defaultValue1',
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
key: 'key2',
|
|
178
|
+
defaultValue: 'defaultValue2',
|
|
179
|
+
},
|
|
180
|
+
], []);
|
|
181
|
+
values.map((value, i) => expect(value).toEqual(`defaultValue${i}`));
|
|
182
|
+
process.env.NODE_ENV = 'node-common-test';
|
|
183
|
+
}));
|
|
184
|
+
it('can get multiple settings', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
185
|
+
const mock = mockMultipleSettings(['key0', 'key1', 'key2'], ['value0', 'value1', 'value2']);
|
|
186
|
+
const settings = new index_1.default({
|
|
187
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
188
|
+
});
|
|
189
|
+
const values = yield settings.getMultiple([
|
|
190
|
+
{
|
|
191
|
+
key: 'key0',
|
|
192
|
+
defaultValue: 'defaultValue0',
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
key: 'key1',
|
|
196
|
+
defaultValue: 'defaultValue1',
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
key: 'key2',
|
|
200
|
+
defaultValue: 'defaultValue2',
|
|
201
|
+
},
|
|
202
|
+
], []);
|
|
203
|
+
expect(mock.isDone()).toBeTruthy();
|
|
204
|
+
values.forEach((v, index) => {
|
|
205
|
+
expect(v).toEqual(`value${index}`);
|
|
206
|
+
});
|
|
207
|
+
}));
|
|
208
|
+
});
|
|
151
209
|
});
|
package/package.json
CHANGED
package/src/index.test.ts
CHANGED
|
@@ -15,158 +15,225 @@ const mockSetting = (key: string, value: string | boolean | number | never, labe
|
|
|
15
15
|
return n.reply(response, { value });
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
const mockMultipleSettings = (keys: string[], values: string[], response = 200) => {
|
|
19
|
+
const n = nock(`http://${serviceUrl}`)
|
|
20
|
+
.get('/api/v1/settings')
|
|
21
|
+
.query({
|
|
22
|
+
keys,
|
|
22
23
|
});
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
});
|
|
25
|
+
return n.reply(response, { values });
|
|
26
|
+
};
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
describe('Settings', () => {
|
|
29
|
+
describe('get', () => {
|
|
30
|
+
it('Throws exeption if there is no defualt value', async () => {
|
|
31
|
+
const settings = new Settings({
|
|
32
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
expect(settings.get('key1'))
|
|
36
|
+
.rejects.toEqual(new Error('Can\'t get a key without defaultValue'));
|
|
31
37
|
});
|
|
32
38
|
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
it('Can get 0 as default value', async () => {
|
|
40
|
+
const settings = new Settings({
|
|
41
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
42
|
+
});
|
|
35
43
|
|
|
36
|
-
|
|
37
|
-
const settings = new Settings({
|
|
38
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
44
|
+
expect(await settings.get('key1', 0)).toBe(0);
|
|
39
45
|
});
|
|
40
46
|
|
|
41
|
-
|
|
42
|
-
|
|
47
|
+
it('Can get false as default value', async () => {
|
|
48
|
+
const settings = new Settings({
|
|
49
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
50
|
+
});
|
|
43
51
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
process.env.SETTING_MS_SERVICE_HOST = serviceUrl;
|
|
47
|
-
const settings = new Settings();
|
|
52
|
+
expect(await settings.get('key1', false)).toBe(false);
|
|
53
|
+
});
|
|
48
54
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
});
|
|
55
|
+
it('Uses env.SETTING_MS_SERVICE_HOST as service host if no one defined', async () => {
|
|
56
|
+
const m = mockSetting('key1', 'value1');
|
|
57
|
+
process.env.SETTING_MS_SERVICE_HOST = serviceUrl;
|
|
58
|
+
const settings = new Settings();
|
|
54
59
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
60
|
+
const value = await settings.get('key1', 'dv');
|
|
61
|
+
expect(value).toEqual('value1');
|
|
62
|
+
expect(m.isDone()).toBeTruthy();
|
|
63
|
+
process.env.SETTING_MS_SERVICE_HOST = '';
|
|
59
64
|
});
|
|
60
65
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
it('Get a key from server', async () => {
|
|
67
|
+
const m = mockSetting('key1', 'value1');
|
|
68
|
+
const settings = new Settings({
|
|
69
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
70
|
+
});
|
|
65
71
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
72
|
+
const value = await settings.get('key1', 'dv');
|
|
73
|
+
expect(value).toEqual('value1');
|
|
74
|
+
expect(m.isDone()).toBeTruthy();
|
|
70
75
|
});
|
|
71
76
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
77
|
+
it('Cache from server', async () => {
|
|
78
|
+
const m = mockSetting('key1', 'value1');
|
|
79
|
+
const settings = new Settings({
|
|
80
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
81
|
+
});
|
|
75
82
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
83
|
+
const value = await settings.get('key1', 'dv');
|
|
84
|
+
expect(value).toEqual('value1');
|
|
85
|
+
expect(m.isDone()).toBeTruthy();
|
|
79
86
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const settings = new Settings({
|
|
83
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
87
|
+
const value2 = await settings.get('key1', 'dv');
|
|
88
|
+
expect(value2).toEqual('value1');
|
|
84
89
|
});
|
|
85
90
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
settings
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
expect(m.isDone()).toBeTruthy();
|
|
91
|
+
it('Use only one request for similar keys', async () => {
|
|
92
|
+
const m = mockSetting('key1EM', 'value1EM');
|
|
93
|
+
const settings = new Settings({
|
|
94
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
95
|
+
});
|
|
92
96
|
|
|
93
|
-
|
|
94
|
-
|
|
97
|
+
const [value, value2] = await Promise.all([
|
|
98
|
+
settings.get('key1EM', 'dv'),
|
|
99
|
+
settings.get('key1EM', 'dv'),
|
|
100
|
+
]);
|
|
101
|
+
expect(value).toEqual('value1EM');
|
|
102
|
+
expect(m.isDone()).toBeTruthy();
|
|
95
103
|
|
|
96
|
-
|
|
97
|
-
const labels = [{ fleetId: 'uuid' }];
|
|
98
|
-
const m = mockSetting('key1', 'value1', labels);
|
|
99
|
-
const settings = new Settings({
|
|
100
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
104
|
+
expect(value2).toEqual('value1EM');
|
|
101
105
|
});
|
|
102
106
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
it('Finds with labels', async () => {
|
|
108
|
+
const labels = [{ fleetId: 'uuid' }];
|
|
109
|
+
const m = mockSetting('key1', 'value1', labels);
|
|
110
|
+
const settings = new Settings({
|
|
111
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
112
|
+
});
|
|
107
113
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
114
|
+
const value = await settings.get('key1', 'dv', labels);
|
|
115
|
+
expect(value).toEqual('value1');
|
|
116
|
+
expect(m.isDone()).toBeTruthy();
|
|
111
117
|
});
|
|
112
118
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
119
|
+
it('Returns default value when network error', async () => {
|
|
120
|
+
const settings = new Settings({
|
|
121
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
122
|
+
});
|
|
116
123
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
serviceUrl: `http://${serviceUrl}/`,
|
|
124
|
+
const value = await settings.get('key1', 'dv', [{ fleetId: 'uuid' }]);
|
|
125
|
+
expect(value).toEqual('dv');
|
|
120
126
|
});
|
|
121
127
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
128
|
+
it('Throws an error if network error and no default value', async () => {
|
|
129
|
+
const settings = new Settings({
|
|
130
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
131
|
+
});
|
|
126
132
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
133
|
+
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE, [{ fleetId: 'uuid' }]);
|
|
134
|
+
expect(f())
|
|
135
|
+
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
130
136
|
});
|
|
131
137
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
138
|
+
it('Throws an error if network error and no default value and success the next time', async (done) => {
|
|
139
|
+
const settings = new Settings({
|
|
140
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE);
|
|
144
|
+
expect(f())
|
|
145
|
+
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
146
|
+
|
|
147
|
+
setTimeout(async () => {
|
|
148
|
+
const m = mockSetting('key1', 'value1');
|
|
149
|
+
expect(await settings.get('key1', settings.NEVER_DEFAULT_VALUE)).toEqual('value1');
|
|
150
|
+
expect(m.isDone());
|
|
151
|
+
done();
|
|
152
|
+
}, 100);
|
|
153
|
+
});
|
|
135
154
|
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
});
|
|
155
|
+
it('Values can be flushed', async () => {
|
|
156
|
+
const m1 = mockSetting('key1', 'value1');
|
|
157
|
+
const settings = new Settings({
|
|
158
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
159
|
+
});
|
|
160
|
+
settings.flush();
|
|
143
161
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
162
|
+
const value = await settings.get('key1', 'dv');
|
|
163
|
+
expect(value).toEqual('value1');
|
|
164
|
+
expect(m1.isDone()).toBeTruthy();
|
|
165
|
+
|
|
166
|
+
settings.flush();
|
|
167
|
+
const m2 = mockSetting('key1', 'value2');
|
|
150
168
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
169
|
+
const value2 = await settings.get('key1', 'dv');
|
|
170
|
+
expect(value2).toEqual('value2');
|
|
171
|
+
expect(m2.isDone()).toBeTruthy();
|
|
172
|
+
});
|
|
154
173
|
|
|
155
|
-
|
|
156
|
-
|
|
174
|
+
it('when NODE_ENV === test return the default value', async () => {
|
|
175
|
+
process.env.NODE_ENV = 'test';
|
|
176
|
+
const settings = new Settings({
|
|
177
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
178
|
+
});
|
|
157
179
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
180
|
+
const value = await settings.get('key1', 'dv');
|
|
181
|
+
expect(value).toEqual('dv');
|
|
182
|
+
process.env.NODE_ENV = 'node-common-test';
|
|
183
|
+
});
|
|
161
184
|
});
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
185
|
+
|
|
186
|
+
describe('getMultiple', () => {
|
|
187
|
+
it('when NODE_ENV === test return the default values', async () => {
|
|
188
|
+
process.env.NODE_ENV = 'test';
|
|
189
|
+
const settings = new Settings({
|
|
190
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const values = await settings.getMultiple([
|
|
194
|
+
{
|
|
195
|
+
key: 'key0',
|
|
196
|
+
defaultValue: 'defaultValue0',
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
key: 'key1',
|
|
200
|
+
defaultValue: 'defaultValue1',
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
key: 'key2',
|
|
204
|
+
defaultValue: 'defaultValue2',
|
|
205
|
+
},
|
|
206
|
+
], []);
|
|
207
|
+
values.map((value: any, i: number) => expect(value).toEqual(`defaultValue${i}`));
|
|
208
|
+
process.env.NODE_ENV = 'node-common-test';
|
|
166
209
|
});
|
|
167
210
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
211
|
+
it('can get multiple settings', async () => {
|
|
212
|
+
const mock = mockMultipleSettings(['key0', 'key1', 'key2'], ['value0', 'value1', 'value2']);
|
|
213
|
+
|
|
214
|
+
const settings = new Settings({
|
|
215
|
+
serviceUrl: `http://${serviceUrl}/`,
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
const values = await settings.getMultiple([
|
|
219
|
+
{
|
|
220
|
+
key: 'key0',
|
|
221
|
+
defaultValue: 'defaultValue0',
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
key: 'key1',
|
|
225
|
+
defaultValue: 'defaultValue1',
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
key: 'key2',
|
|
229
|
+
defaultValue: 'defaultValue2',
|
|
230
|
+
},
|
|
231
|
+
], []);
|
|
232
|
+
|
|
233
|
+
expect(mock.isDone()).toBeTruthy();
|
|
234
|
+
values.forEach((v: any, index: number) => {
|
|
235
|
+
expect(v).toEqual(`value${index}`);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
171
238
|
});
|
|
172
239
|
});
|
package/src/index.ts
CHANGED
|
@@ -175,7 +175,7 @@ class SettingsManager {
|
|
|
175
175
|
this.settingsCache.set(cacheKey, waitingToNetwork, this.ttl);
|
|
176
176
|
});
|
|
177
177
|
|
|
178
|
-
let
|
|
178
|
+
let values: any[];
|
|
179
179
|
try {
|
|
180
180
|
const { data } = await this.network.get('/api/v1/settings', {
|
|
181
181
|
params: {
|
|
@@ -184,21 +184,20 @@ class SettingsManager {
|
|
|
184
184
|
},
|
|
185
185
|
timeout,
|
|
186
186
|
});
|
|
187
|
-
|
|
187
|
+
values = data.values;
|
|
188
188
|
} catch (err) {
|
|
189
|
-
logger.error('Cant get setting from network');
|
|
189
|
+
logger.error('Cant get setting from network', { err });
|
|
190
190
|
if (rejectOnFail) {
|
|
191
191
|
throw new ConnotGetNetworkValueError();
|
|
192
192
|
}
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
this.networkEvents.emit(cacheKey, setting);
|
|
195
|
+
return settingsToGet.map((setting: any, index: number) => {
|
|
196
|
+
const cacheKey = `${setting.key}_${JSON.stringify(labels)}`;
|
|
197
|
+
this.networkEvents.emit(cacheKey, values[index]);
|
|
199
198
|
let returnValue;
|
|
200
199
|
try {
|
|
201
|
-
returnValue = SettingsManager.readNetworkValue(
|
|
200
|
+
returnValue = SettingsManager.readNetworkValue(values[index], setting.defaultValue);
|
|
202
201
|
} catch (err) {
|
|
203
202
|
this.settingsCache.del(cacheKey);
|
|
204
203
|
throw err;
|
|
@@ -206,8 +205,6 @@ class SettingsManager {
|
|
|
206
205
|
this.settingsCache.set(cacheKey, returnValue, this.ttl);
|
|
207
206
|
return returnValue;
|
|
208
207
|
});
|
|
209
|
-
|
|
210
|
-
return settings;
|
|
211
208
|
}
|
|
212
209
|
|
|
213
210
|
setLocal(key: string, labels: LabelsArray, value: SettingValue) {
|