Files
gestion-materiaux-destock/src/app/api/images/upload/route.ts
T

33 lines
1.1 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { uploadProductImage } from '@/lib/shopify'
export async function POST(req: NextRequest) {
let body: Record<string, unknown>
try {
body = await req.json()
} catch {
return NextResponse.json({ error: 'Corps JSON invalide' }, { status: 400 })
}
const { shopifyProductId, imageBase64, filename } = body
if (!shopifyProductId || typeof shopifyProductId !== 'string') {
return NextResponse.json({ error: 'shopifyProductId requis' }, { status: 400 })
}
if (!imageBase64 || typeof imageBase64 !== 'string') {
return NextResponse.json({ error: 'imageBase64 requis' }, { status: 400 })
}
if (!filename || typeof filename !== 'string') {
return NextResponse.json({ error: 'filename requis' }, { status: 400 })
}
try {
const result = await uploadProductImage(shopifyProductId, imageBase64, filename)
return NextResponse.json(result)
} catch (err) {
const message = err instanceof Error ? err.message : 'Erreur inconnue'
const status = (err as { status?: number }).status === 404 ? 404 : 500
return NextResponse.json({ error: message }, { status })
}
}