@autofleet/settings 1.0.1 → 1.0.2
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 +32 -1
- package/dist/index.js +1 -1
- package/dist/index.test.d.ts +1 -4
- package/dist/index.test.js +18 -14
- package/package.json +3 -2
- package/src/index.test.ts +2 -1
- package/src/index.ts +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import NodeCache from 'node-cache';
|
|
3
|
+
import EventEmitter from 'events';
|
|
4
|
+
interface SettingsClassOptions {
|
|
5
|
+
serviceUrl?: string;
|
|
6
|
+
ttl?: number;
|
|
7
|
+
}
|
|
8
|
+
declare type SettingValue = string | boolean | number | never;
|
|
9
|
+
interface Label {
|
|
10
|
+
businessModelId?: string;
|
|
11
|
+
demandSourceId?: string;
|
|
12
|
+
fleetId?: string;
|
|
13
|
+
}
|
|
14
|
+
declare type LabelsArray = Label[];
|
|
15
|
+
interface GetSettingOption {
|
|
16
|
+
timeout?: number;
|
|
17
|
+
rejectOnFail?: boolean;
|
|
18
|
+
}
|
|
19
|
+
declare class SettingsManager {
|
|
20
|
+
ttl: number;
|
|
21
|
+
settingsCache: NodeCache;
|
|
22
|
+
networkEvents: EventEmitter;
|
|
23
|
+
network: any;
|
|
24
|
+
NEVER_DEFAULT_VALUE: string;
|
|
25
|
+
static get NEVER_DEFAULT_VALUE(): string;
|
|
26
|
+
static readNetworkValue(networkValue: SettingValue, defaultValue: SettingValue): string | number | boolean;
|
|
27
|
+
constructor({ serviceUrl, ttl }?: SettingsClassOptions);
|
|
28
|
+
get(key: SettingValue, defaultValue?: SettingValue, labels?: LabelsArray, { timeout, rejectOnFail }?: GetSettingOption): Promise<unknown>;
|
|
29
|
+
setLocal(key: string, labels: LabelsArray, value: SettingValue): void;
|
|
30
|
+
flush(): void;
|
|
31
|
+
}
|
|
32
|
+
export default SettingsManager;
|
package/dist/index.js
CHANGED
package/dist/index.test.d.ts
CHANGED
package/dist/index.test.js
CHANGED
|
@@ -8,8 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const index_1 = __importDefault(require("./index"));
|
|
11
16
|
const nock = require('nock');
|
|
12
|
-
const Settings = require('./index');
|
|
13
17
|
process.env.NODE_ENV = 'node-common-test';
|
|
14
18
|
const serviceUrl = 'localhost:8085';
|
|
15
19
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -21,20 +25,20 @@ const mockSetting = (key, value, labels = undefined, response = 200) => {
|
|
|
21
25
|
};
|
|
22
26
|
describe('Settings', () => {
|
|
23
27
|
it('Throws exeption if there is no defualt value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
-
const settings = new
|
|
28
|
+
const settings = new index_1.default({
|
|
25
29
|
serviceUrl: `http://${serviceUrl}/`,
|
|
26
30
|
});
|
|
27
31
|
expect(settings.get('key1'))
|
|
28
32
|
.rejects.toEqual(new Error('Can\'t get a key without defaultValue'));
|
|
29
33
|
}));
|
|
30
34
|
it('Can get 0 as default value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
31
|
-
const settings = new
|
|
35
|
+
const settings = new index_1.default({
|
|
32
36
|
serviceUrl: `http://${serviceUrl}/`,
|
|
33
37
|
});
|
|
34
38
|
expect(yield settings.get('key1', 0)).toBe(0);
|
|
35
39
|
}));
|
|
36
40
|
it('Can get false as default value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
37
|
-
const settings = new
|
|
41
|
+
const settings = new index_1.default({
|
|
38
42
|
serviceUrl: `http://${serviceUrl}/`,
|
|
39
43
|
});
|
|
40
44
|
expect(yield settings.get('key1', false)).toBe(false);
|
|
@@ -42,7 +46,7 @@ describe('Settings', () => {
|
|
|
42
46
|
it('Uses env.SETTING_MS_SERVICE_HOST as service host if no one defined', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
43
47
|
const m = mockSetting('key1', 'value1');
|
|
44
48
|
process.env.SETTING_MS_SERVICE_HOST = serviceUrl;
|
|
45
|
-
const settings = new
|
|
49
|
+
const settings = new index_1.default();
|
|
46
50
|
const value = yield settings.get('key1', 'dv');
|
|
47
51
|
expect(value).toEqual('value1');
|
|
48
52
|
expect(m.isDone()).toBeTruthy();
|
|
@@ -50,7 +54,7 @@ describe('Settings', () => {
|
|
|
50
54
|
}));
|
|
51
55
|
it('Get a key from server', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
52
56
|
const m = mockSetting('key1', 'value1');
|
|
53
|
-
const settings = new
|
|
57
|
+
const settings = new index_1.default({
|
|
54
58
|
serviceUrl: `http://${serviceUrl}/`,
|
|
55
59
|
});
|
|
56
60
|
const value = yield settings.get('key1', 'dv');
|
|
@@ -59,7 +63,7 @@ describe('Settings', () => {
|
|
|
59
63
|
}));
|
|
60
64
|
it('Cache from server', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
65
|
const m = mockSetting('key1', 'value1');
|
|
62
|
-
const settings = new
|
|
66
|
+
const settings = new index_1.default({
|
|
63
67
|
serviceUrl: `http://${serviceUrl}/`,
|
|
64
68
|
});
|
|
65
69
|
const value = yield settings.get('key1', 'dv');
|
|
@@ -70,7 +74,7 @@ describe('Settings', () => {
|
|
|
70
74
|
}));
|
|
71
75
|
it('Use only one request for similar keys', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
72
76
|
const m = mockSetting('key1EM', 'value1EM');
|
|
73
|
-
const settings = new
|
|
77
|
+
const settings = new index_1.default({
|
|
74
78
|
serviceUrl: `http://${serviceUrl}/`,
|
|
75
79
|
});
|
|
76
80
|
const [value, value2] = yield Promise.all([
|
|
@@ -84,7 +88,7 @@ describe('Settings', () => {
|
|
|
84
88
|
it('Finds with labels', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
85
89
|
const labels = [{ fleetId: 'uuid' }];
|
|
86
90
|
const m = mockSetting('key1', 'value1', labels);
|
|
87
|
-
const settings = new
|
|
91
|
+
const settings = new index_1.default({
|
|
88
92
|
serviceUrl: `http://${serviceUrl}/`,
|
|
89
93
|
});
|
|
90
94
|
const value = yield settings.get('key1', 'dv', labels);
|
|
@@ -92,14 +96,14 @@ describe('Settings', () => {
|
|
|
92
96
|
expect(m.isDone()).toBeTruthy();
|
|
93
97
|
}));
|
|
94
98
|
it('Returns default value when network error', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
95
|
-
const settings = new
|
|
99
|
+
const settings = new index_1.default({
|
|
96
100
|
serviceUrl: `http://${serviceUrl}/`,
|
|
97
101
|
});
|
|
98
102
|
const value = yield settings.get('key1', 'dv', [{ fleetId: 'uuid' }]);
|
|
99
103
|
expect(value).toEqual('dv');
|
|
100
104
|
}));
|
|
101
105
|
it('Throws an error if network error and no default value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
102
|
-
const settings = new
|
|
106
|
+
const settings = new index_1.default({
|
|
103
107
|
serviceUrl: `http://${serviceUrl}/`,
|
|
104
108
|
});
|
|
105
109
|
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE, [{ fleetId: 'uuid' }]);
|
|
@@ -107,7 +111,7 @@ describe('Settings', () => {
|
|
|
107
111
|
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
108
112
|
}));
|
|
109
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* () {
|
|
110
|
-
const settings = new
|
|
114
|
+
const settings = new index_1.default({
|
|
111
115
|
serviceUrl: `http://${serviceUrl}/`,
|
|
112
116
|
});
|
|
113
117
|
const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE);
|
|
@@ -122,7 +126,7 @@ describe('Settings', () => {
|
|
|
122
126
|
}));
|
|
123
127
|
it('Values can be flushed', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
124
128
|
const m1 = mockSetting('key1', 'value1');
|
|
125
|
-
const settings = new
|
|
129
|
+
const settings = new index_1.default({
|
|
126
130
|
serviceUrl: `http://${serviceUrl}/`,
|
|
127
131
|
});
|
|
128
132
|
settings.flush();
|
|
@@ -137,7 +141,7 @@ describe('Settings', () => {
|
|
|
137
141
|
}));
|
|
138
142
|
it('when NODE_ENV === test return the default value', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
139
143
|
process.env.NODE_ENV = 'test';
|
|
140
|
-
const settings = new
|
|
144
|
+
const settings = new index_1.default({
|
|
141
145
|
serviceUrl: `http://${serviceUrl}/`,
|
|
142
146
|
});
|
|
143
147
|
const value = yield settings.get('key1', 'dv');
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/settings",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"start": "ts-node src/index.ts",
|
|
8
8
|
"example": "ts-node src/example.ts",
|
|
9
|
-
"
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"prepublish": "npm run build",
|
|
10
11
|
"linter": "./node_modules/.bin/eslint .",
|
|
11
12
|
"test": "jest --forceExit",
|
|
12
13
|
"coverage": "jest --coverage --forceExit --runInBand && rm -rf ./coverage",
|
package/src/index.test.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -80,7 +80,7 @@ class SettingsManager {
|
|
|
80
80
|
|
|
81
81
|
async get(
|
|
82
82
|
key: SettingValue,
|
|
83
|
-
defaultValue
|
|
83
|
+
defaultValue?: SettingValue,
|
|
84
84
|
labels:LabelsArray = [],
|
|
85
85
|
{ timeout = 5000, rejectOnFail = false } : GetSettingOption = {},
|
|
86
86
|
) {
|
|
@@ -145,4 +145,4 @@ class SettingsManager {
|
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
|
|
148
|
+
export default SettingsManager;
|