2 * FD1 Products API Route
4 * GET /api/v1/fd1/products - List products using FD1 protocol
7 * - search: Search term for product name/sku
8 * - department: Filter by department
10 * - limit: Number of results (default 100)
11 * - offset: Pagination offset (default 0)
12 * - fields: Comma-separated list of fields to return
15import { NextRequest, NextResponse } from 'next/server';
16import { getRequestContext } from '@/lib/server/sessionUtils';
17import { createFD1Client } from '@/lib/client/fd1Client';
19export async function GET(request: NextRequest) {
21 const context = await getRequestContext(request);
23 if (!context?.session?.storeUrl) {
24 return NextResponse.json(
25 { success: false, error: 'Not authenticated' },
30 // Parse query parameters
31 const searchParams = request.nextUrl.searchParams;
32 const search = searchParams.get('search');
33 const department = searchParams.get('department');
34 const sku = searchParams.get('sku');
35 const limit = parseInt(searchParams.get('limit') || '100', 10);
36 const offset = parseInt(searchParams.get('offset') || '0', 10);
37 const fieldsParam = searchParams.get('fields');
39 // Build query filters
40 const filters: Record<string, any> = {};
41 if (search) filters.search = search;
42 if (department) filters.department = department;
43 if (sku) filters.sku = sku;
45 // Build field selection
46 let fields: Record<string, any> = {
48 internal_guid: 'physkey',
59 // Parse custom fields
61 fieldsParam.split(',').forEach(field => {
62 fields[field.trim()] = true;
67 const fd1Client = createFD1Client({
68 baseUrl: context.session.storeUrl!,
69 apiKey: context.session.apiKey,
74 const products = await fd1Client.listProducts(
75 Object.keys(filters).length > 0 ? filters : undefined,
81 return NextResponse.json({
84 count: products.length,
90 console.error('FD1 products API error:', error);
91 return NextResponse.json(
94 error: error instanceof Error ? error.message : 'Failed to fetch products',