import type { ScreenshotRequest, ScreenshotResponse } from '@/schema/proposal'; import { AppError, parseApiError } from '@/lib/errors'; export const screenshotApi = { capture: async (request: ScreenshotRequest & { dummyMode?: boolean }): Promise => { try { // GETメソッドを使用 const params = new URLSearchParams({ url: request.url, width: request.width?.toString() || '1920', height: request.height?.toString() || '1080', ...(request.dummyMode !== undefined && { dummyMode: request.dummyMode.toString() }), }); const response = await fetch(`/api/rpc/screenshot?${params.toString()}`, { method: 'GET', headers: { 'Content-Type': 'application/json', }, }); if (!response.ok) { const errorData = await response.json(); throw parseApiError(errorData); } const data = await response.json(); // レスポンスフォーマットを統一 return { success: data.success, screenshotBase64: data.imageBase64 || '', mimeType: data.mimeType || 'image/jpeg', description: '', }; } catch (error) { // ネットワークエラーやタイムアウトを捕捉 if (error instanceof TypeError && error.message.includes('fetch')) { throw new AppError( 'ネットワークエラーが発生しました', 'NETWORK_ERROR', 503, ); } // 既にAppErrorの場合はそのまま投げる if (error instanceof AppError) { throw error; } // その他のエラー throw new AppError( 'スクリーンショットの取得に失敗しました', 'SCREENSHOT_FAILED', 500, error, ); } }, };