diff --git a/components/pdfmonkey/.gitignore b/components/pdfmonkey/.gitignore deleted file mode 100644 index ec761ccab7595..0000000000000 --- a/components/pdfmonkey/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -*.js -*.mjs -dist \ No newline at end of file diff --git a/components/pdfmonkey/actions/delete-document/delete-document.mjs b/components/pdfmonkey/actions/delete-document/delete-document.mjs new file mode 100644 index 0000000000000..413d3b55f2ad1 --- /dev/null +++ b/components/pdfmonkey/actions/delete-document/delete-document.mjs @@ -0,0 +1,26 @@ +import pdfmonkey from "../../pdfmonkey.app.mjs"; + +export default { + key: "pdfmonkey-delete-document", + name: "Delete Document", + description: "Deletes a specific document using its ID. [See the documentation](https://docs.pdfmonkey.io/references/api/documents)", + version: "0.0.1", + type: "action", + props: { + pdfmonkey, + documentId: { + propDefinition: [ + pdfmonkey, + "documentId", + ], + }, + }, + async run({ $ }) { + const response = await this.pdfmonkey.deleteDocument({ + $, + documentId: this.documentId, + }); + $.export("$summary", `Deleted document with ID ${this.documentId}`); + return response; + }, +}; diff --git a/components/pdfmonkey/actions/find-document/find-document.mjs b/components/pdfmonkey/actions/find-document/find-document.mjs new file mode 100644 index 0000000000000..7034927aaaea2 --- /dev/null +++ b/components/pdfmonkey/actions/find-document/find-document.mjs @@ -0,0 +1,26 @@ +import pdfmonkey from "../../pdfmonkey.app.mjs"; + +export default { + key: "pdfmonkey-find-document", + name: "Find Document", + description: "Find a document within PDFMonkey. [See the documentation](https://docs.pdfmonkey.io/references/api/documents)", + version: "0.0.1", + type: "action", + props: { + pdfmonkey, + documentId: { + propDefinition: [ + pdfmonkey, + "documentId", + ], + }, + }, + async run({ $ }) { + const document = await this.pdfmonkey.getDocument({ + $, + documentId: this.documentId, + }); + $.export("$summary", `Successfully found document with ID ${this.documentId}`); + return document; + }, +}; diff --git a/components/pdfmonkey/actions/generate-document/generate-document.mjs b/components/pdfmonkey/actions/generate-document/generate-document.mjs new file mode 100644 index 0000000000000..d4a17729926f6 --- /dev/null +++ b/components/pdfmonkey/actions/generate-document/generate-document.mjs @@ -0,0 +1,54 @@ +import { STATUS_OPTIONS } from "../../common/constants.mjs"; +import pdfmonkey from "../../pdfmonkey.app.mjs"; + +export default { + key: "pdfmonkey-generate-document", + name: "Generate Document", + description: "Generates a new document using a specified template. [See the documentation](https://docs.pdfmonkey.io/references/api/documents)", + version: "0.0.1", + type: "action", + props: { + pdfmonkey, + templateId: { + propDefinition: [ + pdfmonkey, + "templateId", + ], + }, + payload: { + type: "object", + label: "Payload", + description: "Data to use for the Document generation.", + optional: true, + }, + meta: { + type: "object", + label: "Meta", + description: "Meta-Data to attach to the Document.", + optional: true, + }, + status: { + type: "string", + label: "Status", + description: "The status of the document", + options: STATUS_OPTIONS, + optional: true, + }, + }, + async run({ $ }) { + const response = await this.pdfmonkey.createDocument({ + $, + data: { + document: { + document_template_id: this.templateId, + payload: this.payload, + meta: this.meta, + status: this.status, + }, + }, + }); + + $.export("$summary", `Successfully generated document with ID ${response.document.id}`); + return response; + }, +}; diff --git a/components/pdfmonkey/app/pdfmonkey.app.ts b/components/pdfmonkey/app/pdfmonkey.app.ts deleted file mode 100644 index 013cfeb73cc1a..0000000000000 --- a/components/pdfmonkey/app/pdfmonkey.app.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { defineApp } from "@pipedream/types"; - -export default defineApp({ - type: "app", - app: "pdfmonkey", - propDefinitions: {}, - methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); - }, - }, -}); diff --git a/components/pdfmonkey/common/constants.mjs b/components/pdfmonkey/common/constants.mjs new file mode 100644 index 0000000000000..b8df6991c7e70 --- /dev/null +++ b/components/pdfmonkey/common/constants.mjs @@ -0,0 +1,10 @@ +export const STATUS_OPTIONS = [ + { + label: "Draft (Default)", + value: "draft", + }, + { + label: "Pending (To trigger generation)", + value: "pending", + }, +]; diff --git a/components/pdfmonkey/package.json b/components/pdfmonkey/package.json index a11e79998f246..b447e4ef5188e 100644 --- a/components/pdfmonkey/package.json +++ b/components/pdfmonkey/package.json @@ -1,18 +1,18 @@ { "name": "@pipedream/pdfmonkey", - "version": "0.0.3", + "version": "0.1.0", "description": "Pipedream PDFMonkey Components", - "main": "dist/app/pdfmonkey.app.mjs", + "main": "pdfmonkey.app.mjs", "keywords": [ "pipedream", "pdfmonkey" ], - "files": [ - "dist" - ], "homepage": "https://pipedream.com/apps/pdfmonkey", "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3" } } diff --git a/components/pdfmonkey/pdfmonkey.app.mjs b/components/pdfmonkey/pdfmonkey.app.mjs new file mode 100644 index 0000000000000..10d52ef699c98 --- /dev/null +++ b/components/pdfmonkey/pdfmonkey.app.mjs @@ -0,0 +1,135 @@ +import { axios } from "@pipedream/platform"; + +export default { + type: "app", + app: "pdfmonkey", + propDefinitions: { + templateId: { + type: "string", + label: "Template ID", + description: "The unique identifier of the document template.", + async options({ page }) { + const { document_template_cards: data } = await this.listTemplates({ + params: { + "page[number]": page, + }, + }); + + return data.map(({ + id: value, identifier: label, + }) => ({ + label, + value, + })); + }, + }, + documentId: { + type: "string", + label: "Document ID", + description: "The unique identifier of the document.", + async options({ page }) { + const { document_cards: data } = await this.listDocuments({ + params: { + "page[number]": page, + }, + }); + + return data.map(({ + id: value, filename, + }) => ({ + label: `${value}${filename + ? ` - ${filename}` + : ""}`, + value, + })); + }, + }, + }, + methods: { + _baseUrl() { + return "https://api.pdfmonkey.io/api/v1"; + }, + _headers() { + return { + "Authorization": `Bearer ${this.$auth.api_key}`, + }; + }, + _makeRequest({ + $ = this, path, ...opts + }) { + return axios($, { + url: this._baseUrl() + path, + headers: this._headers(), + ...opts, + }); + }, + createDocument(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/documents", + ...opts, + }); + }, + deleteDocument({ + documentId, ...opts + }) { + return this._makeRequest({ + method: "DELETE", + path: `/documents/${documentId}`, + ...opts, + }); + }, + getDocument({ + documentId, ...opts + }) { + return this._makeRequest({ + path: `/documents/${documentId}`, + ...opts, + }); + }, + listDocuments(opts = {}) { + return this._makeRequest({ + path: "/document_cards", + ...opts, + }); + }, + listTemplates(opts = {}) { + return this._makeRequest({ + path: "/document_template_cards", + ...opts, + }); + }, + async *paginate({ + fn, params = {}, maxResults = null, ...opts + }) { + let hasMore = false; + let count = 0; + let page = 0; + + do { + params["page[number]"] = ++page; + + const { + document_cards: data, + meta: { + current_page, total_pages, + }, + } = await fn({ + params, + ...opts, + }); + + for (const d of data) { + yield d; + + if (maxResults && ++count === maxResults) { + return count; + } + } + + hasMore = current_page < total_pages; + + } while (hasMore); + }, + }, +}; diff --git a/components/pdfmonkey/sources/new-document-generated/new-document-generated.mjs b/components/pdfmonkey/sources/new-document-generated/new-document-generated.mjs new file mode 100644 index 0000000000000..609f808db29b2 --- /dev/null +++ b/components/pdfmonkey/sources/new-document-generated/new-document-generated.mjs @@ -0,0 +1,67 @@ +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; +import pdfmonkey from "../../pdfmonkey.app.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + key: "pdfmonkey-new-document-generated", + name: "New Document Generated", + description: "Emit new event when a document's generation is completed.", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + pdfmonkey, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + }, + methods: { + _getLastDate() { + return this.db.get("lastDate") || 0; + }, + _setLastDate(lastDate) { + this.db.set("lastDate", lastDate); + }, + async emitEvent(maxResults = false) { + const lastDate = this._getLastDate(); + const response = this.pdfmonkey.paginate({ + fn: this.pdfmonkey.listDocuments, + maxResults, + params: { + "q[status]": "success", + "q[updated_since]": lastDate, + }, + }); + + let responseArray = []; + for await (const item of response) { + responseArray.push(item); + } + + if (responseArray.length) { + this._setLastDate(Date.parse(responseArray[0].created_at)); + } + + for (const item of responseArray.reverse()) { + this.$emit(item, { + id: item.id, + summary: `Document ${item.filename || item.id} Generation Completed`, + ts: Date.parse(item.created_at), + }); + } + }, + }, + hooks: { + async deploy() { + await this.emitEvent(25); + }, + }, + async run() { + await this.emitEvent(); + }, + sampleEmit, +}; diff --git a/components/pdfmonkey/sources/new-document-generated/test-event.mjs b/components/pdfmonkey/sources/new-document-generated/test-event.mjs new file mode 100644 index 0000000000000..050ab7bb9b2ef --- /dev/null +++ b/components/pdfmonkey/sources/new-document-generated/test-event.mjs @@ -0,0 +1,14 @@ +export default { + "id": "11475e57-0334-4ad5-8896-9462a2243957", + "app_id": "c2b67b84-4aac-49ea-bed8-69a15d7a65d3", + "created_at": "2022-04-07T11:01:38.201+02:00", + "document_template_id": "96611e9e-ab03-4ac3-8551-1b485210c892", + "document_template_identifier": "My Awesome Template", + "download_url": "https://pdfmonkey.s3.eu-west-1.amazonaws.com/production/backend/document/11475e57-0334-4ad5-8896-9462a2243957/my-test-document.pdf?response-content-disposition=attachment&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJ2ZTKW4HMOLK63IQ%2F20220406%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20220407T204150Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host&X-Amz-Signature=24e3a8c0801ad8d1efd6aaa22d946ee70f5c8d5b55c586f346a094afa5046c77", + "failure_cause": null, + "filename": "my-test-document.pdf", + "meta": "{ \"_filename\":\"my-test-document.pdf\" }", + "public_share_link": "https://files.pdfmonkey.io/share/5CEA8C37-D130-4C19-9E11-72BE2293C82B/my-test-document.pdf", + "status": "success", + "updated_at": "2022-04-03T11:12:56.023+02:00" +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4f738769868e4..9288246a49e8d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7177,7 +7177,10 @@ importers: '@pdfless/pdfless-js': 1.0.510 components/pdfmonkey: - specifiers: {} + specifiers: + '@pipedream/platform': ^3.0.3 + dependencies: + '@pipedream/platform': 3.0.3 components/peach: specifiers: