Skip to content

Commit 007bac0

Browse files
committed
[Components] cloudflare_browser_rendering - new components
1 parent 15cc437 commit 007bac0

File tree

10 files changed

+673
-10
lines changed

10 files changed

+673
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import app from "../../cloudflare_browser_rendering.app.mjs";
2+
3+
export default {
4+
props: {
5+
app,
6+
// eslint-disable-next-line pipedream/props-label, pipedream/props-description
7+
info: {
8+
type: "alert",
9+
alertType: "info",
10+
content: "Please note that either **HTML** or **URL** must be set.",
11+
},
12+
html: {
13+
propDefinition: [
14+
app,
15+
"html",
16+
],
17+
},
18+
url: {
19+
propDefinition: [
20+
app,
21+
"url",
22+
],
23+
},
24+
viewportHeight: {
25+
propDefinition: [
26+
app,
27+
"viewportHeight",
28+
],
29+
},
30+
viewportWidth: {
31+
propDefinition: [
32+
app,
33+
"viewportWidth",
34+
],
35+
},
36+
viewportDeviceScaleFactor: {
37+
propDefinition: [
38+
app,
39+
"viewportDeviceScaleFactor",
40+
],
41+
},
42+
viewportHasTouch: {
43+
propDefinition: [
44+
app,
45+
"viewportHasTouch",
46+
],
47+
},
48+
viewportIsLandscape: {
49+
propDefinition: [
50+
app,
51+
"viewportIsLandscape",
52+
],
53+
},
54+
viewportIsMobile: {
55+
propDefinition: [
56+
app,
57+
"viewportIsMobile",
58+
],
59+
},
60+
userAgent: {
61+
propDefinition: [
62+
app,
63+
"userAgent",
64+
],
65+
},
66+
additionalSettings: {
67+
propDefinition: [
68+
app,
69+
"additionalSettings",
70+
],
71+
},
72+
},
73+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import common from "../common/base.mjs";
3+
import utils from "../../common/utils.mjs";
4+
5+
export default {
6+
...common,
7+
key: "cloudflare_browser_rendering-get-html-content",
8+
name: "Get HTML Content",
9+
description: "Fetches rendered HTML content from provided URL or HTML. [See the documentation](https://developers.cloudflare.com/api/resources/browser_rendering/subresources/content/)",
10+
version: "0.0.1",
11+
type: "action",
12+
methods: {
13+
getHtmlContent(args = {}) {
14+
return this.app.post({
15+
path: "/content",
16+
...args,
17+
});
18+
},
19+
},
20+
async run({ $ }) {
21+
const {
22+
getHtmlContent,
23+
html,
24+
url,
25+
viewportHeight,
26+
viewportWidth,
27+
viewportDeviceScaleFactor,
28+
viewportHasTouch,
29+
viewportIsLandscape,
30+
viewportIsMobile,
31+
userAgent,
32+
additionalSettings,
33+
} = this;
34+
35+
if (!html && !url) {
36+
throw new ConfigurationError("Either **HTML** or **URL** is required");
37+
}
38+
39+
if ((viewportHeight && !viewportWidth) || (!viewportHeight && viewportWidth)) {
40+
throw new ConfigurationError("Both **Viewport - Height** and **Viewport - Width** are required when either is provided");
41+
}
42+
43+
const response = await getHtmlContent({
44+
$,
45+
data: {
46+
html,
47+
url,
48+
...(viewportHeight && {
49+
viewport: {
50+
height: viewportHeight,
51+
width: viewportWidth,
52+
deviceScaleFactor: viewportDeviceScaleFactor,
53+
hasTouch: viewportHasTouch,
54+
isLandscape: viewportIsLandscape,
55+
isMobile: viewportIsMobile,
56+
},
57+
}),
58+
userAgent,
59+
...utils.parseJson(additionalSettings),
60+
},
61+
});
62+
63+
$.export("$summary", "Successfully fetched HTML content");
64+
return response;
65+
},
66+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import { ConfigurationError } from "@pipedream/platform";
4+
import common from "../common/base.mjs";
5+
import utils from "../../common/utils.mjs";
6+
7+
export default {
8+
...common,
9+
key: "cloudflare_browser_rendering-get-pdf",
10+
name: "Get PDF",
11+
description: "Fetches rendered PDF from provided URL or HTML. [See the documentation](https://developers.cloudflare.com/api/resources/browser_rendering/subresources/pdf/methods/create/)",
12+
version: "0.0.1",
13+
type: "action",
14+
props: {
15+
...common.props,
16+
fileName: {
17+
type: "string",
18+
label: "PDF File Name",
19+
description: "The name of the PDF file",
20+
default: "content.pdf",
21+
},
22+
},
23+
methods: {
24+
async getPdf(args = {}) {
25+
try {
26+
return await this.app.post({
27+
path: "/pdf",
28+
responseType: "arraybuffer",
29+
...args,
30+
});
31+
} catch (error) {
32+
throw new Error(error.response.data.toString());
33+
}
34+
},
35+
},
36+
async run({ $ }) {
37+
const {
38+
getPdf,
39+
html,
40+
url,
41+
viewportHeight,
42+
viewportWidth,
43+
viewportDeviceScaleFactor,
44+
viewportHasTouch,
45+
viewportIsLandscape,
46+
viewportIsMobile,
47+
userAgent,
48+
additionalSettings,
49+
fileName,
50+
} = this;
51+
52+
if (!html && !url) {
53+
throw new ConfigurationError("Either **HTML** or **URL** is required");
54+
}
55+
56+
if ((viewportHeight && !viewportWidth) || (!viewportHeight && viewportWidth)) {
57+
throw new ConfigurationError("Both **Viewport - Height** and **Viewport - Width** are required when either is provided");
58+
}
59+
60+
const response = await getPdf({
61+
$,
62+
data: {
63+
html,
64+
url,
65+
...(viewportHeight && {
66+
viewport: {
67+
height: viewportHeight,
68+
width: viewportWidth,
69+
deviceScaleFactor: viewportDeviceScaleFactor,
70+
hasTouch: viewportHasTouch,
71+
isLandscape: viewportIsLandscape,
72+
isMobile: viewportIsMobile,
73+
},
74+
}),
75+
userAgent,
76+
...utils.parseJson(additionalSettings),
77+
},
78+
});
79+
80+
const pdfPath = path.join("/tmp", fileName);
81+
fs.writeFileSync(pdfPath, response);
82+
83+
$.export("$summary", "Successfully fetched PDF content");
84+
85+
return pdfPath;
86+
},
87+
};

0 commit comments

Comments
 (0)