@autofleet/node-common 1.1.3 → 1.1.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/index.js +2 -0
- package/network/index.js +2 -0
- package/package.json +3 -1
- package/settings/example.js +30 -0
- package/settings/index.js +52 -0
- package/settings/index.test.js +94 -0
package/index.js
CHANGED
package/network/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const axios = require('axios');
|
|
2
2
|
const axiosRetry = require('axios-retry');
|
|
3
3
|
const Logger = require('../logger');
|
|
4
|
+
const qs = require('qs');
|
|
4
5
|
const httpAdapter = require('axios/lib/adapters/http');
|
|
5
6
|
require('dotenv').config();
|
|
6
7
|
/**
|
|
@@ -24,6 +25,7 @@ const HTTPMethods = [
|
|
|
24
25
|
const defaultSettings = {
|
|
25
26
|
timeout: 2500,
|
|
26
27
|
headers: { 'X-AF-AUTH': 'ANYONE' },
|
|
28
|
+
paramsSerializer: params => qs.stringify(params),
|
|
27
29
|
};
|
|
28
30
|
|
|
29
31
|
module.exports = class Network {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/node-common",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"coverage": "jest --coverage --forceExit --runInBand",
|
|
@@ -26,6 +26,8 @@
|
|
|
26
26
|
"axios-retry": "^3.1.0",
|
|
27
27
|
"dotenv": "^5.0.1",
|
|
28
28
|
"jest": "^22.4.3",
|
|
29
|
+
"node-cache": "^4.2.0",
|
|
30
|
+
"qs": "^6.5.2",
|
|
29
31
|
"winston": "^3.0.0-rc4"
|
|
30
32
|
},
|
|
31
33
|
"devDependencies": {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/* eslint no-console: 0 */
|
|
2
|
+
const Settings = require('./index');
|
|
3
|
+
|
|
4
|
+
const settings = new Settings({
|
|
5
|
+
serviceUrl: 'http://setting-ms.autofleet.io/',
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
const fleetId = 'e7bbfcad-98a8-421d-962f-fd1c7790c789';
|
|
9
|
+
|
|
10
|
+
const testSettings = async () => {
|
|
11
|
+
const generalSetting = await settings.get('ride.matching_retry', 1);
|
|
12
|
+
const fleetSetting = await settings.get('ride.matching_retry', 1, [{ fleetId }]);
|
|
13
|
+
const notFoundKey = await settings.get('not.found.key', 1, [{ fleetId }]);
|
|
14
|
+
await settings.get('not.found.key', 1, [{ fleetId }, { dor: 1 }]);
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
await settings.get('not.found.key');
|
|
18
|
+
} catch (error) {
|
|
19
|
+
console.log(error);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
console.log(generalSetting);
|
|
23
|
+
console.log(fleetSetting);
|
|
24
|
+
console.log(notFoundKey);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
testSettings();
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
testSettings();
|
|
30
|
+
}, 500);
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const NodeCache = require('node-cache');
|
|
2
|
+
const Network = require('../network');
|
|
3
|
+
const Logger = require('../logger');
|
|
4
|
+
|
|
5
|
+
const logger = Logger();
|
|
6
|
+
require('dotenv').config();
|
|
7
|
+
|
|
8
|
+
const fiveMinutes = 60 * 5;
|
|
9
|
+
|
|
10
|
+
module.exports = class SettingsManager {
|
|
11
|
+
constructor({ serviceUrl, ttl = fiveMinutes } = {}) {
|
|
12
|
+
const localServiceUrl = serviceUrl || process.env.SETTING_MS_SERVICE_HOST;
|
|
13
|
+
|
|
14
|
+
this.ttl = ttl;
|
|
15
|
+
this.settingsCache = new NodeCache();
|
|
16
|
+
this.network = new Network({
|
|
17
|
+
serviceUrl: localServiceUrl,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async get(key, defaultValue, labels = []) {
|
|
22
|
+
if (!defaultValue) {
|
|
23
|
+
throw new Error('Can\'t get a key without defaultValue');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const cacheKey = `${key}_${JSON.stringify(labels)}`;
|
|
27
|
+
const cacheValue = this.settingsCache.get(cacheKey);
|
|
28
|
+
if (cacheValue !== undefined) {
|
|
29
|
+
return cacheValue;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let networkValue;
|
|
33
|
+
try {
|
|
34
|
+
const networkReponse = await this.network.get(`/api/v1/settings/get-setting/${key}`, {
|
|
35
|
+
params: {
|
|
36
|
+
labels,
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
networkValue = networkReponse.data.value;
|
|
40
|
+
} catch (error) {
|
|
41
|
+
logger.error('Cant get setting from network', error);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const returnValue = networkValue || defaultValue;
|
|
45
|
+
this.settingsCache.set(cacheKey, returnValue, this.ttl);
|
|
46
|
+
return returnValue;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
flush() {
|
|
50
|
+
return this.settingsCache.flushAll();
|
|
51
|
+
}
|
|
52
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
const nock = require('nock');
|
|
2
|
+
const Settings = require('./index');
|
|
3
|
+
|
|
4
|
+
const serviceUrl = 'http://localhost:8085';
|
|
5
|
+
|
|
6
|
+
const mockSetting = (key, value, labels, response = 200) => nock(serviceUrl)
|
|
7
|
+
.get(`/api/v1/setting/get-setting/${key}`)
|
|
8
|
+
.query(labels ? { labels } : undefined)
|
|
9
|
+
.reply(response, { value });
|
|
10
|
+
|
|
11
|
+
describe('Settings', () => {
|
|
12
|
+
it('Throws exeption if there is no defualt value', async () => {
|
|
13
|
+
const settings = new Settings({
|
|
14
|
+
serviceUrl,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
expect(settings.get('key1'))
|
|
18
|
+
.rejects.toEqual(new Error('Can\'t get a key without defaultValue'));
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('Uses env.SETTING_MS_SERVICE_HOST as serivce host if no one defind', async () => {
|
|
22
|
+
const m = mockSetting('key1', 'value1');
|
|
23
|
+
process.env.SETTING_MS_SERVICE_HOST = serviceUrl;
|
|
24
|
+
const settings = new Settings();
|
|
25
|
+
|
|
26
|
+
const value = await settings.get('key1', 'dv');
|
|
27
|
+
expect(value).toEqual('value1');
|
|
28
|
+
expect(m.isDone()).toBeTruthy();
|
|
29
|
+
process.env.SETTING_MS_SERVICE_HOST = '';
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('Get a key from server', async () => {
|
|
33
|
+
const m = mockSetting('key1', 'value1');
|
|
34
|
+
const settings = new Settings({
|
|
35
|
+
serviceUrl,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const value = await settings.get('key1', 'dv');
|
|
39
|
+
expect(value).toEqual('value1');
|
|
40
|
+
expect(m.isDone()).toBeTruthy();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('Cache from server', async () => {
|
|
44
|
+
const m = mockSetting('key1', 'value1');
|
|
45
|
+
const settings = new Settings({
|
|
46
|
+
serviceUrl,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const value = await settings.get('key1', 'dv');
|
|
50
|
+
expect(value).toEqual('value1');
|
|
51
|
+
expect(m.isDone()).toBeTruthy();
|
|
52
|
+
|
|
53
|
+
const value2 = await settings.get('key1', 'dv');
|
|
54
|
+
expect(value2).toEqual('value1');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('Finds with labels', async () => {
|
|
58
|
+
const m = mockSetting('key1', 'value1', [{ fleetId: 'uuid' }]);
|
|
59
|
+
const settings = new Settings({
|
|
60
|
+
serviceUrl,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const value = await settings.get('key1', 'dv', [{ fleetId: 'uuid' }]);
|
|
64
|
+
expect(value).toEqual('value1');
|
|
65
|
+
expect(m.isDone()).toBeTruthy();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('Returns default value when network error', async () => {
|
|
69
|
+
const settings = new Settings({
|
|
70
|
+
serviceUrl,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const value = await settings.get('key1', 'dv', [{ fleetId: 'uuid' }]);
|
|
74
|
+
expect(value).toEqual('dv');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('Values can be flushed', async () => {
|
|
78
|
+
const m1 = mockSetting('key1', 'value1');
|
|
79
|
+
const settings = new Settings({
|
|
80
|
+
serviceUrl,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const value = await settings.get('key1', 'dv');
|
|
84
|
+
expect(value).toEqual('value1');
|
|
85
|
+
expect(m1.isDone()).toBeTruthy();
|
|
86
|
+
|
|
87
|
+
settings.flush();
|
|
88
|
+
const m2 = mockSetting('key1', 'value2');
|
|
89
|
+
|
|
90
|
+
const value2 = await settings.get('key1', 'dv');
|
|
91
|
+
expect(value2).toEqual('value2');
|
|
92
|
+
expect(m2.isDone()).toBeTruthy();
|
|
93
|
+
});
|
|
94
|
+
});
|