Skip to content

Commit 625c3bc

Browse files
committed
[Components] pdfmonkey #13201
Sources - New Document Generated Actions - Generate Document - Delete Document - Find Document
1 parent dfb8d8d commit 625c3bc

File tree

10 files changed

+199
-162
lines changed

10 files changed

+199
-162
lines changed

components/pdfmonkey/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

components/pdfmonkey/actions/delete-document/delete-document.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import pdfmonkey from "../../pdfmonkey.app.mjs";
2-
import { axios } from "@pipedream/platform";
32

43
export default {
54
key: "pdfmonkey-delete-document",
65
name: "Delete Document",
76
description: "Deletes a specific document using its ID. [See the documentation](https://docs.pdfmonkey.io/references/api/documents)",
8-
version: "0.0.{{ts}}",
7+
version: "0.0.1",
98
type: "action",
109
props: {
1110
pdfmonkey,
@@ -17,7 +16,10 @@ export default {
1716
},
1817
},
1918
async run({ $ }) {
20-
const response = await this.pdfmonkey.deleteDocument(this.documentId);
19+
const response = await this.pdfmonkey.deleteDocument({
20+
$,
21+
documentId: this.documentId,
22+
});
2123
$.export("$summary", `Deleted document with ID ${this.documentId}`);
2224
return response;
2325
},

components/pdfmonkey/actions/find-document/find-document.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import pdfmonkey from "../../pdfmonkey.app.mjs";
2-
import { axios } from "@pipedream/platform";
32

43
export default {
54
key: "pdfmonkey-find-document",
65
name: "Find Document",
76
description: "Find a document within PDFMonkey. [See the documentation](https://docs.pdfmonkey.io/references/api/documents)",
8-
version: "0.0.{{ts}}",
7+
version: "0.0.1",
98
type: "action",
109
props: {
1110
pdfmonkey,
@@ -17,7 +16,10 @@ export default {
1716
},
1817
},
1918
async run({ $ }) {
20-
const document = await this.pdfmonkey.findDocument(this.documentId);
19+
const document = await this.pdfmonkey.getDocument({
20+
$,
21+
documentId: this.documentId,
22+
});
2123
$.export("$summary", `Successfully found document with ID ${this.documentId}`);
2224
return document;
2325
},

components/pdfmonkey/actions/generate-document/generate-document.mjs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { STATUS_OPTIONS } from "../../common/constants.mjs";
12
import pdfmonkey from "../../pdfmonkey.app.mjs";
23

34
export default {
45
key: "pdfmonkey-generate-document",
56
name: "Generate Document",
67
description: "Generates a new document using a specified template. [See the documentation](https://docs.pdfmonkey.io/references/api/documents)",
7-
version: "0.0.{{ts}}",
8+
version: "0.0.1",
89
type: "action",
910
props: {
1011
pdfmonkey,
@@ -14,24 +15,37 @@ export default {
1415
"templateId",
1516
],
1617
},
17-
data: {
18-
propDefinition: [
19-
pdfmonkey,
20-
"data",
21-
],
18+
payload: {
19+
type: "object",
20+
label: "Payload",
21+
description: "Data to use for the Document generation.",
22+
optional: true,
2223
},
23-
metadata: {
24-
propDefinition: [
25-
pdfmonkey,
26-
"metadata",
27-
],
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,
2836
},
2937
},
3038
async run({ $ }) {
3139
const response = await this.pdfmonkey.createDocument({
32-
templateId: this.templateId,
33-
data: this.data,
34-
metadata: this.metadata,
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+
},
3549
});
3650

3751
$.export("$summary", `Successfully generated document with ID ${response.document.id}`);

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 <[email protected]> (https://pipedream.com/)",
1512
"publishConfig": {
1613
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1717
}
1818
}

components/pdfmonkey/pdfmonkey.app.mjs

Lines changed: 94 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,100 +4,132 @@ export default {
44
type: "app",
55
app: "pdfmonkey",
66
propDefinitions: {
7-
documentId: {
8-
type: "string",
9-
label: "Document ID",
10-
description: "The unique identifier of the document.",
11-
},
127
templateId: {
138
type: "string",
149
label: "Template ID",
1510
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+
},
1625
},
17-
data: {
18-
type: "object",
19-
label: "Payload",
20-
description: "Data used for the document generation.",
21-
optional: true,
22-
},
23-
metadata: {
24-
type: "object",
25-
label: "Metadata",
26-
description: "Metadata to attach to the document.",
27-
optional: true,
28-
},
29-
documentName: {
26+
documentId: {
3027
type: "string",
31-
label: "Document Name",
32-
description: "Optional name for the document.",
33-
optional: true,
34-
},
35-
additionalMetadata: {
36-
type: "object",
37-
label: "Additional Metadata",
38-
description: "Optional additional metadata for the document.",
39-
optional: true,
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+
},
4046
},
4147
},
4248
methods: {
4349
_baseUrl() {
4450
return "https://api.pdfmonkey.io/api/v1";
4551
},
46-
async _makeRequest(opts = {}) {
47-
const {
48-
$ = this, method = "GET", path = "/", headers, ...otherOpts
49-
} = opts;
52+
_headers() {
53+
return {
54+
"Authorization": `Bearer ${this.$auth.api_key}`,
55+
};
56+
},
57+
_makeRequest({
58+
$ = this, path, ...opts
59+
}) {
5060
return axios($, {
51-
...otherOpts,
52-
method,
5361
url: this._baseUrl() + path,
54-
headers: {
55-
...headers,
56-
"Authorization": `Bearer ${this.$auth.api_key}`,
57-
"Content-Type": "application/json",
58-
},
62+
headers: this._headers(),
63+
...opts,
5964
});
6065
},
61-
async createDocument({
62-
templateId, data, metadata,
63-
}) {
66+
createDocument(opts = {}) {
6467
return this._makeRequest({
6568
method: "POST",
6669
path: "/documents",
67-
data: {
68-
document: {
69-
document_template_id: templateId,
70-
status: "pending",
71-
payload: data,
72-
meta: metadata,
73-
},
74-
},
70+
...opts,
7571
});
7672
},
77-
async deleteDocument(documentId) {
73+
deleteDocument({
74+
documentId, ...opts
75+
}) {
7876
return this._makeRequest({
7977
method: "DELETE",
8078
path: `/documents/${documentId}`,
79+
...opts,
8180
});
8281
},
83-
async findDocument(documentId) {
82+
getDocument({
83+
documentId, ...opts
84+
}) {
8485
return this._makeRequest({
85-
method: "GET",
8686
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,
87100
});
88101
},
89-
async emitDocumentCompletion({
90-
documentId, documentName, additionalMetadata,
102+
async *paginate({
103+
fn, params = {}, maxResults = null, ...opts
91104
}) {
92-
const document = await this.findDocument(documentId);
93-
if (document.status === "success") {
94-
this.$emit(document, {
95-
id: document.id,
96-
summary: `Document ${documentName || document.filename} Generation Completed`,
97-
ts: Date.now(),
98-
additionalMetadata,
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,
99120
});
100-
}
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);
101133
},
102134
},
103135
};

0 commit comments

Comments
 (0)