-
Notifications
You must be signed in to change notification settings - Fork 0
/
with-task-pagination.js
46 lines (38 loc) · 1.32 KB
/
with-task-pagination.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
import dotenv from "dotenv";
import axios from "axios";
dotenv.config();
// store your secrets in env vars
const apiKey = process.env.API_KEY;
// create a client to avoid redundant code
const client = axios.create({
baseURL: "https://api.awork.com/api/v1",
headers: {
Authorization: `Bearer ${apiKey}`,
},
});
// example of getting paginated tasks -> we would get the page 1 with a limit of 5 items per page
async function getTasksForProject(projectId, pageLimit) {
let page = 1;
let hasNext = true;
let tasks = [];
while (hasNext) {
const response = await client.get(
`/projects/${projectId}/projecttasks?page=${page}&pageSize=${pageLimit}`
);
tasks = [...tasks, ...response.data];
// we can get pagination information from the headers
const currentPage = response.headers["aw-page"];
const totalItems = response.headers["aw-totalitems"];
// the number of pages is the number of total items divided by the pageSize provided in the querystring plus 1
hasNext = currentPage < totalItems / pageLimit + 1;
page++;
}
return tasks;
}
(async function () {
const projectId = "your-project-id";
// we're limiting the results by 5 items per page
const pageLimit = 5;
const resultTasks = await getTasksForProject(projectId, pageLimit);
console.log(resultTasks.length);
})();