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 { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
4
5/**
6 * OpenAPI Suppliers Endpoint
7 * Uses Fieldpine's OpenAPI2_RmSystem endpoints
8 * Documentation: https://docs.fieldpine.com/openapi/
9 */
10export async function GET(request: NextRequest) {
11 try {
12 // Verify authentication
13 const authData = await getStoredAuth();
14 if (!authData || !authData.authenticated) {
15 return NextResponse.json(
16 { error: 'Authentication required' },
17 { status: 401 }
18 );
19 }
20
21 // Rate limiting
22 const clientId = request.headers.get('x-forwarded-for') ||
23 request.headers.get('x-real-ip') ||
24 authData.userId ||
25 'unknown';
26 if (!fieldpineServerApi.checkClientRateLimit(clientId)) {
27 return NextResponse.json(
28 { error: 'Rate limit exceeded' },
29 { status: 429 }
30 );
31 }
32
33 // Parse query parameters
34 const { searchParams } = new URL(request.url);
35 const params: Record<string, string | number> = {};
36
37 const search = searchParams.get('search');
38 if (search) params.search = search;
39
40 const supplierId = searchParams.get('supplierId');
41 if (supplierId) params.supplierId = supplierId;
42
43 const limit = searchParams.get('limit');
44 if (limit) params.limit = parseInt(limit);
45
46 // Call Fieldpine OpenAPI directly (no BUCK fallback)
47 try {
48 const suppliers = await fieldpineServerApi.apiCall("/Suppliers", {
49 params,
50 cookie: authData.apiKey,
51 useOpenApi: true
52 });
53
54 return NextResponse.json({
55 success: true,
56 data: suppliers,
57 source: 'openapi'
58 });
59
60 } catch (error) {
61 console.error('OpenAPI suppliers error:', error);
62 return NextResponse.json(
63 { error: 'OpenAPI endpoint unavailable', source: 'openapi' },
64 { status: 503 }
65 );
66 }
67
68 } catch (error) {
69 console.error('OpenAPI suppliers error:', error);
70 return NextResponse.json(
71 { error: 'Failed to fetch suppliers' },
72 { status: 500 }
73 );
74 }
75}