@autofleet/settings 1.2.0 → 1.2.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 CHANGED
@@ -4,13 +4,19 @@ import EventEmitter from 'events';
4
4
  interface SettingsClassOptions {
5
5
  serviceUrl?: string;
6
6
  ttl?: number;
7
+ disableTestEnvCheck?: boolean;
7
8
  }
8
9
  declare type SettingValue = string | boolean | number | any[] | never;
10
+ interface Location {
11
+ lat: number;
12
+ lng: number;
13
+ }
9
14
  interface Label {
10
15
  businessModelId?: string;
11
16
  demandSourceId?: string;
12
17
  fleetId?: string;
13
18
  time?: string | Date;
19
+ location?: Location;
14
20
  }
15
21
  declare type LabelsArray = Label[];
16
22
  interface GetSettingOption {
@@ -23,9 +29,10 @@ declare class SettingsManager {
23
29
  networkEvents: EventEmitter;
24
30
  network: any;
25
31
  NEVER_DEFAULT_VALUE: string;
32
+ disableTestEnvCheck: boolean;
26
33
  static get NEVER_DEFAULT_VALUE(): string;
27
34
  static readNetworkValue(networkValue: SettingValue, defaultValue: SettingValue): SettingValue;
28
- constructor({ serviceUrl, ttl }?: SettingsClassOptions);
35
+ constructor({ serviceUrl, ttl, disableTestEnvCheck }?: SettingsClassOptions);
29
36
  get(key: SettingValue, defaultValue?: any, labels?: LabelsArray, { timeout, rejectOnFail }?: GetSettingOption): Promise<any>;
30
37
  getMultiple(settingsToGet: {
31
38
  key: SettingValue;
package/dist/index.js CHANGED
@@ -22,6 +22,7 @@ const node_cache_1 = __importDefault(require("node-cache"));
22
22
  const events_1 = __importDefault(require("events"));
23
23
  const util_1 = __importDefault(require("util"));
24
24
  const moment_1 = __importDefault(require("moment"));
25
+ const h3_js_1 = __importDefault(require("h3-js"));
25
26
  dotenv_1.default.config();
26
27
  const nextTick = util_1.default.promisify(process.nextTick);
27
28
  const logger = logger_1.default();
@@ -33,10 +34,11 @@ const findUrl = (serviceUrl) => serviceUrl
33
34
  class CannotGetNetworkValueError extends Error {
34
35
  }
35
36
  class SettingsManager {
36
- constructor({ serviceUrl, ttl = fiveMinutes } = {}) {
37
+ constructor({ serviceUrl, ttl = fiveMinutes, disableTestEnvCheck = false } = {}) {
37
38
  const localServiceUrl = findUrl(serviceUrl);
38
39
  this.NEVER_DEFAULT_VALUE = SettingsManager.NEVER_DEFAULT_VALUE;
39
40
  this.ttl = ttl;
41
+ this.disableTestEnvCheck = disableTestEnvCheck;
40
42
  this.settingsCache = new node_cache_1.default();
41
43
  this.networkEvents = new events_1.default();
42
44
  this.network = new network_1.default({
@@ -74,7 +76,7 @@ class SettingsManager {
74
76
  }
75
77
  return cacheValue;
76
78
  }
77
- if (process.env.NODE_ENV === 'test') {
79
+ if (!this.disableTestEnvCheck && process.env.NODE_ENV === 'test') {
78
80
  return defaultValue;
79
81
  }
80
82
  let networkValue;
@@ -177,6 +179,10 @@ class SettingsManager {
177
179
  if (l.time) {
178
180
  return Object.assign(Object.assign({}, l), { time: moment_1.default(l.time).startOf('hour').format(), day: moment_1.default(l.time).day() });
179
181
  }
182
+ if (l.location && l.location.lat && l.location.lng) {
183
+ const h3Index = h3_js_1.default.geoToH3(l.location.lat, l.location.lng, 8);
184
+ return Object.assign(Object.assign({}, l), { location: h3Index });
185
+ }
180
186
  return l;
181
187
  });
182
188
  const cacheKey = `${key}_${JSON.stringify(newLabels)}`;
@@ -117,6 +117,31 @@ describe('Settings', () => {
117
117
  expect(value).toEqual('value1');
118
118
  expect(m.isDone()).toBeTruthy();
119
119
  }));
120
+ it('Finds with location label', () => __awaiter(void 0, void 0, void 0, function* () {
121
+ const location = { lat: 32.07917469952991, lng: 34.777393341064446 };
122
+ const labels = [{ fleetId: 'uuid', location }];
123
+ const m = mockSetting('key1', 'value1', labels);
124
+ const settings = new index_1.default({
125
+ serviceUrl: `http://${serviceUrl}/`,
126
+ });
127
+ const settingLabel = [{ fleetId: 'uuid', location }];
128
+ const value = yield settings.get('key1', 'dv', settingLabel);
129
+ expect(value).toEqual('value1');
130
+ expect(m.isDone()).toBeTruthy();
131
+ }));
132
+ it('Finds with location and time label', () => __awaiter(void 0, void 0, void 0, function* () {
133
+ const time = new Date();
134
+ const location = { lat: 32.07917469952991, lng: 34.777393341064446 };
135
+ const labels = [{ fleetId: 'uuid', location, time }];
136
+ const m = mockSetting('key1', 'value1', labels);
137
+ const settings = new index_1.default({
138
+ serviceUrl: `http://${serviceUrl}/`,
139
+ });
140
+ const settingLabel = [{ fleetId: 'uuid', location, time }];
141
+ const value = yield settings.get('key1', 'dv', settingLabel);
142
+ expect(value).toEqual('value1');
143
+ expect(m.isDone()).toBeTruthy();
144
+ }));
120
145
  it('Returns default value when network error', () => __awaiter(void 0, void 0, void 0, function* () {
121
146
  const settings = new index_1.default({
122
147
  serviceUrl: `http://${serviceUrl}/`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/settings",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -19,6 +19,7 @@
19
19
  "@types/jest": "^22.0.0",
20
20
  "bluebird": "^3.7.2",
21
21
  "dotenv": "^8.2.0",
22
+ "h3-js": "^3.7.2",
22
23
  "jest": "^22.4.4",
23
24
  "moment": "^2.29.1",
24
25
  "nock": "^10.0.2",
package/src/index.test.ts CHANGED
@@ -131,6 +131,35 @@ describe('Settings', () => {
131
131
  expect(m.isDone()).toBeTruthy();
132
132
  });
133
133
 
134
+ it('Finds with location label', async () => {
135
+ const location = { lat: 32.07917469952991, lng: 34.777393341064446 };
136
+ const labels = [{ fleetId: 'uuid', location }];
137
+ const m = mockSetting('key1', 'value1', labels);
138
+ const settings = new Settings({
139
+ serviceUrl: `http://${serviceUrl}/`,
140
+ });
141
+
142
+ const settingLabel = [{ fleetId: 'uuid', location }];
143
+ const value = await settings.get('key1', 'dv', settingLabel);
144
+ expect(value).toEqual('value1');
145
+ expect(m.isDone()).toBeTruthy();
146
+ });
147
+
148
+ it('Finds with location and time label', async () => {
149
+ const time = new Date();
150
+ const location = { lat: 32.07917469952991, lng: 34.777393341064446 };
151
+ const labels = [{ fleetId: 'uuid', location, time }];
152
+ const m = mockSetting('key1', 'value1', labels);
153
+ const settings = new Settings({
154
+ serviceUrl: `http://${serviceUrl}/`,
155
+ });
156
+
157
+ const settingLabel = [{ fleetId: 'uuid', location, time }];
158
+ const value = await settings.get('key1', 'dv', settingLabel);
159
+ expect(value).toEqual('value1');
160
+ expect(m.isDone()).toBeTruthy();
161
+ });
162
+
134
163
  it('Returns default value when network error', async () => {
135
164
  const settings = new Settings({
136
165
  serviceUrl: `http://${serviceUrl}/`,
package/src/index.ts CHANGED
@@ -8,6 +8,7 @@ import NodeCache from 'node-cache';
8
8
  import EventEmitter from 'events';
9
9
  import util from 'util';
10
10
  import moment from 'moment';
11
+ import h3 from 'h3-js';
11
12
 
12
13
  dotenv.config();
13
14
 
@@ -24,15 +25,21 @@ const findUrl = (serviceUrl?: string) => serviceUrl
24
25
  interface SettingsClassOptions {
25
26
  serviceUrl?: string;
26
27
  ttl?: number;
28
+ disableTestEnvCheck?: boolean;
27
29
  }
28
30
 
29
31
  type SettingValue = string | boolean | number | any[] | never;
30
32
 
33
+ interface Location {
34
+ lat: number;
35
+ lng: number;
36
+ }
31
37
  interface Label {
32
38
  businessModelId?: string;
33
39
  demandSourceId?: string;
34
40
  fleetId?: string;
35
41
  time?: string | Date;
42
+ location?: Location;
36
43
  }
37
44
 
38
45
  type LabelsArray = Label[];
@@ -56,6 +63,8 @@ class SettingsManager {
56
63
 
57
64
  NEVER_DEFAULT_VALUE: string;
58
65
 
66
+ disableTestEnvCheck: boolean;
67
+
59
68
  static get NEVER_DEFAULT_VALUE() {
60
69
  return 'NEVER_DEFAULT_VALUE';
61
70
  }
@@ -70,11 +79,12 @@ class SettingsManager {
70
79
  return returnValue;
71
80
  }
72
81
 
73
- constructor({ serviceUrl, ttl = fiveMinutes }:SettingsClassOptions = {}) {
82
+ constructor({ serviceUrl, ttl = fiveMinutes, disableTestEnvCheck = false }:SettingsClassOptions = {}) {
74
83
  const localServiceUrl = findUrl(serviceUrl);
75
84
 
76
85
  this.NEVER_DEFAULT_VALUE = SettingsManager.NEVER_DEFAULT_VALUE;
77
86
  this.ttl = ttl;
87
+ this.disableTestEnvCheck = disableTestEnvCheck;
78
88
  this.settingsCache = new NodeCache();
79
89
  this.networkEvents = new EventEmitter();
80
90
  this.network = new Network({
@@ -109,7 +119,7 @@ class SettingsManager {
109
119
  return cacheValue;
110
120
  }
111
121
 
112
- if (process.env.NODE_ENV === 'test') {
122
+ if (!this.disableTestEnvCheck && process.env.NODE_ENV === 'test') {
113
123
  return defaultValue;
114
124
  }
115
125
 
@@ -226,6 +236,15 @@ class SettingsManager {
226
236
  day: moment(l.time).day(),
227
237
  };
228
238
  }
239
+
240
+ if (l.location && l.location.lat && l.location.lng) {
241
+ const h3Index = h3.geoToH3(l.location.lat, l.location.lng, 8);
242
+ return {
243
+ ...l,
244
+ location: h3Index,
245
+ };
246
+ }
247
+
229
248
  return l;
230
249
  });
231
250