70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { z } from 'zod'
|
|
import { scrapeWebsite, ScrapingError } from '@/lib/scraper'
|
|
import { performDeepAnalysis } from '@/lib/analysis-pipeline'
|
|
|
|
const bodySchema = z.object({
|
|
url: z.string().min(1)
|
|
})
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json()
|
|
const { url } = bodySchema.parse(body)
|
|
|
|
if (!process.env.OPENAI_API_KEY) {
|
|
return NextResponse.json(
|
|
{ error: 'OpenAI API key not configured' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
|
|
console.log(`🌐 Scraping: ${url}`)
|
|
const scrapedContent = await scrapeWebsite(url)
|
|
console.log(` ✓ Scraped ${scrapedContent.headings.length} headings, ${scrapedContent.paragraphs.length} paragraphs`)
|
|
|
|
console.log('🤖 Starting enhanced analysis...')
|
|
const analysis = await performDeepAnalysis(scrapedContent)
|
|
console.log(` ✓ Analysis complete: ${analysis.features.length} features, ${analysis.keywords.length} keywords, ${analysis.dorkQueries.length} queries`)
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: analysis,
|
|
stats: {
|
|
features: analysis.features.length,
|
|
keywords: analysis.keywords.length,
|
|
personas: analysis.personas.length,
|
|
useCases: analysis.useCases.length,
|
|
competitors: analysis.competitors.length,
|
|
dorkQueries: analysis.dorkQueries.length
|
|
}
|
|
})
|
|
|
|
} catch (error: any) {
|
|
console.error('❌ Analysis error:', error)
|
|
|
|
if (error instanceof ScrapingError) {
|
|
return NextResponse.json(
|
|
{
|
|
error: error.message,
|
|
code: error.code,
|
|
needsManualInput: true
|
|
},
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
if (error.name === 'ZodError') {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid URL provided' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json(
|
|
{ error: error.message || 'Failed to analyze website' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|