File size: 1,834 Bytes
68f7925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import type { ScreenshotRequest, ScreenshotResponse } from '@/schema/proposal';
import { AppError, parseApiError } from '@/lib/errors';

export const screenshotApi = {
  capture: async (request: ScreenshotRequest & { dummyMode?: boolean }): Promise<ScreenshotResponse> => {
    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,
      );
    }
  },
};