Skip to content

Commit 29e5b10

Browse files
authored
New Components - documerge (#12900)
* documerge init * new components * pnpm-lock.yaml
1 parent eb5e7fc commit 29e5b10

File tree

11 files changed

+432
-8
lines changed

11 files changed

+432
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import documerge from "../../documerge.app.mjs";
2+
import fs from "fs";
3+
4+
export default {
5+
key: "documerge-combine-files",
6+
name: "Combine Files",
7+
description: "Merges multiple user-specified files into a single PDF or DOCX. [See the documentation](https://app.documerge.ai/api-docs/#tools-POSTapi-tools-combine)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
documerge,
12+
name: {
13+
type: "string",
14+
label: "Name",
15+
description: "Name of the new file",
16+
},
17+
output: {
18+
type: "string",
19+
label: "Output",
20+
description: "The output file type",
21+
options: [
22+
"pdf",
23+
"docx",
24+
],
25+
},
26+
urls: {
27+
type: "string[]",
28+
label: "URLs",
29+
description: "Array of URLs to combine",
30+
},
31+
},
32+
async run({ $ }) {
33+
const fileContent = await this.documerge.combineFiles({
34+
$,
35+
data: {
36+
output: this.output,
37+
files: this.urls.map((url) => ({
38+
name: this.name,
39+
url,
40+
})),
41+
},
42+
});
43+
44+
const filename = this.name.includes(".pdf") || this.name.includes(".docx")
45+
? this.name
46+
: `${this.name}.${this.output}`;
47+
const path = `/tmp/${filename}`;
48+
await fs.writeFileSync(path, Buffer.from(fileContent));
49+
50+
$.export("$summary", "Successfully combined files");
51+
return path;
52+
},
53+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import documerge from "../../documerge.app.mjs";
2+
import fs from "fs";
3+
4+
export default {
5+
key: "documerge-convert-file-to-pdf",
6+
name: "Convert File to PDF",
7+
description: "Converts a specified file into a PDF. [See the documentation](https://app.documerge.ai/api-docs/#tools-POSTapi-tools-pdf-convert)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
documerge,
12+
name: {
13+
type: "string",
14+
label: "Name",
15+
description: "Name of the new file",
16+
},
17+
url: {
18+
type: "string",
19+
label: "URL",
20+
description: "The URL of the file to convert",
21+
optional: true,
22+
},
23+
},
24+
async run({ $ }) {
25+
const fileContent = await this.documerge.convertToPdf({
26+
$,
27+
data: {
28+
file: {
29+
name: this.name,
30+
url: this.url,
31+
},
32+
},
33+
});
34+
35+
const filename = this.name.includes(".pdf")
36+
? this.name
37+
: `${this.name}.pdf`;
38+
const path = `/tmp/${filename}`;
39+
await fs.writeFileSync(path, Buffer.from(fileContent));
40+
41+
$.export("$summary", "Successfully converted file to PDF");
42+
return path;
43+
},
44+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import documerge from "../../documerge.app.mjs";
2+
3+
export default {
4+
key: "documerge-get-document-fields",
5+
name: "Get Document Fields",
6+
description: "Extracts and returns data from fields in a given document. [See the documentation](https://app.documerge.ai/api-docs/#documents-GETapi-documents-fields--document_id-)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
documerge,
11+
documentId: {
12+
propDefinition: [
13+
documerge,
14+
"documentId",
15+
],
16+
},
17+
},
18+
async run({ $ }) {
19+
const response = await this.documerge.getDocumentFields({
20+
$,
21+
documentId: this.documentId,
22+
});
23+
$.export("$summary", `Successfully extracted field values from document with ID: ${this.documentId}`);
24+
return response;
25+
},
26+
};
+123-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,129 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "documerge",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
documentId: {
8+
type: "string",
9+
label: "Document ID",
10+
description: "Identifier of a document",
11+
async options() {
12+
const { data } = await this.listDocuments();
13+
return data?.map(({
14+
id: value, name: label,
15+
}) => ({
16+
value,
17+
label,
18+
})) || [];
19+
},
20+
},
21+
routeId: {
22+
type: "string",
23+
label: "Routet ID",
24+
description: "Identifier of a route",
25+
async options() {
26+
const { data } = await this.listRoutes();
27+
return data?.map(({
28+
id: value, name: label,
29+
}) => ({
30+
value,
31+
label,
32+
})) || [];
33+
},
34+
},
35+
},
536
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
37+
_baseUrl() {
38+
return "https://app.documerge.ai/api";
39+
},
40+
_makeRequest(opts = {}) {
41+
const {
42+
$ = this,
43+
path,
44+
...otherOpts
45+
} = opts;
46+
return axios($, {
47+
...otherOpts,
48+
url: `${this._baseUrl()}${path}`,
49+
headers: {
50+
"Authorization": `Bearer ${this.$auth.api_token}`,
51+
"Content-Type": "application/json",
52+
"Accept": "application/json",
53+
},
54+
});
55+
},
56+
listDocuments(opts = {}) {
57+
return this._makeRequest({
58+
path: "/documents",
59+
...opts,
60+
});
61+
},
62+
listRoutes(opts = {}) {
63+
return this._makeRequest({
64+
path: "/routes",
65+
...opts,
66+
});
67+
},
68+
getDocumentFields({
69+
documentId, ...opts
70+
}) {
71+
return this._makeRequest({
72+
path: `/documents/fields/${documentId}`,
73+
...opts,
74+
});
75+
},
76+
combineFiles(opts = {}) {
77+
return this._makeRequest({
78+
method: "POST",
79+
path: "/tools/combine",
80+
responseType: "arraybuffer",
81+
...opts,
82+
});
83+
},
84+
convertToPdf(opts = {}) {
85+
return this._makeRequest({
86+
method: "POST",
87+
path: "/tools/pdf/convert",
88+
responseType: "arraybuffer",
89+
...opts,
90+
});
91+
},
92+
createDocumentDeliveryMethod({
93+
documentId, ...opts
94+
}) {
95+
return this._makeRequest({
96+
method: "POST",
97+
path: `/documents/delivery-methods/${documentId}`,
98+
...opts,
99+
});
100+
},
101+
deleteDocumentDeliveryMethod({
102+
documentId, deliveryMethodId, ...opts
103+
}) {
104+
return this._makeRequest({
105+
method: "DELETE",
106+
path: `/documents/delivery-methods/${documentId}/${deliveryMethodId}`,
107+
...opts,
108+
});
109+
},
110+
createRouteDeliveryMethod({
111+
routeId, ...opts
112+
}) {
113+
return this._makeRequest({
114+
method: "POST",
115+
path: `/routes/delivery-methods/${routeId}`,
116+
...opts,
117+
});
118+
},
119+
deleteRouteDeliveryMethod({
120+
routeId, deliveryMethodId, ...opts
121+
}) {
122+
return this._makeRequest({
123+
method: "DELETE",
124+
path: `/routes/delivery-methods/${routeId}/${deliveryMethodId}`,
125+
...opts,
126+
});
9127
},
10128
},
11-
};
129+
};

components/documerge/package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/documerge",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream DocuMerge Components",
55
"main": "documerge.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.0"
1417
}
15-
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import documerge from "../../documerge.app.mjs";
2+
3+
export default {
4+
props: {
5+
documerge,
6+
http: "$.interface.http",
7+
db: "$.service.db",
8+
},
9+
methods: {
10+
_getDeliveryMethodIds() {
11+
return this.db.get("deliveryMethodIds") || {};
12+
},
13+
_setDeliveryMethodIds(deliveryMethodIds) {
14+
this.db.set("deliveryMethodIds", deliveryMethodIds);
15+
},
16+
getWebhookSettings() {
17+
return {
18+
type: "webhook",
19+
settings: {
20+
url: this.http.endpoint,
21+
always_send: true,
22+
send_temporary_download_url: true,
23+
send_data_using_json: true,
24+
send_merge_data: true,
25+
},
26+
};
27+
},
28+
generateMeta(body) {
29+
return {
30+
id: body.merge_id,
31+
summary: this.getSummary(body),
32+
ts: Date.now(),
33+
};
34+
},
35+
getSummary() {
36+
throw new Error("getSummary is not implemented");
37+
},
38+
},
39+
async run(event) {
40+
const { body } = event;
41+
const meta = this.generateMeta(body);
42+
this.$emit(body, meta);
43+
},
44+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
3+
4+
export default {
5+
...common,
6+
key: "documerge-new-merged-document-instant",
7+
name: "New Merged Document (Instant)",
8+
description: "Emit new event when a merged document is created in documerge.",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
props: {
13+
...common.props,
14+
documentIds: {
15+
propDefinition: [
16+
common.props.documerge,
17+
"documentId",
18+
],
19+
type: "string[]",
20+
label: "Document IDs",
21+
description: "An array of document identifiers of the documents to watch",
22+
},
23+
},
24+
hooks: {
25+
async activate() {
26+
const deliveryMethodIds = {};
27+
for (const documentId of this.documentIds) {
28+
const { data: { id } } = await this.documerge.createDocumentDeliveryMethod({
29+
documentId,
30+
data: this.getWebhookSettings(),
31+
});
32+
deliveryMethodIds[documentId] = id;
33+
}
34+
this._setDeliveryMethodIds(deliveryMethodIds);
35+
},
36+
async deactivate() {
37+
const deliveryMethodIds = this._getDeliveryMethodIds();
38+
for (const documentId of this.documentIds) {
39+
await this.documerge.deleteDocumentDeliveryMethod({
40+
documentId,
41+
deliveryMethodId: deliveryMethodIds[documentId],
42+
});
43+
}
44+
this._setDeliveryMethodIds({});
45+
},
46+
},
47+
methods: {
48+
...common.methods,
49+
getSummary(body) {
50+
return `Merged Document: ${body.file_name}`;
51+
},
52+
},
53+
sampleEmit,
54+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
"file_url": "https://documerge.blob.core.windows.net/documerge/documentMerges/408/294186/doc1_-_test.pdf?sv=2017-11-09&sr=b&se=2024-07-18T17:16:51Z&sp=r&spr=https&sig=%2F02yOVeuTEB66AihSptlTg7a7GYfxz3JGkxKE2kt94Y%3D",
3+
"file_name": "doc1 - test.pdf",
4+
"merge_id": 320214,
5+
"fields": {
6+
"field1": "test",
7+
"_date": "2024-07-18",
8+
"_datetime": "2024-07-18 12:16 pm",
9+
"_ip": "10.1.0.4",
10+
"_auto_number": null
11+
}
12+
}

0 commit comments

Comments
 (0)