diff --git a/components/customjs/actions/common/utils.mjs b/components/customjs/actions/common/utils.mjs new file mode 100644 index 0000000000000..4c9858fbb0c89 --- /dev/null +++ b/components/customjs/actions/common/utils.mjs @@ -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, +}; diff --git a/components/customjs/actions/convert-html-to-pdf/convert-html-to-pdf.mjs b/components/customjs/actions/convert-html-to-pdf/convert-html-to-pdf.mjs new file mode 100644 index 0000000000000..7db489dfae338 --- /dev/null +++ b/components/customjs/actions/convert-html-to-pdf/convert-html-to-pdf.mjs @@ -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, + }; + }, +}; diff --git a/components/customjs/actions/create-screenshot/create-screenshot.mjs b/components/customjs/actions/create-screenshot/create-screenshot.mjs new file mode 100644 index 0000000000000..020ec88cb817a --- /dev/null +++ b/components/customjs/actions/create-screenshot/create-screenshot.mjs @@ -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, + }; + }, +}; diff --git a/components/customjs/actions/merge-pdfs/merge-pdfs.mjs b/components/customjs/actions/merge-pdfs/merge-pdfs.mjs new file mode 100644 index 0000000000000..fa19e37cd5a0b --- /dev/null +++ b/components/customjs/actions/merge-pdfs/merge-pdfs.mjs @@ -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, + }; + }, +}; diff --git a/components/customjs/customjs.app.mjs b/components/customjs/customjs.app.mjs index 4baaba3a38b63..986e175cec562 100644 --- a/components/customjs/customjs.app.mjs +++ b/components/customjs/customjs.app.mjs @@ -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, + }); }, }, -}; \ No newline at end of file +}; diff --git a/components/customjs/package.json b/components/customjs/package.json index 4022aa6bb8af8..36e17d4b7f2b4 100644 --- a/components/customjs/package.json +++ b/components/customjs/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/customjs", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream CustomJS Components", "main": "customjs.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3" } -} \ No newline at end of file +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cc52977d08776..04f751e866362 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2218,7 +2218,10 @@ importers: specifiers: {} components/customjs: - specifiers: {} + specifiers: + '@pipedream/platform': ^3.0.3 + dependencies: + '@pipedream/platform': 3.0.3 components/cutt_ly: specifiers: