pim-import 4.4.0 → 4.5.2
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/dist/browser.js +2 -2
- package/dist/browser.js.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/libs/contentful.js +6 -6
- package/dist/libs/contentful.js.map +1 -1
- package/dist/libs/pdf.js +52 -0
- package/dist/libs/pdf.js.map +1 -0
- package/dist/pim/methods/products.js +13 -15
- package/dist/pim/methods/products.js.map +1 -1
- package/package.json +1 -2
- package/src/algolia/config.ts +1 -1
- package/src/browser.ts +1 -1
- package/src/index.ts +1 -1
- package/src/libs/contentful.ts +8 -7
- package/src/libs/pdf.ts +89 -0
- package/src/pim/methods/products.ts +14 -15
- package/types/browser.d.ts +1 -1
- package/types/index.d.ts +1 -1
- package/types/libs/pdf.d.ts +17 -0
- package/types/libs/puppeteer.d.ts +1 -1
- package/types/pim/methods/products.d.ts +1 -1
- package/dist/libs/puppeteer.js +0 -171
- package/dist/libs/puppeteer.js.map +0 -1
- package/src/libs/puppeteer.ts +0 -263
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pim-import",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.5.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -41,7 +41,6 @@
|
|
|
41
41
|
"imgix-management-js": "^1.2.1",
|
|
42
42
|
"mime-types": "^2.1.33",
|
|
43
43
|
"node-gzip": "^1.1.2",
|
|
44
|
-
"puppeteer-core": "^15.4.0",
|
|
45
44
|
"remove-markdown": "^0.3.0",
|
|
46
45
|
"s3-streamlogger": "^1.7.0",
|
|
47
46
|
"winston": "^3.7.2"
|
package/src/algolia/config.ts
CHANGED
|
@@ -321,7 +321,7 @@ export const resetIndexSettings = (indexKey: AvailableIndicesKey) => {
|
|
|
321
321
|
};
|
|
322
322
|
|
|
323
323
|
/**
|
|
324
|
-
* Remove object from
|
|
324
|
+
* Remove object from specific index
|
|
325
325
|
*
|
|
326
326
|
* @param objectId The id of the object
|
|
327
327
|
* @param indexKey The key of the index
|
package/src/browser.ts
CHANGED
|
@@ -112,4 +112,4 @@ export { getLocalISOTime } from "./utils";
|
|
|
112
112
|
export { getStaticDailyProducts } from "./pim/endpoints";
|
|
113
113
|
export { log, setLogId } from "./libs/logs";
|
|
114
114
|
export { removeRecordsByStatus } from "./algolia/clean";
|
|
115
|
-
export { generatePDFByUrl } from "./libs/
|
|
115
|
+
export { generatePDFByUrl } from "./libs/pdf";
|
package/src/index.ts
CHANGED
package/src/libs/contentful.ts
CHANGED
|
@@ -1363,13 +1363,6 @@ export const archiveEntry = async (
|
|
|
1363
1363
|
isProduct: boolean = false
|
|
1364
1364
|
) => {
|
|
1365
1365
|
if (!entry.isArchived()) {
|
|
1366
|
-
if (entry.isPublished()) {
|
|
1367
|
-
log(`Unpublish entry ${entry.sys.id}`);
|
|
1368
|
-
entry = await entry.unpublish();
|
|
1369
|
-
}
|
|
1370
|
-
log(`Archive entry ${entry.sys.id}`);
|
|
1371
|
-
entry = await entry.archive();
|
|
1372
|
-
|
|
1373
1366
|
if (isProduct) {
|
|
1374
1367
|
const defEnvLocaleCode = await getEnvironmentDefaultLocaleCode();
|
|
1375
1368
|
const recordObjectId = entry.fields.code[defEnvLocaleCode];
|
|
@@ -1379,6 +1372,14 @@ export const archiveEntry = async (
|
|
|
1379
1372
|
log(`recordObjectId not found`, "WARN");
|
|
1380
1373
|
}
|
|
1381
1374
|
}
|
|
1375
|
+
|
|
1376
|
+
if (entry.isPublished()) {
|
|
1377
|
+
log(`Unpublish entry ${entry.sys.id}`);
|
|
1378
|
+
entry = await entry.unpublish();
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
log(`Archive entry ${entry.sys.id}`);
|
|
1382
|
+
entry = await entry.archive();
|
|
1382
1383
|
} else {
|
|
1383
1384
|
log(`Entry ${entry.sys.id} is already archived`);
|
|
1384
1385
|
}
|
package/src/libs/pdf.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { secondBetweenTwoDate } from "../utils";
|
|
2
|
+
import { log, serverUtils } from "./logs";
|
|
3
|
+
import axios from "@atoms-studio/axios";
|
|
4
|
+
|
|
5
|
+
interface HeaderColumns {
|
|
6
|
+
left?: string;
|
|
7
|
+
right?: string;
|
|
8
|
+
}
|
|
9
|
+
interface FooterColumns {
|
|
10
|
+
topLeft?: string;
|
|
11
|
+
topRight?: string;
|
|
12
|
+
bottomLeft?: string;
|
|
13
|
+
bottomRight?: string;
|
|
14
|
+
mail?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface PDFHeaderAndFooter {
|
|
18
|
+
header?: HeaderColumns;
|
|
19
|
+
footer?: FooterColumns;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const generatePDFByUrl = async (
|
|
23
|
+
url: string,
|
|
24
|
+
fileName: string = "",
|
|
25
|
+
contents?: PDFHeaderAndFooter
|
|
26
|
+
) => {
|
|
27
|
+
const pdf = await generatePDFByAWSLambda(url, fileName, contents);
|
|
28
|
+
|
|
29
|
+
return pdf;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Generate PDF by URL
|
|
34
|
+
*
|
|
35
|
+
* @param url E.g. https://my-site.com/slug
|
|
36
|
+
* @param fileName E.g. my-file.pdf
|
|
37
|
+
* @param contents E.g. {header{left:'Foo', right:'Bar'}, footer{left:'Foo', right:'Bar'}}
|
|
38
|
+
* @returns
|
|
39
|
+
*/
|
|
40
|
+
const generatePDFByAWSLambda = async (
|
|
41
|
+
url: string,
|
|
42
|
+
fileName: string = "",
|
|
43
|
+
contents?: PDFHeaderAndFooter
|
|
44
|
+
) => {
|
|
45
|
+
let logMsg = `generatePDFByUrl - url: ${url}`;
|
|
46
|
+
log(logMsg, "DEBUG");
|
|
47
|
+
if (serverUtils) {
|
|
48
|
+
serverUtils.log(logMsg);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!process.env.FPID_AWS_GEN_PDF_LAMBDA_URL) {
|
|
52
|
+
logMsg = `FPID_AWS_GEN_PDF_LAMBDA_URL environment variable not found`;
|
|
53
|
+
if (serverUtils) {
|
|
54
|
+
serverUtils.log(logMsg);
|
|
55
|
+
}
|
|
56
|
+
log(logMsg, "ERROR");
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const timeStart = new Date();
|
|
61
|
+
|
|
62
|
+
logMsg = `Axios request...`;
|
|
63
|
+
log(logMsg, "DEBUG");
|
|
64
|
+
if (serverUtils) {
|
|
65
|
+
serverUtils.log(logMsg);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const options: any = {
|
|
69
|
+
method: "POST",
|
|
70
|
+
url: process.env.FPID_AWS_GEN_PDF_LAMBDA_URL,
|
|
71
|
+
data: {
|
|
72
|
+
url,
|
|
73
|
+
fileName,
|
|
74
|
+
contents,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
const response = await axios.request(options);
|
|
78
|
+
|
|
79
|
+
logMsg = `End axios request - ${secondBetweenTwoDate(
|
|
80
|
+
timeStart,
|
|
81
|
+
new Date()
|
|
82
|
+
)} seconds`;
|
|
83
|
+
log(logMsg, "DEBUG");
|
|
84
|
+
if (serverUtils) {
|
|
85
|
+
serverUtils.log(logMsg);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return response.data;
|
|
89
|
+
};
|
|
@@ -53,7 +53,6 @@ import {
|
|
|
53
53
|
getFileFromS3,
|
|
54
54
|
saveJsonToS3,
|
|
55
55
|
upload as uploadToS3,
|
|
56
|
-
savePDFToS3,
|
|
57
56
|
} from "../../libs/s3";
|
|
58
57
|
import { getAudit, getProductDetails } from "../endpoints";
|
|
59
58
|
import { getCategoryTopicCode } from "./catalogs";
|
|
@@ -63,7 +62,7 @@ import {
|
|
|
63
62
|
getImgixPimUrlByOriginPath,
|
|
64
63
|
getOriginPathByPimUrl,
|
|
65
64
|
} from "../../libs/imgix";
|
|
66
|
-
import { generatePDFByUrl } from "../../libs/
|
|
65
|
+
import { generatePDFByUrl } from "../../libs/pdf";
|
|
67
66
|
|
|
68
67
|
export type AvailableProductStatus =
|
|
69
68
|
| "To be review"
|
|
@@ -1686,7 +1685,7 @@ export const audit = async (
|
|
|
1686
1685
|
log(`${++count} of ${total}`);
|
|
1687
1686
|
log(`I process the product ${audit.product} with status ${audit.what}`);
|
|
1688
1687
|
|
|
1689
|
-
log(`Search product entry...`);
|
|
1688
|
+
log(`Search product entry with id ${audit.product}...`);
|
|
1690
1689
|
let productEntry = entries.find(
|
|
1691
1690
|
(currentEntry) =>
|
|
1692
1691
|
currentEntry?.fields?.code?.[defaultEnvironmentLocaleCode] ===
|
|
@@ -1712,8 +1711,8 @@ export const audit = async (
|
|
|
1712
1711
|
|
|
1713
1712
|
log(`Founded product with id ${productEntry.sys.id}`);
|
|
1714
1713
|
|
|
1715
|
-
log(`Search product page entry...`);
|
|
1716
1714
|
const pageEntryFromId = getProductPageIdByCode(audit.product);
|
|
1715
|
+
log(`Search product page entry with id ${pageEntryFromId}...`);
|
|
1717
1716
|
let pageEntryFrom = entriesPage.find(
|
|
1718
1717
|
(currentEntry) => currentEntry.sys.id === pageEntryFromId
|
|
1719
1718
|
);
|
|
@@ -1740,7 +1739,14 @@ export const audit = async (
|
|
|
1740
1739
|
log(`Set the product status as archive...`);
|
|
1741
1740
|
// Set the product status as archive
|
|
1742
1741
|
productEntry = await archiveEntry(productEntry, true);
|
|
1743
|
-
if (pageEntryFrom
|
|
1742
|
+
if (!pageEntryFrom) {
|
|
1743
|
+
log(`${pageEntryFromId} page from not found`);
|
|
1744
|
+
} else if (pageEntryFrom.isArchived()) {
|
|
1745
|
+
log(
|
|
1746
|
+
`Can't create redirect ${audit.product} to ${audit.upgradeProduct} because the page ${pageEntryFrom.sys.id} is archived.`,
|
|
1747
|
+
"WARN"
|
|
1748
|
+
);
|
|
1749
|
+
} else if (pageEntryFrom?.fields) {
|
|
1744
1750
|
if (audit.upgradeProduct && audit.upgradeProduct !== "0") {
|
|
1745
1751
|
log(
|
|
1746
1752
|
`Creating redirect from ${audit.product} to ${audit.upgradeProduct}`
|
|
@@ -1769,8 +1775,6 @@ export const audit = async (
|
|
|
1769
1775
|
}
|
|
1770
1776
|
|
|
1771
1777
|
pageEntryFrom = await archiveEntry(pageEntryFrom);
|
|
1772
|
-
} else {
|
|
1773
|
-
log(`${pageEntryFromId} page from not found`);
|
|
1774
1778
|
}
|
|
1775
1779
|
} else if (
|
|
1776
1780
|
audit.what === "REMOVE_PRODUCT_RELATIONS" &&
|
|
@@ -1986,15 +1990,10 @@ export const generateTechSpecPdf = async (topicProductId: string) => {
|
|
|
1986
1990
|
serverUtils.updateProgress(35);
|
|
1987
1991
|
}
|
|
1988
1992
|
|
|
1989
|
-
const
|
|
1990
|
-
if (
|
|
1991
|
-
if (serverUtils) {
|
|
1992
|
-
serverUtils.log("Saving to S3...");
|
|
1993
|
-
serverUtils.updateProgress(45);
|
|
1994
|
-
}
|
|
1995
|
-
const s3Url = await savePDFToS3(pdf, fileName, "tech-spec");
|
|
1993
|
+
const s3Url = await generatePDFByUrl(layoutUrl, fileName, contents);
|
|
1994
|
+
if (s3Url) {
|
|
1996
1995
|
if (serverUtils) {
|
|
1997
|
-
serverUtils.log("
|
|
1996
|
+
serverUtils.log("PDF saved to S3!");
|
|
1998
1997
|
serverUtils.log(`Update techSpec field...`);
|
|
1999
1998
|
serverUtils.updateProgress(75);
|
|
2000
1999
|
}
|
package/types/browser.d.ts
CHANGED
|
@@ -26,4 +26,4 @@ export { getLocalISOTime } from "./utils";
|
|
|
26
26
|
export { getStaticDailyProducts } from "./pim/endpoints";
|
|
27
27
|
export { log, setLogId } from "./libs/logs";
|
|
28
28
|
export { removeRecordsByStatus } from "./algolia/clean";
|
|
29
|
-
export { generatePDFByUrl } from "./libs/
|
|
29
|
+
export { generatePDFByUrl } from "./libs/pdf";
|
package/types/index.d.ts
CHANGED
|
@@ -27,4 +27,4 @@ export { getLocalISOTime } from "./utils";
|
|
|
27
27
|
export { getStaticDailyProducts, getLatestProducts } from "./pim/endpoints";
|
|
28
28
|
export { log, setServerUtils, setLogId, setLogPath, setLogFilename, getLogFolder, } from "./libs/logs";
|
|
29
29
|
export { removeRecordsByStatus } from "./algolia/clean";
|
|
30
|
-
export { generatePDFByUrl } from "./libs/
|
|
30
|
+
export { generatePDFByUrl } from "./libs/pdf";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
interface HeaderColumns {
|
|
2
|
+
left?: string;
|
|
3
|
+
right?: string;
|
|
4
|
+
}
|
|
5
|
+
interface FooterColumns {
|
|
6
|
+
topLeft?: string;
|
|
7
|
+
topRight?: string;
|
|
8
|
+
bottomLeft?: string;
|
|
9
|
+
bottomRight?: string;
|
|
10
|
+
mail?: string;
|
|
11
|
+
}
|
|
12
|
+
interface PDFHeaderAndFooter {
|
|
13
|
+
header?: HeaderColumns;
|
|
14
|
+
footer?: FooterColumns;
|
|
15
|
+
}
|
|
16
|
+
export declare const generatePDFByUrl: (url: string, fileName?: string, contents?: PDFHeaderAndFooter | undefined) => Promise<any>;
|
|
17
|
+
export {};
|
|
@@ -13,5 +13,5 @@ interface PDFHeaderAndFooter {
|
|
|
13
13
|
header?: HeaderColumns;
|
|
14
14
|
footer?: FooterColumns;
|
|
15
15
|
}
|
|
16
|
-
export declare const generatePDFByUrl: (url: string,
|
|
16
|
+
export declare const generatePDFByUrl: (url: string, fileName?: string, contents?: PDFHeaderAndFooter | undefined) => Promise<any>;
|
|
17
17
|
export {};
|
|
@@ -38,7 +38,7 @@ export declare const audit: (lastModified: string, catalog: AvailableCatalogs, o
|
|
|
38
38
|
total?: undefined;
|
|
39
39
|
}>;
|
|
40
40
|
export declare const importProductByCode: (code: string, catalog: AvailableCatalogs) => Promise<void>;
|
|
41
|
-
export declare const generateTechSpecPdf: (topicProductId: string) => Promise<
|
|
41
|
+
export declare const generateTechSpecPdf: (topicProductId: string) => Promise<any>;
|
|
42
42
|
export declare const setProductsAutodescription: (catalog: AvailableCatalogs, offset?: number, limit?: number) => Promise<{
|
|
43
43
|
catalog: AvailableCatalogs;
|
|
44
44
|
offset: number;
|
package/dist/libs/puppeteer.js
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.generatePDFByUrl = void 0;
|
|
7
|
-
const puppeteer_core_1 = __importDefault(require("puppeteer-core"));
|
|
8
|
-
const utils_1 = require("../utils");
|
|
9
|
-
const logs_1 = require("./logs");
|
|
10
|
-
const axios_1 = __importDefault(require("@atoms-studio/axios"));
|
|
11
|
-
const generatePDFByUrl = async (url, path = "", contents) => {
|
|
12
|
-
logs_1.log(`generatePDFByUrl - url: ${url} path: ${path}`);
|
|
13
|
-
if (!process.env.BROWSERLESS_TOKEN) {
|
|
14
|
-
logs_1.log(`BROWSERLESS_TOKEN environment variable not found`, "ERROR");
|
|
15
|
-
}
|
|
16
|
-
let pdf;
|
|
17
|
-
if (path) {
|
|
18
|
-
pdf = await generatePDFByUrlWithPuppeteer(url, path, contents);
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
pdf = await generatePDFByUrlWithAxios(url, contents);
|
|
22
|
-
}
|
|
23
|
-
return pdf;
|
|
24
|
-
};
|
|
25
|
-
exports.generatePDFByUrl = generatePDFByUrl;
|
|
26
|
-
const getPDFOptions = (contents) => {
|
|
27
|
-
const pageMargin = "33px";
|
|
28
|
-
const headerTemplate = ` `;
|
|
29
|
-
const footerTopLeft = contents?.footer?.topLeft || ``;
|
|
30
|
-
const footerTopRight = contents?.footer?.topRight || ``;
|
|
31
|
-
const date = new Date(new Date().toLocaleString("en-US", { timeZone: "Europe/Rome" }));
|
|
32
|
-
const footerBottomLeft = contents?.footer?.bottomLeft ||
|
|
33
|
-
`©2022 Flos - P.IVA 00290820174 - ${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`;
|
|
34
|
-
const footerMail = contents?.footer?.mail || "info@flos.com";
|
|
35
|
-
const footerBottomRight = contents?.footer?.bottomRight ||
|
|
36
|
-
`<a style="text-decoration: none; color:#000" href="https://professional.flos.com/en/global/">professional.flos.com</a> | <a style="text-decoration: none; color:#000" href="mailto:${footerMail}">${footerMail}</a> <span class="pageNumber" style="margin-left:4px"></span>/<span class="totalPages"></span>`;
|
|
37
|
-
const footerBottom = "In our constantly evolving world and business, technical upgrades happen every day. This means all product specifications and data are subject to change without warning in order t improve reliability, function, performance or otherwise. We make every effort to ensure the accuracy of our product images, however due to different lighting and screens used for viewing, the colours may vary. Images are indicative of the quality and style of the product but may not represent the precise details of the product you receive. This is because we are constantly working to make improvements. For aesthetic reasons, cables and/or electrical elements are often not shown. Refer to technical data sheets for all technical data. Images and colours are not part of any contract or warranty in any way.";
|
|
38
|
-
const footerStyle = `<style>.pdf-footer { font-family: Arial, Helvetica, sans-serif; font-weight: 300; }</style>`;
|
|
39
|
-
const footerTemplate = `${footerStyle}<div class="pdf-footer" style="margin: 0 ${pageMargin}; padding: 10px 0; width: 100%; font-size: 5pt; font-weight:300;"> <div class="pdf-row" style="display: flex; flex-wrap: wrap; border-bottom: 1px solid rgb(168, 168, 168); border-bottom-style:solid; padding-bottom: 6px; justify-content: center; align-items: center;"> <span class="left" style="margin:0; padding: 0; width: 80%; white-space: normal; overflow: hidden;">${footerTopLeft}</span> <span class="right" style="margin:0; padding: 0; width: 20%; text-align: right;">${footerTopRight}</span> </div> <div class="pdf-row" style="display: flex; flex-wrap: wrap; padding-top: 6px; justify-content: center; align-items: center;"> <span style="width: 50%;">${footerBottomLeft}</span> <span style="width: 50%; text-align: right;"> ${footerBottomRight} </span> </div><div class="pdf-row" style="font-size: 4pt; margin-top:5px;">${footerBottom}</div> </div>`;
|
|
40
|
-
return {
|
|
41
|
-
format: "A4",
|
|
42
|
-
printBackground: true,
|
|
43
|
-
displayHeaderFooter: true,
|
|
44
|
-
headerTemplate,
|
|
45
|
-
footerTemplate,
|
|
46
|
-
margin: {
|
|
47
|
-
top: pageMargin,
|
|
48
|
-
left: pageMargin,
|
|
49
|
-
right: pageMargin,
|
|
50
|
-
bottom: "114px",
|
|
51
|
-
},
|
|
52
|
-
};
|
|
53
|
-
};
|
|
54
|
-
const generatePDFByUrlWithPuppeteer = async (url, path = "", contents) => {
|
|
55
|
-
logs_1.log(`generatePDFByUrl - url: ${url} path: ${path}`);
|
|
56
|
-
if (!process.env.BROWSERLESS_TOKEN) {
|
|
57
|
-
logs_1.log(`BROWSERLESS_TOKEN environment variable not found`, "ERROR");
|
|
58
|
-
}
|
|
59
|
-
const timeStart = new Date();
|
|
60
|
-
let logMsg = `Before puppeteer.connect - ${utils_1.secondBetweenTwoDate(timeStart, new Date())} seconds`;
|
|
61
|
-
logs_1.log(logMsg, "DEBUG");
|
|
62
|
-
if (logs_1.serverUtils) {
|
|
63
|
-
logs_1.serverUtils.log(logMsg);
|
|
64
|
-
}
|
|
65
|
-
const browser = await puppeteer_core_1.default.connect({
|
|
66
|
-
browserWSEndpoint: `wss://chrome.browserless.io?token=${process.env.BROWSERLESS_TOKEN}`,
|
|
67
|
-
});
|
|
68
|
-
logMsg = `After puppeteer.connect - ${utils_1.secondBetweenTwoDate(timeStart, new Date())} seconds`;
|
|
69
|
-
logs_1.log(logMsg, "DEBUG");
|
|
70
|
-
if (logs_1.serverUtils) {
|
|
71
|
-
logs_1.serverUtils.log(logMsg);
|
|
72
|
-
}
|
|
73
|
-
const page = await browser.newPage();
|
|
74
|
-
logMsg = `Before page.goto - ${utils_1.secondBetweenTwoDate(timeStart, new Date())} seconds`;
|
|
75
|
-
logs_1.log(logMsg, "DEBUG");
|
|
76
|
-
if (logs_1.serverUtils) {
|
|
77
|
-
logs_1.serverUtils.log(logMsg);
|
|
78
|
-
}
|
|
79
|
-
const res = await page.goto(url, { waitUntil: "networkidle0", timeout: 0 });
|
|
80
|
-
logMsg = `After page.goto - ${utils_1.secondBetweenTwoDate(timeStart, new Date())} seconds`;
|
|
81
|
-
logs_1.log(logMsg, "DEBUG");
|
|
82
|
-
if (logs_1.serverUtils) {
|
|
83
|
-
logs_1.serverUtils.log(logMsg);
|
|
84
|
-
}
|
|
85
|
-
if (!res) {
|
|
86
|
-
await browser.close();
|
|
87
|
-
console.log(`Error no response`);
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
if (res.status() !== 200) {
|
|
91
|
-
await browser.close();
|
|
92
|
-
console.log(`Error ${res.status()} - ${res.url()}`);
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
try {
|
|
96
|
-
const options = getPDFOptions(contents);
|
|
97
|
-
if (path) {
|
|
98
|
-
options.path = path;
|
|
99
|
-
}
|
|
100
|
-
logMsg = `Before page.pdf - ${utils_1.secondBetweenTwoDate(timeStart, new Date())} seconds`;
|
|
101
|
-
logs_1.log(logMsg, "DEBUG");
|
|
102
|
-
if (logs_1.serverUtils) {
|
|
103
|
-
logs_1.serverUtils.log(logMsg);
|
|
104
|
-
}
|
|
105
|
-
const pdf = await page.pdf(options);
|
|
106
|
-
logMsg = `After page.pdf - ${utils_1.secondBetweenTwoDate(timeStart, new Date())} seconds`;
|
|
107
|
-
logs_1.log(logMsg, "DEBUG");
|
|
108
|
-
if (logs_1.serverUtils) {
|
|
109
|
-
logs_1.serverUtils.log(logMsg);
|
|
110
|
-
}
|
|
111
|
-
const timeEnd = new Date();
|
|
112
|
-
const seconds = utils_1.secondBetweenTwoDate(timeStart, timeEnd);
|
|
113
|
-
logMsg = `Request time: ${seconds} seconds - generatePDFByUrl`;
|
|
114
|
-
logs_1.log(logMsg, "DEBUG");
|
|
115
|
-
if (logs_1.serverUtils) {
|
|
116
|
-
logs_1.serverUtils.log(logMsg);
|
|
117
|
-
}
|
|
118
|
-
return pdf;
|
|
119
|
-
}
|
|
120
|
-
catch (err) {
|
|
121
|
-
console.log(err);
|
|
122
|
-
return null;
|
|
123
|
-
}
|
|
124
|
-
finally {
|
|
125
|
-
await browser.close();
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
const generatePDFByUrlWithAxios = async (url, contents) => {
|
|
129
|
-
let logMsg = `generatePDFByUrl - url: ${url}`;
|
|
130
|
-
logs_1.log(logMsg, "DEBUG");
|
|
131
|
-
if (logs_1.serverUtils) {
|
|
132
|
-
logs_1.serverUtils.log(logMsg);
|
|
133
|
-
}
|
|
134
|
-
const timeStart = new Date();
|
|
135
|
-
if (!process.env.BROWSERLESS_TOKEN) {
|
|
136
|
-
logMsg = `BROWSERLESS_TOKEN environment variable not found`;
|
|
137
|
-
if (logs_1.serverUtils) {
|
|
138
|
-
logs_1.serverUtils.log(logMsg);
|
|
139
|
-
}
|
|
140
|
-
logs_1.log(logMsg, "ERROR");
|
|
141
|
-
return null;
|
|
142
|
-
}
|
|
143
|
-
logMsg = `Before axios request - ${utils_1.secondBetweenTwoDate(timeStart, new Date())} seconds`;
|
|
144
|
-
logs_1.log(logMsg, "DEBUG");
|
|
145
|
-
if (logs_1.serverUtils) {
|
|
146
|
-
logs_1.serverUtils.log(logMsg);
|
|
147
|
-
}
|
|
148
|
-
const options = {
|
|
149
|
-
method: "POST",
|
|
150
|
-
url: "https://chrome.browserless.io/pdf",
|
|
151
|
-
params: { token: process.env.BROWSERLESS_TOKEN },
|
|
152
|
-
responseType: "arraybuffer",
|
|
153
|
-
headers: {
|
|
154
|
-
"cache-control": "no-cache",
|
|
155
|
-
"content-type": "application/json",
|
|
156
|
-
},
|
|
157
|
-
data: {
|
|
158
|
-
url,
|
|
159
|
-
gotoOptions: { waitUntil: "networkidle0" },
|
|
160
|
-
options: getPDFOptions(contents),
|
|
161
|
-
},
|
|
162
|
-
};
|
|
163
|
-
const response = await axios_1.default.request(options);
|
|
164
|
-
logMsg = `after axios request - ${utils_1.secondBetweenTwoDate(timeStart, new Date())} seconds`;
|
|
165
|
-
logs_1.log(logMsg, "DEBUG");
|
|
166
|
-
if (logs_1.serverUtils) {
|
|
167
|
-
logs_1.serverUtils.log(logMsg);
|
|
168
|
-
}
|
|
169
|
-
return response.data;
|
|
170
|
-
};
|
|
171
|
-
//# sourceMappingURL=puppeteer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"puppeteer.js","sourceRoot":"","sources":["../../src/libs/puppeteer.ts"],"names":[],"mappings":";;;;;;AAAA,oEAAuC;AACvC,oCAAgD;AAChD,iCAA0C;AAE1C,gEAAwC;AAmBjC,MAAM,gBAAgB,GAAG,KAAK,EACnC,GAAW,EACX,OAAe,EAAE,EACjB,QAA6B,EAC7B,EAAE;IACF,UAAG,CAAC,2BAA2B,GAAG,UAAU,IAAI,EAAE,CAAC,CAAC;IAEpD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE;QAClC,UAAG,CAAC,kDAAkD,EAAE,OAAO,CAAC,CAAC;KAClE;IACD,IAAI,GAAG,CAAC;IACR,IAAI,IAAI,EAAE;QACR,GAAG,GAAG,MAAM,6BAA6B,CAAC,GAAG,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;KAChE;SAAM;QACL,GAAG,GAAG,MAAM,yBAAyB,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;KACtD;IAED,OAAO,GAAG,CAAC;AACb,CAAC,CAAC;AAlBW,QAAA,gBAAgB,oBAkB3B;AAGF,MAAM,aAAa,GAAG,CACpB,QAAwC,EAC5B,EAAE;IACd,MAAM,UAAU,GAAG,MAAM,CAAC;IAC1B,MAAM,cAAc,GAAG,GAAG,CAAC;IAC3B,MAAM,aAAa,GAAG,QAAQ,EAAE,MAAM,EAAE,OAAO,IAAI,EAAE,CAAC;IACtD,MAAM,cAAc,GAAG,QAAQ,EAAE,MAAM,E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