Skip to content

Commit dfb8d8d

Browse files
committed
pdfmonkey init
1 parent 7843436 commit dfb8d8d

File tree

5 files changed

+279
-0
lines changed

5 files changed

+279
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pdfmonkey from "../../pdfmonkey.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "pdfmonkey-delete-document",
6+
name: "Delete Document",
7+
description: "Deletes a specific document using its ID. [See the documentation](https://docs.pdfmonkey.io/references/api/documents)",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
pdfmonkey,
12+
documentId: {
13+
propDefinition: [
14+
pdfmonkey,
15+
"documentId",
16+
],
17+
},
18+
},
19+
async run({ $ }) {
20+
const response = await this.pdfmonkey.deleteDocument(this.documentId);
21+
$.export("$summary", `Deleted document with ID ${this.documentId}`);
22+
return response;
23+
},
24+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pdfmonkey from "../../pdfmonkey.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "pdfmonkey-find-document",
6+
name: "Find Document",
7+
description: "Find a document within PDFMonkey. [See the documentation](https://docs.pdfmonkey.io/references/api/documents)",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
pdfmonkey,
12+
documentId: {
13+
propDefinition: [
14+
pdfmonkey,
15+
"documentId",
16+
],
17+
},
18+
},
19+
async run({ $ }) {
20+
const document = await this.pdfmonkey.findDocument(this.documentId);
21+
$.export("$summary", `Successfully found document with ID ${this.documentId}`);
22+
return document;
23+
},
24+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pdfmonkey from "../../pdfmonkey.app.mjs";
2+
3+
export default {
4+
key: "pdfmonkey-generate-document",
5+
name: "Generate Document",
6+
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+
type: "action",
9+
props: {
10+
pdfmonkey,
11+
templateId: {
12+
propDefinition: [
13+
pdfmonkey,
14+
"templateId",
15+
],
16+
},
17+
data: {
18+
propDefinition: [
19+
pdfmonkey,
20+
"data",
21+
],
22+
},
23+
metadata: {
24+
propDefinition: [
25+
pdfmonkey,
26+
"metadata",
27+
],
28+
},
29+
},
30+
async run({ $ }) {
31+
const response = await this.pdfmonkey.createDocument({
32+
templateId: this.templateId,
33+
data: this.data,
34+
metadata: this.metadata,
35+
});
36+
37+
$.export("$summary", `Successfully generated document with ID ${response.document.id}`);
38+
return response;
39+
},
40+
};
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { axios } from "@pipedream/platform";
2+
3+
export default {
4+
type: "app",
5+
app: "pdfmonkey",
6+
propDefinitions: {
7+
documentId: {
8+
type: "string",
9+
label: "Document ID",
10+
description: "The unique identifier of the document.",
11+
},
12+
templateId: {
13+
type: "string",
14+
label: "Template ID",
15+
description: "The unique identifier of the document template.",
16+
},
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: {
30+
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,
40+
},
41+
},
42+
methods: {
43+
_baseUrl() {
44+
return "https://api.pdfmonkey.io/api/v1";
45+
},
46+
async _makeRequest(opts = {}) {
47+
const {
48+
$ = this, method = "GET", path = "/", headers, ...otherOpts
49+
} = opts;
50+
return axios($, {
51+
...otherOpts,
52+
method,
53+
url: this._baseUrl() + path,
54+
headers: {
55+
...headers,
56+
"Authorization": `Bearer ${this.$auth.api_key}`,
57+
"Content-Type": "application/json",
58+
},
59+
});
60+
},
61+
async createDocument({
62+
templateId, data, metadata,
63+
}) {
64+
return this._makeRequest({
65+
method: "POST",
66+
path: "/documents",
67+
data: {
68+
document: {
69+
document_template_id: templateId,
70+
status: "pending",
71+
payload: data,
72+
meta: metadata,
73+
},
74+
},
75+
});
76+
},
77+
async deleteDocument(documentId) {
78+
return this._makeRequest({
79+
method: "DELETE",
80+
path: `/documents/${documentId}`,
81+
});
82+
},
83+
async findDocument(documentId) {
84+
return this._makeRequest({
85+
method: "GET",
86+
path: `/documents/${documentId}`,
87+
});
88+
},
89+
async emitDocumentCompletion({
90+
documentId, documentName, additionalMetadata,
91+
}) {
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,
99+
});
100+
}
101+
},
102+
},
103+
};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import pdfmonkey from "../../pdfmonkey.app.mjs";
2+
import {
3+
axios, DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
4+
} from "@pipedream/platform";
5+
6+
export default {
7+
key: "pdfmonkey-new-document-generated",
8+
name: "New Document Generated",
9+
description: "Emit new event when a document's generation is completed. [See the documentation](https://docs.pdfmonkey.io/references/api/documents)",
10+
version: "0.0.{{ts}}",
11+
type: "source",
12+
dedupe: "unique",
13+
props: {
14+
pdfmonkey,
15+
db: "$.service.db",
16+
timer: {
17+
type: "$.interface.timer",
18+
default: {
19+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
20+
},
21+
},
22+
documentId: {
23+
propDefinition: [
24+
pdfmonkey,
25+
"documentId",
26+
],
27+
},
28+
documentName: {
29+
propDefinition: [
30+
pdfmonkey,
31+
"documentName",
32+
],
33+
optional: true,
34+
},
35+
additionalMetadata: {
36+
propDefinition: [
37+
pdfmonkey,
38+
"additionalMetadata",
39+
],
40+
optional: true,
41+
},
42+
},
43+
methods: {
44+
async checkDocumentStatus() {
45+
return await this.pdfmonkey.findDocument(this.documentId);
46+
},
47+
_getLastTimestamp() {
48+
return this.db.get("lastTimestamp") || 0;
49+
},
50+
_setLastTimestamp(timestamp) {
51+
this.db.set("lastTimestamp", timestamp);
52+
},
53+
},
54+
hooks: {
55+
async deploy() {
56+
const document = await this.checkDocumentStatus();
57+
if (document.status === "success") {
58+
this.$emit(document, {
59+
id: document.id,
60+
summary: `Document ${this.documentName || document.filename} Generation Completed`,
61+
ts: Date.parse(document.updated_at),
62+
additionalMetadata: this.additionalMetadata,
63+
});
64+
this._setLastTimestamp(Date.parse(document.updated_at));
65+
}
66+
},
67+
async activate() {
68+
// No webhook subscription required
69+
},
70+
async deactivate() {
71+
// No webhook unsubscription required
72+
},
73+
},
74+
async run() {
75+
const lastTimestamp = this._getLastTimestamp();
76+
const document = await this.checkDocumentStatus();
77+
78+
if (Date.parse(document.updated_at) > lastTimestamp && document.status === "success") {
79+
this.$emit(document, {
80+
id: document.id,
81+
summary: `Document ${this.documentName || document.filename} Generation Completed`,
82+
ts: Date.parse(document.updated_at),
83+
additionalMetadata: this.additionalMetadata,
84+
});
85+
this._setLastTimestamp(Date.parse(document.updated_at));
86+
}
87+
},
88+
};

0 commit comments

Comments
 (0)