Supra_App/src/routes/api/catalog-create/+server.ts

63 lines
2.1 KiB
TypeScript
Raw Normal View History

import { json, error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { env } from '$env/dynamic/public';
// New promo catalogs are created by the same taobin_image service as menu images.
const API_BASE = env.PUBLIC_POST_IMAGE;
export const POST: RequestHandler = async ({ request }) => {
try {
const formData = await request.formData();
const country = formData.get('country') as string;
const uid = formData.get('uid') as string;
const displayName = formData.get('displayName') as string;
const email = formData.get('email') as string;
const slug = formData.get('slug') as string;
const name = formData.get('name') as string;
const start = formData.get('start') as string;
// 'NONE' means open-ended (no end date).
const end = (formData.get('end') as string) || 'NONE';
const bannerIndex = (formData.get('banner_index') as string) ?? '1';
const banner = formData.get('banner') as File;
if (!country || !uid || !displayName || !email || !slug || !name || !start || !banner) {
throw error(400, 'Missing required fields');
}
const endpoint =
`${API_BASE}/catalog/create/${encodeURIComponent(country)}/${encodeURIComponent(uid)}` +
`/${encodeURIComponent(displayName)}/${encodeURIComponent(email)}`;
console.log('[Catalog Create Proxy] Endpoint:', endpoint, 'slug:', slug);
const upstream = new FormData();
upstream.append('slug', slug);
upstream.append('name', name);
upstream.append('start', start);
upstream.append('end', end);
upstream.append('banner_index', bannerIndex);
upstream.append('banner', banner);
const response = await fetch(endpoint, {
method: 'POST',
body: upstream
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({ detail: response.statusText }));
throw error(response.status, errorData.detail || 'Create catalog failed');
}
return json(await response.json());
} catch (err) {
console.error('[Catalog Create Proxy] Error:', err);
if (err && typeof err === 'object' && 'status' in err) {
throw err;
}
throw error(500, err instanceof Error ? err.message : 'Internal server error');
}
};