Skip to content

Commit 5c08a3c

Browse files
authored
New Components - paperform (#14306)
* init * new component * pnpm-lock.yaml
1 parent 7432e03 commit 5c08a3c

File tree

6 files changed

+231
-73
lines changed

6 files changed

+231
-73
lines changed

components/paperform/.gitignore

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

components/paperform/app/paperform.app.ts

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

components/paperform/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"name": "@pipedream/paperform",
3-
"version": "0.0.3",
3+
"version": "0.1.0",
44
"description": "Pipedream PaperForm Components",
5-
"main": "dist/app/paperform.app.mjs",
5+
"main": "paperform.app.mjs",
66
"keywords": [
77
"pipedream",
88
"paperform"
99
],
10-
"files": [
11-
"dist"
12-
],
1310
"homepage": "https://pipedream.com/apps/paperform",
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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { axios } from "@pipedream/platform";
2+
const DEFAULT_LIMIT = 100;
3+
4+
export default {
5+
type: "app",
6+
app: "paperform",
7+
propDefinitions: {
8+
formId: {
9+
type: "string",
10+
label: "Form ID",
11+
description: "The ID of the form to monitor",
12+
async options({ page }) {
13+
const { results: { forms } } = await this.listForms({
14+
params: {
15+
limit: DEFAULT_LIMIT,
16+
skip: page * DEFAULT_LIMIT,
17+
},
18+
});
19+
return forms?.map(({
20+
id: value, title: label,
21+
}) => ({
22+
label,
23+
value,
24+
})) || [];
25+
},
26+
},
27+
},
28+
methods: {
29+
_baseUrl() {
30+
return "https://api.paperform.co/v1";
31+
},
32+
_makeRequest(opts = {}) {
33+
const {
34+
$ = this,
35+
path,
36+
...otherOpts
37+
} = opts;
38+
return axios($, {
39+
...otherOpts,
40+
url: `${this._baseUrl()}${path}`,
41+
headers: {
42+
Authorization: `Bearer ${this.$auth.api_key}`,
43+
},
44+
});
45+
},
46+
listForms(opts = {}) {
47+
return this._makeRequest({
48+
path: "/forms",
49+
...opts,
50+
});
51+
},
52+
listSubmissions({
53+
formId, ...opts
54+
}) {
55+
return this._makeRequest({
56+
path: `/forms/${formId}/submissions`,
57+
...opts,
58+
});
59+
},
60+
async *paginate({
61+
fn, args, resourceKey, max,
62+
}) {
63+
args = {
64+
...args,
65+
params: {
66+
...args?.params,
67+
limit: DEFAULT_LIMIT,
68+
skip: 0,
69+
},
70+
};
71+
let hasMore, count = 0;
72+
do {
73+
const {
74+
results, has_more: more,
75+
} = await fn(args);
76+
const items = results[resourceKey];
77+
for (const item of items) {
78+
yield item;
79+
if (max && ++count >= max) {
80+
return;
81+
}
82+
}
83+
hasMore = more;
84+
args.params.skip += args.params.limit;
85+
} while (hasMore);
86+
},
87+
},
88+
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import paperform from "../../paperform.app.mjs";
2+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
3+
4+
export default {
5+
key: "paperform-new-submission",
6+
name: "New Submission",
7+
description: "Emit new event when a new submission is made on the specified form in Paperform. [See the documentation](https://paperform.readme.io/reference/listformsubmissions)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
paperform,
13+
timer: {
14+
type: "$.interface.timer",
15+
default: {
16+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
17+
},
18+
},
19+
db: "$.service.db",
20+
formId: {
21+
propDefinition: [
22+
paperform,
23+
"formId",
24+
],
25+
},
26+
},
27+
hooks: {
28+
async deploy() {
29+
await this.processEvent(25);
30+
},
31+
},
32+
methods: {
33+
_getLastTs() {
34+
return this.db.get("lastTs") || 0;
35+
},
36+
_setLastTs(lastTs) {
37+
this.db.set("lastTs", lastTs);
38+
},
39+
generateMeta(submission) {
40+
return {
41+
id: submission.id,
42+
summary: `New Submission ID: ${submission.id}`,
43+
ts: Date.parse(submission.created_at),
44+
};
45+
},
46+
async processEvent(max) {
47+
const lastTs = this._getLastTs();
48+
49+
const items = this.paperform.paginate({
50+
fn: this.paperform.listSubmissions,
51+
args: {
52+
formId: this.formId,
53+
},
54+
resourceKey: "submissions",
55+
max,
56+
});
57+
58+
const submissions = [];
59+
for await (const item of items) {
60+
const ts = Date.parse(item.created_at);
61+
if (ts >= lastTs) {
62+
submissions.push(item);
63+
} else {
64+
break;
65+
}
66+
}
67+
68+
if (!submissions?.length) {
69+
return;
70+
}
71+
72+
this._setLastTs(Date.parse(submissions[0].created_at));
73+
74+
submissions.forEach((submission) => {
75+
const meta = this.generateMeta(submission);
76+
this.$emit(submission, meta);
77+
});
78+
},
79+
},
80+
async run() {
81+
await this.processEvent();
82+
},
83+
};

pnpm-lock.yaml

Lines changed: 55 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)