@griddo/cx 1.75.185 → 1.75.187

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.185",
4
+ "version": "1.75.187",
5
5
  "authors": [
6
6
  "Álvaro Sánchez' <alvaro.sanches@secuoyas.com>",
7
7
  "Diego M. Béjar <diego.bejar@secuoyas.com>",
@@ -32,12 +32,12 @@
32
32
  "watch:tscheck": "tsc --noEmit --watch"
33
33
  },
34
34
  "dependencies": {
35
- "@babel/core": "^7.6.0",
35
+ "@babel/core": "^7.21.0",
36
36
  "@babel/plugin-proposal-class-properties": "^7.7.4",
37
37
  "@babel/preset-env": "^7.14.5",
38
38
  "@babel/preset-react": "^7.14.5",
39
39
  "@babel/preset-typescript": "^7.16.5",
40
- "@griddo/core": "^1.75.185",
40
+ "@griddo/core": "^1.75.187",
41
41
  "@svgr/webpack": "^5.5.0",
42
42
  "babel-loader": "^8.0.6",
43
43
  "babel-plugin-styled-components": "^1.10.7",
@@ -69,7 +69,7 @@
69
69
  "react-helmet": "^6.0.0"
70
70
  },
71
71
  "devDependencies": {
72
- "@griddo/eslint-config-back": "^1.75.185",
72
+ "@griddo/eslint-config-back": "^1.75.187",
73
73
  "@types/babel__core": "^7.20.0",
74
74
  "@types/babel__preset-env": "^7.9.2",
75
75
  "@types/csvtojson": "^2.0.0",
@@ -117,5 +117,5 @@
117
117
  "publishConfig": {
118
118
  "access": "public"
119
119
  },
120
- "gitHead": "56f210bb1f9aadf8827b33f0f759155531702526"
120
+ "gitHead": "7f7d7f9fc1368ba04ac5b5ec23af4ed2374901a9"
121
121
  }
package/src/types/api.ts CHANGED
@@ -107,7 +107,7 @@ export interface LanguagesResponse {
107
107
  }
108
108
 
109
109
  /** Describes common props for api responses. */
110
- interface APIRequest {
110
+ export interface APIRequest {
111
111
  /** The URL of the API endpoint. */
112
112
  endpoint: string;
113
113
  /** The parameters to be sent in the request body. */
@@ -116,10 +116,12 @@ interface APIRequest {
116
116
  cached?: unknown;
117
117
  /** Number of connection attempts (in case it fails on the first attempt). */
118
118
  attempt?: number;
119
+ /** Headers for the post api fetch */
120
+ headers?: any;
119
121
  }
120
122
 
121
123
  /** Type with the POST request properties. */
122
- export interface PostAPI extends APIRequest {
124
+ export interface PostAPI extends Omit<APIRequest, "headers"> {
123
125
  headers?: { lang?: number } & Record<string, unknown>;
124
126
  }
125
127
 
package/src/utils/api.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  // Types
2
2
  import type {
3
+ APIRequest,
3
4
  APIResponses,
4
5
  Error,
5
6
  GetAPI,
@@ -8,7 +9,7 @@ import type {
8
9
  } from "../types/api";
9
10
 
10
11
  // External libraries
11
- import axios from "axios";
12
+ import axios, { Method } from "axios";
12
13
  import chalk from "chalk";
13
14
  import dotenv from "dotenv";
14
15
 
@@ -27,63 +28,47 @@ const {
27
28
  } = process;
28
29
 
29
30
  /**
30
- * Make a GET request to the Griddo API.
31
+ * Make a GET/PUT/POST request to the Griddo API.
31
32
  *
32
33
  * @template T Response Type returned.
33
34
  * @returns {Promise<T>} A promise that is resolved with the data from the API response.
34
35
  * @todo Maybe remove the loggin responsability
35
36
  * @example
36
- * const response = await get<Site>({
37
+ * const response = await requestAPI<Site>({
37
38
  * endpoint: "...",
38
39
  * cached: true,
39
- * });
40
+ * }, { method:"get" });
40
41
  */
41
- async function getApi<T extends APIResponses>(props: GetAPI): Promise<T> {
42
- const { endpoint, body, cached, attempt } = props;
42
+ async function requestAPI<T extends APIResponses>(
43
+ props: APIRequest & { method: Method }
44
+ ): Promise<T> {
45
+ const { endpoint, body, cached, attempt, method, headers } = props;
43
46
  const cacheOptions = { endpoint, body, cached };
44
47
 
45
- // Start a timer
46
- const start = new Date();
47
-
48
- // Filesystem
48
+ // Cache
49
49
  if (cached) {
50
- const cachedResponse = getCache<T>(cacheOptions);
51
-
52
- if (cachedResponse) {
53
- const duration = msToSec(new Date().getTime() - start.getTime());
54
- const siteId = getSafeSiteId(cachedResponse);
55
- const siteIdMsg = siteId ? `site: ${siteId} ` : "";
50
+ const cacheData = getCache<T>(cacheOptions);
56
51
 
57
- logInfo(`GET (cache) ${siteIdMsg}${endpoint} - ${duration}s`);
58
-
59
- return cachedResponse;
52
+ if (cacheData) {
53
+ return cacheData;
60
54
  }
61
55
  }
62
56
 
63
- // Network API
57
+ // Network
64
58
  try {
65
- // Success. Connection stablished.
66
59
  const { data }: { data: T } = await axios({
67
60
  url: endpoint,
68
- method: "get",
69
- headers: { ...AuthService.headers },
61
+ method,
62
+ headers: { ...headers, ...AuthService.headers },
70
63
  data: body,
71
64
  });
72
65
 
73
- const duration = msToSec(new Date().getTime() - start.getTime());
74
- const siteId = getSafeSiteId(data);
75
- const siteIdMsg = siteId ? `site: ${siteId} ` : "";
76
-
77
- logInfo(`GET (fetch) ${siteIdMsg}${endpoint} - ${duration}s`);
78
-
79
- // Save page object into filesystem
80
66
  saveCache(cacheOptions, data);
81
67
 
82
- // Return page object from API
83
68
  return data;
84
69
  } catch (e) {
85
- // Failure. Network error
86
70
  const error = e as Error;
71
+
87
72
  showApiError(
88
73
  error,
89
74
  { endpoint, body, attempt },
@@ -95,168 +80,82 @@ async function getApi<T extends APIResponses>(props: GetAPI): Promise<T> {
95
80
  return null;
96
81
  }
97
82
 
98
- // Try again `RETRY_ATTEMPTS` times
99
83
  if (attempt && attempt < parseInt(RETRY_ATTEMPTS)) {
100
- console.warn("Waiting for retry: GET", endpoint);
84
+ console.warn(`Waiting for retry: ${method}`, endpoint);
85
+
101
86
  await delay(parseInt(RETRY_WAIT_SECONDS) * 1000);
102
87
 
103
- // Lets try again!
104
- return getApi({ endpoint, body, cached, attempt: attempt + 1 });
88
+ return requestAPI({
89
+ endpoint,
90
+ body,
91
+ cached,
92
+ method,
93
+ attempt: attempt + 1,
94
+ });
105
95
  } else {
106
- throw new Error("Error in getApi()");
96
+ throw new Error(`Error in ${method}`);
107
97
  }
108
98
  }
109
99
  }
110
100
 
111
- /**
112
- * Make a PUT request to the Griddo API.
113
- *
114
- * @template T Response Type returned.
115
- * @returns {Promise<T>} A promise that is resolved with the data from the API response.
116
- */
117
- async function putApi<T extends APIResponses>(props: PutAPI): Promise<T> {
118
- const { endpoint, body, cached = false, attempt = 0 } = props;
119
- const cacheOptions = { endpoint, body, cached };
120
-
121
- // Start a timer
101
+ async function getApi<T extends APIResponses>(props: GetAPI) {
102
+ // Starts
122
103
  const start = new Date();
104
+ const { endpoint } = props;
123
105
 
124
- // Filesystem
125
- if (cached) {
126
- const cachedResponse = getCache<T>(cacheOptions);
127
-
128
- if (cachedResponse) {
129
- const duration = msToSec(new Date().getTime() - start.getTime());
130
-
131
- logInfo(`PUT (cache) ${endpoint} - ${duration}s`);
132
-
133
- return cachedResponse;
134
- }
135
- }
106
+ // Fetching...
107
+ const response = await requestAPI<T>({ ...props, method: "get" });
136
108
 
137
- // Network API
138
- try {
139
- const { data }: { data: T } = await axios({
140
- url: endpoint,
141
- method: "put",
142
- headers: { ...AuthService.headers },
143
- data: body,
144
- });
109
+ // Ends
110
+ const cacheMsg = props.cached ? "(cached)" : "(fetch)";
111
+ const siteId = getSafeSiteId(response);
112
+ const siteIdMsg = siteId ? `site: ${siteId} ` : "";
113
+ const duration = msToSec(new Date().getTime() - start.getTime());
145
114
 
146
- const duration = msToSec(new Date().getTime() - start.getTime());
115
+ logInfo(`GET ${cacheMsg} ${siteIdMsg} ${endpoint} - ${duration}s`);
147
116
 
148
- logInfo(`PUT (fetch) ${endpoint} - ${duration}s`);
117
+ return response;
118
+ }
149
119
 
150
- // Save page object to filesystem
151
- saveCache(cacheOptions, data);
120
+ async function putApi<T extends APIResponses>(props: PutAPI) {
121
+ // Starts
122
+ const start = new Date();
123
+ const { endpoint } = props;
152
124
 
153
- // Return page object from API
154
- return data;
155
- } catch (e) {
156
- // Failure. Network error
157
- const error = e as Error;
158
- showApiError(
159
- error,
160
- { endpoint, body, attempt },
161
- error.response.status !== 404
162
- );
125
+ // Fetching...
126
+ const response = await requestAPI<T>({ ...props, method: "put" });
163
127
 
164
- if (error.response.status === 404) {
165
- // @ts-expect-error
166
- return null;
167
- }
128
+ // Ends
129
+ const cacheMsg = props.cached ? "(cached)" : "(fetch)";
130
+ const duration = msToSec(new Date().getTime() - start.getTime());
168
131
 
169
- // Try again `RETRY_ATTEMPTS` times
170
- if (attempt < parseInt(RETRY_ATTEMPTS)) {
171
- console.warn("Waiting for retry: PUT", endpoint);
172
- await delay(parseInt(RETRY_WAIT_SECONDS) * 1000);
132
+ logInfo(`PUT ${cacheMsg} ${endpoint} - ${duration}s`);
173
133
 
174
- // Lets try again!
175
- return putApi({ endpoint, body, cached, attempt: attempt + 1 });
176
- } else {
177
- throw new Error("Error in putApi()");
178
- }
179
- }
134
+ return response;
180
135
  }
181
136
 
182
- /**
183
- * Make a POST request to the Griddo API.
184
- *
185
- * @template T Response Type returned.
186
- * @returns {Promise<T>} A promise that is resolved with the data from the API response.
187
- */
188
- async function postApi<T extends APIResponses>(props: PostAPI): Promise<T> {
189
- const { endpoint, body, headers, cached, attempt = 0 } = props;
190
-
191
- const cacheOptions = { endpoint, body, headers, cached };
192
-
193
- // Start a timer
137
+ async function postApi<T extends APIResponses>(props: PostAPI) {
138
+ // Starts
194
139
  const start = new Date();
140
+ const { endpoint, body, headers } = props;
195
141
  const distributorBodyParams = endpoint.endsWith("/distributor")
196
142
  ? `# Distributor body: ${JSON.stringify(body)} - lang: ${JSON.stringify(
197
143
  headers?.lang
198
144
  )}`
199
145
  : "";
200
146
 
201
- // Filesystem
202
- if (cached) {
203
- const cachedResponse = getCache<T>(cacheOptions);
204
-
205
- if (cachedResponse) {
206
- const duration = msToSec(new Date().getTime() - start.getTime());
207
-
208
- logInfo(
209
- `POST (cache) ${endpoint} - ${duration}s ${distributorBodyParams}`
210
- );
211
-
212
- return cachedResponse;
213
- }
214
- }
215
-
216
- // Network API
217
- try {
218
- // Success. Connection stablished.
219
- const { data }: { data: T } = await axios({
220
- url: endpoint,
221
- method: "post",
222
- headers: { ...headers, ...AuthService.headers },
223
- data: body,
224
- });
147
+ // Fetching...
148
+ const response = await requestAPI<T>({ ...props, method: "post" });
225
149
 
226
- const duration = msToSec(new Date().getTime() - start.getTime());
150
+ // Ends
151
+ const cacheMsg = props.cached ? "(cached)" : "(fetch)";
152
+ const duration = msToSec(new Date().getTime() - start.getTime());
227
153
 
228
- logInfo(`POST (fetch) ${endpoint} - ${duration}s`);
229
-
230
- // Save page object to filesystem
231
- saveCache(cacheOptions, data);
232
-
233
- // Return page object from API
234
- return data;
235
- } catch (e) {
236
- // Failure. Network error
237
- const error = e as Error;
238
- showApiError(
239
- error,
240
- { endpoint, body, headers, attempt },
241
- error.response.status !== 404
242
- );
243
-
244
- if (error.response.status === 404) {
245
- // @ts-expect-error
246
- return null;
247
- }
248
-
249
- // Try again `RETRY_ATTEMPTS` times
250
- if (attempt < parseInt(RETRY_ATTEMPTS)) {
251
- console.warn("Waiting for retry: POST", endpoint);
252
- await delay(parseInt(RETRY_WAIT_SECONDS) * 1000);
154
+ logInfo(
155
+ `POST ${cacheMsg} ${endpoint} - ${duration}s ${distributorBodyParams}`
156
+ );
253
157
 
254
- // Lets try again!
255
- return postApi({ endpoint, body, headers, cached, attempt: attempt + 1 });
256
- } else {
257
- throw new Error("Error in postApi()");
258
- }
259
- }
158
+ return response;
260
159
  }
261
160
 
262
161
  /**