@autofleet/node-common 1.1.51 → 1.1.52

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.
Files changed (2) hide show
  1. package/network/index.js +16 -20
  2. package/package.json +1 -1
package/network/index.js CHANGED
@@ -11,7 +11,6 @@ require('dotenv').config();
11
11
  if (process.env.NODE_ENV === 'test') {
12
12
  axios.defaults.adapter = httpAdapter;
13
13
  }
14
-
15
14
  const HTTPMethods = [
16
15
  'get',
17
16
  'post',
@@ -21,36 +20,42 @@ const HTTPMethods = [
21
20
  'patch',
22
21
  'options',
23
22
  ];
24
-
25
23
  const defaultSettings = {
26
24
  timeout: 2500,
27
25
  headers: { 'X-AF-AUTH': 'ANYONE' },
28
26
  paramsSerializer: params => qs.stringify(params, { arrayFormat: 'brackets' }),
29
27
  };
30
-
31
- const getHttpRequestObject = (response) => {
28
+ const getHttpResponseObject = (response) => {
32
29
  const maxBodySize = 1000;
33
30
  const jsonBody = response.config.data ? JSON.stringify(response.config.data) : '';
31
+ const body = jsonBody.length > maxBodySize ? jsonBody.substring(0, maxBodySize) : response.config.data;
34
32
  return {
35
33
  status: response.status,
36
34
  requestUrl: response.config.url,
37
35
  query: response.config.params,
38
- body: jsonBody.length > maxBodySize ? jsonBody.substring(0, maxBodySize) : response.config.data,
36
+ body,
39
37
  requestMethod: response.config.method.toUpperCase(),
40
38
  };
41
39
  };
42
-
40
+ const getHttpRequestObject = (request) => {
41
+ const maxBodySize = 1000;
42
+ const jsonBody = request.data ? JSON.stringify(request.data) : '';
43
+ const body = jsonBody.length > maxBodySize ? jsonBody.substring(0, maxBodySize) : request.data;
44
+ return {
45
+ headers: request.headers,
46
+ query: request.params,
47
+ body,
48
+ };
49
+ }
43
50
  module.exports = class Network {
44
51
  constructor(settings = {}) {
45
52
  this.settings = Object.assign({}, defaultSettings, settings);
46
53
  this.createBaseUrl();
47
-
48
54
  this.axios = axios.create(this.settings);
49
55
  this.addRetry(settings);
50
56
  this.buildClassHttpMethods();
51
57
  this.addLogs();
52
58
  }
53
-
54
59
  addRetry(settings) {
55
60
  axiosRetry(this.axios, {
56
61
  retries: 0,
@@ -58,33 +63,25 @@ module.exports = class Network {
58
63
  ...settings,
59
64
  });
60
65
  }
61
-
62
66
  addLogs() {
63
67
  const logger = Logger();
64
68
  this.axios.interceptors.request.use((request) => {
65
- logger.info(`Start Request: [${request.method.toUpperCase()}] ${request.baseURL} ${request.url}`, {
66
- headers: request.headers,
67
- query: request.params,
68
- body: request.data,
69
- });
69
+ logger.info(`Start Request: [${request.method.toUpperCase()}] ${request.baseURL} ${request.url}`, getHttpRequestObject(request));
70
70
  return request;
71
71
  });
72
-
73
72
  this.axios.interceptors.response.use((response) => {
74
- logger.info(`Finish Request: [${response.config.method.toUpperCase()} ${response.config.url}`, getHttpRequestObject(response));
73
+ logger.info(`Finish Request: [${response.config.method.toUpperCase()} ${response.config.url}`, getHttpResponseObject(response));
75
74
  return response;
76
75
  }, (error) => {
77
- logger.error('Finish Request with error', { data: error.response ? error.response.data : undefined, ...getHttpRequestObject(error.response || error) });
76
+ logger.error('Finish Request with error', { data: error.response ? error.response.data : undefined, ...getHttpResponseObject(error.response || error) });
78
77
  // throw error;
79
78
  return error;
80
79
  });
81
80
  }
82
-
83
81
  createBaseUrl() {
84
82
  if (!this.settings.serviceUrl && !this.settings.serviceName) {
85
83
  throw new Error('At least one of the settings Missing serviceUrl or serviceName');
86
84
  }
87
-
88
85
  const { settings } = this;
89
86
  if (settings.serviceUrl) {
90
87
  settings.baseURL = settings.serviceUrl;
@@ -96,7 +93,6 @@ module.exports = class Network {
96
93
  }
97
94
  }
98
95
  }
99
-
100
96
  /**
101
97
  * Build class methods that wrap axios methods
102
98
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/node-common",
3
- "version": "1.1.51",
3
+ "version": "1.1.52",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "coverage": "jest --coverage --forceExit --runInBand",