Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/api/stats/route.empty-fallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type { ContributionCalendar } from '../../../types';

vi.mock('../../../lib/github', () => ({
fetchGitHubContributions: vi.fn(),
contributionsCache: { has: vi.fn().mockResolvedValue(false) },
cacheKey: vi.fn().mockReturnValue('key'),
}));

import { fetchGitHubContributions } from '../../../lib/github';
Expand Down
2 changes: 2 additions & 0 deletions app/api/stats/route.mouse-interactivity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type { ContributionCalendar, ExtendedContributionData } from '@/types';

vi.mock('@/lib/github', () => ({
fetchGitHubContributions: vi.fn(),
contributionsCache: { has: vi.fn().mockResolvedValue(false) },
cacheKey: vi.fn().mockReturnValue('key'),
}));

const mockCalendar: ContributionCalendar = {
Expand Down
2 changes: 2 additions & 0 deletions app/api/stats/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { GET } from './route';

vi.mock('../../../lib/github', () => ({
fetchGitHubContributions: vi.fn(),
contributionsCache: { has: vi.fn().mockResolvedValue(false) },
cacheKey: vi.fn().mockReturnValue('key'),
}));

import { fetchGitHubContributions } from '../../../lib/github';
Expand Down
7 changes: 5 additions & 2 deletions app/api/stats/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// app/api/stats/route.ts
import { NextResponse } from 'next/server';
import { fetchGitHubContributions } from '@/lib/github';
import { fetchGitHubContributions, contributionsCache, cacheKey } from '@/lib/github';
import { calculateStreak } from '@/lib/calculate';
import { statsParamsSchema } from '@/lib/validations';
import { getClientIp } from '@/utils/getClientIp';
Expand Down Expand Up @@ -122,6 +122,9 @@ export async function GET(request: Request) {
}

try {
const key = cacheKey('contributions', user);
const wasCachedBefore = await contributionsCache.has(key);

// Authenticated -> user's OAuth token (their quota); anonymous -> undefined (global PAT).
const userToken = await getUserGitHubToken();
const userData = await fetchGitHubContributions(user, {
Expand All @@ -144,7 +147,7 @@ export async function GET(request: Request) {
headers.set('Pragma', 'no-cache');
headers.set('Expires', '0');
}
headers.set('X-Cache-Status', shouldBypassCache ? 'MISS' : 'HIT');
headers.set('X-Cache-Status', shouldBypassCache ? 'MISS' : wasCachedBefore ? 'HIT' : 'MISS');
headers.set(
'X-Refresh-Status',
shouldBypassCache ? 'Fresh' : isRefreshRequested ? 'Cooldown-Served-Cached' : 'Cached'
Expand Down
6 changes: 6 additions & 0 deletions app/api/stats/route.validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { GET } from './route';

vi.mock('@/lib/github', () => ({
fetchGitHubContributions: vi.fn(),
contributionsCache: { has: vi.fn().mockResolvedValue(true) },
cacheKey: vi.fn().mockReturnValue('key'),
}));

vi.mock('@/lib/githubtoken', () => ({
getUserGitHubToken: vi.fn().mockResolvedValue(undefined),
}));

import { fetchGitHubContributions } from '@/lib/github';
Expand Down
Loading