@griddo/cx 1.75.235 → 1.75.237

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/gatsby-node.ts CHANGED
@@ -12,7 +12,7 @@ import {
12
12
  logInfo,
13
13
  logPageSize,
14
14
  msToSec,
15
- // sanitizeApiCache,
15
+ sanitizeApiCache,
16
16
  splash,
17
17
  } from "./src/utils/shared";
18
18
  import { generateBuildReport, generateSitemaps } from "./src/utils/sites";
@@ -22,7 +22,7 @@ dotenv.config();
22
22
  // Consts
23
23
  const PUBLIC_FOLDER = path.resolve(__dirname, "./public");
24
24
  const STORE_DIR_PATH = path.resolve(__dirname, "./store/");
25
- // const API_CACHE_FOLDER = path.resolve(__dirname, "./apiCache");
25
+ const API_CACHE_FOLDER = path.resolve(__dirname, "./apiCache");
26
26
  const REPORT_FILE = path.resolve(PUBLIC_FOLDER, "__build-report__.json");
27
27
 
28
28
  // -----------------------------------------------------------------------------
@@ -33,12 +33,11 @@ export const onPreInit = async () => {
33
33
  initCache();
34
34
  prepareStaticFolder();
35
35
 
36
- StoreService.init(STORE_DIR_PATH, { sanitize: true });
36
+ StoreService.init(STORE_DIR_PATH);
37
37
  await StoreService.createBuildSource();
38
38
 
39
39
  // Remove old cahe page files
40
- // TODO: Use disabled
41
- // sanitizeApiCache(API_CACHE_FOLDER);
40
+ sanitizeApiCache(API_CACHE_FOLDER);
42
41
 
43
42
  console.log("\n✨ onPreInit\n");
44
43
  };
@@ -56,7 +55,7 @@ export const createPages: GatsbyNode["createPages"] = async ({
56
55
  // We need to use a `for..of` to iterate over `pages` since it could be a
57
56
  // generator and for example a `forEach` doesn't work on it. Besides a
58
57
  // `for..of` is probably the best performance option.
59
- for (const page of pages) {
58
+ for await (const page of pages) {
60
59
  const startTime = performance.now();
61
60
 
62
61
  createPage(page);
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.235",
4
+ "version": "1.75.237",
5
5
  "authors": [
6
6
  "Álvaro Sánchez' <alvaro.sanches@secuoyas.com>",
7
7
  "Diego M. Béjar <diego.bejar@secuoyas.com>",
@@ -68,7 +68,7 @@
68
68
  "react-helmet": "^6.0.0"
69
69
  },
70
70
  "devDependencies": {
71
- "@griddo/eslint-config-back": "^1.75.235",
71
+ "@griddo/eslint-config-back": "^1.75.237",
72
72
  "@types/babel__core": "^7.20.0",
73
73
  "@types/babel__preset-env": "^7.9.2",
74
74
  "@types/csvtojson": "^2.0.0",
@@ -116,5 +116,5 @@
116
116
  "publishConfig": {
117
117
  "access": "public"
118
118
  },
119
- "gitHead": "3ea759bf34b590c02b27b8cf2b66ab29933ceb31"
119
+ "gitHead": "0849725480a8e13fc3bbeb6226078530016f022e"
120
120
  }
@@ -3,7 +3,7 @@
3
3
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
4
4
  /* eslint-disable node/shebang */
5
5
 
6
- import { execSync } from "child_process";
6
+ import { spawnSync } from "child_process";
7
7
  import fs from "fs";
8
8
  import path from "path";
9
9
 
@@ -13,6 +13,7 @@ import pkgDir from "pkg-dir";
13
13
 
14
14
  import { initCache } from "../src/utils/cache";
15
15
  import { findDomains } from "../src/utils/domains";
16
+ import { buildHealthCheck } from "../src/utils/health-checks";
16
17
  import { exporterLogo as splash, measureFunctions } from "../src/utils/shared";
17
18
 
18
19
  dotenv.config();
@@ -56,18 +57,29 @@ type Env = Record<string, unknown>;
56
57
  // -----------------------------------------------------------------------------
57
58
  const getEnvRunner = (env: Env) => (command: string) => runner(command, env);
58
59
 
59
- const runner = (command: string, env: Env = {}) =>
60
+ const runner = (command: string, env: Env) => {
60
61
  // TODO: Cuando esto falle, ejecutar `cleanAfterFail`
61
- execSync(command, {
62
+
63
+ const [commandName, ...args] = command.split(" ");
64
+ const { error, status } = spawnSync(commandName, args, {
62
65
  cwd: workingPath,
63
- stdio: "inherit",
64
- env: {
65
- ...process.env,
66
- ...env,
67
- GRIDDO_EXPORTER: "true",
68
- },
66
+ stdio: ["ignore", "inherit", "ignore"],
67
+ encoding: "utf8",
68
+ shell: true,
69
+ env: Object.assign({ GRIDDO_EXPORTER: "true" }, process.env, env),
69
70
  });
70
71
 
72
+ if (error) {
73
+ console.error(error);
74
+ process.exit(1);
75
+ }
76
+
77
+ if (status !== 0) {
78
+ console.error(`Command \`${command}\` exited with code ${status}`);
79
+ process.exit(1);
80
+ }
81
+ };
82
+
71
83
  const getAssetPrefix = (domain: string) => {
72
84
  // TODO: Usar asset_prefix del dominio
73
85
  if (!(process.env.GRIDDO_ASSET_PREFIX || process.env.ASSET_PREFIX) || !domain)
@@ -114,6 +126,8 @@ const domainExport = async (domain: string) => {
114
126
 
115
127
  const launchExports = async () => {
116
128
  splash();
129
+ // Check the server-environment throwing an error and exit if the check is not successfully.
130
+ buildHealthCheck();
117
131
  initCache();
118
132
 
119
133
  const domains = await findDomains();
@@ -20,7 +20,6 @@ const Template = (data: TemplateProps) => {
20
20
  page,
21
21
  siteLangs,
22
22
  siteMetadata,
23
- sitePages,
24
23
  socials,
25
24
  theme,
26
25
  },
@@ -50,7 +49,6 @@ const Template = (data: TemplateProps) => {
50
49
  siteId={page.site}
51
50
  siteLangs={siteLangs}
52
51
  siteMetadata={siteMetadata}
53
- sitePages={sitePages}
54
52
  socials={socials}
55
53
  theme={mappedTheme}
56
54
  >
@@ -1,4 +1,3 @@
1
- import type { AllPagesResponse } from "../types/api";
2
1
  import type { GatsbyPageObject } from "../types/pages";
3
2
  import type { Core } from "@griddo/core";
4
3
  import type { HeadProps } from "gatsby";
@@ -16,7 +15,6 @@ export interface CustomHeadProps extends HeadProps {
16
15
  export interface TemplateProps extends Omit<GatsbyPageObject, "context"> {
17
16
  pageContext: GatsbyPageObject["context"] & {
18
17
  page: Core.Page;
19
- sitePages: AllPagesResponse;
20
18
  };
21
19
  location: {
22
20
  pathname: string;
@@ -1,5 +1,4 @@
1
1
  import type {
2
- AllPagesResponse,
3
2
  AllSitesReponse,
4
3
  DistributorBody,
5
4
  DistributorResponse,
@@ -173,21 +172,6 @@ class SitesService {
173
172
 
174
173
  return response;
175
174
  }
176
-
177
- /**
178
- * TODO: JSDoc
179
- */
180
- static async getPages(id: number, cacheKey = "") {
181
- const {
182
- GET_PAGES: [prefix, suffix],
183
- } = ENDPOINTS;
184
- const response = await get<AllPagesResponse>({
185
- endpoint: `${prefix}${id}${suffix}`,
186
- cacheKey,
187
- });
188
-
189
- return response;
190
- }
191
175
  }
192
176
 
193
177
  export { SitesService };
@@ -1,35 +1,17 @@
1
- // //
2
- // ┌───────────────────┐ //
3
- // │ Page from API │ ∞ loop //
4
- // └─────────┬─────────┘ //
5
- // ┌─┼─┐ //
6
- // navigations //
7
- // distributors //
8
- // etc.. //
9
- // └─┼─┘ //
10
- // ┌──────────────┼─────────────┐ //
11
- // ▼ ▼ ▼ //
12
- // ┌─────────────┐ ┌───────────┐ ┌───────────┐ //
13
- // │ Single page │ │ Multipage │ │ Listpages │ //
14
- // └─────────────┘ └───────────┘ └───────────┘ //
15
- // ▼ ▼ ▼ //
16
- // └──────┐ | ┌─────┘ //
17
- // ▼ ▼ ▼ //
18
- // ┌─────── S T O R E ─────┐ //
19
- // │ Array of page objects │ //
20
- // ├─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─┤ //
21
- // │ Build metadata │ //
22
- // └─────────────────────────┘ //
23
-
24
1
  import type { BuildProcessData } from "../types/global";
25
- import type { GatsbyPageObject, PageAdditionalInfo } from "../types/pages";
2
+ import type {
3
+ GatsbyPageObject,
4
+ GriddoListPage,
5
+ GriddoMultiPage,
6
+ GriddoSinglePage,
7
+ PageAdditionalInfo,
8
+ } from "../types/pages";
26
9
  import type { Site } from "../types/sites";
27
10
 
28
11
  import fs from "fs";
29
12
  import fsp from "fs/promises";
30
13
  import path from "path";
31
14
 
32
- import chalk from "chalk";
33
15
  import dotenv from "dotenv";
34
16
  import fsx from "fs-extra";
35
17
  import pLimit from "p-limit";
@@ -43,9 +25,9 @@ import { version as griddoVersion } from "../../package.json";
43
25
  import { createSha256, updatedSiteHash } from "../utils/cache";
44
26
  import { deleteSites } from "../utils/folders";
45
27
  import {
46
- createGriddoListPages,
47
- createGriddoMultiPages,
48
- createGriddoSinglePage,
28
+ createGriddoListPagesForGatsby,
29
+ createGriddoMultiPagesForGatsby,
30
+ createGriddoSinglePageForGatsby,
49
31
  getMultiPageElements,
50
32
  getPaginatedPages,
51
33
  } from "../utils/pages";
@@ -59,7 +41,8 @@ const API_URL = process.env.API_URL as string;
59
41
  const CXBRANCH = process.env.CXBRANCH;
60
42
  const REACT_APP_INSTANCE =
61
43
  process.env.GRIDDO_REACT_APP_INSTANCE || process.env.REACT_APP_INSTANCE;
62
- const GRIDDO_API_CONCURRENCY_COUNT = process.env.GRIDDO_API_CONCURRENCY_COUNT;
44
+ const API_CONCURRENCY_COUNT =
45
+ parseInt(process.env.GRIDDO_API_CONCURRENCY_COUNT || "") || 10;
63
46
  const PUBLIC_API_URL = process.env.PUBLIC_API_URL as string;
64
47
  const RENDER_ID =
65
48
  process.env.GRIDDO_RENDERID || new Date().valueOf().toString();
@@ -67,7 +50,6 @@ const RENDER_ID =
67
50
  // Consts
68
51
  const CREATED_PAGES: Array<number> = [];
69
52
  const BUILD_PROCESS_DATA: BuildProcessData = {};
70
- const UNWANTED_PAGE_PROPS = ["editorID", "parentEditorID"];
71
53
 
72
54
  /**
73
55
  * Store service to fetch, process and save object pages and sites data to be
@@ -77,7 +59,7 @@ const UNWANTED_PAGE_PROPS = ["editorID", "parentEditorID"];
77
59
  * import { StoreService } from "./store"
78
60
  *
79
61
  * // Init the store service.
80
- * StoreService.init("foo/pages", { sanitize: true })
62
+ * StoreService.init("foo/pages")
81
63
  * // Save the data into files.
82
64
  * await StoreService.createBuildSource()
83
65
  * // Consume the pages as an interator.
@@ -85,7 +67,6 @@ const UNWANTED_PAGE_PROPS = ["editorID", "parentEditorID"];
85
67
  */
86
68
  class StoreService {
87
69
  static basePath: string;
88
- static sanitize? = false;
89
70
  static pages: Array<GatsbyPageObject> = [];
90
71
  static sitesToPublish: Array<Site>;
91
72
  static buildProcessData: BuildProcessData;
@@ -98,17 +79,12 @@ class StoreService {
98
79
  * @param options An object configuration
99
80
  * @param options.mode Store mode
100
81
  */
101
- public static init(basePath: string, options: { sanitize?: boolean }) {
102
- this.sanitize = options?.sanitize;
103
- const griddoCxThreads = parseInt(GRIDDO_API_CONCURRENCY_COUNT || "10");
104
-
105
- console.log(`🧶 Processing API page calls with ${griddoCxThreads} threads`);
82
+ public static init(basePath: string) {
83
+ console.log(
84
+ `🧶 Processing API page calls with ${API_CONCURRENCY_COUNT} threads`
85
+ );
106
86
  console.log(`📦 Griddo Store initialized`);
107
87
 
108
- if (this.sanitize) {
109
- console.log(`🏥 Sanitize pages enabled`);
110
- }
111
-
112
88
  this.basePath = basePath;
113
89
 
114
90
  // Remove old store
@@ -139,19 +115,15 @@ class StoreService {
139
115
  process.exit(0);
140
116
  }
141
117
 
142
- console.log(
143
- chalk.green(`\n Sites to publish: ${siteList(sitesToPublish)}`)
144
- );
145
-
146
- console.log(
147
- chalk.red(`\n⬇ Sites to unpublish: ${siteList(sitesToUnpublish)}\n`)
148
- );
118
+ console.log(`\n⬆ Sites to publish: ${siteList(sitesToPublish)}`);
119
+ console.log(`\n Sites to unpublish: ${siteList(sitesToUnpublish)}\n`);
149
120
 
150
121
  // Unpublish (API) and delete sites
151
122
  // TODO: Manage this to save end data to the __report-build__ file ???
152
123
  await unpublishSites(sitesToUnpublish);
153
124
 
154
125
  // Delete (clean) sites to publish
126
+ // SPREAD: await deleteSites(sitesToPublish);
155
127
  await deleteSites([...sitesToPublish]);
156
128
 
157
129
  // Set robots information to use later in the `onPostBuild` phase.
@@ -250,12 +222,14 @@ class StoreService {
250
222
  CREATED_PAGES.push(pageId);
251
223
 
252
224
  // TODO: Use structuredClone() when node 18 is available.
225
+ // SHAME: This new pageAdditionalInfo needs to be a copy because
226
+ // additionalInfo referenced from outside this function and
227
+ // its used by other pages.
253
228
  const pageAdditionalInfo = JSON.parse(
254
229
  JSON.stringify(additionalInfo)
255
230
  ) as PageAdditionalInfo;
256
231
 
257
232
  // Updated with navigations (header & footer)
258
- // NavService.getPageNavigations returns { header, footer }
259
233
  pageAdditionalInfo.navigations = NavService.getPageNavigations(page);
260
234
 
261
235
  // Content Type Data Query
@@ -270,68 +244,66 @@ class StoreService {
270
244
  // MultiPage Query
271
245
  // Where the pair `hasGriddoMultiPage:true` prop exists, this function
272
246
  // will process the schema and return a multiPageElemtens array to use
273
- // in `createGriddoMultipages()`
247
+ // in `createGriddoMultiPagesForGatsby()`
274
248
  const multiPageElements = await getMultiPageElements(template);
275
249
 
276
250
  // -----------------------------------------------------------------------
277
- // Build page objects
251
+ // Build Griddo page objects.
278
252
  // -----------------------------------------------------------------------
279
- // Three types of pages
280
- // - List with pagination (n pages)
281
- // - Multi-page module (n pages)
282
- // - Single (1 page)
283
-
284
- // List
285
- if (page?.mode === "list") {
286
- gatsbyPageObjects = await createGriddoListPages(
287
- {
288
- ...page, // Revisar la inclusión de este ...page.
289
- // The first page, the one without the `/2, /3, /4, etc..` slug
290
- page,
291
- // The other pages
292
- // In this case `getPaginatedPages` doesn't require the whole page object, just the `template` part.
293
- // This is because only the `template` prop will be different for each page.
294
- pages: getPaginatedPages(template),
295
- // The `template` key with the (maybe) distributor data items queried
296
- template,
297
- // Pass all data items
298
- totalQueriedItems: template.queriedItems,
299
- // Default site language
300
- defaultLang,
301
- },
253
+ // A page from the API could be one of those:
254
+ // - List with pagination (static list template): needs to be splitted in n pages
255
+ // - Multi-page module: needs to be splitted in n pages
256
+ // - Single: just one page
257
+
258
+ // Griddo page types
259
+ const isStaticListPage = page?.mode === "list";
260
+ const isMultiPage = !isStaticListPage && multiPageElements;
261
+ const isSinglePage = !isMultiPage && !isStaticListPage;
262
+
263
+ // >> List (static list template) <<
264
+ if (isStaticListPage) {
265
+ const griddoListPage = {
266
+ page: page,
267
+ pages: getPaginatedPages(template),
268
+ isRoot: false,
269
+ defaultLang: defaultLang,
270
+ template: template,
271
+ totalQueriedItems: template.queriedItems,
272
+ } as GriddoListPage;
273
+
274
+ // pageObjects = await createGriddoListPages({ adapter: "Gatsby" })
275
+ gatsbyPageObjects = await createGriddoListPagesForGatsby(
276
+ griddoListPage,
302
277
  pageAdditionalInfo
303
278
  );
304
279
  }
305
280
 
306
- // >> Multi-page
307
- if (multiPageElements) {
308
- gatsbyPageObjects = await createGriddoMultiPages(
309
- {
310
- // The whole page object
311
- ...page,
312
- // The `template` key with the (maybe) distributor data items queried
313
- template,
314
- // Array of object of every element of the MultiPage
315
- multiPageElements,
316
- // Default site language
317
- defaultLang,
318
- },
281
+ // >> Multi-page <<
282
+ if (isMultiPage) {
283
+ const griddoMultipage = page as GriddoMultiPage;
284
+
285
+ // The `template` key with the (maybe) distributor data items queried
286
+ griddoMultipage.template = template;
287
+ griddoMultipage.multiPageElements = multiPageElements;
288
+ griddoMultipage.defaultLang = defaultLang;
289
+
290
+ gatsbyPageObjects = await createGriddoMultiPagesForGatsby(
291
+ griddoMultipage,
319
292
  pageAdditionalInfo
320
293
  );
321
294
  }
322
295
 
323
- // >> Single template
324
- if (!multiPageElements && page.mode !== "list") {
296
+ // >> Single template <<
297
+ if (isSinglePage) {
298
+ const griddoSinglePage = page as GriddoSinglePage;
299
+
300
+ // The `template` key with the (maybe) distributor data items queried
301
+ griddoSinglePage.template = template;
302
+ griddoSinglePage.defaultLang = defaultLang;
303
+
325
304
  gatsbyPageObjects = [
326
- await createGriddoSinglePage(
327
- {
328
- // The whole page object
329
- ...page,
330
- // The `template` key with the (maybe) distributor data items queried
331
- template,
332
- // Default site language
333
- defaultLang,
334
- },
305
+ await createGriddoSinglePageForGatsby(
306
+ griddoSinglePage,
335
307
  pageAdditionalInfo
336
308
  ),
337
309
  ];
@@ -347,10 +319,8 @@ class StoreService {
347
319
  this.setCreatedPages(CREATED_PAGES);
348
320
  };
349
321
 
350
- const griddoCxThreads = parseInt(GRIDDO_API_CONCURRENCY_COUNT || "10");
351
-
352
322
  // Create the pLimit array of promises
353
- const limit = pLimit(griddoCxThreads);
323
+ const limit = pLimit(API_CONCURRENCY_COUNT);
354
324
  const pagesToRender = validPagesIds.map((id: number) =>
355
325
  limit(() => fetchPageAndSaveToStore(id))
356
326
  );
@@ -372,9 +342,9 @@ class StoreService {
372
342
  * @param pages An array of pages to save in the store.
373
343
  */
374
344
  public static async savePages(pages: Array<GatsbyPageObject>) {
375
- if (this.sanitize) {
376
- pages.forEach((page) => removeProperties(page, UNWANTED_PAGE_PROPS));
377
- }
345
+ pages.forEach((page) =>
346
+ removeProperties(page, ["editorID", "parentEditorID"])
347
+ );
378
348
 
379
349
  const promises = pages.map(async (pageObject) => {
380
350
  const filename = `${createSha256(pageObject.path)}.json`;
@@ -427,7 +397,7 @@ class StoreService {
427
397
  /**
428
398
  * Return an iterator with pages read from the file system.
429
399
  */
430
- public static *getPages() {
400
+ public static async *getPages() {
431
401
  const pagesDirPath = path.resolve(this.basePath);
432
402
  const jsonFilePaths = walk(pagesDirPath).filter(
433
403
  (file) => path.extname(file) === ".json"
@@ -435,10 +405,10 @@ class StoreService {
435
405
 
436
406
  for (const filePath of jsonFilePaths) {
437
407
  try {
438
- const fileStats = fs.statSync(filePath);
439
- const page = fsx.readJSONSync(filePath, {
408
+ const fileStats = await fsp.stat(filePath);
409
+ const page = (await fsx.readJSON(filePath, {
440
410
  encoding: "utf-8",
441
- }) as GatsbyPageObject;
411
+ })) as GatsbyPageObject;
442
412
  page.size = fileStats.size / 1024;
443
413
  yield page;
444
414
  } catch (error) {
package/src/types/api.ts CHANGED
@@ -15,7 +15,7 @@ export type EndSiteRenderBody = {
15
15
 
16
16
  /** Distributor Body */
17
17
  export interface DistributorBody {
18
- mode: "auto" | "manual" | "navigation";
18
+ mode?: "auto" | "manual" | "navigation";
19
19
  order?: string;
20
20
  source?: Array<string>;
21
21
  quantity?: number;
@@ -152,9 +152,6 @@ export interface EndPageInfoResponse {
152
152
  /** Describes a response type for GET all sites */
153
153
  export type AllSitesReponse = Array<Site>;
154
154
 
155
- /** Describes a response type for GET all pages */
156
- export type AllPagesResponse = Array<APIPageObject>;
157
-
158
155
  /** Describes a response type for GET one page */
159
156
  export type PageResponse = APIPageObject;
160
157
 
@@ -172,8 +169,7 @@ export type APIResponses =
172
169
  | PageResponse
173
170
  | EndPageInfoResponse
174
171
  | DistributorResponse
175
- | PostSearchInfoResponse
176
- | AllPagesResponse;
172
+ | PostSearchInfoResponse;
177
173
 
178
174
  // TODO: JSDoc
179
175
  export interface ShowApiErrorOptions {
@@ -1,9 +1,9 @@
1
- import type { AllPagesResponse, SocialsResponse } from "./api";
1
+ import type { SocialsResponse } from "./api";
2
2
  import type { Footer, Header } from "./navigation";
3
3
  import type { Core } from "@griddo/core";
4
4
 
5
5
  /**
6
- * Describe a Griddo site object from API.
6
+ * Describes a Griddo site object from API.
7
7
  * This takes some type props from Core.Site which is a Site for the Gatsby template.tsx.
8
8
  */
9
9
  export interface Site
@@ -56,7 +56,6 @@ export interface SiteData {
56
56
  headers: Array<Header>;
57
57
  footers: Array<Footer>;
58
58
  socials: SocialsResponse;
59
- sitePages: AllPagesResponse;
60
59
  }
61
60
 
62
61
  // TODO: JSDoc
@@ -1,6 +1,6 @@
1
1
  import type { Core, Fields } from "@griddo/core";
2
2
 
3
- /** Describe a template object from a Griddo Page */
3
+ /** Describes a template object from a Griddo Page */
4
4
  export type Template = Core.Page["template"];
5
5
 
6
6
  /** Describes a template with distributor data */
package/src/utils/api.ts CHANGED
@@ -20,7 +20,7 @@ dotenv.config();
20
20
 
21
21
  // Envs
22
22
  const {
23
- env: { RETRY_WAIT_SECONDS = "4", RETRY_ATTEMPTS = "1" },
23
+ env: { RETRY_WAIT_SECONDS = "4", RETRY_ATTEMPTS = "4" },
24
24
  } = process;
25
25
 
26
26
  /**
@@ -30,16 +30,18 @@ const {
30
30
  * @returns {Promise<T>} A promise that is resolved with the data from the API response.
31
31
  *
32
32
  * @example
33
- * const response = await requestAPI<Site>({
34
- * endpoint: "...",
35
- * cacheKey: "..."",
36
- * }, { method:"get" });
33
+ * const response = await requestAPI<Site>(
34
+ * { endpoint: "...", cacheKey: "...", ... },
35
+ * "get",
36
+ * "..."
37
+ * );
37
38
  */
38
39
  async function requestAPI<T extends APIResponses>(
39
- props: APIRequest & { method: Method },
40
+ props: APIRequest,
41
+ method: Method,
40
42
  appendToLog = ""
41
43
  ): Promise<T> {
42
- const { endpoint, body, cacheKey = "", attempt = 1, method, headers } = props;
44
+ const { endpoint, body, cacheKey = "", attempt = 1, headers } = props;
43
45
  const cacheOptions = { endpoint, body, headers, cacheKey };
44
46
 
45
47
  // Cache
@@ -64,7 +66,7 @@ async function requestAPI<T extends APIResponses>(
64
66
  const { data }: { data: T } = await axios({
65
67
  url: endpoint,
66
68
  method,
67
- headers: { ...headers, ...AuthService.headers },
69
+ headers: Object.assign({}, headers, AuthService.headers),
68
70
  data: body,
69
71
  });
70
72
 
@@ -86,7 +88,13 @@ async function requestAPI<T extends APIResponses>(
86
88
  return null;
87
89
  }
88
90
 
89
- if (attempt > parseInt(RETRY_ATTEMPTS) || !error.response) {
91
+ if (attempt > parseInt(RETRY_ATTEMPTS)) {
92
+ console.log(`Max attempts ${RETRY_ATTEMPTS} reached`);
93
+ process.exit(1);
94
+ }
95
+
96
+ if (!error.response) {
97
+ console.log("Unknown error occurred");
90
98
  console.log(JSON.stringify(error, null, 2));
91
99
  process.exit(1);
92
100
  }
@@ -102,9 +110,9 @@ async function requestAPI<T extends APIResponses>(
102
110
  endpoint,
103
111
  body,
104
112
  cacheKey,
105
- method,
106
113
  attempt: attempt + 1,
107
114
  },
115
+ method,
108
116
  appendToLog
109
117
  );
110
118
  }
@@ -117,7 +125,7 @@ async function requestAPI<T extends APIResponses>(
117
125
  * @returns A promise that is resolved with the data from the API response.
118
126
  */
119
127
  async function getApi<T extends APIResponses>(props: GetAPI) {
120
- return await requestAPI<T>({ ...props, method: "get" });
128
+ return await requestAPI<T>(props, "get");
121
129
  }
122
130
 
123
131
  /**
@@ -127,7 +135,7 @@ async function getApi<T extends APIResponses>(props: GetAPI) {
127
135
  * @returns A promise that is resolved with the data from the API response.
128
136
  */
129
137
  async function putApi<T extends APIResponses>(props: PutAPI) {
130
- return await requestAPI<T>({ ...props, method: "put" });
138
+ return await requestAPI<T>(props, "put");
131
139
  }
132
140
 
133
141
  /**
@@ -144,10 +152,7 @@ async function postApi<T extends APIResponses>(props: PostAPI) {
144
152
  headers?.lang
145
153
  )}`;
146
154
 
147
- return await requestAPI<T>(
148
- { ...props, method: "post" },
149
- distributorBodyParams || ""
150
- );
155
+ return await requestAPI<T>(props, "post", distributorBodyParams || "");
151
156
  }
152
157
 
153
158
  /**
@@ -159,13 +164,12 @@ function showApiError(error: AxiosError, options: ShowApiErrorOptions) {
159
164
  const { status, statusText, data } = response || {};
160
165
  const callInfoArray = [];
161
166
 
162
- for (const item of Object.keys(callInfo)) {
163
- const itemCasted = item as keyof typeof callInfo;
167
+ for (const item of Object.keys(callInfo) as Array<keyof typeof callInfo>) {
164
168
  callInfoArray.push(
165
169
  `${item}: ${
166
- typeof callInfo[itemCasted] === "object"
167
- ? JSON.stringify(callInfo[itemCasted])
168
- : callInfo[itemCasted]
170
+ typeof callInfo[item] === "object"
171
+ ? JSON.stringify(callInfo[item])
172
+ : callInfo[item]
169
173
  }`
170
174
  );
171
175
  }