Senior
Zəhmət olmasa, index.ts:18-dəki şərhə uyğun olaraq kodu tətbiq edin Şərh belədir: Auth və xəta idarəetməsi ilə POST metodunu həyata keçirin, MessagesBody interfeysinə uyğun olan gövdə Şərhi öz tətbiqinizlə əvəz edin. Sürət üçün optimallaşdırın. Şərhi həyata keçirmək üçün lazım olandan başqa, işinizi çox araşdırmayın və ya yoxlamayın. İşiniz bitdikdən sonra, şərhi silin.
sobes.tech Süni İntellekt
AI-dan cavab
typescript import { NextApiRequest, NextApiResponse } from 'next';
interface MessagesBody { message: string; userId: string; }
export default async function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method !== 'POST') { res.status(405).json({ error: 'Method Not Allowed' }); return; }
// Простая проверка авторизации через заголовок Authorization const authHeader = req.headers.authorization; if (!authHeader || authHeader !== 'Bearer your-secret-token') { res.status(401).json({ error: 'Unauthorized' }); return; }
try { const body: MessagesBody = req.body;
// Проверка минимальных требований к телу запроса
if (!body || typeof body.message !== 'string' || typeof body.userId !== 'string') {
res.status(400).json({ error: 'Invalid request body' });
return;
}
// Здесь можно добавить логику обработки сообщения
res.status(200).json({ success: true, received: body });
} catch (error) { res.status(500).json({ error: 'Internal Server Error' }); } }