Some checks are pending
CI / Crawler (Python ${{ matrix.python-version }}) (3.10) (push) Waiting to run
CI / Crawler (Python ${{ matrix.python-version }}) (3.11) (push) Waiting to run
CI / API (Python 3.11) (push) Waiting to run
CI / Database migration (push) Waiting to run
CI / App web build (Node 20) (push) Waiting to run
64 lines
1.9 KiB
JavaScript
64 lines
1.9 KiB
JavaScript
import { appConfig } from '../config/appConfig';
|
|
|
|
const buildQuery = (params) => {
|
|
const entries = Object.entries(params)
|
|
.filter(([, value]) => value !== undefined && value !== null && value !== '')
|
|
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
|
|
return entries.length > 0 ? `?${entries.join('&')}` : '';
|
|
};
|
|
|
|
const requestJson = async (path, options = {}) => {
|
|
const response = await fetch(`${appConfig.apiBaseUrl}${path}`, options);
|
|
let payload = null;
|
|
|
|
try {
|
|
payload = await response.json();
|
|
} catch {
|
|
// Preserve the HTTP status when an upstream proxy returns a non-JSON body.
|
|
}
|
|
|
|
if (!response.ok) {
|
|
const message = payload?.detail || payload?.message || `HTTP Error: ${response.status}`;
|
|
throw new Error(message);
|
|
}
|
|
|
|
return payload;
|
|
};
|
|
|
|
export const getPlants = (companyId = appConfig.companyId) => (
|
|
requestJson(`/plants/${companyId}`)
|
|
);
|
|
|
|
export const updatePlantAlerts = (companyId, plantId, alertsEnabled) => {
|
|
if (!companyId) {
|
|
throw new Error('발전소의 업체 ID가 없습니다.');
|
|
}
|
|
|
|
return requestJson(`/plants/${companyId}/${plantId}/alerts`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ alerts_enabled: alertsEnabled }),
|
|
});
|
|
};
|
|
|
|
export const getHourlyPlantStats = (plantId, date) => (
|
|
requestJson(`/plants/${plantId}/stats/today${buildQuery({ date })}`)
|
|
);
|
|
|
|
export const getPlantStats = (plantId, params) => (
|
|
requestJson(`/plants/${plantId}/stats${buildQuery(params)}`)
|
|
);
|
|
|
|
export const getComparisonStats = (params) => (
|
|
requestJson('/plants/stats/comparison' + buildQuery(params))
|
|
);
|
|
|
|
export const uploadGenerationData = (plantId, uploadType, formData) => {
|
|
const suffix = uploadType === 'monthly' ? '/monthly' : '';
|
|
return requestJson(`/plants/${plantId}/upload${suffix}`, {
|
|
method: 'POST',
|
|
body: formData,
|
|
});
|
|
};
|