Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Components - fileforge #13989

Merged
merged 6 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
85 changes: 85 additions & 0 deletions components/fileforge/actions/generate-pdf/generate-pdf.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import FormData from "form-data";
import fs from "fs";
import {
checkTmp,
parseObject,
} from "../../common/utils.mjs";
import fileforge from "../../fileforge.app.mjs";

export default {
key: "fileforge-generate-pdf",
name: "Generate PDF",
description: "Generate a PDF from provided HTML. [See the documentation](https://docs.fileforge.com/api-reference/api-reference/pdf/generate)",
version: "0.0.1",
type: "action",
props: {
fileforge,
alert: {
type: "alert",
alertType: "warning",
content: `An **\`index.html\`** file is required, and will be used as the main document.
Other documents may also be attached, such as stylesheets or images.
The path in the **\`filename\`** part of the multipart attachement will be respected during generation.
**Important notice:** during generation, the **\`index.html\`** file will be processed to include the base URL of the document.
This is required for assets to be loaded correctly.
To link your assets from the HTML file, you should not use a leading slash in the URL.
For example, use **\`<img src="image.jpg" />\`** instead of **\`<img src="/image.jpg" />\`**.`,
},
files: {
type: "string[]",
label: "HTML Files",
description: "The HTML files to convert to PDF. Each file should be a valid path to an HTML file.",
},
test: {
type: "boolean",
label: "Test",
description: "Generate a test document instead of a production document. The generated document will contain a watermark. Defaults to true.",
optional: true,
},
expiresAt: {
type: "string",
label: "Expires At",
description: "The expiration timestamp for the PDF in ISO 8601 format",
optional: true,
},
fileName: {
type: "string",
label: "Filename",
description: "The desired filename for the generated PDF.",
optional: true,
},
allowViewing: {
type: "boolean",
label: "Allow Viewing",
description: "Specifies whether viewing is allowed.",
optional: true,
},
},
async run({ $ }) {
const {
fileforge,
files,
...data
} = this;

const formData = new FormData();
const parsedFiles = parseObject(files);

for (const file of parsedFiles) {
formData.append("files", fs.createReadStream(checkTmp(file)));
}

formData.append("options", JSON.stringify(data), {
contentType: "application/json",
});

const response = await fileforge.generatePDF({
$,
data: formData,
headers: formData.getHeaders(),
});

$.export("$summary", "Successfully generated the PDF file.");
return response;
},
};
31 changes: 31 additions & 0 deletions components/fileforge/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const parseObject = (obj) => {
if (!obj) return undefined;

if (Array.isArray(obj)) {
return obj.map((item) => {
if (typeof item === "string") {
try {
return JSON.parse(item);
} catch (e) {
return item;
}
}
return item;
});
}
if (typeof obj === "string") {
try {
return JSON.parse(obj);
} catch (e) {
return obj;
}
}
return obj;
};

export const checkTmp = (filename) => {
if (!filename.startsWith("/tmp")) {
return `/tmp/${filename}`;
}
return filename;
};
32 changes: 27 additions & 5 deletions components/fileforge/fileforge.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "fileforge",
propDefinitions: {},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return "https://api.fileforge.com";
},
_headers(headers = {}) {
return {
"X-API-Key": this.$auth.api_key,
...headers,
};
},
_makeRequest({
$ = this, path, headers, ...opts
}) {
return axios($, {
url: this._baseUrl() + path,
headers: this._headers(headers),
...opts,
});
},
generatePDF(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/pdf/generate/",
...opts,
});
},
},
};
};
7 changes: 5 additions & 2 deletions components/fileforge/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/fileforge",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Fileforge Components",
"main": "fileforge.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.1"
}
}
}
107 changes: 55 additions & 52 deletions pnpm-lock.yaml

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

Loading