Skip to content

Commit fe1cacd

Browse files
committed
[Components] opnform #15639
Sources - New Submission (Instant)
1 parent 400b10f commit fe1cacd

File tree

4 files changed

+91
-117
lines changed

4 files changed

+91
-117
lines changed

components/opnform/opnform.app.mjs

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,30 @@ import { axios } from "@pipedream/platform";
33
export default {
44
type: "app",
55
app: "opnform",
6-
version: "0.0.{{ts}}",
76
propDefinitions: {
87
workspaceId: {
98
type: "string",
109
label: "Workspace ID",
1110
description: "The ID of the workspace containing the forms.",
11+
async options() {
12+
const worspaces = await this.listWorkspaces();
13+
return worspaces.map(({
14+
id: value, name: label,
15+
}) => ({
16+
label,
17+
value,
18+
}));
19+
},
1220
},
1321
formId: {
1422
type: "string",
1523
label: "Form",
1624
description: "Select the form to monitor for new submissions.",
17-
async options() {
18-
const { workspaceId } = this;
25+
async options({ workspaceId }) {
1926
const forms = await this.listForms({
20-
workspaceId,
27+
params: {
28+
workspace_id: workspaceId,
29+
},
2130
});
2231
return forms.map((form) => ({
2332
label: form.name,
@@ -27,76 +36,48 @@ export default {
2736
},
2837
},
2938
methods: {
30-
authKeys() {
31-
console.log(Object.keys(this.$auth));
32-
},
3339
_baseUrl() {
3440
return "https://api.opnform.com/external/zapier";
3541
},
36-
async _makeRequest(opts = {}) {
37-
const {
38-
$ = this, method = "GET", path = "/", headers, params, data, ...otherOpts
39-
} = opts;
42+
_headers() {
43+
return {
44+
Authorization: `Bearer ${this.$auth.api_key}`,
45+
};
46+
},
47+
_makeRequest({
48+
$ = this, path, ...opts
49+
}) {
4050
return axios($, {
41-
method,
4251
url: this._baseUrl() + path,
43-
headers: {
44-
...headers,
45-
Authorization: `Bearer ${this.$auth.api_key}`,
46-
},
47-
params,
48-
data,
49-
...otherOpts,
52+
headers: this._headers(),
53+
...opts,
5054
});
5155
},
52-
async listForms({
53-
workspaceId, ...opts
54-
} = {}) {
55-
if (!workspaceId) {
56-
throw new Error("workspaceId is required to list forms.");
57-
}
56+
listForms(opts = {}) {
5857
return this._makeRequest({
59-
method: "GET",
6058
path: "/forms",
61-
params: {
62-
workspace_id: workspaceId,
63-
},
6459
...opts,
6560
});
6661
},
67-
async createWebhook({
68-
hookUrl, formId, ...opts
69-
} = {}) {
62+
listWorkspaces(opts = {}) {
63+
return this._makeRequest({
64+
path: "/workspaces",
65+
...opts,
66+
});
67+
},
68+
createWebhook(opts = {}) {
7069
return this._makeRequest({
7170
method: "POST",
7271
path: "/webhook",
73-
data: {
74-
hookUrl,
75-
form_id: formId,
76-
},
7772
...opts,
7873
});
7974
},
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;
75+
deleteWebhook(opts = {}) {
76+
return this._makeRequest({
77+
method: "DELETE",
78+
path: "/webhook",
79+
...opts,
80+
});
10081
},
10182
},
10283
};

components/opnform/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/opnform",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream OpnForm Components",
55
"main": "opnform.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.3"
1417
}
1518
}
Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,58 @@
1-
import { axios } from "@pipedream/platform";
21
import opnform from "../../opnform.app.mjs";
2+
import sampleEmit from "./test-event.mjs";
33

44
export default {
55
key: "opnform-new-submission-instant",
66
name: "New Submission Instant",
7-
description: "Emit a new event when a form receives a submission. [See the documentation]()",
8-
version: "0.0.{{ts}}",
7+
description: "Emit new event when a form receives a submission.",
8+
version: "0.0.1",
99
type: "source",
1010
dedupe: "unique",
1111
props: {
12-
opnform: {
13-
type: "app",
14-
app: "opnform",
15-
},
12+
opnform,
13+
http: "$.interface.http",
14+
db: "$.service.db",
1615
workspaceId: {
1716
propDefinition: [
18-
"opnform",
17+
opnform,
1918
"workspaceId",
2019
],
2120
},
2221
formId: {
2322
propDefinition: [
24-
"opnform",
23+
opnform,
2524
"formId",
25+
({ workspaceId }) => ({
26+
workspaceId,
27+
}),
2628
],
2729
},
28-
http: {
29-
type: "$.interface.http",
30-
customResponse: true,
31-
},
32-
db: "$.service.db",
3330
},
3431
hooks: {
3532
async activate() {
36-
const hookUrl = this.http.endpoint;
37-
const webhook = await this.opnform.createWebhook({
38-
hookUrl,
39-
formId: this.formId,
33+
await this.opnform.createWebhook({
34+
data: {
35+
hookUrl: this.http.endpoint,
36+
form_id: this.formId,
37+
},
4038
});
41-
await this.db.set("webhookId", webhook.id);
4239
},
4340
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-
},
41+
await this.opnform.deleteWebhook({
42+
data: {
43+
hookUrl: this.http.endpoint,
44+
form_id: this.formId,
6245
},
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-
);
7646
});
7747
},
7848
},
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,
49+
async run({ body }) {
50+
const ts = Date.now();
51+
this.$emit(body, {
52+
id: `${body.form_slug}-${ts}`,
53+
summary: `New submission for "${body.form_title}"`,
8954
ts,
9055
});
9156
},
57+
sampleEmit,
9258
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default {
2+
"form_title": "My Form",
3+
"form_slug": "my-form-dtewbq",
4+
"submission": {
5+
"Name": "User Name",
6+
"Email": "[email protected]",
7+
"Message": "User Message"
8+
},
9+
"data": {
10+
"3cc8fbba-1234-1234-979d-0e81d8f6845c": {
11+
"value": "USer Name",
12+
"name": "Name"
13+
},
14+
"9a1778ff-1234-1234-a62c-197dae1ed3c0": {
15+
"value": "[email protected]",
16+
"name": "Email"
17+
},
18+
"ddd28630-1234-1234-bbe5-4f58c485d470": {
19+
"value": "USer Message",
20+
"name": "Message"
21+
}
22+
},
23+
"message": "Please do not use the `submission` field. It is deprecated and will be removed in the future."
24+
}

0 commit comments

Comments
 (0)