EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
route.ts
Go to the documentation of this file.
1/**
2 * FD1 Customers API Route
3 *
4 * GET /api/v1/fd1/customers - List customers using FD1 protocol
5 *
6 * Query parameters:
7 * - search: Search term for customer name/email/phone
8 * - limit: Number of results (default 100)
9 * - offset: Pagination offset (default 0)
10 */
11
12import { NextRequest, NextResponse } from 'next/server';
13import { getRequestContext } from '@/lib/server/sessionUtils';
14import { createFD1Client } from '@/lib/client/fd1Client';
15
16export async function GET(request: NextRequest) {
17 try {
18 const context = await getRequestContext(request);
19
20 if (!context?.session?.storeUrl) {
21 return NextResponse.json(
22 { success: false, error: 'Not authenticated' },
23 { status: 401 }
24 );
25 }
26
27 // Parse query parameters
28 const searchParams = request.nextUrl.searchParams;
29 const search = searchParams.get('search');
30 const limit = parseInt(searchParams.get('limit') || '100', 10);
31 const offset = parseInt(searchParams.get('offset') || '0', 10);
32
33 // Build query filters
34 const filters: Record<string, any> = {};
35 if (search) filters.search = search;
36
37 // Create FD1 client
38 const fd1Client = createFD1Client({
39 baseUrl: context.session.storeUrl!,
40 apiKey: context.session.apiKey,
41 protocol: 'fd3',
42 });
43
44 // Fetch customers
45 const customers = await fd1Client.listCustomers(
46 Object.keys(filters).length > 0 ? filters : undefined,
47 undefined,
48 limit,
49 offset
50 );
51
52 return NextResponse.json({
53 success: true,
54 data: customers,
55 count: customers.length,
56 limit,
57 offset,
58 });
59
60 } catch (error) {
61 console.error('FD1 customers API error:', error);
62 return NextResponse.json(
63 {
64 success: false,
65 error: error instanceof Error ? error.message : 'Failed to fetch customers',
66 },
67 { status: 500 }
68 );
69 }
70}