2025-11-14 05:18:08 +00:00
|
|
|
'use server';
|
|
|
|
|
|
2025-11-19 23:40:28 +00:00
|
|
|
import { streamSearch } from '@/features/search/searchApi';
|
|
|
|
|
import { searchRequestSchema } from '@/features/search/types';
|
2025-11-15 04:53:55 +00:00
|
|
|
import { schemaValidationError, serviceErrorResponse } from '@/lib/serviceError';
|
2025-11-19 23:40:28 +00:00
|
|
|
import { isServiceError } from '@/lib/utils';
|
2025-11-15 04:53:55 +00:00
|
|
|
import { NextRequest } from 'next/server';
|
|
|
|
|
|
2025-11-14 05:18:08 +00:00
|
|
|
export const POST = async (request: NextRequest) => {
|
2025-11-19 23:40:28 +00:00
|
|
|
const body = await request.json();
|
|
|
|
|
const parsed = await searchRequestSchema.safeParseAsync(body);
|
2025-11-14 05:18:08 +00:00
|
|
|
|
2025-11-19 23:40:28 +00:00
|
|
|
if (!parsed.success) {
|
|
|
|
|
return serviceErrorResponse(schemaValidationError(parsed.error));
|
2025-11-14 05:18:08 +00:00
|
|
|
}
|
2025-11-19 20:33:57 +00:00
|
|
|
|
2025-11-20 23:42:31 +00:00
|
|
|
const {
|
|
|
|
|
query,
|
|
|
|
|
...options
|
|
|
|
|
} = parsed.data;
|
|
|
|
|
|
|
|
|
|
const stream = await streamSearch({
|
|
|
|
|
queryType: 'string',
|
|
|
|
|
query,
|
|
|
|
|
options,
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-19 23:40:28 +00:00
|
|
|
if (isServiceError(stream)) {
|
|
|
|
|
return serviceErrorResponse(stream);
|
|
|
|
|
}
|
2025-11-15 04:53:55 +00:00
|
|
|
|
2025-11-19 23:40:28 +00:00
|
|
|
return new Response(stream, {
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'text/event-stream',
|
|
|
|
|
'Cache-Control': 'no-cache, no-transform',
|
|
|
|
|
'Connection': 'keep-alive',
|
|
|
|
|
'X-Accel-Buffering': 'no', // Disable nginx buffering if applicable
|
2025-11-15 04:53:55 +00:00
|
|
|
},
|
|
|
|
|
});
|
2025-11-19 23:40:28 +00:00
|
|
|
};
|