File size: 1,228 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
import * as fs from 'node:fs';
import * as path from 'node:path';

/**
 * ダミーデータファイルを読み込む
 * @param fileName ファイル名(例: 'get_check_url.json')
 * @returns パースされたJSONデータ
 * @throws ファイルが存在しない場合やJSON解析に失敗した場合
 */
export function getDummyData<T = unknown>(fileName: string): T {
  const dummyFilePath = path.join(process.cwd(), 'public', 'dummy', fileName);

  if (!fs.existsSync(dummyFilePath)) {
    throw new Error(`ダミーデータファイルが見つかりません: ${fileName}`);
  }

  try {
    const fileContent = fs.readFileSync(dummyFilePath, 'utf-8');
    return JSON.parse(fileContent) as T;
  } catch (error) {
    if (error instanceof SyntaxError) {
      throw new Error(`ダミーデータのJSON解析に失敗しました: ${fileName}`);
    }
    throw error;
  }
}

/**
 * ダミーデータファイルが存在するかチェック
 * @param fileName ファイル名
 * @returns ファイルが存在する場合true
 */
export function hasDummyData(fileName: string): boolean {
  const dummyFilePath = path.join(process.cwd(), 'public', 'dummy', fileName);
  return fs.existsSync(dummyFilePath);
}