1import { NextRequest, NextResponse } from 'next/server';
2import { fieldpineServerApi } from '@/lib/server/fieldpineApi';
3import { getStoredAuth } from '@/lib/server/auth';
6 * OpenAPI Products Search Endpoint with Natural Language
7 * GET /api/v1/openapi/products-search?querytext=sold this month
8 * Supports free-form natural language queries
10export async function GET(request: NextRequest) {
12 const authData = await getStoredAuth();
13 if (!authData || !authData.authenticated) {
14 return NextResponse.json(
15 { error: 'Authentication required' },
20 const { searchParams } = new URL(request.url);
21 const params: Record<string, string | number> = {};
23 const querytext = searchParams.get('querytext');
24 if (querytext) params.querytext = querytext;
26 const want = searchParams.get('want');
27 if (want) params.want = want;
29 const like = searchParams.get('like');
30 if (like) params.like = like;
32 const limit = searchParams.get('limit');
33 if (limit) params.limit = parseInt(limit);
35 const option = searchParams.get('option');
36 if (option) params.option = option;
38 const ref = searchParams.get('ref');
39 if (ref) params.ref = ref;
41 const inputmethod = searchParams.get('inputmethod');
42 if (inputmethod) params.inputmethod = inputmethod;
44 const products = await fieldpineServerApi.apiCall("/ProductsSearch", {
46 cookie: authData.apiKey,
50 return NextResponse.json({
57 console.error('ProductsSearch API error:', error);
58 return NextResponse.json(
59 { error: 'Failed to search products' },