Skip to content

Commit 2d94b75

Browse files
committed
new components
1 parent bb29328 commit 2d94b75

File tree

6 files changed

+204
-7
lines changed

6 files changed

+204
-7
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const normalizeFilepath = (filename, ext = "pdf") => {
2+
let filepath = filename.includes("/tmp")
3+
? filename
4+
: `/tmp/${filename}`;
5+
filepath = filepath.includes(`.${ext}`)
6+
? filepath
7+
: `${filepath}.${ext}`;
8+
return filepath;
9+
};
10+
11+
export {
12+
normalizeFilepath,
13+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import customjs from "../../customjs.app.mjs";
2+
import fs from "fs";
3+
import { normalizeFilepath } from "../common/utils.mjs";
4+
5+
export default {
6+
key: "customjs-convert-html-to-pdf",
7+
name: "Convert HTML to PDF",
8+
description: "Converts an HTML string to a PDF document. [See the documentation](https://www.customjs.space/api/docs#_1-html-to-pdf)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
customjs,
13+
html: {
14+
type: "string",
15+
label: "HTML",
16+
description: "The HTML string to be converted to a PDF",
17+
},
18+
filename: {
19+
propDefinition: [
20+
customjs,
21+
"filename",
22+
],
23+
},
24+
},
25+
async run({ $ }) {
26+
const fileContent = await this.customjs.convertHtmlToPdf({
27+
$,
28+
data: {
29+
input: this.html,
30+
code: "const { HTML2PDF } = require(\"./utils\"); return HTML2PDF(input);",
31+
returnBinary: "true",
32+
},
33+
});
34+
35+
const filepath = normalizeFilepath(this.filename);
36+
fs.writeFileSync(filepath, Buffer.from(fileContent));
37+
38+
$.export("$summary", "Successfully converted HTML to PDF");
39+
return {
40+
filename: this.filename,
41+
filepath,
42+
};
43+
},
44+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import customjs from "../../customjs.app.mjs";
2+
import fs from "fs";
3+
import { normalizeFilepath } from "../common/utils.mjs";
4+
5+
export default {
6+
key: "customjs-create-screenshot",
7+
name: "Create Screenshot",
8+
description: "Create a screenshot of a website. [See the documentation](https://www.customjs.space/api/docs#_3-create-screenshot)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
customjs,
13+
url: {
14+
type: "string",
15+
label: "URL",
16+
description: "The URL of the website to take a screenshot of",
17+
},
18+
filename: {
19+
propDefinition: [
20+
customjs,
21+
"filename",
22+
],
23+
},
24+
},
25+
async run({ $ }) {
26+
const fileContent = await this.customjs.createScreenshot({
27+
$,
28+
data: {
29+
input: this.url,
30+
code: "const { SCREENSHOT } = require(\"./utils\"); return SCREENSHOT(input);",
31+
returnBinary: "true",
32+
},
33+
});
34+
35+
const filepath = normalizeFilepath(this.filename, "png");
36+
fs.writeFileSync(filepath, Buffer.from(fileContent));
37+
38+
$.export("$summary", `Successfully created screenshot of ${this.url}`);
39+
return {
40+
filename: this.filename,
41+
filepath,
42+
};
43+
},
44+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import customjs from "../../customjs.app.mjs";
2+
import fs from "fs";
3+
import { normalizeFilepath } from "../common/utils.mjs";
4+
5+
export default {
6+
key: "customjs-merge-pdfs",
7+
name: "Merge PDFs",
8+
description: "Merges multiple PDF documents into one. [See the documentation](https://www.customjs.space/api/docs#_2-merge-pdfs)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
customjs,
13+
pdfs: {
14+
type: "string[]",
15+
label: "PDFs",
16+
description: "The array of URLs to the PDF documents to merge",
17+
},
18+
filename: {
19+
propDefinition: [
20+
customjs,
21+
"filename",
22+
],
23+
},
24+
},
25+
async run({ $ }) {
26+
const pdfs = typeof this.pdfs === "string"
27+
? JSON.parse(this.pdfs)
28+
: this.pdfs;
29+
30+
const fileContent = await this.customjs.mergePdfs({
31+
$,
32+
data: {
33+
input: pdfs,
34+
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);",
35+
returnBinary: "true",
36+
},
37+
});
38+
39+
const filepath = normalizeFilepath(this.filename);
40+
fs.writeFileSync(filepath, Buffer.from(fileContent));
41+
42+
$.export("$summary", "Successfully merged PDFs");
43+
return {
44+
filename: this.filename,
45+
filepath,
46+
};
47+
},
48+
};
Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,56 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "customjs",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
filename: {
8+
type: "string",
9+
label: "Filename",
10+
description: "Download the file to the `/tmp` directory with the specified filename.",
11+
},
12+
},
513
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
14+
_makeRequest(opts = {}) {
15+
const {
16+
$ = this,
17+
headers,
18+
...otherOpts
19+
} = opts;
20+
return axios($, {
21+
...otherOpts,
22+
method: "POST",
23+
url: `https://e.customjs.io/__js1-${this.$auth.api_key}`,
24+
headers: {
25+
...headers,
26+
"Content-Type": "application/json",
27+
},
28+
responseType: "arraybuffer",
29+
});
30+
},
31+
convertHtmlToPdf(opts = {}) {
32+
return this._makeRequest({
33+
headers: {
34+
"customjs-origin": "zapier/generatePDF",
35+
},
36+
...opts,
37+
});
38+
},
39+
createScreenshot(opts = {}) {
40+
return this._makeRequest({
41+
headers: {
42+
"customjs-origin": "zapier/screenshot",
43+
},
44+
...opts,
45+
});
46+
},
47+
mergePdfs(opts = {}) {
48+
return this._makeRequest({
49+
headers: {
50+
"customjs-origin": "zapier/mergePDFs",
51+
},
52+
...opts,
53+
});
954
},
1055
},
11-
};
56+
};

components/customjs/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/customjs",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream CustomJS Components",
55
"main": "customjs.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1417
}
15-
}
18+
}

0 commit comments

Comments
 (0)