GithubUtils.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import CacheUtils from "./CacheUtils.js"
  2. export default class GithubUtils {
  3. static async getTop100Contributors(repo,modifier){
  4. const cacheKey="ghrepo/"+repo+"/contribs";
  5. let contributors=CacheUtils.get(cacheKey);
  6. if(!contributors){
  7. const endPoint=this.githubContribEndpoint(repo);
  8. contributors= await fetch(endPoint, {
  9. method: 'GET',
  10. headers: {
  11. 'Content-Type': 'application/json',
  12. 'Accept': 'application/json',
  13. }
  14. }).then((res) =>{
  15. if(res.ok&&res.status==200)return res.json() ;
  16. else throw "Failed to fetch!";
  17. });
  18. contributors=contributors.filter(u=>u.type=="User");
  19. if(!contributors)throw "Undefined contributors?";
  20. if(modifier)contributors=modifier(contributors);
  21. CacheUtils.set(cacheKey,contributors);
  22. }
  23. return contributors;
  24. }
  25. static async resolveUser(userId){
  26. const cacheKey="ghuser/"+userId;
  27. let userData=CacheUtils.get(cacheKey);
  28. if(!userData){
  29. const endPoint=this.githubUserEndPoint(userId);
  30. userData= await fetch(endPoint, {
  31. method: 'GET',
  32. headers: {
  33. 'Content-Type': 'application/json',
  34. 'Accept': 'application/json',
  35. }
  36. }).then((res) =>{
  37. if(res.ok&&res.status==200)return res.json() ;
  38. else throw "Failed to fetch!";
  39. });
  40. if(!userData)throw "Undefined userData?";
  41. CacheUtils.set(cacheKey,userData);
  42. }
  43. return userData;
  44. }
  45. }
  46. GithubUtils.githubContribEndpoint = (repo)=> `https://api.github.com/repos/${repo}/contributors?per_page=100`;
  47. GithubUtils.githubUserEndPoint = (user)=> `https://api.github.com/users/${user}`;