Skip to content

Commit 694bd6d

Browse files
committed
[Components] cloudflare_browser_rendering - new components
1 parent f0d6c94 commit 694bd6d

File tree

10 files changed

+671
-10
lines changed

10 files changed

+671
-10
lines changed
Lines changed: 73 additions & 0 deletions
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+
};
Lines changed: 66 additions & 0 deletions
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+
};
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
getPdf(args = {}) {
25+
return this.app.post({
26+
path: "/pdf",
27+
...args,
28+
});
29+
},
30+
},
31+
async run({ $ }) {
32+
const {
33+
getPdf,
34+
html,
35+
url,
36+
viewportHeight,
37+
viewportWidth,
38+
viewportDeviceScaleFactor,
39+
viewportHasTouch,
40+
viewportIsLandscape,
41+
viewportIsMobile,
42+
userAgent,
43+
additionalSettings,
44+
fileName,
45+
} = this;
46+
47+
if (!html && !url) {
48+
throw new ConfigurationError("Either **HTML** or **URL** is required");
49+
}
50+
51+
if ((viewportHeight && !viewportWidth) || (!viewportHeight && viewportWidth)) {
52+
throw new ConfigurationError("Both **Viewport - Height** and **Viewport - Width** are required when either is provided");
53+
}
54+
55+
const response = await getPdf({
56+
$,
57+
data: {
58+
html,
59+
url,
60+
...(viewportHeight && {
61+
viewport: {
62+
height: viewportHeight,
63+
width: viewportWidth,
64+
deviceScaleFactor: viewportDeviceScaleFactor,
65+
hasTouch: viewportHasTouch,
66+
isLandscape: viewportIsLandscape,
67+
isMobile: viewportIsMobile,
68+
},
69+
}),
70+
userAgent,
71+
...utils.parseJson(additionalSettings),
72+
},
73+
});
74+
75+
const pdfPath = path.join("/tmp", fileName);
76+
fs.writeFileSync(pdfPath, response, "binary");
77+
78+
$.export("$summary", "Successfully fetched PDF content");
79+
80+
return pdfPath;
81+
},
82+
};

0 commit comments

Comments
 (0)