Skip to content

Commit

Permalink
Add a simple github api request
Browse files Browse the repository at this point in the history
  • Loading branch information
vn7n24fzkq committed Aug 16, 2020
1 parent 5cdd595 commit e3bf5c0
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"github"
],
"author": "vn7 <[email protected]>",
"license": "MIT"
"license": "MIT",
"dependencies": {
"axios": "^0.19.2"
}
}
87 changes: 87 additions & 0 deletions src/utils/github-api/langs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const request = require("../../utils/request");

const fetcher = (token, variables) => {
//contain private repo need token permission
return request(
{
Authorization: `bearer ${token}`,
},
{
query: `
query userInfo($login: String!,$endCursor: String) {
user(login: $login) {
repositories(isFork: false, first: 100, after: $endCursor,ownerAffiliations: OWNER) {
nodes {
languages(first: 10, orderBy: {field: SIZE, direction: DESC}) {
edges {
size
node {
color
name
}
}
}
}
pageInfo{
endCursor
hasNextPage
}
}
}
}
`,
variables,
}
);
};

//repos per language
async function getRepoLanguage(username,token) {
let hasNextPage = true;
let cursor = null;
let languageMap = new Map();
let nodes=[];


try {
while (hasNextPage) {
let res = await fetcher(token, {
login: username,
endCursor: cursor,
});

if (res.data.errors) {
throw Error(res.data.errors[0].message || "Could not fetch user");
}
cursor = res.data.data.user.repositories.pageInfo.endCursor;
hasNextPage = res.data.data.user.repositories.pageInfo.hasNextPage;
nodes.push(...res.data.data.user.repositories.nodes);
}

nodes.forEach(node=>{
node.languages.edges.forEach(edge=>{
let langName = edge.node.name;
console.log(languageMap.has(langName));
if(languageMap.has(langName)){
let lang = languageMap.get(langName);
lang.count+=1;
languageMap.set(langName, lang);
}else{
languageMap.set(langName, {count:1,color:edge.node.color});
}
});
});

} catch (e) {
console.log("error");
if(e.response){
console.log(e.response.data);
}else{
console.log(e);
}
}

return languageMap;
}

module.exports = getRepoLanguage;
12 changes: 12 additions & 0 deletions src/utils/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const axios = require("axios");

function request(header,data) {
return axios({
url: "https://api.github.com/graphql",
method: "post",
headers: header,
data: data,
});
}

module.exports = request;

0 comments on commit e3bf5c0

Please sign in to comment.