forked from PipedreamHQ/pipedream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformstack.app.js
82 lines (81 loc) · 2.08 KB
/
formstack.app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const axios = require("axios");
module.exports = {
type: "app",
app: "formstack",
propDefinitions: {
formId: {
type: "string",
label: "Forms",
optional: true,
async options({ page, prevContext }) {
const options = [];
const per_page = 100;
let results = [];
page = prevContext.page || 1;
let total = prevContext.total >= 0 ? prevContext.total : per_page;
if (total === per_page) {
results = await this.getForms(page, per_page);
for (const form of results) {
options.push({ label: form.name, value: form.id });
}
}
total = results.length;
page++;
return {
options,
context: { page, total },
};
},
},
},
methods: {
monthAgo() {
const monthAgo = new Date();
monthAgo.setMonth(monthAgo.getMonth() - 1);
return monthAgo;
},
_getBaseURL() {
return "https://www.formstack.com/api/v2";
},
_getAuthHeaders() {
return {
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
"Content-Type": "application/json",
};
},
async createHook({ id, url }) {
const config = {
method: "POST",
url: `${this._getBaseURL()}/form/${id}/webhook.json`,
headers: this._getAuthHeaders(),
params: {
url,
content_type: "json",
handshake_key: this.$auth.oauth_refresh_token,
},
};
return (await axios(config)).data;
},
async deleteHook({ hookId }) {
const config = {
method: "DELETE",
url: `${this._getBaseURL()}/webhook/${hookId}.json`,
headers: this._getAuthHeaders(),
};
return await axios(config);
},
async getForms(page, per_page) {
const config = {
method: "GET",
url: `${this._getBaseURL()}/form.json`,
headers: this._getAuthHeaders(),
params: {
page,
per_page,
folders: false,
},
};
return (await axios(config)).data.forms;
},
},
};