From dd38f0b5d11bc8f756386e9543a465f5698143fe Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Wed, 30 Apr 2025 16:07:32 -0400 Subject: [PATCH 1/2] new components --- .../actions/generate-pdf/generate-pdf.mjs | 84 +++++++++++++++++++ .../get-template-variables.mjs | 26 ++++++ .../actions/list-templates/list-templates.mjs | 30 +++++++ components/mergemole/mergemole.app.mjs | 62 ++++++++++++-- components/mergemole/package.json | 7 +- 5 files changed, 202 insertions(+), 7 deletions(-) create mode 100644 components/mergemole/actions/generate-pdf/generate-pdf.mjs create mode 100644 components/mergemole/actions/get-template-variables/get-template-variables.mjs create mode 100644 components/mergemole/actions/list-templates/list-templates.mjs diff --git a/components/mergemole/actions/generate-pdf/generate-pdf.mjs b/components/mergemole/actions/generate-pdf/generate-pdf.mjs new file mode 100644 index 0000000000000..92aa8d54e334d --- /dev/null +++ b/components/mergemole/actions/generate-pdf/generate-pdf.mjs @@ -0,0 +1,84 @@ +import mergemole from "../../mergemole.app.mjs"; +import { ConfigurationError } from "@pipedream/platform"; +import fs from "fs"; + +export default { + key: "mergemole-generate-pdf", + name: "Generate PDF", + description: "Generate a PDF document based on the specified template. [See the documentation](https://documenter.getpostman.com/view/41321603/2sB2j3AWqz#a389449f-ada9-4e2e-9d8a-f1bde20da980)", + version: "0.0.1", + type: "action", + props: { + mergemole, + templateId: { + propDefinition: [ + mergemole, + "templateId", + ], + reloadProps: true, + }, + documentName: { + type: "string", + label: "Document Name", + description: "The name of the generated PDF document", + }, + }, + async additionalProps() { + const props = {}; + if (!this.templateId) { + return props; + } + const templateVariables = await this.mergemole.getTemplateVariables({ + templateId: this.templateId, + }); + if (!templateVariables?.length) { + throw new ConfigurationError(`No template variables found for template \`${this.templateId}\``); + } + for (const variable of templateVariables) { + props[variable.key] = { + type: "string", + label: variable.label, + optional: true, + }; + } + return props; + }, + async run({ $ }) { + const { + mergemole, + templateId, + documentName, + ...templateVariables + } = this; + + const data = []; + for (const [ + key, + value, + ] of Object.entries(templateVariables)) { + data.push({ + placeholder: key, + value, + }); + } + + const response = await mergemole.generatePdf({ + $, + data: { + data, + template_id: templateId, + document_name: documentName, + }, + responseType: "arraybuffer", + }); + + fs.writeFileSync(`/tmp/${documentName}`, Buffer.from(response)); + + $.export("$summary", "Successfully generated PDF"); + + return { + filename: documentName, + downloadedFilepath: `/tmp/${documentName}`, + }; + }, +}; diff --git a/components/mergemole/actions/get-template-variables/get-template-variables.mjs b/components/mergemole/actions/get-template-variables/get-template-variables.mjs new file mode 100644 index 0000000000000..3d3fc7759e81f --- /dev/null +++ b/components/mergemole/actions/get-template-variables/get-template-variables.mjs @@ -0,0 +1,26 @@ +import mergemole from "../../mergemole.app.mjs"; + +export default { + key: "mergemole-get-template-variables", + name: "Get Template Variables", + description: "Get all data variables of a specified template. [See the documentation](https://documenter.getpostman.com/view/41321603/2sB2j3AWqz#14da32c9-9b15-421e-89c9-8a977b04dc32)", + version: "0.0.1", + type: "action", + props: { + mergemole, + templateId: { + propDefinition: [ + mergemole, + "templateId", + ], + }, + }, + async run({ $ }) { + const response = await this.mergemole.getTemplateVariables({ + $, + templateId: this.templateId, + }); + $.export("$summary", `Successfully retrieved variables for template with ID: ${this.templateId}`); + return response; + }, +}; diff --git a/components/mergemole/actions/list-templates/list-templates.mjs b/components/mergemole/actions/list-templates/list-templates.mjs new file mode 100644 index 0000000000000..4cb2e3d159dd5 --- /dev/null +++ b/components/mergemole/actions/list-templates/list-templates.mjs @@ -0,0 +1,30 @@ +import mergemole from "../../mergemole.app.mjs"; + +export default { + key: "mergemole-list-templates", + name: "List Templates", + description: "Retrieve a list of all templates under your account. [See the documentation](https://documenter.getpostman.com/view/41321603/2sB2j3AWqz#f75d7ffa-df2e-42ca-ad32-1db280acb9e2)", + version: "0.0.1", + type: "action", + props: { + mergemole, + search: { + type: "string", + label: "Search", + description: "Search templates by name", + optional: true, + }, + }, + async run({ $ }) { + const response = await this.mergemole.listTemplates({ + $, + data: { + search: this.search, + }, + }); + $.export("$summary", `Successfully retrieved ${response.length} template${response.length === 1 + ? "" + : "s"}`); + return response; + }, +}; diff --git a/components/mergemole/mergemole.app.mjs b/components/mergemole/mergemole.app.mjs index 548d29c1990cd..8adbd0cca19f0 100644 --- a/components/mergemole/mergemole.app.mjs +++ b/components/mergemole/mergemole.app.mjs @@ -1,11 +1,63 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "mergemole", - propDefinitions: {}, + propDefinitions: { + templateId: { + type: "string", + label: "Template ID", + description: "The identifier of a template", + async options() { + const templates = await this.listTemplates(); + return templates?.map(({ + id: value, label, + }) => ({ + label, + value, + })) || []; + }, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://mergemole.com/api"; + }, + _makeRequest({ + $ = this, + path, + ...opts + }) { + return axios($, { + url: `${this._baseUrl()}${path}`, + headers: { + "x-api-token": `${this.$auth.api_key}`, + "x-api-secret": `${this.$auth.secret_key}`, + }, + ...opts, + }); + }, + generatePdf(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/pdf/generate", + ...opts, + }); + }, + listTemplates(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/template-files", + ...opts, + }); + }, + getTemplateVariables({ + templateId, ...opts + }) { + return this._makeRequest({ + path: `/template-variables-action/${templateId}`, + ...opts, + }); }, }, -}; \ No newline at end of file +}; diff --git a/components/mergemole/package.json b/components/mergemole/package.json index c576a6bd140c8..b85751ef32369 100644 --- a/components/mergemole/package.json +++ b/components/mergemole/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/mergemole", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream MergeMole Components", "main": "mergemole.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 +} From 8dc47b7c6957b6ade8dccb51252eb3d7bdb42df4 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Wed, 30 Apr 2025 16:09:55 -0400 Subject: [PATCH 2/2] pnpm-lock.yaml --- pnpm-lock.yaml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 444614bbe09b6..8758aad0e8296 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7925,7 +7925,11 @@ importers: specifier: ^3.0.0 version: 3.0.3 - components/mergemole: {} + components/mergemole: + dependencies: + '@pipedream/platform': + specifier: ^3.0.3 + version: 3.0.3 components/message_bird: dependencies: @@ -34735,6 +34739,8 @@ snapshots: '@putout/operator-filesystem': 5.0.0(putout@36.13.1(eslint@8.57.1)(typescript@5.6.3)) '@putout/operator-json': 2.2.0 putout: 36.13.1(eslint@8.57.1)(typescript@5.6.3) + transitivePeerDependencies: + - supports-color '@putout/operator-regexp@1.0.0(putout@36.13.1(eslint@8.57.1)(typescript@5.6.3))': dependencies: