I want to fetch the data from all the projects hosted by my GitLab instance. Now I know that GitLab displays 20 projects by default, so I need to set a bigger number of instances displayed per page:
https://gitlab-repo.com/api/v4/projects?per_page=100
It therefore leads to the corresponding axios code:
const url='https://gitlab-repo.com/api/v4/projects?per_page=100'
const vm = new Vue({
el: "#app",
data: {
results: [],
loading: true,
errored: false
},
mounted() {
axios
.get(url)
.then(response => (this.results = response.data))
.catch(error => {
console.log(error)
this.errored = true
})
.finally(() => this.loading = false)
}
})
The thing is, the maximum number of projects that GitLab can display per page is 100, and I have more projects than that.
I thought about putting a condition on the number of instances (projects) in the params
section of axios's get function but can't figure how to write that.
How can I indicate in my axios request that if the number of projects is bigger than 100, I want to load the data that's on the next page(s)?
You're first gonna want to write a function that requests a specific page, something like this:
function getPage(page) {
let url = `https://gitlab-repo.com/api/v4/projects?per_page=100&page=${page}`;
return axios
.get(url)
.then(response => (response.data))
}
If you have async/await
in your environment, I would then do something like this, to keep requesting the next page until you get a result that's < 100 results
let resultCount = 100;
let results = [];
let page = 1;
while (resultCount === 100) {
let newResults = await getPage(page);
page++;
resultCount = newResults.length;
results = results.concat(newResults);
}