pim-import 4.3.1 → 4.5.0
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/algolia/downloads.js +2 -2
- package/dist/algolia/downloads.js.map +1 -1
- package/dist/algolia/families.js +2 -2
- package/dist/algolia/families.js.map +1 -1
- package/dist/algolia/models.js +5 -5
- package/dist/algolia/models.js.map +1 -1
- package/dist/algolia/news.js +3 -3
- package/dist/algolia/news.js.map +1 -1
- package/dist/algolia/pressRelease.js +3 -3
- package/dist/algolia/pressRelease.js.map +1 -1
- package/dist/algolia/pressReview.js +1 -1
- package/dist/algolia/pressReview.js.map +1 -1
- package/dist/algolia/products.js +20 -20
- package/dist/algolia/products.js.map +1 -1
- package/dist/algolia/projects.js +1 -1
- package/dist/algolia/projects.js.map +1 -1
- package/dist/algolia/stories.js +8 -10
- package/dist/algolia/stories.js.map +1 -1
- package/dist/algolia/subFamilies.js +5 -5
- package/dist/algolia/subFamilies.js.map +1 -1
- package/dist/algolia/subModels.js +6 -6
- package/dist/algolia/subModels.js.map +1 -1
- package/dist/browser.js +2 -2
- package/dist/browser.js.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/libs/contentful-cda.js +6 -11
- package/dist/libs/contentful-cda.js.map +1 -1
- package/dist/libs/contentful.js +13 -19
- package/dist/libs/contentful.js.map +1 -1
- package/dist/libs/imgix.js +3 -3
- package/dist/libs/imgix.js.map +1 -1
- package/dist/libs/pdf.js +52 -0
- package/dist/libs/pdf.js.map +1 -0
- package/dist/pim/methods/bulkPublish.js +4 -4
- package/dist/pim/methods/products.js +4 -9
- package/dist/pim/methods/products.js.map +1 -1
- package/dist/utils.js +11 -1
- package/dist/utils.js.map +1 -1
- package/package.json +1 -2
- package/src/algolia/downloads.ts +1 -3
- package/src/algolia/families.ts +2 -4
- package/src/algolia/models.ts +4 -7
- package/src/algolia/news.ts +10 -4
- package/src/algolia/pressRelease.ts +11 -4
- package/src/algolia/pressReview.ts +8 -2
- package/src/algolia/products.ts +9 -26
- package/src/algolia/projects.ts +6 -2
- package/src/algolia/stories.ts +21 -12
- package/src/algolia/subFamilies.ts +9 -9
- package/src/algolia/subModels.ts +10 -10
- package/src/browser.ts +1 -1
- package/src/index.ts +2 -2
- package/src/libs/contentful-cda.ts +17 -15
- package/src/libs/contentful.ts +15 -24
- package/src/libs/imgix.ts +6 -4
- package/src/libs/pdf.ts +89 -0
- package/src/pim/methods/bulkPublish.ts +2 -2
- package/src/pim/methods/products.ts +13 -14
- package/src/utils.ts +22 -0
- package/types/browser.d.ts +1 -1
- package/types/index.d.ts +2 -2
- package/types/libs/contentful-cda.d.ts +1 -1
- package/types/libs/contentful.d.ts +1 -1
- package/types/libs/pdf.d.ts +17 -0
- package/types/libs/puppeteer.d.ts +1 -1
- package/types/pim/methods/bulkPublish.d.ts +1 -1
- package/types/pim/methods/products.d.ts +1 -1
- package/types/utils.d.ts +1 -0
- package/dist/libs/puppeteer.js +0 -171
- package/dist/libs/puppeteer.js.map +0 -1
- package/src/libs/puppeteer.ts +0 -263
|
@@ -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] ===
|
|
@@ -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/src/utils.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
} from "./types";
|
|
9
9
|
import { AssetsEntity } from "./resources/ProductDetails";
|
|
10
10
|
import { log } from "./libs/logs";
|
|
11
|
+
import { cfLocales, getEnvironmentDefaultLocaleCode } from "./libs/contentful";
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Merge two objects
|
|
@@ -326,3 +327,24 @@ export const replaceAll = (original: string, from: string, to: string) => {
|
|
|
326
327
|
const regExp = new RegExp(from, "g");
|
|
327
328
|
return original.replace(regExp, to);
|
|
328
329
|
};
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Get all locale translations
|
|
333
|
+
*
|
|
334
|
+
*
|
|
335
|
+
* @param {object} values
|
|
336
|
+
*
|
|
337
|
+
* @example getAllTranslations({en:foo}) => {en:foo, it: foo, de: foo...}
|
|
338
|
+
*
|
|
339
|
+
* @returns
|
|
340
|
+
*/
|
|
341
|
+
export const getAllTranslations = async (values: any) => {
|
|
342
|
+
const defaultLocale = await getEnvironmentDefaultLocaleCode();
|
|
343
|
+
|
|
344
|
+
const newValue: any = {};
|
|
345
|
+
for (const locale of cfLocales) {
|
|
346
|
+
newValue[locale] = values?.[locale] || values[defaultLocale] || "";
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return keysToLowerCase(newValue);
|
|
350
|
+
};
|
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
|
@@ -9,7 +9,7 @@ export { importSubModels } from "./pim/methods/submodels";
|
|
|
9
9
|
export { importSubFamilies } from "./pim/methods/subfamilies";
|
|
10
10
|
export { importFamilies } from "./pim/methods/families";
|
|
11
11
|
export { importProductByCode, setProductsRelationships, setProductRelationships, getAllProductEntriesByCatalog, audit, generateTechSpecPdf, setProductsAutodescription, getProductAutodescription, setProductAutodescriptionByTopicId, removeProductFromColorVariantsByProductLine, removeAllProductModelProductRelations, getProductPageIdByCode, } from "./pim/methods/products";
|
|
12
|
-
export {
|
|
12
|
+
export { publishAllProductDrafts } from "./pim/methods/bulkPublish";
|
|
13
13
|
export { resetIndexSettings } from "./algolia/config";
|
|
14
14
|
export { reindexFamilies, reindexFamily, removeFamilyObject, } from "./algolia/families";
|
|
15
15
|
export { reindexSubFamilies, reindexSubFamily, removeSubFamilyObject, } from "./algolia/subFamilies";
|
|
@@ -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";
|
|
@@ -8,7 +8,7 @@ export declare const getEntryByID: (entryID: string, contentTypeId: string, sele
|
|
|
8
8
|
export declare const getAllEntries: (contentType: string, select?: string | undefined, filterKey?: string | undefined, filterValue?: string | undefined, limit?: number, otherFilters?: OtherFilters[] | undefined, include?: number) => Promise<Entry[]>;
|
|
9
9
|
export declare const getAssetDetails: (assetId: string) => Promise<AssetPropFieldsWithoutLocaleKey>;
|
|
10
10
|
export declare const getEntryImageDetails: (entry: Entry, fieldKey: string) => Promise<string>;
|
|
11
|
-
export declare const getTopicDetails: (topicEntry: Entry, fieldKey: string, contentType: string,
|
|
11
|
+
export declare const getTopicDetails: (topicEntry: Entry, fieldKey: string, contentType: string, showRelatedEntities?: boolean, isCatalogEntry?: boolean, skipPageSlugs?: boolean) => Promise<TopicDetailsResponse[]>;
|
|
12
12
|
export declare const getSubFamilySlugDetails: (topicSubFamily: Entry) => Promise<{
|
|
13
13
|
catalogPageSlugs: any;
|
|
14
14
|
familyPageSlugs: any;
|
|
@@ -47,7 +47,7 @@ export declare const deleteEntries: (contentType: string, catalogCode?: Availabl
|
|
|
47
47
|
export declare const addToRelationFields: (entry: Entry | any, fieldKey: string, entrySysIdValue: string, manyReferences?: boolean, resetOldValues?: boolean, linkType?: "Asset" | "Entry") => Promise<Entry>;
|
|
48
48
|
export declare const removeFromRelationFields: (entry: Entry | any, fieldKey: string, entrySysIdValue: string, manyReferences?: boolean) => Promise<any>;
|
|
49
49
|
export declare const addFieldValue: (entry: Entry | any, fieldKey: string, fieldValue: any, manyReferences?: boolean) => Promise<any>;
|
|
50
|
-
export declare const getTopicDetails: (topicEntry: Entry, fieldKey: string, contentType: string,
|
|
50
|
+
export declare const getTopicDetails: (topicEntry: Entry, fieldKey: string, contentType: string, showRelatedEntities?: boolean, isCatalogEntry?: boolean, skipPageSlugs?: boolean) => Promise<TopicDetailsResponse[]>;
|
|
51
51
|
export declare const getAssetDetails: (assetId: string) => Promise<AssetPropFields>;
|
|
52
52
|
export declare const getEntryImageDetails: (entry: Entry, fieldKey: string) => Promise<string>;
|
|
53
53
|
export declare const getDictionaryJson: () => Promise<any>;
|
|
@@ -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 {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AvailableCatalogs } from "../../types";
|
|
2
2
|
import type { Entry } from "contentful-management/dist/typings/entities/entry";
|
|
3
3
|
export declare const publishTopicProduct: (productEntry: Entry) => Promise<void>;
|
|
4
|
-
export declare const
|
|
4
|
+
export declare const publishAllProductDrafts: (filters: {
|
|
5
5
|
catalog?: AvailableCatalogs;
|
|
6
6
|
family?: string;
|
|
7
7
|
}, limit?: number) => Promise<{
|
|
@@ -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/types/utils.d.ts
CHANGED
|
@@ -17,3 +17,4 @@ export declare const capitalizeFirstLetter: (text: string) => string;
|
|
|
17
17
|
export declare const keysToLowerCase: (obj: any, includeNestedKeys?: boolean) => any;
|
|
18
18
|
export declare const addProductFieldValueCodesByPimDetails: (pimDetails: any, productFields: any, fieldKey: string, fieldParentKey?: string | undefined) => string[];
|
|
19
19
|
export declare const replaceAll: (original: string, from: string, to: string) => string;
|
|
20
|
+
export declare const getAllTranslations: (values: any) => Promise<any>;
|
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,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kBAAW,EAAE;QACf,kBAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;KACzB;IAED,MAAM,OAAO,GAAQ;QACnB,MAAM,EAAE,MAAM;QACd,GAAG,EAAE,mCAAmC;QACxC,MAAM,EAAE,EAAE,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,iBAAiB,EAAE;QAChD,YAAY,EAAE,aAAa;QAC3B,OAAO,EAAE;YACP,eAAe,EAAE,UAAU;YAC3B,cAAc,EAAE,kBAAkB;SACnC;QACD,IAAI,EAAE;YACJ,GAAG;YACH,WAAW,EAAE,EAAE,SAAS,EAAE,cAAc,EAAE;YAC1C,OAAO,EAAE,aAAa,CAAC,QAAQ,CAAC;SACjC;KACF,CAAC;IAEF,MAAM,QAAQ,GAAG,MAAM,eAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAE9C,MAAM,GAAG,yBAAyB,4BAAoB,CACpD,SAAS,EACT,IAAI,IAAI,EAAE,CACX,UAAU,CAAC;IACZ,UAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrB,IAAI,kBAAW,EAAE;QACf,kBAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;KACzB;IAED,OAAO,QAAQ,CAAC,IAAI,CAAC;AACvB,CAAC,CAAC"}
|
package/src/libs/puppeteer.ts
DELETED
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
import puppeteer from "puppeteer-core";
|
|
2
|
-
import { secondBetweenTwoDate } from "../utils";
|
|
3
|
-
import { log, serverUtils } from "./logs";
|
|
4
|
-
import { PDFOptions } from "puppeteer-core/lib/types";
|
|
5
|
-
import axios from "@atoms-studio/axios";
|
|
6
|
-
|
|
7
|
-
interface HeaderColumns {
|
|
8
|
-
left?: string;
|
|
9
|
-
right?: string;
|
|
10
|
-
}
|
|
11
|
-
interface FooterColumns {
|
|
12
|
-
topLeft?: string;
|
|
13
|
-
topRight?: string;
|
|
14
|
-
bottomLeft?: string;
|
|
15
|
-
bottomRight?: string;
|
|
16
|
-
mail?: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
interface PDFHeaderAndFooter {
|
|
20
|
-
header?: HeaderColumns;
|
|
21
|
-
footer?: FooterColumns;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const generatePDFByUrl = async (
|
|
25
|
-
url: string,
|
|
26
|
-
path: string = "",
|
|
27
|
-
contents?: PDFHeaderAndFooter
|
|
28
|
-
) => {
|
|
29
|
-
log(`generatePDFByUrl - url: ${url} path: ${path}`);
|
|
30
|
-
|
|
31
|
-
if (!process.env.BROWSERLESS_TOKEN) {
|
|
32
|
-
log(`BROWSERLESS_TOKEN environment variable not found`, "ERROR");
|
|
33
|
-
}
|
|
34
|
-
let pdf;
|
|
35
|
-
if (path) {
|
|
36
|
-
pdf = await generatePDFByUrlWithPuppeteer(url, path, contents);
|
|
37
|
-
} else {
|
|
38
|
-
pdf = await generatePDFByUrlWithAxios(url, contents);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return pdf;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
// See: https://github.com/puppeteer/puppeteer/blob/v11.0.0/docs/api.md#pagepdfoptions
|
|
45
|
-
const getPDFOptions = (
|
|
46
|
-
contents: PDFHeaderAndFooter | undefined
|
|
47
|
-
): PDFOptions => {
|
|
48
|
-
const pageMargin = "33px";
|
|
49
|
-
const headerTemplate = ` `;
|
|
50
|
-
const footerTopLeft = contents?.footer?.topLeft || ``;
|
|
51
|
-
const footerTopRight = contents?.footer?.topRight || ``;
|
|
52
|
-
const date = new Date(
|
|
53
|
-
new Date().toLocaleString("en-US", { timeZone: "Europe/Rome" })
|
|
54
|
-
);
|
|
55
|
-
const footerBottomLeft =
|
|
56
|
-
contents?.footer?.bottomLeft ||
|
|
57
|
-
`©2022 Flos - P.IVA 00290820174 - ${
|
|
58
|
-
date.getMonth() + 1
|
|
59
|
-
}/${date.getDate()}/${date.getFullYear()}`;
|
|
60
|
-
const footerMail = contents?.footer?.mail || "info@flos.com";
|
|
61
|
-
const footerBottomRight =
|
|
62
|
-
contents?.footer?.bottomRight ||
|
|
63
|
-
`<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>`;
|
|
64
|
-
const footerBottom =
|
|
65
|
-
"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.";
|
|
66
|
-
const footerStyle = `<style>.pdf-footer { font-family: Arial, Helvetica, sans-serif; font-weight: 300; }</style>`;
|
|
67
|
-
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>`;
|
|
68
|
-
|
|
69
|
-
// See: https://github.com/puppeteer/puppeteer/blob/v11.0.0/docs/api.md#pagepdfoptions
|
|
70
|
-
return {
|
|
71
|
-
format: "A4",
|
|
72
|
-
printBackground: true,
|
|
73
|
-
displayHeaderFooter: true,
|
|
74
|
-
headerTemplate,
|
|
75
|
-
footerTemplate,
|
|
76
|
-
margin: {
|
|
77
|
-
top: pageMargin,
|
|
78
|
-
left: pageMargin,
|
|
79
|
-
right: pageMargin,
|
|
80
|
-
bottom: "114px",
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Generate PDF by URL
|
|
87
|
-
*
|
|
88
|
-
* @param url E.g. https://my-site.com/slug
|
|
89
|
-
* @param path E.g. my-file.pdf
|
|
90
|
-
* @param contents E.g. {header{left:'Foo', right:'Bar'}, footer{left:'Foo', right:'Bar'}}
|
|
91
|
-
* @returns
|
|
92
|
-
*/
|
|
93
|
-
const generatePDFByUrlWithPuppeteer = async (
|
|
94
|
-
url: string,
|
|
95
|
-
path: string = "",
|
|
96
|
-
contents?: PDFHeaderAndFooter
|
|
97
|
-
) => {
|
|
98
|
-
log(`generatePDFByUrl - url: ${url} path: ${path}`);
|
|
99
|
-
|
|
100
|
-
if (!process.env.BROWSERLESS_TOKEN) {
|
|
101
|
-
log(`BROWSERLESS_TOKEN environment variable not found`, "ERROR");
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const timeStart = new Date();
|
|
105
|
-
let logMsg = `Before puppeteer.connect - ${secondBetweenTwoDate(
|
|
106
|
-
timeStart,
|
|
107
|
-
new Date()
|
|
108
|
-
)} seconds`;
|
|
109
|
-
log(logMsg, "DEBUG");
|
|
110
|
-
if (serverUtils) {
|
|
111
|
-
serverUtils.log(logMsg);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const browser = await puppeteer.connect({
|
|
115
|
-
browserWSEndpoint: `wss://chrome.browserless.io?token=${process.env.BROWSERLESS_TOKEN}`,
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
logMsg = `After puppeteer.connect - ${secondBetweenTwoDate(
|
|
119
|
-
timeStart,
|
|
120
|
-
new Date()
|
|
121
|
-
)} seconds`;
|
|
122
|
-
log(logMsg, "DEBUG");
|
|
123
|
-
if (serverUtils) {
|
|
124
|
-
serverUtils.log(logMsg);
|
|
125
|
-
}
|
|
126
|
-
const page = await browser.newPage();
|
|
127
|
-
|
|
128
|
-
logMsg = `Before page.goto - ${secondBetweenTwoDate(
|
|
129
|
-
timeStart,
|
|
130
|
-
new Date()
|
|
131
|
-
)} seconds`;
|
|
132
|
-
log(logMsg, "DEBUG");
|
|
133
|
-
if (serverUtils) {
|
|
134
|
-
serverUtils.log(logMsg);
|
|
135
|
-
}
|
|
136
|
-
const res = await page.goto(url, { waitUntil: "networkidle0", timeout: 0 });
|
|
137
|
-
logMsg = `After page.goto - ${secondBetweenTwoDate(
|
|
138
|
-
timeStart,
|
|
139
|
-
new Date()
|
|
140
|
-
)} seconds`;
|
|
141
|
-
log(logMsg, "DEBUG");
|
|
142
|
-
if (serverUtils) {
|
|
143
|
-
serverUtils.log(logMsg);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (!res) {
|
|
147
|
-
await browser.close();
|
|
148
|
-
console.log(`Error no response`);
|
|
149
|
-
return null;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (res.status() !== 200) {
|
|
153
|
-
await browser.close();
|
|
154
|
-
console.log(`Error ${res.status()} - ${res.url()}`);
|
|
155
|
-
return null;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
try {
|
|
159
|
-
const options: PDFOptions = getPDFOptions(contents);
|
|
160
|
-
if (path) {
|
|
161
|
-
options.path = path;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
logMsg = `Before page.pdf - ${secondBetweenTwoDate(
|
|
165
|
-
timeStart,
|
|
166
|
-
new Date()
|
|
167
|
-
)} seconds`;
|
|
168
|
-
log(logMsg, "DEBUG");
|
|
169
|
-
if (serverUtils) {
|
|
170
|
-
serverUtils.log(logMsg);
|
|
171
|
-
}
|
|
172
|
-
const pdf = await page.pdf(options);
|
|
173
|
-
logMsg = `After page.pdf - ${secondBetweenTwoDate(
|
|
174
|
-
timeStart,
|
|
175
|
-
new Date()
|
|
176
|
-
)} seconds`;
|
|
177
|
-
log(logMsg, "DEBUG");
|
|
178
|
-
if (serverUtils) {
|
|
179
|
-
serverUtils.log(logMsg);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const timeEnd = new Date();
|
|
183
|
-
const seconds = secondBetweenTwoDate(timeStart, timeEnd);
|
|
184
|
-
logMsg = `Request time: ${seconds} seconds - generatePDFByUrl`;
|
|
185
|
-
log(logMsg, "DEBUG");
|
|
186
|
-
if (serverUtils) {
|
|
187
|
-
serverUtils.log(logMsg);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return pdf;
|
|
191
|
-
} catch (err: any) {
|
|
192
|
-
console.log(err);
|
|
193
|
-
return null;
|
|
194
|
-
} finally {
|
|
195
|
-
await browser.close();
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Generate PDF by URL
|
|
201
|
-
*
|
|
202
|
-
* @param url E.g. https://my-site.com/slug
|
|
203
|
-
* @param contents E.g. {header{left:'Foo', right:'Bar'}, footer{left:'Foo', right:'Bar'}}
|
|
204
|
-
* @returns
|
|
205
|
-
*/
|
|
206
|
-
const generatePDFByUrlWithAxios = async (
|
|
207
|
-
url: string,
|
|
208
|
-
contents?: PDFHeaderAndFooter
|
|
209
|
-
) => {
|
|
210
|
-
let logMsg = `generatePDFByUrl - url: ${url}`;
|
|
211
|
-
log(logMsg, "DEBUG");
|
|
212
|
-
if (serverUtils) {
|
|
213
|
-
serverUtils.log(logMsg);
|
|
214
|
-
}
|
|
215
|
-
const timeStart = new Date();
|
|
216
|
-
|
|
217
|
-
if (!process.env.BROWSERLESS_TOKEN) {
|
|
218
|
-
logMsg = `BROWSERLESS_TOKEN environment variable not found`;
|
|
219
|
-
if (serverUtils) {
|
|
220
|
-
serverUtils.log(logMsg);
|
|
221
|
-
}
|
|
222
|
-
log(logMsg, "ERROR");
|
|
223
|
-
return null;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
logMsg = `Before axios request - ${secondBetweenTwoDate(
|
|
227
|
-
timeStart,
|
|
228
|
-
new Date()
|
|
229
|
-
)} seconds`;
|
|
230
|
-
log(logMsg, "DEBUG");
|
|
231
|
-
if (serverUtils) {
|
|
232
|
-
serverUtils.log(logMsg);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const options: any = {
|
|
236
|
-
method: "POST",
|
|
237
|
-
url: "https://chrome.browserless.io/pdf",
|
|
238
|
-
params: { token: process.env.BROWSERLESS_TOKEN },
|
|
239
|
-
responseType: "arraybuffer",
|
|
240
|
-
headers: {
|
|
241
|
-
"cache-control": "no-cache",
|
|
242
|
-
"content-type": "application/json",
|
|
243
|
-
},
|
|
244
|
-
data: {
|
|
245
|
-
url,
|
|
246
|
-
gotoOptions: { waitUntil: "networkidle0" },
|
|
247
|
-
options: getPDFOptions(contents),
|
|
248
|
-
},
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
const response = await axios.request(options);
|
|
252
|
-
|
|
253
|
-
logMsg = `after axios request - ${secondBetweenTwoDate(
|
|
254
|
-
timeStart,
|
|
255
|
-
new Date()
|
|
256
|
-
)} seconds`;
|
|
257
|
-
log(logMsg, "DEBUG");
|
|
258
|
-
if (serverUtils) {
|
|
259
|
-
serverUtils.log(logMsg);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return response.data;
|
|
263
|
-
};
|