@griddo/cx 11.7.12-rc.4 → 11.7.12
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/README.md +226 -27
- package/build/adapters/gatsby/index.d.ts +4 -0
- package/build/adapters/gatsby/utils.d.ts +22 -0
- package/build/artifacts/index.d.ts +6 -0
- package/build/commands/end-render.d.ts +2 -0
- package/build/commands/move-assets.d.ts +1 -0
- package/build/commands/prepare-domains-render.d.ts +1 -0
- package/build/commands/reset-render.d.ts +2 -0
- package/build/commands/start-render.d.ts +2 -0
- package/build/commands/upload-search-content.d.ts +2 -0
- package/build/constants/endpoints.d.ts +19 -0
- package/build/constants/envs.d.ts +37 -0
- package/build/constants/index.d.ts +57 -0
- package/build/end-render.js +74 -0
- package/build/end-render.js.map +7 -0
- package/build/errors/errors-data.d.ts +22 -0
- package/build/errors/index.d.ts +15 -0
- package/build/index.d.ts +29 -0
- package/build/index.js +73 -0
- package/build/index.js.map +7 -0
- package/build/prepare-domains-render.js +73 -0
- package/build/prepare-domains-render.js.map +7 -0
- package/build/react/Favicon/index.d.ts +5 -0
- package/build/react/Favicon/utils.d.ts +9 -0
- package/build/react/GriddoIntegrations/index.d.ts +20 -0
- package/build/react/GriddoIntegrations/utils.d.ts +26 -0
- package/{react/index.tsx → build/react/index.d.ts} +0 -1
- package/build/react/index.js +3 -0
- package/build/registers/api.d.ts +9 -0
- package/build/registers/gatsby.d.ts +9 -0
- package/build/registers/index.d.ts +3 -0
- package/build/reset-render.js +74 -0
- package/build/reset-render.js.map +7 -0
- package/build/services/auth.d.ts +10 -0
- package/build/services/domains.d.ts +6 -0
- package/build/services/navigation.d.ts +50 -0
- package/build/services/reference-fields.d.ts +20 -0
- package/build/services/register.d.ts +36 -0
- package/build/services/robots.d.ts +19 -0
- package/build/services/settings.d.ts +4 -0
- package/build/services/sites.d.ts +29 -0
- package/build/services/store.d.ts +6 -0
- package/build/start-render.js +100 -0
- package/build/start-render.js.map +7 -0
- package/build/types/api.d.ts +142 -0
- package/build/types/global.d.ts +84 -0
- package/build/types/navigation.d.ts +28 -0
- package/build/types/pages.d.ts +144 -0
- package/build/types/sites.d.ts +57 -0
- package/build/types/templates.d.ts +8 -0
- package/build/upload-search-content.js +74 -0
- package/build/upload-search-content.js.map +7 -0
- package/build/utils/alerts.d.ts +3 -0
- package/build/utils/api.d.ts +23 -0
- package/build/utils/cache.d.ts +35 -0
- package/build/utils/core-utils.d.ts +107 -0
- package/build/utils/create-build-data.d.ts +8 -0
- package/build/utils/domains.d.ts +13 -0
- package/build/utils/folders.d.ts +53 -0
- package/build/utils/health-checks.d.ts +7 -0
- package/build/utils/images.d.ts +16 -0
- package/build/utils/instance.d.ts +21 -0
- package/build/utils/loggin.d.ts +51 -0
- package/build/utils/pages.d.ts +34 -0
- package/build/utils/render.d.ts +13 -0
- package/build/utils/searches.d.ts +15 -0
- package/build/utils/sites.d.ts +31 -0
- package/build/utils/store.d.ts +81 -0
- package/cx.config.d.ts +5 -0
- package/cx.config.js +36 -0
- package/exporter/adapters/gatsby/index.ts +153 -73
- package/exporter/adapters/gatsby/utils.ts +161 -0
- package/exporter/artifacts/README.md +34 -0
- package/exporter/artifacts/index.ts +33 -0
- package/exporter/build.sh +38 -4
- package/exporter/commands/end-render.ts +70 -72
- package/exporter/commands/move-assets.ts +11 -0
- package/exporter/commands/prepare-domains-render.ts +31 -143
- package/exporter/commands/reset-render.ts +3 -10
- package/exporter/commands/start-render.ts +45 -22
- package/exporter/commands/upload-search-content.ts +21 -194
- package/exporter/constants/endpoints.ts +1 -2
- package/exporter/constants/envs.ts +55 -57
- package/exporter/constants/index.ts +129 -0
- package/exporter/{constants/errors.ts → errors/errors-data.ts} +14 -24
- package/exporter/{utils/errors.ts → errors/index.ts} +9 -10
- package/exporter/index.ts +82 -0
- package/{react → exporter/react}/Favicon/index.tsx +7 -1
- package/{react → exporter/react}/GriddoIntegrations/index.tsx +22 -16
- package/{react → exporter/react}/GriddoIntegrations/utils.ts +9 -21
- package/exporter/react/index.tsx +11 -0
- package/exporter/registers/api.ts +14 -0
- package/exporter/registers/gatsby.ts +14 -0
- package/exporter/registers/index.ts +4 -0
- package/exporter/services/auth.ts +6 -7
- package/exporter/services/domains.ts +16 -0
- package/exporter/services/navigation.ts +10 -4
- package/exporter/services/reference-fields.ts +24 -9
- package/exporter/services/register.ts +113 -0
- package/exporter/services/robots.ts +16 -9
- package/exporter/services/settings.ts +17 -0
- package/exporter/services/sites.ts +24 -36
- package/exporter/services/store.ts +96 -107
- package/exporter/types/api.ts +28 -27
- package/exporter/types/global.ts +11 -8
- package/exporter/types/navigation.ts +1 -1
- package/exporter/types/pages.ts +3 -2
- package/exporter/types/sites.ts +2 -1
- package/exporter/utils/alerts.ts +29 -0
- package/exporter/utils/api.ts +75 -55
- package/exporter/utils/cache.ts +62 -37
- package/exporter/utils/core-utils.ts +272 -133
- package/exporter/utils/create-build-data.ts +17 -0
- package/exporter/utils/domains.ts +7 -10
- package/exporter/utils/folders.ts +98 -163
- package/exporter/utils/health-checks.ts +64 -0
- package/exporter/utils/images.ts +6 -1
- package/exporter/utils/instance.ts +13 -9
- package/exporter/utils/loggin.ts +91 -44
- package/exporter/utils/pages.ts +88 -23
- package/exporter/utils/render.ts +48 -180
- package/exporter/utils/searches.ts +156 -0
- package/exporter/utils/sites.ts +197 -19
- package/exporter/utils/store.ts +180 -87
- package/gatsby-browser.tsx +58 -41
- package/gatsby-config.ts +17 -10
- package/gatsby-node.ts +78 -17
- package/gatsby-ssr.tsx +1 -2
- package/package.json +86 -36
- package/src/README.md +7 -0
- package/src/components/Head.tsx +46 -13
- package/src/components/template.tsx +30 -8
- package/src/gatsby-node-utils.ts +2 -73
- package/src/html.tsx +11 -2
- package/src/types.ts +5 -5
- package/start-render.js +7 -0
- package/tsconfig.json +3 -5
- package/build/adapters/gatsby/actions/clean.js +0 -10
- package/build/adapters/gatsby/actions/clean.js.map +0 -1
- package/build/adapters/gatsby/actions/close.js +0 -12
- package/build/adapters/gatsby/actions/close.js.map +0 -1
- package/build/adapters/gatsby/actions/data.js +0 -18
- package/build/adapters/gatsby/actions/data.js.map +0 -1
- package/build/adapters/gatsby/actions/healthCheck.js +0 -10
- package/build/adapters/gatsby/actions/healthCheck.js.map +0 -1
- package/build/adapters/gatsby/actions/init.js +0 -22
- package/build/adapters/gatsby/actions/init.js.map +0 -1
- package/build/adapters/gatsby/actions/logs.js +0 -12
- package/build/adapters/gatsby/actions/logs.js.map +0 -1
- package/build/adapters/gatsby/actions/meta.js +0 -13
- package/build/adapters/gatsby/actions/meta.js.map +0 -1
- package/build/adapters/gatsby/actions/prepare.js +0 -9
- package/build/adapters/gatsby/actions/prepare.js.map +0 -1
- package/build/adapters/gatsby/actions/relocation.js +0 -15
- package/build/adapters/gatsby/actions/relocation.js.map +0 -1
- package/build/adapters/gatsby/actions/restore.js +0 -26
- package/build/adapters/gatsby/actions/restore.js.map +0 -1
- package/build/adapters/gatsby/actions/ssg.js +0 -9
- package/build/adapters/gatsby/actions/ssg.js.map +0 -1
- package/build/adapters/gatsby/actions/sync.js +0 -51
- package/build/adapters/gatsby/actions/sync.js.map +0 -1
- package/build/adapters/gatsby/index.js +0 -78
- package/build/adapters/gatsby/index.js.map +0 -1
- package/build/adapters/gatsby/shared/artifacts.js +0 -20
- package/build/adapters/gatsby/shared/artifacts.js.map +0 -1
- package/build/adapters/gatsby/shared/context.js +0 -31
- package/build/adapters/gatsby/shared/context.js.map +0 -1
- package/build/adapters/gatsby/shared/diff-assets.js +0 -101
- package/build/adapters/gatsby/shared/diff-assets.js.map +0 -1
- package/build/adapters/gatsby/shared/extract-assets.js +0 -58
- package/build/adapters/gatsby/shared/extract-assets.js.map +0 -1
- package/build/adapters/gatsby/shared/gatsby-build.js +0 -55
- package/build/adapters/gatsby/shared/gatsby-build.js.map +0 -1
- package/build/adapters/gatsby/shared/sync-render.js +0 -210
- package/build/adapters/gatsby/shared/sync-render.js.map +0 -1
- package/build/adapters/gatsby/shared/types.js +0 -3
- package/build/adapters/gatsby/shared/types.js.map +0 -1
- package/build/commands/end-render.js +0 -87
- package/build/commands/end-render.js.map +0 -1
- package/build/commands/prepare-assets-directory.js +0 -30
- package/build/commands/prepare-assets-directory.js.map +0 -1
- package/build/commands/prepare-domains-render.js +0 -133
- package/build/commands/prepare-domains-render.js.map +0 -1
- package/build/commands/reset-render.js +0 -21
- package/build/commands/reset-render.js.map +0 -1
- package/build/commands/start-render.js +0 -44
- package/build/commands/start-render.js.map +0 -1
- package/build/commands/upload-search-content.js +0 -180
- package/build/commands/upload-search-content.js.map +0 -1
- package/build/constants/endpoints.js +0 -43
- package/build/constants/endpoints.js.map +0 -1
- package/build/constants/envs.js +0 -68
- package/build/constants/envs.js.map +0 -1
- package/build/constants/errors.js +0 -81
- package/build/constants/errors.js.map +0 -1
- package/build/services/auth.js +0 -40
- package/build/services/auth.js.map +0 -1
- package/build/services/db-class.js +0 -49
- package/build/services/db-class.js.map +0 -1
- package/build/services/db.js +0 -34
- package/build/services/db.js.map +0 -1
- package/build/services/navigation.js +0 -96
- package/build/services/navigation.js.map +0 -1
- package/build/services/reference-fields.js +0 -131
- package/build/services/reference-fields.js.map +0 -1
- package/build/services/robots.js +0 -60
- package/build/services/robots.js.map +0 -1
- package/build/services/sites.js +0 -110
- package/build/services/sites.js.map +0 -1
- package/build/services/store.js +0 -257
- package/build/services/store.js.map +0 -1
- package/build/types/api.js +0 -3
- package/build/types/api.js.map +0 -1
- package/build/types/global.js +0 -3
- package/build/types/global.js.map +0 -1
- package/build/types/navigation.js +0 -3
- package/build/types/navigation.js.map +0 -1
- package/build/types/pages.js +0 -3
- package/build/types/pages.js.map +0 -1
- package/build/types/render.js +0 -10
- package/build/types/render.js.map +0 -1
- package/build/types/sites.js +0 -3
- package/build/types/sites.js.map +0 -1
- package/build/types/templates.js +0 -3
- package/build/types/templates.js.map +0 -1
- package/build/utils/api.js +0 -165
- package/build/utils/api.js.map +0 -1
- package/build/utils/artifacts.js +0 -38
- package/build/utils/artifacts.js.map +0 -1
- package/build/utils/brush.js +0 -30
- package/build/utils/brush.js.map +0 -1
- package/build/utils/cache.js +0 -106
- package/build/utils/cache.js.map +0 -1
- package/build/utils/check-environment-health.js +0 -73
- package/build/utils/check-environment-health.js.map +0 -1
- package/build/utils/core-utils.js +0 -270
- package/build/utils/core-utils.js.map +0 -1
- package/build/utils/domains.js +0 -37
- package/build/utils/domains.js.map +0 -1
- package/build/utils/errors.js +0 -30
- package/build/utils/errors.js.map +0 -1
- package/build/utils/folders.js +0 -341
- package/build/utils/folders.js.map +0 -1
- package/build/utils/images.js +0 -45
- package/build/utils/images.js.map +0 -1
- package/build/utils/instance.js +0 -66
- package/build/utils/instance.js.map +0 -1
- package/build/utils/loggin.js +0 -119
- package/build/utils/loggin.js.map +0 -1
- package/build/utils/npm-deps/find-up-simple.js +0 -75
- package/build/utils/npm-deps/find-up-simple.js.map +0 -1
- package/build/utils/npm-deps/pkg-dir.js +0 -20
- package/build/utils/npm-deps/pkg-dir.js.map +0 -1
- package/build/utils/npm-deps/xml-parser.js +0 -49
- package/build/utils/npm-deps/xml-parser.js.map +0 -1
- package/build/utils/pages.js +0 -359
- package/build/utils/pages.js.map +0 -1
- package/build/utils/render.js +0 -168
- package/build/utils/render.js.map +0 -1
- package/build/utils/sitemaps.js +0 -110
- package/build/utils/sitemaps.js.map +0 -1
- package/build/utils/sites.js +0 -105
- package/build/utils/sites.js.map +0 -1
- package/build/utils/store.js +0 -193
- package/build/utils/store.js.map +0 -1
- package/cli.mjs +0 -136
- package/exporter/adapters/gatsby/actions/clean.ts +0 -14
- package/exporter/adapters/gatsby/actions/close.ts +0 -17
- package/exporter/adapters/gatsby/actions/data.ts +0 -25
- package/exporter/adapters/gatsby/actions/healthCheck.ts +0 -10
- package/exporter/adapters/gatsby/actions/init.ts +0 -26
- package/exporter/adapters/gatsby/actions/logs.ts +0 -13
- package/exporter/adapters/gatsby/actions/meta.ts +0 -16
- package/exporter/adapters/gatsby/actions/prepare.ts +0 -9
- package/exporter/adapters/gatsby/actions/relocation.ts +0 -15
- package/exporter/adapters/gatsby/actions/restore.ts +0 -36
- package/exporter/adapters/gatsby/actions/ssg.ts +0 -12
- package/exporter/adapters/gatsby/actions/sync.ts +0 -71
- package/exporter/adapters/gatsby/shared/artifacts.ts +0 -16
- package/exporter/adapters/gatsby/shared/context.ts +0 -50
- package/exporter/adapters/gatsby/shared/diff-assets.ts +0 -113
- package/exporter/adapters/gatsby/shared/extract-assets.ts +0 -61
- package/exporter/adapters/gatsby/shared/gatsby-build.ts +0 -58
- package/exporter/adapters/gatsby/shared/sync-render.ts +0 -276
- package/exporter/adapters/gatsby/shared/types.ts +0 -35
- package/exporter/build-esbuild.noop +0 -42
- package/exporter/commands/prepare-assets-directory.ts +0 -30
- package/exporter/services/db-class.ts +0 -54
- package/exporter/services/db.ts +0 -32
- package/exporter/types/render.ts +0 -59
- package/exporter/utils/artifacts.ts +0 -38
- package/exporter/utils/brush.ts +0 -34
- package/exporter/utils/check-environment-health.ts +0 -84
- package/exporter/utils/npm-deps/find-up-simple.ts +0 -100
- package/exporter/utils/npm-deps/pkg-dir.ts +0 -17
- package/exporter/utils/npm-deps/xml-parser.ts +0 -57
- package/exporter/utils/sitemaps.ts +0 -129
- package/plugins/gatsby-plugin-svgr-loader/gatsby-node.js +0 -55
- package/plugins/gatsby-plugin-svgr-loader/package.json +0 -8
- package/react/DynamicScript/index.tsx +0 -33
- package/tsconfig.commands.json +0 -36
- package/tsconfig.exporter.json +0 -19
- /package/{react → exporter/react}/Favicon/utils.ts +0 -0
package/exporter/types/pages.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { Core, Fields } from "@griddo/core";
|
|
2
1
|
import type { SocialsResponse } from "./api";
|
|
3
2
|
import type { Settings } from "./global";
|
|
4
3
|
import type { Site } from "./sites";
|
|
4
|
+
import type { Core, Fields } from "@griddo/core";
|
|
5
5
|
|
|
6
6
|
// TODO: In @griddo/core the type Core.Page has header/footer as React-Types,
|
|
7
7
|
// but API return `number | null`.
|
|
@@ -156,7 +156,8 @@ export interface Dimensions {
|
|
|
156
156
|
dimensionsSelect?: Array<string>;
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
-
export interface GatsbyPageTemplateProps
|
|
159
|
+
export interface GatsbyPageTemplateProps
|
|
160
|
+
extends Omit<GriddoPageObject, "context"> {
|
|
160
161
|
pageContext: GriddoPageObject["context"] & {
|
|
161
162
|
page: Core.Page;
|
|
162
163
|
};
|
package/exporter/types/sites.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Core } from "@griddo/core";
|
|
2
1
|
import type { SocialsResponse } from "./api";
|
|
3
2
|
import type { Footer, Header } from "./navigation";
|
|
3
|
+
import type { Core } from "@griddo/core";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Describes a Griddo site object from API.
|
|
@@ -43,6 +43,7 @@ export interface Site
|
|
|
43
43
|
*/
|
|
44
44
|
pages: Array<number>;
|
|
45
45
|
onlinePages: Array<number>;
|
|
46
|
+
changedPages: Array<number>;
|
|
46
47
|
published: string;
|
|
47
48
|
rendering: boolean;
|
|
48
49
|
renderingHours: number;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { GriddoAlertRegisterProps } from "@griddo/core";
|
|
2
|
+
|
|
3
|
+
import axios from "axios";
|
|
4
|
+
|
|
5
|
+
import { endpoints } from "../constants";
|
|
6
|
+
|
|
7
|
+
// Esta función está duplicada de core de manera intencionada por si esta recibe
|
|
8
|
+
// cambios necesitados solo por CX.
|
|
9
|
+
// Nota: Sí estamos reutilizando el type.
|
|
10
|
+
async function insertAlert({
|
|
11
|
+
area,
|
|
12
|
+
description,
|
|
13
|
+
fullData,
|
|
14
|
+
instantNotification = false,
|
|
15
|
+
level,
|
|
16
|
+
}: Omit<GriddoAlertRegisterProps, "publicApiUrl">) {
|
|
17
|
+
const url = endpoints.ALERT;
|
|
18
|
+
const body = { level, area, description, fullData, instantNotification };
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
await axios.post(url, body, {
|
|
22
|
+
headers: { "Content-Type": "application/json", Connection: "close" },
|
|
23
|
+
});
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.error("Error creating Griddo alert:", error);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { insertAlert };
|
package/exporter/utils/api.ts
CHANGED
|
@@ -6,16 +6,25 @@ import type {
|
|
|
6
6
|
PutAPI,
|
|
7
7
|
ShowApiErrorOptions,
|
|
8
8
|
} from "../types/api";
|
|
9
|
+
import type { AxiosError, Method } from "axios";
|
|
10
|
+
|
|
11
|
+
import axios from "axios";
|
|
12
|
+
import dotenv from "dotenv";
|
|
13
|
+
import kleur from "kleur";
|
|
9
14
|
|
|
10
|
-
import { AuthService } from "../services/auth";
|
|
11
|
-
import { brush } from "./brush";
|
|
12
15
|
import { saveCache, searchCacheData } from "./cache";
|
|
13
16
|
import { delay, getSafeSiteId, msToSec } from "./core-utils";
|
|
14
|
-
import {
|
|
17
|
+
import { buildLog } from "./loggin";
|
|
18
|
+
import { envs } from "../constants";
|
|
19
|
+
import { apiRegister } from "../registers";
|
|
20
|
+
import { AuthService } from "../services/auth";
|
|
21
|
+
|
|
22
|
+
dotenv.config();
|
|
15
23
|
|
|
16
24
|
// Envs
|
|
17
|
-
const {
|
|
18
|
-
|
|
25
|
+
const {
|
|
26
|
+
env: { RETRY_WAIT_SECONDS = "4", RETRY_ATTEMPTS = "4" },
|
|
27
|
+
} = process;
|
|
19
28
|
|
|
20
29
|
/**
|
|
21
30
|
* Make a GET/PUT/POST request to the Griddo API.
|
|
@@ -32,7 +41,7 @@ const { RETRY_WAIT_SECONDS = "4", RETRY_ATTEMPTS = "4" } = env;
|
|
|
32
41
|
*/
|
|
33
42
|
async function requestAPI<T extends APIResponses>(
|
|
34
43
|
props: APIRequest,
|
|
35
|
-
method:
|
|
44
|
+
method: Method,
|
|
36
45
|
appendToLog = "",
|
|
37
46
|
): Promise<T> {
|
|
38
47
|
const {
|
|
@@ -42,24 +51,21 @@ async function requestAPI<T extends APIResponses>(
|
|
|
42
51
|
attempt = 1,
|
|
43
52
|
headers,
|
|
44
53
|
useApiCacheDir = true,
|
|
45
|
-
logToFile = true,
|
|
46
54
|
} = props;
|
|
47
55
|
const cacheOptions = { endpoint, body, headers, cacheKey };
|
|
48
56
|
|
|
49
57
|
// Cache
|
|
50
58
|
if (cacheKey && useApiCacheDir) {
|
|
51
59
|
const start = new Date();
|
|
52
|
-
const cacheData =
|
|
60
|
+
const cacheData = searchCacheData<T>(cacheOptions);
|
|
53
61
|
|
|
54
62
|
if (cacheData) {
|
|
55
63
|
const siteId = getSafeSiteId(cacheData);
|
|
56
64
|
const siteIdMsg = siteId ? `site: ${siteId}` : "";
|
|
57
|
-
const duration = msToSec(Date.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
);
|
|
62
|
-
}
|
|
65
|
+
const duration = msToSec(new Date().getTime() - start.getTime());
|
|
66
|
+
buildLog(
|
|
67
|
+
`${method} (cache) ${siteIdMsg} ${endpoint} - ${duration}s ${appendToLog}`,
|
|
68
|
+
);
|
|
63
69
|
return cacheData;
|
|
64
70
|
}
|
|
65
71
|
}
|
|
@@ -67,50 +73,47 @@ async function requestAPI<T extends APIResponses>(
|
|
|
67
73
|
// Network
|
|
68
74
|
try {
|
|
69
75
|
const start = new Date();
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
fetchOptions.body = JSON.stringify(body);
|
|
80
|
-
if (!fetchOptions.headers) fetchOptions.headers = {};
|
|
81
|
-
(fetchOptions.headers as Record<string, string>)["Content-Type"] = "application/json";
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const response = await fetch(endpoint, fetchOptions);
|
|
85
|
-
|
|
86
|
-
// Handle non-2xx responses
|
|
87
|
-
if (!response.ok) {
|
|
88
|
-
if (response.status === 404) {
|
|
89
|
-
// @ts-expect-error page maybe will be 404
|
|
90
|
-
return null;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const data: T = await response.json();
|
|
76
|
+
const {
|
|
77
|
+
data,
|
|
78
|
+
headers: responseHeaders,
|
|
79
|
+
}: { data: T; headers: { "content-length": string } } = await axios({
|
|
80
|
+
url: endpoint,
|
|
81
|
+
method,
|
|
82
|
+
headers: Object.assign({}, headers, AuthService.headers),
|
|
83
|
+
data: body,
|
|
84
|
+
});
|
|
97
85
|
|
|
98
86
|
const siteId = getSafeSiteId(data);
|
|
99
87
|
const siteIdMsg = siteId ? `site: ${siteId}` : "";
|
|
100
|
-
const duration = msToSec(Date.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
);
|
|
105
|
-
}
|
|
88
|
+
const duration = msToSec(new Date().getTime() - start.getTime());
|
|
89
|
+
buildLog(
|
|
90
|
+
`${method} (fetch) ${siteIdMsg} ${endpoint} - ${duration}s ${appendToLog}`,
|
|
91
|
+
);
|
|
106
92
|
|
|
107
93
|
if (useApiCacheDir) {
|
|
108
|
-
|
|
94
|
+
saveCache(cacheOptions, data);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Only save registers if alerts are enabled
|
|
98
|
+
if (envs.GRIDDO_ALERT_FEATURE) {
|
|
99
|
+
const responseSize = Number.parseInt(responseHeaders["content-length"]);
|
|
100
|
+
if (responseSize > envs.GRIDDO_API_MAX_RESPONSE_SIZE) {
|
|
101
|
+
apiRegister.insert("API_RESPONSE_TOO_BIG", {
|
|
102
|
+
endpoint,
|
|
103
|
+
responseSize,
|
|
104
|
+
duration,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
109
107
|
}
|
|
110
108
|
|
|
111
109
|
return data;
|
|
112
110
|
} catch (e) {
|
|
113
|
-
const error = e as
|
|
111
|
+
const error = e as AxiosError;
|
|
112
|
+
|
|
113
|
+
if (error.response?.status === 404) {
|
|
114
|
+
// @ts-expect-error page maybe will be 404
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
114
117
|
|
|
115
118
|
if (attempt > parseInt(RETRY_ATTEMPTS)) {
|
|
116
119
|
console.log(`
|
|
@@ -119,7 +122,7 @@ Max attempts ${RETRY_ATTEMPTS} reached
|
|
|
119
122
|
- ${method.toUpperCase()} ${endpoint}
|
|
120
123
|
- BODY: ${JSON.stringify(body)}
|
|
121
124
|
- HEADERS: ${JSON.stringify(headers)}
|
|
122
|
-
-
|
|
125
|
+
- RESPONSE: ${error.response?.status} ${JSON.stringify(error.response?.data)}
|
|
123
126
|
--------------------------------------
|
|
124
127
|
`);
|
|
125
128
|
|
|
@@ -127,6 +130,12 @@ Max attempts ${RETRY_ATTEMPTS} reached
|
|
|
127
130
|
process.exit(1);
|
|
128
131
|
}
|
|
129
132
|
|
|
133
|
+
if (!error.response) {
|
|
134
|
+
console.log("Unknown error occurred");
|
|
135
|
+
console.log(JSON.stringify(error, null, 2));
|
|
136
|
+
process.exit(1);
|
|
137
|
+
}
|
|
138
|
+
|
|
130
139
|
showApiError(error, {
|
|
131
140
|
callInfo: { endpoint, body },
|
|
132
141
|
});
|
|
@@ -179,7 +188,9 @@ async function postApi<T extends APIResponses>(props: PostAPI) {
|
|
|
179
188
|
const { endpoint, body, headers } = props;
|
|
180
189
|
const referenceFieldBodyParams =
|
|
181
190
|
endpoint.endsWith("/distributor") &&
|
|
182
|
-
`# ReferenceField body: ${JSON.stringify(body)} lang: ${JSON.stringify(
|
|
191
|
+
`# ReferenceField body: ${JSON.stringify(body)} lang: ${JSON.stringify(
|
|
192
|
+
headers?.lang,
|
|
193
|
+
)}`;
|
|
183
194
|
|
|
184
195
|
return requestAPI<T>(props, "post", referenceFieldBodyParams || "");
|
|
185
196
|
}
|
|
@@ -187,31 +198,40 @@ async function postApi<T extends APIResponses>(props: PostAPI) {
|
|
|
187
198
|
/**
|
|
188
199
|
* Shows an API error through the terminal.
|
|
189
200
|
*/
|
|
190
|
-
function showApiError(error:
|
|
191
|
-
const { message, stack } = error;
|
|
201
|
+
function showApiError(error: AxiosError, options: ShowApiErrorOptions) {
|
|
202
|
+
const { response, message, stack } = error;
|
|
192
203
|
const { callInfo } = options;
|
|
204
|
+
const { status, statusText, data } = response || {};
|
|
193
205
|
const callInfoArray = [];
|
|
194
206
|
|
|
195
207
|
for (const item of Object.keys(callInfo) as Array<keyof typeof callInfo>) {
|
|
196
208
|
callInfoArray.push(
|
|
197
209
|
`${item}: ${
|
|
198
|
-
typeof callInfo[item] === "object"
|
|
210
|
+
typeof callInfo[item] === "object"
|
|
211
|
+
? JSON.stringify(callInfo[item])
|
|
212
|
+
: callInfo[item]
|
|
199
213
|
}`,
|
|
200
214
|
);
|
|
201
215
|
}
|
|
202
216
|
|
|
203
217
|
// Compose the errors output
|
|
204
218
|
const callInfoStr = callInfoArray.join("\n");
|
|
219
|
+
const apiResponseStr = response
|
|
220
|
+
? `Code: ${status} - ${statusText}\nResponse: ${JSON.stringify(data)}`
|
|
221
|
+
: "";
|
|
205
222
|
const errorDetailsStr = `${message}\n${stack}`;
|
|
206
223
|
|
|
207
224
|
// Print the error
|
|
208
225
|
console.warn(
|
|
209
|
-
|
|
226
|
+
kleur.red(`
|
|
210
227
|
=============
|
|
211
228
|
|
|
212
229
|
{ Call info }
|
|
213
230
|
${callInfoStr}
|
|
214
231
|
|
|
232
|
+
{ API Response }
|
|
233
|
+
${apiResponseStr}
|
|
234
|
+
|
|
215
235
|
{ Error details }
|
|
216
236
|
${errorDetailsStr}
|
|
217
237
|
|
package/exporter/utils/cache.ts
CHANGED
|
@@ -2,21 +2,24 @@ import type { Petition } from "../types/global";
|
|
|
2
2
|
import type { HashSites, SiteHash } from "../types/sites";
|
|
3
3
|
|
|
4
4
|
import crypto from "node:crypto";
|
|
5
|
-
import
|
|
5
|
+
import fs from "node:fs";
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
|
|
8
|
-
import
|
|
9
|
-
|
|
8
|
+
import fsx from "fs-extra";
|
|
9
|
+
|
|
10
|
+
import { getConfig } from "./core-utils";
|
|
11
|
+
|
|
12
|
+
const config = getConfig();
|
|
10
13
|
|
|
11
14
|
/**
|
|
12
15
|
* Creates an `apiCache` dir to store pages fetched from them API.
|
|
13
16
|
*/
|
|
14
|
-
|
|
15
|
-
const {
|
|
16
|
-
const apiCacheDir = path.join(
|
|
17
|
+
function createAPICacheDir() {
|
|
18
|
+
const { __cx } = config.paths();
|
|
19
|
+
const apiCacheDir = path.join(__cx, "apiCache");
|
|
17
20
|
|
|
18
|
-
if (!(
|
|
19
|
-
|
|
21
|
+
if (!fs.existsSync(apiCacheDir)) {
|
|
22
|
+
fs.mkdirSync(apiCacheDir);
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
console.info("Cache initialized");
|
|
@@ -24,11 +27,13 @@ async function createAPICacheDir(options: { basePath: string }) {
|
|
|
24
27
|
|
|
25
28
|
/**
|
|
26
29
|
* Generate a filename with a hash using a Petition object
|
|
30
|
+
*
|
|
31
|
+
* @todo Merge with createSha256
|
|
27
32
|
* @param petition An object
|
|
28
33
|
*/
|
|
29
|
-
|
|
30
|
-
const {
|
|
31
|
-
const apiCacheDir = path.join(
|
|
34
|
+
function generateFilenameWithHash(petition: Petition) {
|
|
35
|
+
const { __cx } = config.paths();
|
|
36
|
+
const apiCacheDir = path.join(__cx, "apiCache");
|
|
32
37
|
|
|
33
38
|
const hashSum = crypto.createHash("sha256");
|
|
34
39
|
hashSum.update(JSON.stringify(petition));
|
|
@@ -36,22 +41,35 @@ async function generateFilenameWithHash(petition: Petition) {
|
|
|
36
41
|
return `${apiCacheDir}/${hashSum.digest("hex")}`;
|
|
37
42
|
}
|
|
38
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Generate a filename with a hash using a string.
|
|
46
|
+
*
|
|
47
|
+
* @todo Merge with generateFilenameWithHash
|
|
48
|
+
* @param data A string to create a sha256 based on.
|
|
49
|
+
*/
|
|
50
|
+
function createSha256(data: string) {
|
|
51
|
+
const uniqueString = crypto.randomBytes(16).toString("hex");
|
|
52
|
+
const hash = crypto.createHash("sha256");
|
|
53
|
+
hash.update(`${data}${uniqueString}`);
|
|
54
|
+
|
|
55
|
+
return hash.digest("hex");
|
|
56
|
+
}
|
|
57
|
+
|
|
39
58
|
/**
|
|
40
59
|
* Save a file using a hash name.
|
|
41
60
|
*
|
|
42
61
|
* @param petition An object.
|
|
43
62
|
* @param content Content to be saved.
|
|
44
63
|
*/
|
|
45
|
-
|
|
46
|
-
const stringContent =
|
|
47
|
-
|
|
64
|
+
function saveCache<T>(petition: Petition, content: T) {
|
|
65
|
+
const stringContent =
|
|
66
|
+
typeof content === "string" ? content : JSON.stringify(content);
|
|
67
|
+
const filename = generateFilenameWithHash(petition);
|
|
48
68
|
const filepath = path.dirname(filename);
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
await fsp.mkdir(filepath, { recursive: true });
|
|
69
|
+
if (!fs.existsSync(filepath)) {
|
|
70
|
+
fs.mkdirSync(filepath, { recursive: true });
|
|
52
71
|
}
|
|
53
|
-
|
|
54
|
-
await fsp.writeFile(filename, stringContent, "utf8");
|
|
72
|
+
fs.writeFileSync(filename, stringContent, "utf8");
|
|
55
73
|
}
|
|
56
74
|
|
|
57
75
|
/**
|
|
@@ -60,13 +78,13 @@ async function saveCache<T>(petition: Petition, content: T) {
|
|
|
60
78
|
*
|
|
61
79
|
* @param petition An object
|
|
62
80
|
*/
|
|
63
|
-
|
|
81
|
+
function searchCacheData<T>(petition: Petition) {
|
|
64
82
|
try {
|
|
65
|
-
const file =
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return jsonData;
|
|
83
|
+
const file = generateFilenameWithHash(petition);
|
|
84
|
+
const jsonData = fsx.readJSONSync(file, {
|
|
85
|
+
encoding: "utf-8",
|
|
86
|
+
});
|
|
87
|
+
return jsonData as T;
|
|
70
88
|
} catch {
|
|
71
89
|
return null;
|
|
72
90
|
}
|
|
@@ -75,14 +93,15 @@ async function searchCacheData<T>(petition: Petition) {
|
|
|
75
93
|
/**
|
|
76
94
|
* Get site hashes from the file as an object
|
|
77
95
|
*/
|
|
78
|
-
|
|
79
|
-
const {
|
|
80
|
-
const apiCacheDir = path.join(
|
|
96
|
+
function getHashSites(): HashSites {
|
|
97
|
+
const { __cx } = config.paths();
|
|
98
|
+
const apiCacheDir = path.join(__cx, "apiCache");
|
|
81
99
|
const siteHasFilename = `${apiCacheDir}/siteHash.json`;
|
|
82
100
|
|
|
83
101
|
try {
|
|
84
|
-
const
|
|
85
|
-
|
|
102
|
+
const jsonData = fsx.readJSONSync(siteHasFilename, {
|
|
103
|
+
encoding: "utf-8",
|
|
104
|
+
});
|
|
86
105
|
return jsonData || {};
|
|
87
106
|
} catch {
|
|
88
107
|
return {};
|
|
@@ -95,18 +114,18 @@ async function getHashSites(): Promise<HashSites> {
|
|
|
95
114
|
* @param siteId The id of the site.
|
|
96
115
|
* @param siteHash The has of the site.
|
|
97
116
|
*/
|
|
98
|
-
|
|
99
|
-
const allHash =
|
|
117
|
+
function updatedSiteHash(siteId: number, siteHash: SiteHash) {
|
|
118
|
+
const allHash = getHashSites();
|
|
100
119
|
const lastHash = allHash[siteId];
|
|
101
|
-
const currentHash = siteHash || lastHash || Date.
|
|
120
|
+
const currentHash = siteHash || lastHash || new Date().valueOf();
|
|
102
121
|
|
|
103
|
-
const {
|
|
104
|
-
const apiCacheDir = path.join(
|
|
122
|
+
const { __cx } = config.paths();
|
|
123
|
+
const apiCacheDir = path.join(__cx, "apiCache");
|
|
105
124
|
const siteHasFilename = `${apiCacheDir}/siteHash.json`;
|
|
106
125
|
|
|
107
126
|
if (currentHash !== lastHash) {
|
|
108
127
|
allHash[siteId] = currentHash;
|
|
109
|
-
|
|
128
|
+
fs.writeFileSync(siteHasFilename, JSON.stringify(allHash), {
|
|
110
129
|
encoding: "utf-8",
|
|
111
130
|
});
|
|
112
131
|
}
|
|
@@ -114,4 +133,10 @@ async function updatedSiteHash(siteId: number, siteHash: SiteHash) {
|
|
|
114
133
|
return currentHash.toString();
|
|
115
134
|
}
|
|
116
135
|
|
|
117
|
-
export {
|
|
136
|
+
export {
|
|
137
|
+
createAPICacheDir,
|
|
138
|
+
createSha256,
|
|
139
|
+
saveCache,
|
|
140
|
+
searchCacheData,
|
|
141
|
+
updatedSiteHash,
|
|
142
|
+
};
|