generated from djyde/nextjs-saas-starter
-
Notifications
You must be signed in to change notification settings - Fork 14
/
core.ts
73 lines (71 loc) · 1.75 KB
/
core.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
67
68
69
70
71
72
73
import axios from "axios";
import dayjs from "dayjs";
import { nanoid } from "nanoid";
import { Cache } from "./cache";
import { apiClient } from "./utils.client";
import { HTTPException } from "./utils.server";
var utc = require("dayjs/plugin/utc");
dayjs.extend(utc);
export async function getYearCalendar(params: {
login: string;
fromYear: string;
token: string
}) {
const query = `query GetCalendar($login: String!, $from: DateTime) {
user(login: $login) {
contributionsCollection(from: $from) {
contributionCalendar {
totalContributions,
weeks {
contributionDays {
color,
date
}
}
}
}
}
}`;
const result = await axios.post(
"https://api.github.com/graphql",
{
query,
variables: {
login: params.login,
// @ts-expect-error
from: dayjs.utc(params.fromYear),
},
},
{
headers: {
Authorization: `bearer ${params.token}`,
},
}
);
if (result.data.errors?.length) {
throw HTTPException.notFound(result.data.errors[0].message);
}
return result.data.data.user.contributionsCollection.contributionCalendar;
}
export async function getFiveYearsCalendar(params: { token: string, login: string }, options?: {
offset?: string
}) {
const offset = options.offset ? Number(options.offset) : 0;
const cal = await Promise.all(
[0, 0, 0, 0, 0, 0].map(async (_, index) => {
const year = dayjs()
.subtract(index + 1 + offset, "year")
.get("year")
.toString();
return {
id: nanoid(12),
...(await getYearCalendar({
login: params.login,
fromYear: year,
token: params.token
})),
};
})
);
return cal;
}