@griddo/cx 1.75.152 → 1.75.153

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,7 +1,7 @@
1
1
  {
2
2
  "name": "@griddo/cx",
3
3
  "description": "Griddo SSG based on Gatsby",
4
- "version": "1.75.152",
4
+ "version": "1.75.153",
5
5
  "authors": [
6
6
  "Álvaro Sánchez' <alvaro.sanches@secuoyas.com>",
7
7
  "Carlos Torres <carlos.torres@secuoyas.com>",
@@ -66,7 +66,7 @@
66
66
  "react-helmet": "^6.0.0"
67
67
  },
68
68
  "devDependencies": {
69
- "@griddo/eslint-config-back": "^1.75.152",
69
+ "@griddo/eslint-config-back": "^1.75.153",
70
70
  "eslint": "^7.5.0",
71
71
  "eslint-plugin-node": "^11.1.0",
72
72
  "eslint-plugin-react": "7.14.3",
@@ -98,5 +98,5 @@
98
98
  "publishConfig": {
99
99
  "access": "public"
100
100
  },
101
- "gitHead": "3f755edf0c92d7825ce289e5f216bf6874077940"
101
+ "gitHead": "5545e6e239870a0bb469f04c346dfba2baf647cb"
102
102
  }
package/src/api/index.js CHANGED
@@ -1,10 +1,15 @@
1
- const axios = require("axios")
1
+ const axios = require('axios');
2
2
  const auth = require('../services/auth').default;
3
3
  const { saveCache, getCache } = require('../utils/cache');
4
+ const { delay } = require('../utils/delay');
4
5
 
5
- require("dotenv").config()
6
+ require('dotenv').config();
6
7
 
7
- const getApi = async (endpoint, body, cached = false) => {
8
+ const {
9
+ env: { RETRY_WAIT_SECONDS = 4, RETRY_ATTEMPTS = 1 },
10
+ } = process;
11
+
12
+ const getApi = async (endpoint, body, cached = false, attempt = 0) => {
8
13
  const cacheOptions = { endpoint, body, cached };
9
14
  if (cached) {
10
15
  const cachedResponse = getCache(cacheOptions);
@@ -13,18 +18,23 @@ const getApi = async (endpoint, body, cached = false) => {
13
18
  try {
14
19
  const { data } = await axios({
15
20
  url: endpoint,
16
- method: "get",
21
+ method: 'get',
17
22
  headers: { ...auth.headers },
18
23
  data: body,
19
24
  });
20
25
  saveCache(cacheOptions, data);
21
26
  return data;
22
27
  } catch (e) {
23
- showApiError(e, { endpoint, body });
28
+ showApiError(e, { endpoint, body, attempt });
29
+ if (attempt < parseInt(RETRY_ATTEMPTS)) {
30
+ console.log('WAITING FOR RETRY: GET', endpoint);
31
+ await delay(parseInt(RETRY_WAIT_SECONDS) * 1000);
32
+ return getApi(endpoint, body, cached, attempt + 1);
33
+ }
24
34
  }
25
35
  };
26
36
 
27
- const putApi = async (endpoint, body, cached = false) => {
37
+ const putApi = async (endpoint, body, cached = false, attempt = 0) => {
28
38
  const cacheOptions = { endpoint, body, cached };
29
39
  if (cached) {
30
40
  const cachedResponse = getCache(cacheOptions);
@@ -33,18 +43,23 @@ const putApi = async (endpoint, body, cached = false) => {
33
43
  try {
34
44
  const { data } = await axios({
35
45
  url: endpoint,
36
- method: "put",
46
+ method: 'put',
37
47
  headers: { ...auth.headers },
38
48
  data: body,
39
49
  });
40
50
  saveCache(cacheOptions, data);
41
51
  return data;
42
52
  } catch (e) {
43
- showApiError(e, { endpoint, body });
53
+ showApiError(e, { endpoint, body, attempt });
54
+ if (attempt < parseInt(RETRY_ATTEMPTS)) {
55
+ console.log('WAITING FOR RETRY: PUT', endpoint);
56
+ await delay(parseInt(RETRY_WAIT_SECONDS) * 1000);
57
+ return putApi(endpoint, body, cached, attempt + 1);
58
+ }
44
59
  }
45
60
  };
46
61
 
47
- const postApi = async (endpoint, body, headers, cached) => {
62
+ const postApi = async (endpoint, body, headers, cached, attempt = 0) => {
48
63
  const cacheOptions = { endpoint, body, headers, cached };
49
64
  if (cached) {
50
65
  const cachedResponse = getCache(cacheOptions);
@@ -53,36 +68,54 @@ const postApi = async (endpoint, body, headers, cached) => {
53
68
  try {
54
69
  const { data } = await axios({
55
70
  url: endpoint,
56
- method: "post",
71
+ method: 'post',
57
72
  headers: { ...headers, ...auth.headers },
58
73
  data: body,
59
74
  });
60
75
  saveCache(cacheOptions, data);
61
76
  return data;
62
77
  } catch (e) {
63
- showApiError(e, { endpoint, body, headers });
78
+ showApiError(e, { endpoint, body, headers, attempt });
79
+ if (attempt < parseInt(RETRY_ATTEMPTS)) {
80
+ console.log('WAITING FOR RETRY: POST', endpoint);
81
+ await delay(parseInt(RETRY_WAIT_SECONDS) * 1000);
82
+ return postApi(endpoint, body, headers, cached, attempt + 1);
83
+ }
64
84
  }
65
85
  };
66
86
 
67
87
  const showApiError = (e, callInfo = {}) => {
68
- const {
69
- response,
70
- message,
71
- stack,
72
- } = e;
73
- const {
74
- status,
75
- statusText,
76
- data,
77
- } = response || {};
88
+ const { response, message, stack } = e;
89
+ const { status, statusText, data } = response || {};
78
90
  const callInfoArray = [];
79
91
  for (const item of Object.keys(callInfo)) {
80
- callInfoArray.push(`- ${item}: ${typeof (callInfo[item]) === 'object' ? JSON.stringify(callInfo[item]) : callInfo[item]}`);
92
+ callInfoArray.push(
93
+ `- ${item}: ${
94
+ typeof callInfo[item] === 'object'
95
+ ? JSON.stringify(callInfo[item])
96
+ : callInfo[item]
97
+ }`
98
+ );
81
99
  }
82
- console.error(`===========================\n*** CALL INFO ***\n${callInfoArray.join('\n')}\n\n${response ? `*** API RESPONSE ***\nCode: ${status} - ${statusText}\nResponse: ${JSON.stringify(data)}\n\n` : ''}*** ERROR DETAILS***\n${message}\n${stack}\n===========================`);
83
- process.exit(1);
100
+ console.error(
101
+ `===========================\n*** CALL INFO ***\n${callInfoArray.join(
102
+ '\n'
103
+ )}\n\n${
104
+ response
105
+ ? `*** API RESPONSE ***\nCode: ${status} - ${statusText}\nResponse: ${JSON.stringify(
106
+ data
107
+ )}\n\n`
108
+ : ''
109
+ }*** ERROR DETAILS***\n${message}\n${stack}\n===========================`
110
+ );
111
+
112
+ if (
113
+ typeof callInfo?.attempt !== 'number' ||
114
+ callInfo.attempt >= parseInt(RETRY_ATTEMPTS)
115
+ )
116
+ process.exit(1);
84
117
  };
85
118
 
86
119
  exports.get = getApi;
87
120
  exports.put = putApi;
88
- exports.post = postApi;
121
+ exports.post = postApi;
@@ -0,0 +1,5 @@
1
+ const delay = ms => new Promise(res => setTimeout(res, ms));
2
+
3
+ module.exports = {
4
+ delay,
5
+ };