Skip to content
Open
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
14 changes: 14 additions & 0 deletions app/api/compare/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { getFullDashboardData } from '@/lib/github';
import { getUserGitHubToken } from '@/lib/githubtoken';
import { compareParamsSchema } from '@/lib/validations';
import crypto from 'crypto';
import { getClientIp } from '@/utils/getClientIp';
import { RateLimiter } from '@/lib/rate-limit';

const compareLimiter = new RateLimiter(5, 60_000, 1);
export const revalidate = 3600;

function buildCompareFetchErrorResponse(user: string, reason: unknown): NextResponse {
Expand Down Expand Up @@ -51,6 +54,17 @@ function buildCompareFetchErrorResponse(user: string, reason: unknown): NextResp
}

export async function GET(request: Request) {
const ip = getClientIp(request);
const rateLimitKey =
ip && ip !== 'unknown' ? ip : `unknown:${request.headers.get('user-agent') ?? 'no-agent'}`;

if (!(await compareLimiter.check(rateLimitKey))) {
return NextResponse.json(
{ error: 'Too many requests. Please try again later.' },
{ status: 429 }
);
}

const { searchParams } = new URL(request.url);

const parseResult = compareParamsSchema.safeParse(Object.fromEntries(searchParams.entries()));
Expand Down
Loading