import axios from "axios";
export interface RegionStats {
establishments: number;
employees: number;
year: number;
}
export class EStatClient {
private appId: string;
private baseUrl = "https://api.e-stat.go.jp/rest/3.0/app/json/getStatsData";
constructor(appId: string) {
this.appId = appId;
}
async getRegionStats(prefecture: string, sectorCode: string): Promise<RegionStats | null> {
try {
// Note: In a real implementation, we need to map "prefecture name" to "prefecture code" (e.g., 石川県 -> 17)
// and "sector code" might need mapping or validation.
// For MVP, we'll assume the user might pass the code or we do a simple mapping if possible.
// Let's assume the caller passes the code for now, or we implement a basic map.
const prefCode = this.getPrefectureCode(prefecture);
if (!prefCode) {
console.error(`Unknown prefecture: ${prefecture}`);
return null;
}
// Stats ID for "Economic Census" (Example: 0003448228 is 2021 Economic Census)
// We need to find a stable Stats ID for "Establishments and Employees by Industry"
// This is a placeholder Stats ID. Real usage requires looking up the correct statsDataId.
const statsDataId = "0003448228";
const response = await axios.get(this.baseUrl, {
params: {
appId: this.appId,
statsDataId: statsDataId,
cdArea: prefCode,
cdCat01: sectorCode, // Industry classification
},
});
if (response.data && response.data.GET_STATS_DATA && response.data.GET_STATS_DATA.STATISTICAL_DATA) {
const data = response.data.GET_STATS_DATA.STATISTICAL_DATA.DATA_INF.DATA_OBJ;
// We need to parse the specific values. The API returns a list of values.
// We need to filter by specific indicators (e.g., number of establishments, employees).
// This is complex without exact API response structure for the specific Stats ID.
// For MVP, we will mock the parsing logic or return the raw first value found.
// Mocking return for MVP structure
return {
establishments: 100, // Placeholder
employees: 1000, // Placeholder
year: 2021
};
}
return null;
} catch (error) {
console.error("Error fetching e-Stat data:", error);
throw error;
}
}
private getPrefectureCode(name: string): string | null {
const map: { [key: string]: string } = {
"北海道": "01", "青森県": "02", "岩手県": "03", "宮城県": "04", "秋田県": "05",
"山形県": "06", "福島県": "07", "茨城県": "08", "栃木県": "09", "群馬県": "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"
};
return map[name] || null;
}
}