Skip to content

Commit 6437ac6

Browse files
committed
moaform init
1 parent 41b3962 commit 6437ac6

File tree

3 files changed

+166
-4
lines changed

3 files changed

+166
-4
lines changed

components/moaform/moaform.app.mjs

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,82 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "moaform",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
formId: {
8+
type: "string",
9+
label: "Form ID",
10+
description: "The ID of the form to monitor for new submissions",
11+
async options() {
12+
const forms = await this.getForms();
13+
return forms.map((form) => ({
14+
label: form.name,
15+
value: form.id,
16+
}));
17+
},
18+
},
19+
fields: {
20+
type: "string[]",
21+
label: "Fields to Capture",
22+
description: "Optional fields to capture from the submission",
23+
optional: true,
24+
},
25+
},
526
methods: {
6-
// this.$auth contains connected account data
727
authKeys() {
828
console.log(Object.keys(this.$auth));
929
},
30+
_baseUrl() {
31+
return "https://api.moaform.com";
32+
},
33+
async _makeRequest(opts = {}) {
34+
const {
35+
$ = this, method = "GET", path = "/", headers, ...otherOpts
36+
} = opts;
37+
return axios($, {
38+
...otherOpts,
39+
method,
40+
url: this._baseUrl() + path,
41+
headers: {
42+
...headers,
43+
Authorization: `Bearer ${this.$auth.api_token}`,
44+
},
45+
});
46+
},
47+
async getForms(opts = {}) {
48+
return this._makeRequest({
49+
...opts,
50+
path: "/forms",
51+
});
52+
},
53+
async createWebhook(opts = {}) {
54+
const {
55+
formId, url, fields,
56+
} = opts;
57+
return this._makeRequest({
58+
method: "POST",
59+
path: `/forms/${formId}/webhooks`,
60+
data: {
61+
url,
62+
fields,
63+
},
64+
});
65+
},
66+
async deleteWebhook(opts = {}) {
67+
const {
68+
formId, webhookId,
69+
} = opts;
70+
return this._makeRequest({
71+
method: "DELETE",
72+
path: `/forms/${formId}/webhooks/${webhookId}`,
73+
});
74+
},
75+
async getWebhooks(opts = {}) {
76+
const { formId } = opts;
77+
return this._makeRequest({
78+
path: `/forms/${formId}/webhooks`,
79+
});
80+
},
1081
},
11-
};
82+
};

components/moaform/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
"publishConfig": {
1313
"access": "public"
1414
}
15-
}
15+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { axios } from "@pipedream/platform";
2+
import moaform from "../../moaform.app.mjs";
3+
4+
export default {
5+
key: "moaform-new-submission-instant",
6+
name: "New Submission Instant",
7+
description: "Emit new event every time a new form submission is received. [See the documentation](https://help.moaform.com)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
moaform,
13+
db: "$.service.db",
14+
formId: {
15+
propDefinition: [
16+
moaform,
17+
"formId",
18+
],
19+
},
20+
fields: {
21+
propDefinition: [
22+
moaform,
23+
"fields",
24+
],
25+
},
26+
httpSource: {
27+
type: "$.interface.http",
28+
label: "HTTP Source",
29+
description: "HTTP source to set up webhooks",
30+
},
31+
},
32+
hooks: {
33+
async deploy() {
34+
const webhooks = await this.moaform.getWebhooks({
35+
formId: this.formId,
36+
});
37+
38+
for (const webhook of webhooks) {
39+
this.$emit(webhook, {
40+
id: webhook.id,
41+
summary: `Existing webhook: ${webhook.id}`,
42+
ts: Date.now(),
43+
});
44+
}
45+
},
46+
async activate() {
47+
const {
48+
formId, fields,
49+
} = this;
50+
const url = this.httpSource.url;
51+
const webhook = await this.moaform.createWebhook({
52+
formId,
53+
url,
54+
fields,
55+
});
56+
57+
this.db.set("webhookId", webhook.id);
58+
},
59+
async deactivate() {
60+
const webhookId = this.db.get("webhookId");
61+
await this.moaform.deleteWebhook({
62+
formId: this.formId,
63+
webhookId,
64+
});
65+
},
66+
},
67+
async run(event) {
68+
const {
69+
headers, body,
70+
} = event;
71+
const id = headers["x-moaform-submission-id"];
72+
const formId = body.formId;
73+
const submissionFields = this.fields.length ?
74+
this.fields.reduce((acc, field) => ({
75+
...acc,
76+
[field]: body.fields[field],
77+
}), {})
78+
: body.fields;
79+
80+
const eventToEmit = {
81+
formId,
82+
...submissionFields,
83+
};
84+
85+
this.$emit(eventToEmit, {
86+
id,
87+
summary: `New submission received for form ${formId}`,
88+
ts: Date.now(),
89+
});
90+
},
91+
};

0 commit comments

Comments
 (0)