// 数据读取工具类
class DataFetcher {
  constructor(basePath) {
    this.basePath = basePath;
  }

  // 读取文件的异步方法
  async fetchData(farmId, key) {
    const filePath = `${this.basePath}/${farmId}/${key}.json`;
    return this.fetchDataFilePath(filePath)
  }

  // 读取文件的异步方法
  async fetchDataKey(key) {
    const filePath = `${this.basePath}/${key}.json`;
    return this.fetchDataFilePath(filePath)
  }

  async fetchDataFilePath(filePath) {
    try {
      const response = await fetch(filePath);
      if (!response.ok) {
        throw new Error(`无法读取文件: ${filePath}`);
      }
      const data = await response.json();
      return data;
    } catch (error) {
      console.error(error);
      return null;
    }
  }
}

// 模块化导出
module.exports = DataFetcher;
