2 * FD1 Product Detail API Route
4 * GET /api/v1/fd1/products/[id] - Get single product by ID
7import { NextRequest, NextResponse } from 'next/server';
8import { getRequestContext } from '@/lib/server/sessionUtils';
9import { createFD1Client } from '@/lib/client/fd1Client';
11export async function GET(
13 { params }: { params: Promise<{ id: string }> }
16 const context = await getRequestContext(request);
18 if (!context?.session?.storeUrl) {
19 return NextResponse.json(
20 { success: false, error: 'Not authenticated' },
25 const { id } = await params;
26 const productId = parseInt(id, 10);
28 if (isNaN(productId)) {
29 return NextResponse.json(
30 { success: false, error: 'Invalid product ID' },
35 // Parse query parameters for field selection
36 const searchParams = request.nextUrl.searchParams;
37 const fieldsParam = searchParams.get('fields');
39 let fields: Record<string, any> | undefined;
42 fieldsParam.split(',').forEach(field => {
43 fields![field.trim()] = true;
48 const fd1Client = createFD1Client({
49 baseUrl: context.session.storeUrl!,
50 apiKey: context.session.apiKey,
55 const product = await fd1Client.getProduct(productId, fields);
58 return NextResponse.json(
59 { success: false, error: 'Product not found' },
64 return NextResponse.json({
70 console.error('FD1 product detail API error:', error);
71 return NextResponse.json(
74 error: error instanceof Error ? error.message : 'Failed to fetch product',