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 * eLink/BUCK Suppliers Endpoint
7 * Uses Fieldpine's BUCK API via eLink protocol
8 * Documentation: https://docs.fieldpine.com/pos/elink.htm
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 BUCK API via eLink protocol
47 try {
48 const suppliers = await fieldpineServerApi.getSuppliers(params, authData.apiKey);
49
50 return NextResponse.json({
51 success: true,
52 data: suppliers,
53 source: 'elink'
54 });
55
56 } catch (error) {
57 console.error('eLink suppliers error:', error);
58 return NextResponse.json(
59 { error: 'eLink endpoint unavailable', source: 'elink' },
60 { status: 503 }
61 );
62 }
63
64 } catch (error) {
65 console.error('eLink suppliers error:', error);
66 return NextResponse.json(
67 { error: 'Failed to fetch suppliers' },
68 { status: 500 }
69 );
70 }
71}