Github用户查找API,如何返回多个搜索结果?JS

3

你好,我正在使用 Github API 创建 GitHub 用户查找器。我的问题是如何操作 API 链接以获取包含搜索栏中 e.target.value 的用户,而不仅仅是完全匹配的那个。

以下是我的代码

const [finalData, setFinalData] = useState([]);

const handleSearch = async (e) => {
try {
  const URL = `https://api.github.com/users/${e.target.value}? 
  client_id=e25d1dbedde5215999ef&client_secret=ee080580b7c4f19688ccaef6844c3fe88bb811d`;

  Promise.all([fetch(URL).then((res) => res.json())]).then((data) => {
    if (data) {
      setData(data);
    }
  });
  } catch (err) {
  console.log(err);
  }
  };

 const setData = (data) => {
  data && setFinalData(data);
  };
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

1个回答

3
你可以使用搜索用户终端节点。有一个查询参数(q),允许您使用多个搜索条件,详见此处
以下是使用Octokit的示例,但如果您仍想使用fetch,则终端节点应为https://api.github.com/search/users注意:我真的希望您暴露的客户端密钥是用于测试应用程序的。
搜索 GitHub 用户 在 Fusebit 中查看
const userSearch = ''; // Specify the search text here
const usersResponse = await octokit.rest.search.users({
   q: userSearch,
   per_page:100
});

const { total_count, items } = usersResponse.data;
console.log(`Listing ${items.length} users of ${total_count} \n`, items.map(user => user.login));


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接