Skip to content

Commit 532634e

Browse files
committed
Create route.ts
1 parent 0966cd7 commit 532634e

File tree

1 file changed

+42
-0
lines changed
  • apps/www/src/app/api/v2/extern/download_count/[slug]

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Firebase } from '@/app/api/Firebase';
2+
3+
const WHITELISTED_ORIGINS = ['https://spacetheme.net'];
4+
5+
function getAllowedOrigin(request: Request): string | undefined {
6+
const origin = request.headers.get('Origin');
7+
if (origin && WHITELISTED_ORIGINS.includes(origin)) {
8+
return origin;
9+
}
10+
return undefined;
11+
}
12+
13+
export async function GET(request: Request, { params }: { params: Promise<{ slug: string }> }) {
14+
const { slug } = await params;
15+
const allowedOrigin = getAllowedOrigin(request);
16+
17+
const headers: HeadersInit = {
18+
'Cache-Control': 'public, max-age=86400',
19+
};
20+
if (allowedOrigin) {
21+
headers['Access-Control-Allow-Origin'] = allowedOrigin;
22+
}
23+
24+
if (!slug) {
25+
return new Response("Missing 'id' parameter", { status: 400, headers });
26+
}
27+
28+
const cleanId = slug.trim();
29+
30+
try {
31+
const snap = await Firebase.FromID(cleanId);
32+
33+
if (!snap.exists) {
34+
throw new Error(`Repository with ID '${cleanId}' not found`);
35+
}
36+
37+
const mData = snap.data();
38+
return Response.json({ download_count: mData?.download || 0 }, { headers });
39+
} catch (error) {
40+
return new Response(`Error: ${error.message}`, { status: 404, headers });
41+
}
42+
}

0 commit comments

Comments
 (0)