Skip to content

Commit 400b10f

Browse files
committed
opnform init
1 parent 1427969 commit 400b10f

File tree

2 files changed

+185
-2
lines changed

2 files changed

+185
-2
lines changed

components/opnform/opnform.app.mjs

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,102 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "opnform",
4-
propDefinitions: {},
6+
version: "0.0.{{ts}}",
7+
propDefinitions: {
8+
workspaceId: {
9+
type: "string",
10+
label: "Workspace ID",
11+
description: "The ID of the workspace containing the forms.",
12+
},
13+
formId: {
14+
type: "string",
15+
label: "Form",
16+
description: "Select the form to monitor for new submissions.",
17+
async options() {
18+
const { workspaceId } = this;
19+
const forms = await this.listForms({
20+
workspaceId,
21+
});
22+
return forms.map((form) => ({
23+
label: form.name,
24+
value: form.id,
25+
}));
26+
},
27+
},
28+
},
529
methods: {
6-
// this.$auth contains connected account data
730
authKeys() {
831
console.log(Object.keys(this.$auth));
932
},
33+
_baseUrl() {
34+
return "https://api.opnform.com/external/zapier";
35+
},
36+
async _makeRequest(opts = {}) {
37+
const {
38+
$ = this, method = "GET", path = "/", headers, params, data, ...otherOpts
39+
} = opts;
40+
return axios($, {
41+
method,
42+
url: this._baseUrl() + path,
43+
headers: {
44+
...headers,
45+
Authorization: `Bearer ${this.$auth.api_key}`,
46+
},
47+
params,
48+
data,
49+
...otherOpts,
50+
});
51+
},
52+
async listForms({
53+
workspaceId, ...opts
54+
} = {}) {
55+
if (!workspaceId) {
56+
throw new Error("workspaceId is required to list forms.");
57+
}
58+
return this._makeRequest({
59+
method: "GET",
60+
path: "/forms",
61+
params: {
62+
workspace_id: workspaceId,
63+
},
64+
...opts,
65+
});
66+
},
67+
async createWebhook({
68+
hookUrl, formId, ...opts
69+
} = {}) {
70+
return this._makeRequest({
71+
method: "POST",
72+
path: "/webhook",
73+
data: {
74+
hookUrl,
75+
form_id: formId,
76+
},
77+
...opts,
78+
});
79+
},
80+
async paginate(fn, ...opts) {
81+
let results = [];
82+
let response;
83+
let hasMore = true;
84+
let page = 1;
85+
86+
while (hasMore) {
87+
response = await fn({
88+
page,
89+
...opts,
90+
});
91+
if (Array.isArray(response) && response.length > 0) {
92+
results = results.concat(response);
93+
page += 1;
94+
} else {
95+
hasMore = false;
96+
}
97+
}
98+
99+
return results;
100+
},
10101
},
11102
};
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { axios } from "@pipedream/platform";
2+
import opnform from "../../opnform.app.mjs";
3+
4+
export default {
5+
key: "opnform-new-submission-instant",
6+
name: "New Submission Instant",
7+
description: "Emit a new event when a form receives a submission. [See the documentation]()",
8+
version: "0.0.{{ts}}",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
opnform: {
13+
type: "app",
14+
app: "opnform",
15+
},
16+
workspaceId: {
17+
propDefinition: [
18+
"opnform",
19+
"workspaceId",
20+
],
21+
},
22+
formId: {
23+
propDefinition: [
24+
"opnform",
25+
"formId",
26+
],
27+
},
28+
http: {
29+
type: "$.interface.http",
30+
customResponse: true,
31+
},
32+
db: "$.service.db",
33+
},
34+
hooks: {
35+
async activate() {
36+
const hookUrl = this.http.endpoint;
37+
const webhook = await this.opnform.createWebhook({
38+
hookUrl,
39+
formId: this.formId,
40+
});
41+
await this.db.set("webhookId", webhook.id);
42+
},
43+
async deactivate() {
44+
const webhookId = await this.db.get("webhookId");
45+
if (webhookId) {
46+
await this.opnform._makeRequest({
47+
method: "DELETE",
48+
path: `/webhook/${webhookId}`,
49+
});
50+
await this.db.delete("webhookId");
51+
}
52+
},
53+
async deploy() {
54+
const submissions = await this.opnform.paginate(
55+
this.opnform._makeRequest.bind(this.opnform),
56+
{
57+
method: "GET",
58+
path: `/forms/${this.formId}/submissions`,
59+
params: {
60+
limit: 50,
61+
},
62+
},
63+
);
64+
65+
submissions.reverse().forEach((submission) => {
66+
this.$emit(
67+
submission,
68+
{
69+
id: submission.id || Date.now().toString(),
70+
summary: `New submission for form ${submission.formName}`,
71+
ts: submission.createdAt
72+
? Date.parse(submission.createdAt)
73+
: Date.now(),
74+
},
75+
);
76+
});
77+
},
78+
},
79+
async run(event) {
80+
const submission = event.body;
81+
const id = submission.id || Date.now().toString();
82+
const summary = `New submission for form ${submission.formName}`;
83+
const ts = submission.createdAt
84+
? Date.parse(submission.createdAt)
85+
: Date.now();
86+
this.$emit(submission, {
87+
id,
88+
summary,
89+
ts,
90+
});
91+
},
92+
};

0 commit comments

Comments
 (0)