Skip to content

Commit 792d115

Browse files
New Components - formcarry (#14277)
* new component * pnpm-lock.yaml * Update components/formcarry/package.json Co-authored-by: Luan Cazarine <luanhc@gmail.com> --------- Co-authored-by: Luan Cazarine <luanhc@gmail.com>
1 parent 653b81d commit 792d115

File tree

4 files changed

+162
-4
lines changed

4 files changed

+162
-4
lines changed
+56-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,63 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "formcarry",
4-
propDefinitions: {},
56
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
7+
_baseUrl() {
8+
return "https://formcarry.com/api";
9+
},
10+
_headers() {
11+
return {
12+
api_key: `${this.$auth.api_key}`,
13+
};
14+
},
15+
_makeRequest({
16+
$ = this,
17+
path,
18+
...opts
19+
}) {
20+
return axios($, {
21+
url: `${this._baseUrl()}${path}`,
22+
headers: this._headers(),
23+
...opts,
24+
});
25+
},
26+
listSubmissions({
27+
formId, ...opts
28+
}) {
29+
return this._makeRequest({
30+
path: `/form/${formId}/submissions`,
31+
...opts,
32+
});
33+
},
34+
async *paginate({
35+
fn,
36+
args = {},
37+
resourceKey,
38+
max,
39+
}) {
40+
args = {
41+
...args,
42+
params: {
43+
...args?.params,
44+
limit: 50,
45+
page: 1,
46+
},
47+
};
48+
let nextPage, count = 0;
49+
do {
50+
const response = await fn(args);
51+
const items = response[resourceKey];
52+
for (const item of items) {
53+
yield item;
54+
if (max && ++count >= max) {
55+
return;
56+
}
57+
}
58+
nextPage = response.pagination.next_page;
59+
args.params.page = nextPage;
60+
} while (nextPage);
961
},
1062
},
1163
};

components/formcarry/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "@pipedream/formcarry",
3+
"version": "0.1.0",
4+
"description": "Pipedream Formcarry Components",
5+
"main": "formcarry.app.mjs",
6+
"keywords": [
7+
"pipedream",
8+
"formcarry"
9+
],
10+
"homepage": "https://pipedream.com/apps/formcarry",
11+
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
12+
"publishConfig": {
13+
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import formcarry from "../../formcarry.app.mjs";
2+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
3+
4+
export default {
5+
key: "formcarry-new-form-submission",
6+
name: "New Form Submission",
7+
description: "Emit new event when the specified form receives a new submission. [See the documentation](https://formcarry.com/docs/formcarry-api/submissions-api#cc7f3010897b4c938c8829db46b18656)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
formcarry,
13+
db: "$.service.db",
14+
timer: {
15+
type: "$.interface.timer",
16+
default: {
17+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
18+
},
19+
},
20+
formId: {
21+
type: "string",
22+
label: "Form ID",
23+
description: "The ID of the form to watch for new submissions",
24+
},
25+
},
26+
hooks: {
27+
async deploy() {
28+
await this.processEvent(25);
29+
},
30+
},
31+
methods: {
32+
_getLastTs() {
33+
return this.db.get("lastTs") || 0;
34+
},
35+
_setLastTs(lastTs) {
36+
this.db.set("lastTs", lastTs);
37+
},
38+
generateMeta(submission) {
39+
return {
40+
id: submission._id,
41+
summary: `New Form Submission ID: ${submission._id}`,
42+
ts: Date.parse(submission.createdAt),
43+
};
44+
},
45+
async processEvent(max) {
46+
const lastTs = this._getLastTs();
47+
48+
const results = this.formcarry.paginate({
49+
fn: this.formcarry.listSubmissions,
50+
args: {
51+
formId: this.formId,
52+
},
53+
resourceKey: "submissions",
54+
max,
55+
});
56+
57+
const submissions = [];
58+
for await (const item of results) {
59+
const ts = Date.parse(item.createdAt);
60+
if (ts >= lastTs) {
61+
submissions.push(item);
62+
} else {
63+
break;
64+
}
65+
}
66+
67+
if (!submissions.length) {
68+
return;
69+
}
70+
71+
this._setLastTs(Date.parse(submissions[0].createdAt));
72+
73+
submissions.forEach((submission) => {
74+
const meta = this.generateMeta(submission);
75+
this.$emit(submission, meta);
76+
});
77+
},
78+
},
79+
async run() {
80+
await this.processEvent();
81+
},
82+
};

pnpm-lock.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)