Skip to content

Commit 52f29a4

Browse files
authored
New Components - pdfmonkey (#14237)
* pdfmonkey init * [Components] pdfmonkey #13201 Sources - New Document Generated Actions - Generate Document - Delete Document - Find Document * pnpm update * adjust paginate method
1 parent 792d115 commit 52f29a4

File tree

11 files changed

+341
-22
lines changed

11 files changed

+341
-22
lines changed

components/pdfmonkey/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pdfmonkey from "../../pdfmonkey.app.mjs";
2+
3+
export default {
4+
key: "pdfmonkey-delete-document",
5+
name: "Delete Document",
6+
description: "Deletes a specific document using its ID. [See the documentation](https://docs.pdfmonkey.io/references/api/documents)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
pdfmonkey,
11+
documentId: {
12+
propDefinition: [
13+
pdfmonkey,
14+
"documentId",
15+
],
16+
},
17+
},
18+
async run({ $ }) {
19+
const response = await this.pdfmonkey.deleteDocument({
20+
$,
21+
documentId: this.documentId,
22+
});
23+
$.export("$summary", `Deleted document with ID ${this.documentId}`);
24+
return response;
25+
},
26+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pdfmonkey from "../../pdfmonkey.app.mjs";
2+
3+
export default {
4+
key: "pdfmonkey-find-document",
5+
name: "Find Document",
6+
description: "Find a document within PDFMonkey. [See the documentation](https://docs.pdfmonkey.io/references/api/documents)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
pdfmonkey,
11+
documentId: {
12+
propDefinition: [
13+
pdfmonkey,
14+
"documentId",
15+
],
16+
},
17+
},
18+
async run({ $ }) {
19+
const document = await this.pdfmonkey.getDocument({
20+
$,
21+
documentId: this.documentId,
22+
});
23+
$.export("$summary", `Successfully found document with ID ${this.documentId}`);
24+
return document;
25+
},
26+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { STATUS_OPTIONS } from "../../common/constants.mjs";
2+
import pdfmonkey from "../../pdfmonkey.app.mjs";
3+
4+
export default {
5+
key: "pdfmonkey-generate-document",
6+
name: "Generate Document",
7+
description: "Generates a new document using a specified template. [See the documentation](https://docs.pdfmonkey.io/references/api/documents)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
pdfmonkey,
12+
templateId: {
13+
propDefinition: [
14+
pdfmonkey,
15+
"templateId",
16+
],
17+
},
18+
payload: {
19+
type: "object",
20+
label: "Payload",
21+
description: "Data to use for the Document generation.",
22+
optional: true,
23+
},
24+
meta: {
25+
type: "object",
26+
label: "Meta",
27+
description: "Meta-Data to attach to the Document.",
28+
optional: true,
29+
},
30+
status: {
31+
type: "string",
32+
label: "Status",
33+
description: "The status of the document",
34+
options: STATUS_OPTIONS,
35+
optional: true,
36+
},
37+
},
38+
async run({ $ }) {
39+
const response = await this.pdfmonkey.createDocument({
40+
$,
41+
data: {
42+
document: {
43+
document_template_id: this.templateId,
44+
payload: this.payload,
45+
meta: this.meta,
46+
status: this.status,
47+
},
48+
},
49+
});
50+
51+
$.export("$summary", `Successfully generated document with ID ${response.document.id}`);
52+
return response;
53+
},
54+
};

components/pdfmonkey/app/pdfmonkey.app.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const STATUS_OPTIONS = [
2+
{
3+
label: "Draft (Default)",
4+
value: "draft",
5+
},
6+
{
7+
label: "Pending (To trigger generation)",
8+
value: "pending",
9+
},
10+
];

components/pdfmonkey/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"name": "@pipedream/pdfmonkey",
3-
"version": "0.0.3",
3+
"version": "0.1.0",
44
"description": "Pipedream PDFMonkey Components",
5-
"main": "dist/app/pdfmonkey.app.mjs",
5+
"main": "pdfmonkey.app.mjs",
66
"keywords": [
77
"pipedream",
88
"pdfmonkey"
99
],
10-
"files": [
11-
"dist"
12-
],
1310
"homepage": "https://pipedream.com/apps/pdfmonkey",
1411
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
1512
"publishConfig": {
1613
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1717
}
1818
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { axios } from "@pipedream/platform";
2+
3+
export default {
4+
type: "app",
5+
app: "pdfmonkey",
6+
propDefinitions: {
7+
templateId: {
8+
type: "string",
9+
label: "Template ID",
10+
description: "The unique identifier of the document template.",
11+
async options({ page }) {
12+
const { document_template_cards: data } = await this.listTemplates({
13+
params: {
14+
"page[number]": page,
15+
},
16+
});
17+
18+
return data.map(({
19+
id: value, identifier: label,
20+
}) => ({
21+
label,
22+
value,
23+
}));
24+
},
25+
},
26+
documentId: {
27+
type: "string",
28+
label: "Document ID",
29+
description: "The unique identifier of the document.",
30+
async options({ page }) {
31+
const { document_cards: data } = await this.listDocuments({
32+
params: {
33+
"page[number]": page,
34+
},
35+
});
36+
37+
return data.map(({
38+
id: value, filename,
39+
}) => ({
40+
label: `${value}${filename
41+
? ` - ${filename}`
42+
: ""}`,
43+
value,
44+
}));
45+
},
46+
},
47+
},
48+
methods: {
49+
_baseUrl() {
50+
return "https://api.pdfmonkey.io/api/v1";
51+
},
52+
_headers() {
53+
return {
54+
"Authorization": `Bearer ${this.$auth.api_key}`,
55+
};
56+
},
57+
_makeRequest({
58+
$ = this, path, ...opts
59+
}) {
60+
return axios($, {
61+
url: this._baseUrl() + path,
62+
headers: this._headers(),
63+
...opts,
64+
});
65+
},
66+
createDocument(opts = {}) {
67+
return this._makeRequest({
68+
method: "POST",
69+
path: "/documents",
70+
...opts,
71+
});
72+
},
73+
deleteDocument({
74+
documentId, ...opts
75+
}) {
76+
return this._makeRequest({
77+
method: "DELETE",
78+
path: `/documents/${documentId}`,
79+
...opts,
80+
});
81+
},
82+
getDocument({
83+
documentId, ...opts
84+
}) {
85+
return this._makeRequest({
86+
path: `/documents/${documentId}`,
87+
...opts,
88+
});
89+
},
90+
listDocuments(opts = {}) {
91+
return this._makeRequest({
92+
path: "/document_cards",
93+
...opts,
94+
});
95+
},
96+
listTemplates(opts = {}) {
97+
return this._makeRequest({
98+
path: "/document_template_cards",
99+
...opts,
100+
});
101+
},
102+
async *paginate({
103+
fn, params = {}, maxResults = null, ...opts
104+
}) {
105+
let hasMore = false;
106+
let count = 0;
107+
let page = 0;
108+
109+
do {
110+
params["page[number]"] = ++page;
111+
112+
const {
113+
document_cards: data,
114+
meta: {
115+
current_page, total_pages,
116+
},
117+
} = await fn({
118+
params,
119+
...opts,
120+
});
121+
122+
for (const d of data) {
123+
yield d;
124+
125+
if (maxResults && ++count === maxResults) {
126+
return count;
127+
}
128+
}
129+
130+
hasMore = current_page < total_pages;
131+
132+
} while (hasMore);
133+
},
134+
},
135+
};
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import pdfmonkey from "../../pdfmonkey.app.mjs";
3+
import sampleEmit from "./test-event.mjs";
4+
5+
export default {
6+
key: "pdfmonkey-new-document-generated",
7+
name: "New Document Generated",
8+
description: "Emit new event when a document's generation is completed.",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
props: {
13+
pdfmonkey,
14+
db: "$.service.db",
15+
timer: {
16+
type: "$.interface.timer",
17+
default: {
18+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
19+
},
20+
},
21+
},
22+
methods: {
23+
_getLastDate() {
24+
return this.db.get("lastDate") || 0;
25+
},
26+
_setLastDate(lastDate) {
27+
this.db.set("lastDate", lastDate);
28+
},
29+
async emitEvent(maxResults = false) {
30+
const lastDate = this._getLastDate();
31+
const response = this.pdfmonkey.paginate({
32+
fn: this.pdfmonkey.listDocuments,
33+
maxResults,
34+
params: {
35+
"q[status]": "success",
36+
"q[updated_since]": lastDate,
37+
},
38+
});
39+
40+
let responseArray = [];
41+
for await (const item of response) {
42+
responseArray.push(item);
43+
}
44+
45+
if (responseArray.length) {
46+
this._setLastDate(Date.parse(responseArray[0].created_at));
47+
}
48+
49+
for (const item of responseArray.reverse()) {
50+
this.$emit(item, {
51+
id: item.id,
52+
summary: `Document ${item.filename || item.id} Generation Completed`,
53+
ts: Date.parse(item.created_at),
54+
});
55+
}
56+
},
57+
},
58+
hooks: {
59+
async deploy() {
60+
await this.emitEvent(25);
61+
},
62+
},
63+
async run() {
64+
await this.emitEvent();
65+
},
66+
sampleEmit,
67+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default {
2+
"id": "11475e57-0334-4ad5-8896-9462a2243957",
3+
"app_id": "c2b67b84-4aac-49ea-bed8-69a15d7a65d3",
4+
"created_at": "2022-04-07T11:01:38.201+02:00",
5+
"document_template_id": "96611e9e-ab03-4ac3-8551-1b485210c892",
6+
"document_template_identifier": "My Awesome Template",
7+
"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",
8+
"failure_cause": null,
9+
"filename": "my-test-document.pdf",
10+
"meta": "{ \"_filename\":\"my-test-document.pdf\" }",
11+
"public_share_link": "https://files.pdfmonkey.io/share/5CEA8C37-D130-4C19-9E11-72BE2293C82B/my-test-document.pdf",
12+
"status": "success",
13+
"updated_at": "2022-04-03T11:12:56.023+02:00"
14+
}

0 commit comments

Comments
 (0)