@autofleet/settings 1.0.5-beta → 1.0.5-beta-1
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 +4 -1
- package/dist/index.js +18 -15
- package/package.json +1 -1
- package/src/index.ts +21 -16
package/dist/index.d.ts
CHANGED
|
@@ -26,7 +26,10 @@ 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(
|
|
29
|
+
getMultiple(settingsToGet: {
|
|
30
|
+
key: SettingValue;
|
|
31
|
+
defaultValue: SettingValue;
|
|
32
|
+
}[], labels: LabelsArray, { timeout, rejectOnFail }?: GetSettingOption): Promise<any>;
|
|
30
33
|
setLocal(key: string, labels: LabelsArray, value: SettingValue): void;
|
|
31
34
|
flush(): void;
|
|
32
35
|
}
|
package/dist/index.js
CHANGED
|
@@ -12,6 +12,8 @@ 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 consistent-return */
|
|
16
|
+
/* eslint-disable array-callback-return */
|
|
15
17
|
// eslint-disable-next-line max-classes-per-file
|
|
16
18
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
17
19
|
const network_1 = __importDefault(require("@autofleet/network"));
|
|
@@ -101,38 +103,39 @@ class SettingsManager {
|
|
|
101
103
|
return returnValue;
|
|
102
104
|
});
|
|
103
105
|
}
|
|
104
|
-
getMultiple(
|
|
106
|
+
getMultiple(settingsToGet, labels, { timeout = 5000, rejectOnFail = false } = {}) {
|
|
105
107
|
return __awaiter(this, void 0, void 0, function* () {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
108
|
+
settingsToGet.map((obj) => {
|
|
109
|
+
if (typeof obj.defaultValue === 'undefined') {
|
|
110
|
+
throw new Error(`Missing default value for key ${obj.key}`);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
109
113
|
if (process.env.NODE_ENV === 'test') {
|
|
110
|
-
return
|
|
114
|
+
return settingsToGet.map((obj) => obj.defaultValue);
|
|
111
115
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const cacheKey = `${key}_${JSON.stringify(labels)}`;
|
|
116
|
+
settingsToGet.map((obj) => __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
const cacheKey = `${obj.key}_${JSON.stringify(labels)}`;
|
|
115
118
|
const cacheValue = this.settingsCache.get(cacheKey);
|
|
116
119
|
if (cacheValue !== undefined) {
|
|
117
120
|
if (waitingToNetwork === cacheValue) {
|
|
118
121
|
yield nextTick();
|
|
119
122
|
return new Promise((resolve) => {
|
|
120
123
|
this.networkEvents.once(cacheKey, (networkValue) => {
|
|
121
|
-
resolve(SettingsManager.readNetworkValue(networkValue,
|
|
124
|
+
resolve(SettingsManager.readNetworkValue(networkValue, obj.defaultValue));
|
|
122
125
|
});
|
|
123
126
|
});
|
|
124
127
|
}
|
|
125
128
|
}
|
|
126
129
|
}));
|
|
127
|
-
|
|
128
|
-
const cacheKey = `${key}_${JSON.stringify(labels)}`;
|
|
130
|
+
settingsToGet.map((obj) => {
|
|
131
|
+
const cacheKey = `${obj.key}_${JSON.stringify(labels)}`;
|
|
129
132
|
this.settingsCache.set(cacheKey, waitingToNetwork, this.ttl);
|
|
130
|
-
})
|
|
133
|
+
});
|
|
131
134
|
let settings;
|
|
132
135
|
try {
|
|
133
136
|
const { data } = yield this.network.get('/api/v1/settings', {
|
|
134
137
|
params: {
|
|
135
|
-
keys,
|
|
138
|
+
keys: settingsToGet.map((obj) => obj.key),
|
|
136
139
|
labels,
|
|
137
140
|
},
|
|
138
141
|
timeout,
|
|
@@ -147,11 +150,11 @@ class SettingsManager {
|
|
|
147
150
|
}
|
|
148
151
|
// eslint-disable-next-line array-callback-return
|
|
149
152
|
settings.map((setting, index) => {
|
|
150
|
-
const cacheKey = `${
|
|
153
|
+
const cacheKey = `${settingsToGet[index].key}_${JSON.stringify(labels)}`;
|
|
151
154
|
this.networkEvents.emit(cacheKey, setting);
|
|
152
155
|
let returnValue;
|
|
153
156
|
try {
|
|
154
|
-
returnValue = SettingsManager.readNetworkValue(setting,
|
|
157
|
+
returnValue = SettingsManager.readNetworkValue(setting, settingsToGet[index].defaultValue);
|
|
155
158
|
}
|
|
156
159
|
catch (err) {
|
|
157
160
|
this.settingsCache.del(cacheKey);
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/* eslint-disable consistent-return */
|
|
2
|
+
/* eslint-disable array-callback-return */
|
|
1
3
|
// eslint-disable-next-line max-classes-per-file
|
|
2
4
|
import dotenv from 'dotenv';
|
|
3
5
|
import Network from '@autofleet/network';
|
|
@@ -136,37 +138,40 @@ class SettingsManager {
|
|
|
136
138
|
}
|
|
137
139
|
|
|
138
140
|
async getMultiple(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
141
|
+
settingsToGet: {
|
|
142
|
+
key: SettingValue,
|
|
143
|
+
defaultValue: SettingValue,
|
|
144
|
+
}[],
|
|
145
|
+
labels: LabelsArray,
|
|
142
146
|
{ timeout = 5000, rejectOnFail = false } : GetSettingOption = {},
|
|
143
147
|
): Promise<any> {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
148
|
+
settingsToGet.map((obj) => {
|
|
149
|
+
if (typeof obj.defaultValue === 'undefined') {
|
|
150
|
+
throw new Error(`Missing default value for key ${obj.key}`);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
147
153
|
|
|
148
154
|
if (process.env.NODE_ENV === 'test') {
|
|
149
|
-
return
|
|
155
|
+
return settingsToGet.map((obj) => obj.defaultValue);
|
|
150
156
|
}
|
|
151
157
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const cacheKey = `${key}_${JSON.stringify(labels)}`;
|
|
158
|
+
settingsToGet.map(async (obj) => {
|
|
159
|
+
const cacheKey = `${obj.key}_${JSON.stringify(labels)}`;
|
|
155
160
|
const cacheValue = this.settingsCache.get(cacheKey);
|
|
156
161
|
if (cacheValue !== undefined) {
|
|
157
162
|
if (waitingToNetwork === cacheValue) {
|
|
158
163
|
await nextTick();
|
|
159
164
|
return new Promise((resolve) => {
|
|
160
165
|
this.networkEvents.once(cacheKey, (networkValue) => {
|
|
161
|
-
resolve(SettingsManager.readNetworkValue(networkValue,
|
|
166
|
+
resolve(SettingsManager.readNetworkValue(networkValue, obj.defaultValue));
|
|
162
167
|
});
|
|
163
168
|
});
|
|
164
169
|
}
|
|
165
170
|
}
|
|
166
171
|
});
|
|
167
172
|
|
|
168
|
-
|
|
169
|
-
const cacheKey = `${key}_${JSON.stringify(labels)}`;
|
|
173
|
+
settingsToGet.map((obj) => {
|
|
174
|
+
const cacheKey = `${obj.key}_${JSON.stringify(labels)}`;
|
|
170
175
|
this.settingsCache.set(cacheKey, waitingToNetwork, this.ttl);
|
|
171
176
|
});
|
|
172
177
|
|
|
@@ -174,7 +179,7 @@ class SettingsManager {
|
|
|
174
179
|
try {
|
|
175
180
|
const { data } = await this.network.get('/api/v1/settings', {
|
|
176
181
|
params: {
|
|
177
|
-
keys,
|
|
182
|
+
keys: settingsToGet.map((obj) => obj.key),
|
|
178
183
|
labels,
|
|
179
184
|
},
|
|
180
185
|
timeout,
|
|
@@ -189,11 +194,11 @@ class SettingsManager {
|
|
|
189
194
|
|
|
190
195
|
// eslint-disable-next-line array-callback-return
|
|
191
196
|
settings.map((setting: any, index: number) => {
|
|
192
|
-
const cacheKey = `${
|
|
197
|
+
const cacheKey = `${settingsToGet[index].key}_${JSON.stringify(labels)}`;
|
|
193
198
|
this.networkEvents.emit(cacheKey, setting);
|
|
194
199
|
let returnValue;
|
|
195
200
|
try {
|
|
196
|
-
returnValue = SettingsManager.readNetworkValue(setting,
|
|
201
|
+
returnValue = SettingsManager.readNetworkValue(setting, settingsToGet[index].defaultValue);
|
|
197
202
|
} catch (err) {
|
|
198
203
|
this.settingsCache.del(cacheKey);
|
|
199
204
|
throw err;
|