@autofleet/settings 1.4.3 → 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 CHANGED
@@ -6,7 +6,7 @@ interface SettingsClassOptions {
6
6
  ttl?: number;
7
7
  disableTestEnvCheck?: boolean;
8
8
  }
9
- declare type SettingValue = string | boolean | number | any[] | any;
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
- declare type LabelsArray = Label[];
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,10 +178,10 @@ 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
- const h3Index = h3_js_1.default.geoToH3(l.location.lat, l.location.lng, 8);
184
+ const h3Index = h3_js_1.default.latLngToCell(l.location.lat, l.location.lng, 8);
185
185
  return Object.assign(Object.assign({}, l), { location: h3Index });
186
186
  }
187
187
  return l;
@@ -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 = nock(`http://${serviceUrl}`)
22
- .get(`/api/v1/settings/get-setting/${key}`)
23
- .query(labels ? { labels: labels.map((label) => JSON.stringify(label)) } : undefined);
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 = nock(`http://${serviceUrl}`)
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) => __awaiter(void 0, void 0, void 0, function* () {
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",
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
- "h3-js": "^3.7.2",
23
- "jest": "^22.4.4",
22
+ "h3-js": "^4.1.0",
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
- "ts-jest": "^25.4.0",
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": "^3.9.6"
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
- .get(`/api/v1/settings/get-setting/${key}`)
13
- .query(labels ? { labels: labels.map((label: any) => JSON.stringify(label)) } : undefined);
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', async (done) => {
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
  });
package/src/index.ts CHANGED
@@ -240,7 +240,7 @@ class SettingsManager {
240
240
  }
241
241
 
242
242
  if (l.location && l.location.lat && l.location.lng) {
243
- const h3Index = h3.geoToH3(l.location.lat, l.location.lng, 8);
243
+ const h3Index = h3.latLngToCell(l.location.lat, l.location.lng, 8);
244
244
  return {
245
245
  ...l,
246
246
  location: h3Index,
package/tsconfig.json CHANGED
@@ -5,6 +5,7 @@
5
5
  "declaration": true,
6
6
  "outDir": "./dist",
7
7
  "strict": true,
8
- "esModuleInterop": true
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true,
9
10
  }
10
11
  }