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 Product Detail API Route
3 *
4 * GET /api/v1/fd1/products/[id] - Get single product by ID
5 */
6
7import { NextRequest, NextResponse } from 'next/server';
8import { getRequestContext } from '@/lib/server/sessionUtils';
9import { createFD1Client } from '@/lib/client/fd1Client';
10
11export async function GET(
12 request: NextRequest,
13 { params }: { params: Promise<{ id: string }> }
14) {
15 try {
16 const context = await getRequestContext(request);
17
18 if (!context?.session?.storeUrl) {
19 return NextResponse.json(
20 { success: false, error: 'Not authenticated' },
21 { status: 401 }
22 );
23 }
24
25 const { id } = await params;
26 const productId = parseInt(id, 10);
27
28 if (isNaN(productId)) {
29 return NextResponse.json(
30 { success: false, error: 'Invalid product ID' },
31 { status: 400 }
32 );
33 }
34
35 // Parse query parameters for field selection
36 const searchParams = request.nextUrl.searchParams;
37 const fieldsParam = searchParams.get('fields');
38
39 let fields: Record<string, any> | undefined;
40 if (fieldsParam) {
41 fields = {};
42 fieldsParam.split(',').forEach(field => {
43 fields![field.trim()] = true;
44 });
45 }
46
47 // Create FD1 client
48 const fd1Client = createFD1Client({
49 baseUrl: context.session.storeUrl!,
50 apiKey: context.session.apiKey,
51 protocol: 'fd3',
52 });
53
54 // Fetch product
55 const product = await fd1Client.getProduct(productId, fields);
56
57 if (!product) {
58 return NextResponse.json(
59 { success: false, error: 'Product not found' },
60 { status: 404 }
61 );
62 }
63
64 return NextResponse.json({
65 success: true,
66 data: product,
67 });
68
69 } catch (error) {
70 console.error('FD1 product detail API error:', error);
71 return NextResponse.json(
72 {
73 success: false,
74 error: error instanceof Error ? error.message : 'Failed to fetch product',
75 },
76 { status: 500 }
77 );
78 }
79}