-
Notifications
You must be signed in to change notification settings - Fork 0
/
contributions.ts
66 lines (56 loc) · 1.66 KB
/
contributions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
interface ContributionDay {
color: string;
contributionCount: number;
contributionLevel: string;
date: string;
}
interface Week {
contributionDays: ContributionDay[];
}
interface ContributionCalendar {
totalContributions: number;
colors: string[];
weeks: Week[];
}
const API_URL = Deno.env.get("CI") ? "http://localhost:8000" : "https://api.github.com/graphql";
const getContributions = async (useCache: boolean, user: string, from?: string, to?: string) => {
const cache = await caches.open("gh-api");
const cacheKey = `${API_URL}/${user}/${from}/${to}`;
const cached = await cache.match(cacheKey);
if (useCache && cached) return await cached.json();
const token = Deno.env.get("GH_READ_USER_TOKEN");
const query = `
query($user:String! $from:DateTime $to:DateTime) {
user(login: $user){
contributionsCollection(from: $from, to: $to) {
contributionCalendar {
totalContributions
colors
isHalloween
weeks {
contributionDays {
color
contributionCount
contributionLevel
date
}
}
}
}
}
}
`;
const variables = { user, from, to };
const json = { query, variables };
const res = await fetch(API_URL, {
method: "POST",
headers: { Authorization: `Bearer ${token}`, "Content-type": "application/json" },
body: JSON.stringify(json),
});
if (useCache && res.ok && from && to) {
await cache.put(cacheKey, res.clone());
}
return await res.json();
};
export { getContributions };
export type { ContributionCalendar, ContributionDay, Week };