1

I discovered that by default the API responds with 30 repos.

May I know how should I use for loop to retrieve all repos?

const axios = require('axios');
const repoUrl = `https://api.github.com/users/USERNAME/repos`;

access_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
// console.log
const config = {
    headers: {Authorization: `Bearer ${access_token}`}     # only 30   
};




axios.get(repoUrl, null, config).then((responses) => {

    const repos = responses.data.map(({name, language, html_url, created_at, description}) => {
        return {name, language, html_url, created_at, description};
    })

    console.log("number of repo  ", repos.length);


}).catch(error => {
    console.log(`getrepos error: ${error}`)
});

by https://docs.github.com/en/rest/reference/repos#list-repositories-for-the-authenticated-user--parameters , there is a page parameter

but how should I write the for loop? should I loop the page with 1,2,3,4.... until server give me some error?

1 Answer 1

1

You can make a GraphQL query with Axios, as in this example, or in this article:

axios({
  url: 'https://graphql.com/graphql',
  method: 'post',
  data: {
    query: `
     query {
       viewer {
         repositories(isFork: false) {
           totalCount
         }
       }
    }`
  }
}).then((result) => {
  console.log(result.data)
});

Once you have the total number of repositories, you start looping (only the exact amount of loop), as described here:

  //Start fetching every page of repos.
  const fetchPromises = [], pageCount = Math.ceil(repoCount / 
    MAX_PER_PAGE);
  for (let pageI = 1; pageI <= pageCount; ++pageI) {
    const fetchPagePromise = fetch(baseUrl + '&page=' + pageI);
    fetchPromises.push(fetchPagePromise);
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.