github-profile-trophy-reiyu.../src/github_api_client.ts
2021-05-08 16:18:21 +09:00

61 lines
1.7 KiB
TypeScript

import { soxa } from "../deps.ts";
import { UserInfo } from "./user_info.ts"
import type { GitHubUserData } from "./user_info.ts";
export class GithubAPIClient {
constructor() {
}
async requestUserInfo(username: string): Promise<UserInfo | null> {
const token = Deno.env.get("GITHUB_TOKEN");
const variables = { username: username };
const query = `
query userInfo($username: String!) {
user(login: $username) {
createdAt
contributionsCollection {
totalCommitContributions
restrictedContributionsCount
}
pullRequests(first: 1) {
totalCount
}
issues(first: 1) {
totalCount
}
organizations(first: 1) {
totalCount
}
followers(first: 1) {
totalCount
}
repositories(first: 100, ownerAffiliations: OWNER, isFork: false, orderBy: {direction: DESC, field: STARGAZERS}) {
totalCount
nodes {
languages(first: 3, orderBy: {direction:DESC, field: SIZE}) {
nodes {
name
}
}
stargazers {
totalCount
}
}
}
}
}
`;
const response = await soxa.post(
"https://api.github.com/graphql",
{},
{
data: { query: query, variables },
headers: { Authorization: `bearer ${token}` },
},
);
if (response.status != 200) {
throw new Error(response)
}
const userData: GitHubUserData = response.data.data.user;
return new UserInfo(userData);
}
}