- Switch from @neondatabase/serverless to standard 'pg' client for Node.js 18 support - Update Node.js version to 18.x as required by Vercel - Replace Neon template literals with standard parameterized queries - Add proper connection pooling and SSL configuration for production - All API endpoints updated: /api/v1/cards/, /api/v1/cards/[id], /api/migrate - Build tested and working successfully
133 lines
3.5 KiB
TypeScript
133 lines
3.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { Pool } from 'pg'
|
|
|
|
// Create PostgreSQL connection pool
|
|
const pool = new Pool({
|
|
connectionString: process.env.DATABASE_URL,
|
|
ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false,
|
|
})
|
|
|
|
interface Card {
|
|
id: number
|
|
name: string
|
|
set_name?: string
|
|
set_code?: string
|
|
card_number?: string
|
|
rarity?: string
|
|
game: string
|
|
mana_cost?: string
|
|
cmc?: number
|
|
card_type?: string
|
|
colors?: string[]
|
|
oracle_text?: string
|
|
flavor_text?: string
|
|
power?: string
|
|
toughness?: string
|
|
artist?: string
|
|
image_url?: string
|
|
stock_image_url?: string
|
|
artwork_crop_coords?: any
|
|
current_price?: number
|
|
market_price?: number
|
|
verified: boolean
|
|
created_at?: string
|
|
updated_at?: string
|
|
}
|
|
|
|
export default async function handler(req: NextRequest) {
|
|
// Enable CORS
|
|
const headers = {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
}
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
return new NextResponse(null, { status: 200, headers })
|
|
}
|
|
|
|
try {
|
|
const url = new URL(req.url)
|
|
const searchParams = url.searchParams
|
|
|
|
// Get query parameters
|
|
const skip = parseInt(searchParams.get('skip') || '0')
|
|
const limit = parseInt(searchParams.get('limit') || '100')
|
|
const search = searchParams.get('search')
|
|
const game = searchParams.get('game')
|
|
const set_name = searchParams.get('set_name')
|
|
|
|
// Build the query dynamically
|
|
let baseQuery = 'SELECT * FROM cards WHERE 1=1'
|
|
let queryParams: any[] = []
|
|
let paramIndex = 1
|
|
|
|
if (game) {
|
|
baseQuery += ` AND game = $${paramIndex}`
|
|
queryParams.push(game)
|
|
paramIndex++
|
|
}
|
|
|
|
if (set_name) {
|
|
baseQuery += ` AND set_name = $${paramIndex}`
|
|
queryParams.push(set_name)
|
|
paramIndex++
|
|
}
|
|
|
|
if (search) {
|
|
baseQuery += ` AND (name ILIKE $${paramIndex} OR oracle_text ILIKE $${paramIndex} OR card_type ILIKE $${paramIndex})`
|
|
queryParams.push(`%${search}%`)
|
|
paramIndex++
|
|
}
|
|
|
|
baseQuery += ' ORDER BY name'
|
|
|
|
if (limit > 0) {
|
|
baseQuery += ` LIMIT $${paramIndex}`
|
|
queryParams.push(limit)
|
|
paramIndex++
|
|
}
|
|
|
|
if (skip > 0) {
|
|
baseQuery += ` OFFSET $${paramIndex}`
|
|
queryParams.push(skip)
|
|
}
|
|
|
|
// Execute the query
|
|
const client = await pool.connect()
|
|
try {
|
|
const result = await client.query(baseQuery, queryParams)
|
|
|
|
// Transform the result to match expected format
|
|
const cards = result.rows.map((row: any) => ({
|
|
...row,
|
|
colors: row.colors ? (typeof row.colors === 'string' ? JSON.parse(row.colors) : row.colors) : null,
|
|
artwork_crop_coords: row.artwork_crop_coords ?
|
|
(typeof row.artwork_crop_coords === 'string' ? JSON.parse(row.artwork_crop_coords) : row.artwork_crop_coords) : null
|
|
}))
|
|
|
|
return new NextResponse(JSON.stringify(cards), {
|
|
status: 200,
|
|
headers: {
|
|
...headers,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
})
|
|
} finally {
|
|
client.release()
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('API Error:', error)
|
|
return new NextResponse(JSON.stringify({
|
|
error: 'Internal server error',
|
|
details: process.env.NODE_ENV === 'development' ? (error as Error).message : undefined
|
|
}), {
|
|
status: 500,
|
|
headers: {
|
|
...headers,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
})
|
|
}
|
|
}
|