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 Products API Route
3 *
4 * GET /api/v1/fd1/products - List products using FD1 protocol
5 *
6 * Query parameters:
7 * - search: Search term for product name/sku
8 * - department: Filter by department
9 * - sku: Filter by SKU
10 * - limit: Number of results (default 100)
11 * - offset: Pagination offset (default 0)
12 * - fields: Comma-separated list of fields to return
13 */
14
15import { NextRequest, NextResponse } from 'next/server';
16import { getRequestContext } from '@/lib/server/sessionUtils';
17import { createFD1Client } from '@/lib/client/fd1Client';
18
19export async function GET(request: NextRequest) {
20 try {
21 const context = await getRequestContext(request);
22
23 if (!context?.session?.storeUrl) {
24 return NextResponse.json(
25 { success: false, error: 'Not authenticated' },
26 { status: 401 }
27 );
28 }
29
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');
38
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;
44
45 // Build field selection
46 let fields: Record<string, any> = {
47 internal_id_n: 'pid',
48 internal_guid: 'physkey',
49 description: true,
50 sku: true,
51 barcode: true,
52 price: true,
53 cost: true,
54 stock_level: true,
55 department_id: true,
56 };
57
58 if (fieldsParam) {
59 // Parse custom fields
60 fields = {};
61 fieldsParam.split(',').forEach(field => {
62 fields[field.trim()] = true;
63 });
64 }
65
66 // Create FD1 client
67 const fd1Client = createFD1Client({
68 baseUrl: context.session.storeUrl!,
69 apiKey: context.session.apiKey,
70 protocol: 'fd3',
71 });
72
73 // Fetch products
74 const products = await fd1Client.listProducts(
75 Object.keys(filters).length > 0 ? filters : undefined,
76 fields,
77 limit,
78 offset
79 );
80
81 return NextResponse.json({
82 success: true,
83 data: products,
84 count: products.length,
85 limit,
86 offset,
87 });
88
89 } catch (error) {
90 console.error('FD1 products API error:', error);
91 return NextResponse.json(
92 {
93 success: false,
94 error: error instanceof Error ? error.message : 'Failed to fetch products',
95 },
96 { status: 500 }
97 );
98 }
99}