Skip to content

Commit

Permalink
fix ratelimit issue (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhairyathedev authored Jun 7, 2024
1 parent f265011 commit f37705f
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ NEXT_PUBLIC_SUPABASE_ANON_KEY=<your_supabase_anon_key>
NEXT_PUBLIC_URL=https://git-re.vercel.app/
NEXT_PUBLIC_UPSTASH_REDIS_URL=
NEXT_PUBLIC_UPSTASH_REDIS_TOKEN=

# Generate the token from https://github.com/settings/tokens?type=beta
# Repository Access to public repositories (read-only)
NEXT_PUBLIC_GITHUB_TOKEN=
4 changes: 4 additions & 0 deletions app/api/users/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export async function POST(request: NextRequest) {
const response = await axios.get(
`https://api.github.com/users/${git_username}`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
params: {
fields: "login,bio,avatar_url,name",
},
Expand Down
8 changes: 7 additions & 1 deletion app/resume/[username]/view/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ const ViewResume = () => {
const fetchData = async () => {
try {
const profileResponse = await fetch(
`https://api.github.com/users/${username}`
`https://api.github.com/users/${username}`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
}
);
if (!profileResponse.ok) {
if (profileResponse.status === 400) {
Expand Down
8 changes: 7 additions & 1 deletion components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ export default function Form() {

try {
const githubResponse = await axios.get(
`https://api.github.com/users/${username}`
`https://api.github.com/users/${username}`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
}
);
if (githubResponse.status === 404) {
setUsernameFound(false);
Expand Down
8 changes: 7 additions & 1 deletion components/Resume.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ const Resume = () => {
} else {
// Fetch profile data and cache it
const profileData = await fetch(
`https://api.github.com/users/${username}`
`https://api.github.com/users/${username}`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
}
).then((res) => res.json());
setProfile(profileData);
await redis.set(`profile:${username}`, JSON.stringify(profileData), {
Expand Down
8 changes: 7 additions & 1 deletion utils/GithubAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ export const fetchUserContributionLanguages = async (
): Promise<Language[]> => {
try {
const repoResponse = await axios.get(
`https://api.github.com/search/repositories?q=user:${username}+fork:true`
`https://api.github.com/search/repositories?q=user:${username}+fork:true`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
}
);
const repos = repoResponse.data.items;

Expand Down
85 changes: 73 additions & 12 deletions utils/resumeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const sortLanguages = (
.slice(0, configData.maxLanguages);
};

const getFromCache = async <T,>(key: string): Promise<T | null> => {
const getFromCache = async <T>(key: string): Promise<T | null> => {
const cachedData: any = await redis.get(key);
if (cachedData) {
console.log("LOADED FROM CACHE");
Expand All @@ -89,7 +89,7 @@ const getFromCache = async <T,>(key: string): Promise<T | null> => {

const supabase = supabaseBrowser();

const setInCache = async <T,>(
const setInCache = async <T>(
key: string,
data: T,
expirationSeconds: number
Expand All @@ -113,7 +113,13 @@ export const fetchOrganizations = async (

try {
const response = await axios.get(
`https://api.github.com/users/${username}/orgs`
`https://api.github.com/users/${username}/orgs`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
}
);
const organizations = response.data.map((org: any) => ({
name: org.login,
Expand Down Expand Up @@ -142,7 +148,12 @@ export const fetchContributions = async (

try {
const url = `https://api.github.com/search/issues?q=author:${username}+type:pr+is:merged&per_page=100`;
const response = await axios.get(url);
const response = await axios.get(url, {
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
});

const contributionMap = new Map<
string,
Expand Down Expand Up @@ -206,7 +217,13 @@ export const fetchPopularRepos = async (username: string): Promise<Repo[]> => {
}
try {
const response = await axios.get(
`https://api.github.com/users/${username}/repos?per_page=100`
`https://api.github.com/users/${username}/repos?per_page=100`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
}
);
const repos = response.data;

Expand Down Expand Up @@ -255,7 +272,13 @@ export const fetchLanguageData = async (

try {
const response = await axios.get(
`https://api.github.com/users/${username}/repos`
`https://api.github.com/users/${username}/repos`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
}
);
const repos = response.data;

Expand Down Expand Up @@ -299,15 +322,41 @@ export const fetchUserStats = async (username: string) => {
}

try {
const userRes = await axios.get(`https://api.github.com/users/${username}`);
const userRes = await axios.get(
`https://api.github.com/users/${username}`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
}
);
const orgsRes = await axios.get(
`https://api.github.com/users/${username}/orgs`
`https://api.github.com/users/${username}/orgs`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
}
);
const eventsRes = await axios.get(
`https://api.github.com/users/${username}/events/public`
`https://api.github.com/users/${username}/events/public`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
}
);
const reposRes = await axios.get(
`https://api.github.com/users/${username}/repos?per_page=100`
`https://api.github.com/users/${username}/repos?per_page=100`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
}
);
const starsReceived = reposRes.data.reduce(
(acc: any, repo: { stargazers_count: any }) =>
Expand All @@ -331,12 +380,24 @@ export const fetchUserStats = async (username: string) => {
0
);
const issuesRes = await axios.get(
`https://api.github.com/search/issues?q=author:${username}+type:issue`
`https://api.github.com/search/issues?q=author:${username}+type:issue`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
}
);
const totalIssuesCreated = issuesRes.data.total_count;

const prsRes = await axios.get(
`https://api.github.com/search/issues?q=author:${username}+type:pr+is:merged`
`https://api.github.com/search/issues?q=author:${username}+type:pr+is:merged`,
{
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
Accept: "application/vnd.github+json",
},
}
);
const totalPRsMerged = prsRes.data.total_count;

Expand Down

0 comments on commit f37705f

Please sign in to comment.