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
13 changes: 13 additions & 0 deletions components/customjs/actions/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const normalizeFilepath = (filename, ext = "pdf") => {
let filepath = filename.includes("/tmp")
? filename
: `/tmp/${filename}`;
filepath = filepath.includes(`.${ext}`)
? filepath
: `${filepath}.${ext}`;
return filepath;
};

export {
normalizeFilepath,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import customjs from "../../customjs.app.mjs";
import fs from "fs";
import { normalizeFilepath } from "../common/utils.mjs";

export default {
key: "customjs-convert-html-to-pdf",
name: "Convert HTML to PDF",
description: "Converts an HTML string to a PDF document. [See the documentation](https://www.customjs.space/api/docs#_1-html-to-pdf)",
version: "0.0.1",
type: "action",
props: {
customjs,
html: {
type: "string",
label: "HTML",
description: "The HTML string to be converted to a PDF",
},
filename: {
propDefinition: [
customjs,
"filename",
],
},
},
async run({ $ }) {
const fileContent = await this.customjs.convertHtmlToPdf({
$,
data: {
input: this.html,
code: "const { HTML2PDF } = require(\"./utils\"); return HTML2PDF(input);",
returnBinary: "true",
},
});

const filepath = normalizeFilepath(this.filename);
fs.writeFileSync(filepath, Buffer.from(fileContent));

$.export("$summary", "Successfully converted HTML to PDF");
return {
filename: this.filename,
filepath,
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import customjs from "../../customjs.app.mjs";
import fs from "fs";
import { normalizeFilepath } from "../common/utils.mjs";

export default {
key: "customjs-create-screenshot",
name: "Create Screenshot",
description: "Create a screenshot of a website. [See the documentation](https://www.customjs.space/api/docs#_3-create-screenshot)",
version: "0.0.1",
type: "action",
props: {
customjs,
url: {
type: "string",
label: "URL",
description: "The URL of the website to take a screenshot of",
},
filename: {
propDefinition: [
customjs,
"filename",
],
},
},
async run({ $ }) {
const fileContent = await this.customjs.createScreenshot({
$,
data: {
input: this.url,
code: "const { SCREENSHOT } = require(\"./utils\"); return SCREENSHOT(input);",
returnBinary: "true",
},
});

const filepath = normalizeFilepath(this.filename, "png");
fs.writeFileSync(filepath, Buffer.from(fileContent));

$.export("$summary", `Successfully created screenshot of ${this.url}`);
return {
filename: this.filename,
filepath,
};
},
};
48 changes: 48 additions & 0 deletions components/customjs/actions/merge-pdfs/merge-pdfs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import customjs from "../../customjs.app.mjs";
import fs from "fs";
import { normalizeFilepath } from "../common/utils.mjs";

export default {
key: "customjs-merge-pdfs",
name: "Merge PDFs",
description: "Merges multiple PDF documents into one. [See the documentation](https://www.customjs.space/api/docs#_2-merge-pdfs)",
version: "0.0.1",
type: "action",
props: {
customjs,
pdfs: {
type: "string[]",
label: "PDFs",
description: "The array of URLs to the PDF documents to merge",
},
filename: {
propDefinition: [
customjs,
"filename",
],
},
},
async run({ $ }) {
const pdfs = typeof this.pdfs === "string"
? JSON.parse(this.pdfs)
: this.pdfs;

const fileContent = await this.customjs.mergePdfs({
$,
data: {
input: pdfs,
code: "const { PDF_MERGE } = require(\"./utils\"); const axios = require(\"axios\"); const pdfBuffers = await Promise.all(input.map(async file => { const res = await axios.get(file, { responseType: \"arraybuffer\" }); return Buffer.from(res.data).toString(\"base64\"); })); return PDF_MERGE(pdfBuffers);",
returnBinary: "true",
},
});

const filepath = normalizeFilepath(this.filename);
fs.writeFileSync(filepath, Buffer.from(fileContent));

$.export("$summary", "Successfully merged PDFs");
return {
filename: this.filename,
filepath,
};
},
};
55 changes: 50 additions & 5 deletions components/customjs/customjs.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "customjs",
propDefinitions: {},
propDefinitions: {
filename: {
type: "string",
label: "Filename",
description: "Download the file to the `/tmp` directory with the specified filename.",
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_makeRequest(opts = {}) {
const {
$ = this,
headers,
...otherOpts
} = opts;
return axios($, {
...otherOpts,
method: "POST",
url: `https://e.customjs.io/__js1-${this.$auth.api_key}`,
headers: {
...headers,
"Content-Type": "application/json",
},
responseType: "arraybuffer",
});
},
convertHtmlToPdf(opts = {}) {
return this._makeRequest({
headers: {
"customjs-origin": "zapier/generatePDF",
},
...opts,
});
},
createScreenshot(opts = {}) {
return this._makeRequest({
headers: {
"customjs-origin": "zapier/screenshot",
},
...opts,
});
},
mergePdfs(opts = {}) {
return this._makeRequest({
headers: {
"customjs-origin": "zapier/mergePDFs",
},
...opts,
});
},
},
};
};
7 changes: 5 additions & 2 deletions components/customjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/customjs",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream CustomJS Components",
"main": "customjs.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.3"
}
}
}
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading