forked from triggerdotdev/trigger.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponses.ts
More file actions
80 lines (70 loc) · 1.93 KB
/
Copy pathresponses.ts
File metadata and controls
80 lines (70 loc) · 1.93 KB
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
import { IntegrationTaskKey } from "@trigger.dev/sdk";
import {
GetAllResponsesParams,
GetAllResponsesResponse,
ListResponsesParams,
ListResponsesResponse,
TypeformRunTask,
} from ".";
export class Responses {
runTask: TypeformRunTask;
constructor(runTask: TypeformRunTask) {
this.runTask = runTask;
}
list(key: IntegrationTaskKey, params: ListResponsesParams): Promise<ListResponsesResponse> {
return this.runTask(
key,
async (client, task) => {
return client.responses.list(params);
},
{
name: "List Responses",
params,
properties: [
{
label: "Form ID",
text: params.uid,
},
],
}
);
}
all(key: IntegrationTaskKey, params: GetAllResponsesParams): Promise<GetAllResponsesResponse> {
const pageSize = 50;
const listResponsesForPage = (before?: string) => {
const pageParams = {
...params,
submitted_at: "desc",
before,
pageSize: pageSize,
};
return this.list(`page${before ? `-before-${before}` : ""}`, pageParams);
};
return this.runTask(
key,
async (client, task) => {
// We're going to create a subtask for each page of responses
const firstPage = await listResponsesForPage();
let token = firstPage.items[firstPage.items.length - 1].token;
const totalPages = Math.ceil(firstPage.total_items / pageSize);
const allResponses = firstPage.items;
for (let i = 1; i < totalPages; i++) {
const page = await listResponsesForPage(token);
token = page.items[page.items.length - 1].token;
allResponses.push(...page.items);
}
return allResponses;
},
{
name: "Get All Responses",
params,
properties: [
{
label: "Form ID",
text: params.uid,
},
],
}
);
}
}