@autofleet/node-common 1.1.11 → 1.1.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/node-common",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "coverage": "jest --coverage --forceExit --runInBand",
@@ -23,6 +23,7 @@
23
23
  "homepage": "https://gitlab.com/AutoFleet/node-common#README",
24
24
  "dependencies": {
25
25
  "axios": "^0.18.0",
26
+ "express": "^4.16.2",
26
27
  "axios-retry": "^3.1.0",
27
28
  "dotenv": "^5.0.1",
28
29
  "jest": "^22.4.3",
@@ -0,0 +1,41 @@
1
+ const { Router } = require('express');
2
+ const logger = require('../logger')();
3
+
4
+ const METHODS = [
5
+ 'all',
6
+ 'get',
7
+ 'post',
8
+ 'put',
9
+ 'delete',
10
+ 'patch',
11
+ 'options',
12
+ 'head',
13
+ ];
14
+
15
+ const AfEntryPoint = func => async (req, res, next) => {
16
+ try {
17
+ await func(req, res, next);
18
+ } catch (e) {
19
+ logger.error(e.message);
20
+ res.status(e.statusCode || 500).json({ error: e.message, status: 'ERROR' });
21
+ }
22
+ };
23
+
24
+ const AfRouter = (options) => {
25
+ const myRouter = Router({ mergeParams: true, ...options });
26
+ METHODS.map((method) => {
27
+ const internalMethod = myRouter[method].bind(myRouter);
28
+ myRouter[method] = (...args) => {
29
+ internalMethod(...args.map((arg, index) => {
30
+ if (index === 0) {
31
+ return arg;
32
+ }
33
+ return AfEntryPoint(arg);
34
+ }));
35
+ };
36
+ return true;
37
+ });
38
+ return myRouter;
39
+ };
40
+
41
+ module.exports = AfRouter;
package/settings/index.js CHANGED
@@ -23,7 +23,7 @@ module.exports = class SettingsManager {
23
23
  }
24
24
 
25
25
  async get(key, defaultValue, labels = []) {
26
- if (!defaultValue) {
26
+ if (typeof defaultValue === 'undefined') {
27
27
  throw new Error('Can\'t get a key without defaultValue');
28
28
  }
29
29
 
@@ -18,6 +18,22 @@ describe('Settings', () => {
18
18
  .rejects.toEqual(new Error('Can\'t get a key without defaultValue'));
19
19
  });
20
20
 
21
+ it('Can get 0 as default value', async () => {
22
+ const settings = new Settings({
23
+ serviceUrl: `http://${serviceUrl}/`,
24
+ });
25
+
26
+ expect(await settings.get('key1', 0)).toBe(0);
27
+ });
28
+
29
+ it('Can get false as default value', async () => {
30
+ const settings = new Settings({
31
+ serviceUrl: `http://${serviceUrl}/`,
32
+ });
33
+
34
+ expect(await settings.get('key1', false)).toBe(false);
35
+ });
36
+
21
37
  it('Uses env.SETTING_MS_SERVICE_HOST as serivce host if no one defind', async () => {
22
38
  const m = mockSetting('key1', 'value1');
23
39
  process.env.SETTING_MS_SERVICE_HOST = serviceUrl;