通过 Github api v3 和 javascript 获取所有 github 回购协议

fetch all github repo by Github api v3 and javascript

我发现默认情况下 API 响应 30 个 repos。

我可以知道我应该如何使用 for 循环来检索所有 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 ,有一个页面参数

但是for循环应该怎么写呢?我应该用 1,2,3,4.... 循环页面直到服务器给我一些错误吗?

你可以做一个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)
});

一旦你有了存储库的总数,你就开始循环(只有循环的确切数量),如:

  //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);
  }