@autofleet/settings 1.4.4 → 1.4.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.d.ts +2 -2
- package/dist/index.js +12 -12
- package/dist/index.test.js +9 -7
- package/package.json +11 -8
- package/src/index.test.ts +7 -6
- package/tsconfig.json +2 -1
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ interface SettingsClassOptions {
|
|
|
6
6
|
ttl?: number;
|
|
7
7
|
disableTestEnvCheck?: boolean;
|
|
8
8
|
}
|
|
9
|
-
|
|
9
|
+
type SettingValue = string | boolean | number | any[] | any;
|
|
10
10
|
interface Location {
|
|
11
11
|
lat: number;
|
|
12
12
|
lng: number;
|
|
@@ -19,7 +19,7 @@ interface Label {
|
|
|
19
19
|
time?: string | Date;
|
|
20
20
|
location?: Location;
|
|
21
21
|
}
|
|
22
|
-
|
|
22
|
+
type LabelsArray = Label[];
|
|
23
23
|
interface GetSettingOption {
|
|
24
24
|
timeout?: number;
|
|
25
25
|
rejectOnFail?: boolean;
|
package/dist/index.js
CHANGED
|
@@ -25,7 +25,7 @@ const moment_1 = __importDefault(require("moment"));
|
|
|
25
25
|
const h3_js_1 = __importDefault(require("h3-js"));
|
|
26
26
|
dotenv_1.default.config();
|
|
27
27
|
const nextTick = util_1.default.promisify(process.nextTick);
|
|
28
|
-
const logger = logger_1.default();
|
|
28
|
+
const logger = (0, logger_1.default)();
|
|
29
29
|
const fiveMinutes = 60 * 5;
|
|
30
30
|
const waitingToNetwork = 'waitingToNetwork';
|
|
31
31
|
const findUrl = (serviceUrl) => serviceUrl
|
|
@@ -34,6 +34,16 @@ const findUrl = (serviceUrl) => serviceUrl
|
|
|
34
34
|
class CannotGetNetworkValueError extends Error {
|
|
35
35
|
}
|
|
36
36
|
class SettingsManager {
|
|
37
|
+
static get NEVER_DEFAULT_VALUE() {
|
|
38
|
+
return 'NEVER_DEFAULT_VALUE';
|
|
39
|
+
}
|
|
40
|
+
static readNetworkValue(networkValue, defaultValue) {
|
|
41
|
+
const returnValue = typeof networkValue !== 'undefined' ? networkValue : defaultValue;
|
|
42
|
+
if (returnValue === SettingsManager.NEVER_DEFAULT_VALUE) {
|
|
43
|
+
throw new Error('Cannot find value from network or cache, default value is set to never');
|
|
44
|
+
}
|
|
45
|
+
return returnValue;
|
|
46
|
+
}
|
|
37
47
|
constructor({ serviceUrl, ttl = fiveMinutes, disableTestEnvCheck = false } = {}) {
|
|
38
48
|
const localServiceUrl = findUrl(serviceUrl);
|
|
39
49
|
this.NEVER_DEFAULT_VALUE = SettingsManager.NEVER_DEFAULT_VALUE;
|
|
@@ -45,16 +55,6 @@ class SettingsManager {
|
|
|
45
55
|
serviceUrl: localServiceUrl,
|
|
46
56
|
});
|
|
47
57
|
}
|
|
48
|
-
static get NEVER_DEFAULT_VALUE() {
|
|
49
|
-
return 'NEVER_DEFAULT_VALUE';
|
|
50
|
-
}
|
|
51
|
-
static readNetworkValue(networkValue, defaultValue) {
|
|
52
|
-
const returnValue = typeof networkValue !== 'undefined' ? networkValue : defaultValue;
|
|
53
|
-
if (returnValue === SettingsManager.NEVER_DEFAULT_VALUE) {
|
|
54
|
-
throw new Error('Cannot find value from network or cache, default value is set to never');
|
|
55
|
-
}
|
|
56
|
-
return returnValue;
|
|
57
|
-
}
|
|
58
58
|
get(key, defaultValue, labels = [], { timeout = 5000, rejectOnFail = false } = {}) {
|
|
59
59
|
return __awaiter(this, void 0, void 0, function* () {
|
|
60
60
|
if (typeof defaultValue === 'undefined') {
|
|
@@ -178,7 +178,7 @@ class SettingsManager {
|
|
|
178
178
|
getCacheKey(key, labels) {
|
|
179
179
|
const newLabels = labels.map((l) => {
|
|
180
180
|
if (l.time) {
|
|
181
|
-
return Object.assign(Object.assign({}, l), { time: moment_1.default(l.time).startOf('hour').format(), day: moment_1.default(l.time).day() });
|
|
181
|
+
return Object.assign(Object.assign({}, l), { time: (0, moment_1.default)(l.time).startOf('hour').format(), day: (0, moment_1.default)(l.time).day() });
|
|
182
182
|
}
|
|
183
183
|
if (l.location && l.location.lat && l.location.lng) {
|
|
184
184
|
const h3Index = h3_js_1.default.latLngToCell(l.location.lat, l.location.lng, 8);
|
package/dist/index.test.js
CHANGED
|
@@ -12,19 +12,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
/* eslint-disable import/no-extraneous-dependencies */
|
|
16
|
+
const nock_1 = __importDefault(require("nock"));
|
|
15
17
|
const index_1 = __importDefault(require("./index"));
|
|
16
|
-
const nock = require('nock');
|
|
17
18
|
process.env.NODE_ENV = 'node-common-test';
|
|
18
19
|
const serviceUrl = 'localhost:8085';
|
|
19
20
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
21
|
const mockSetting = (key, value, labels = undefined, response = 200) => {
|
|
21
|
-
const n =
|
|
22
|
-
|
|
23
|
-
.query(
|
|
22
|
+
const n = (0, nock_1.default)(`http://${serviceUrl}`).get(`/api/v1/settings/get-setting/${key}`);
|
|
23
|
+
if (labels) {
|
|
24
|
+
n.query({ labels: labels.map((label) => JSON.stringify(label)) });
|
|
25
|
+
}
|
|
24
26
|
return n.reply(response, { value });
|
|
25
27
|
};
|
|
26
28
|
const mockMultipleSettings = (keys, values, response = 200) => {
|
|
27
|
-
const n =
|
|
29
|
+
const n = (0, nock_1.default)(`http://${serviceUrl}`)
|
|
28
30
|
.post('/api/v1/settings/get-settings/_multiple', {
|
|
29
31
|
keys,
|
|
30
32
|
labels: [],
|
|
@@ -157,7 +159,7 @@ describe('Settings', () => {
|
|
|
157
159
|
expect(f())
|
|
158
160
|
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
159
161
|
}));
|
|
160
|
-
it('Throws an error if network error and no default value and success the next time', (done) =>
|
|
162
|
+
it('Throws an error if network error and no default value and success the next time', (done) => {
|
|
161
163
|
const settings = new index_1.default({
|
|
162
164
|
serviceUrl: `http://${serviceUrl}/`,
|
|
163
165
|
});
|
|
@@ -170,7 +172,7 @@ describe('Settings', () => {
|
|
|
170
172
|
expect(m.isDone());
|
|
171
173
|
done();
|
|
172
174
|
}), 100);
|
|
173
|
-
})
|
|
175
|
+
});
|
|
174
176
|
it('Values can be flushed', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
175
177
|
const m1 = mockSetting('key1', 'value1');
|
|
176
178
|
const settings = new index_1.default({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/settings",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -13,26 +13,29 @@
|
|
|
13
13
|
"coverage": "jest --coverage --forceExit --runInBand && rm -rf ./coverage",
|
|
14
14
|
"dev": "nodemon"
|
|
15
15
|
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"@autofleet/logger": "*"
|
|
18
|
+
},
|
|
16
19
|
"dependencies": {
|
|
17
|
-
"@autofleet/logger": "^1.1.0",
|
|
18
20
|
"@autofleet/network": "^1.4.2",
|
|
19
|
-
"@types/jest": "^22.0.0",
|
|
20
|
-
"bluebird": "^3.7.2",
|
|
21
21
|
"dotenv": "^8.2.0",
|
|
22
22
|
"h3-js": "^4.1.0",
|
|
23
|
-
"jest": "^22.4.4",
|
|
24
23
|
"moment": "^2.29.1",
|
|
25
|
-
"nock": "^10.0.2",
|
|
26
24
|
"node-cache": "^5.1.2"
|
|
27
25
|
},
|
|
28
26
|
"devDependencies": {
|
|
27
|
+
"@autofleet/logger": "^4.0.6",
|
|
28
|
+
"@types/jest": "^29.5.12",
|
|
29
|
+
"@types/nock": "^10.0.3",
|
|
29
30
|
"@typescript-eslint/eslint-plugin": "^4.8.1",
|
|
30
31
|
"eslint": "^7.13.0",
|
|
31
32
|
"eslint-config-airbnb-typescript": "^12.0.0",
|
|
32
33
|
"eslint-plugin-import": "^2.22.1",
|
|
33
|
-
"
|
|
34
|
+
"jest": "^29.7.0",
|
|
35
|
+
"nock": "^10.0.2",
|
|
36
|
+
"ts-jest": "^29.2.3",
|
|
34
37
|
"ts-node": "^8.6.2",
|
|
35
|
-
"typescript": "^
|
|
38
|
+
"typescript": "^4.9.5"
|
|
36
39
|
},
|
|
37
40
|
"author": "",
|
|
38
41
|
"license": "ISC"
|
package/src/index.test.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
+
/* eslint-disable import/no-extraneous-dependencies */
|
|
2
|
+
import nock from 'nock';
|
|
1
3
|
import Settings from './index';
|
|
2
4
|
|
|
3
|
-
const nock = require('nock');
|
|
4
|
-
|
|
5
5
|
process.env.NODE_ENV = 'node-common-test';
|
|
6
6
|
|
|
7
7
|
const serviceUrl = 'localhost:8085';
|
|
8
8
|
|
|
9
9
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
10
|
const mockSetting = (key: string, value: string | boolean | number | never, labels: any = undefined, response = 200) => {
|
|
11
|
-
const n = nock(`http://${serviceUrl}`)
|
|
12
|
-
|
|
13
|
-
.query(
|
|
11
|
+
const n = nock(`http://${serviceUrl}`).get(`/api/v1/settings/get-setting/${key}`);
|
|
12
|
+
if (labels) {
|
|
13
|
+
n.query({ labels: labels.map((label: any) => JSON.stringify(label)) });
|
|
14
|
+
}
|
|
14
15
|
|
|
15
16
|
return n.reply(response, { value });
|
|
16
17
|
};
|
|
@@ -179,7 +180,7 @@ describe('Settings', () => {
|
|
|
179
180
|
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
180
181
|
});
|
|
181
182
|
|
|
182
|
-
it('Throws an error if network error and no default value and success the next time',
|
|
183
|
+
it('Throws an error if network error and no default value and success the next time', (done) => {
|
|
183
184
|
const settings = new Settings({
|
|
184
185
|
serviceUrl: `http://${serviceUrl}/`,
|
|
185
186
|
});
|