File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
apps/www/src/app/api/v2/extern/download_count/[slug] Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments