Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions components/cloudflare_browser_rendering/actions/common/base.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import app from "../../cloudflare_browser_rendering.app.mjs";

export default {
props: {
app,
// eslint-disable-next-line pipedream/props-label, pipedream/props-description
info: {
type: "alert",
alertType: "info",
content: "Please note that either **HTML** or **URL** must be set.",
},
html: {
propDefinition: [
app,
"html",
],
},
url: {
propDefinition: [
app,
"url",
],
},
viewportHeight: {
propDefinition: [
app,
"viewportHeight",
],
},
viewportWidth: {
propDefinition: [
app,
"viewportWidth",
],
},
viewportDeviceScaleFactor: {
propDefinition: [
app,
"viewportDeviceScaleFactor",
],
},
viewportHasTouch: {
propDefinition: [
app,
"viewportHasTouch",
],
},
viewportIsLandscape: {
propDefinition: [
app,
"viewportIsLandscape",
],
},
viewportIsMobile: {
propDefinition: [
app,
"viewportIsMobile",
],
},
userAgent: {
propDefinition: [
app,
"userAgent",
],
},
additionalSettings: {
propDefinition: [
app,
"additionalSettings",
],
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { ConfigurationError } from "@pipedream/platform";
import common from "../common/base.mjs";
import utils from "../../common/utils.mjs";

export default {
...common,
key: "cloudflare_browser_rendering-get-html-content",
name: "Get HTML Content",
description: "Fetches rendered HTML content from provided URL or HTML. [See the documentation](https://developers.cloudflare.com/api/resources/browser_rendering/subresources/content/)",
version: "0.0.1",
type: "action",
methods: {
getHtmlContent(args = {}) {
return this.app.post({
path: "/content",
...args,
});
},
},
async run({ $ }) {
const {
getHtmlContent,
html,
url,
viewportHeight,
viewportWidth,
viewportDeviceScaleFactor,
viewportHasTouch,
viewportIsLandscape,
viewportIsMobile,
userAgent,
additionalSettings,
} = this;

if (!html && !url) {
throw new ConfigurationError("Either **HTML** or **URL** is required");
}

if ((viewportHeight && !viewportWidth) || (!viewportHeight && viewportWidth)) {
throw new ConfigurationError("Both **Viewport - Height** and **Viewport - Width** are required when either is provided");
}

const response = await getHtmlContent({
$,
data: {
html,
url,
...(viewportHeight && {
viewport: {
height: viewportHeight,
width: viewportWidth,
deviceScaleFactor: viewportDeviceScaleFactor,
hasTouch: viewportHasTouch,
isLandscape: viewportIsLandscape,
isMobile: viewportIsMobile,
},
}),
userAgent,
...utils.parseJson(additionalSettings),
},
});

$.export("$summary", "Successfully fetched HTML content");
return response;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import fs from "fs";
import path from "path";
import { ConfigurationError } from "@pipedream/platform";
import common from "../common/base.mjs";
import utils from "../../common/utils.mjs";

export default {
...common,
key: "cloudflare_browser_rendering-get-pdf",
name: "Get PDF",
description: "Fetches rendered PDF from provided URL or HTML. [See the documentation](https://developers.cloudflare.com/api/resources/browser_rendering/subresources/pdf/methods/create/)",
version: "0.0.1",
type: "action",
props: {
...common.props,
fileName: {
type: "string",
label: "PDF File Name",
description: "The name of the PDF file",
default: "content.pdf",
},
},
methods: {
async getPdf(args = {}) {
try {
return await this.app.post({
path: "/pdf",
responseType: "arraybuffer",
...args,
});
} catch (error) {
throw new Error(error.response.data.toString());
}
},
},
async run({ $ }) {
const {
getPdf,
html,
url,
viewportHeight,
viewportWidth,
viewportDeviceScaleFactor,
viewportHasTouch,
viewportIsLandscape,
viewportIsMobile,
userAgent,
additionalSettings,
fileName,
} = this;

if (!html && !url) {
throw new ConfigurationError("Either **HTML** or **URL** is required");
}

if ((viewportHeight && !viewportWidth) || (!viewportHeight && viewportWidth)) {
throw new ConfigurationError("Both **Viewport - Height** and **Viewport - Width** are required when either is provided");
}

const response = await getPdf({
$,
data: {
html,
url,
...(viewportHeight && {
viewport: {
height: viewportHeight,
width: viewportWidth,
deviceScaleFactor: viewportDeviceScaleFactor,
hasTouch: viewportHasTouch,
isLandscape: viewportIsLandscape,
isMobile: viewportIsMobile,
},
}),
userAgent,
...utils.parseJson(additionalSettings),
},
});

const pdfPath = path.join("/tmp", fileName);
fs.writeFileSync(pdfPath, response);

$.export("$summary", "Successfully fetched PDF content");

return pdfPath;
},
};
Loading
Loading