EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
route.ts
Go to the documentation of this file.
1import { NextRequest, NextResponse } from 'next/server';
2import { CWAGnapApi } from '@/lib/cwa-gnap-api';
3
4export async function GET(request: NextRequest) {
5 const { searchParams } = new URL(request.url);
6 const fields = searchParams.get('fields');
7
8 const api = new CWAGnapApi();
9
10 // Try to get API key from cookies or headers
11 const apiKey = request.cookies.get('FieldpineApiKey')?.value ||
12 request.headers.get('Authorization')?.replace('Bearer ', '');
13
14 if (apiKey) {
15 api.setApiKey(apiKey);
16 }
17
18 try {
19 const result = fields
20 ? await api.getStatsTodayWithFields(fields)
21 : await api.getStatsToday();
22
23 if (result.error) {
24 return NextResponse.json(
25 { error: result.error },
26 { status: 500 }
27 );
28 }
29
30 return NextResponse.json(result);
31 } catch (error) {
32 console.error('API Error:', error);
33 return NextResponse.json(
34 { error: 'Internal server error' },
35 { status: 500 }
36 );
37 }
38}