114 lines
4.2 KiB
TypeScript
114 lines
4.2 KiB
TypeScript
import { stClient } from "@/shared/api";
|
|
|
|
import {
|
|
SEWERAGE_AGGREGATE_SUMMARY_EXCEL_MIME,
|
|
type SewerageAggregateSummaryDownloadRequest,
|
|
type SewerageAggregateSummaryDownloadResult,
|
|
type SewerageAggregateSummaryTabListResponse,
|
|
type StatisticsSewerageAggregateSummaryService,
|
|
} from "../model/statistics-total/statistics-sewerage-aggregate-summary-interface";
|
|
|
|
const INDICATORS_URL = "/st-management-service/api/v1/summary-excel/indicators";
|
|
/** middleware `/server` rewrite 대신 장시간 blob용 API Route 프록시 */
|
|
const DOWNLOAD_URL = "/api/st/summary-excel/download";
|
|
/** 확인 안내와 동일 — 최대 1시간 대기 */
|
|
const DOWNLOAD_TIMEOUT_MS = 3_600_000;
|
|
|
|
function parseDownloadFileName(contentDisposition: string): string | undefined {
|
|
if (!contentDisposition) return undefined;
|
|
|
|
const utf8Match = contentDisposition.match(
|
|
/filename\*\s*=\s*UTF-8''([^;]+)/i,
|
|
);
|
|
if (utf8Match?.[1]) {
|
|
return decodeURIComponent(utf8Match[1].trim());
|
|
}
|
|
|
|
const plainMatch = contentDisposition.match(/filename\s*=\s*"?([^";]+)"?/i);
|
|
return plainMatch?.[1]?.trim();
|
|
}
|
|
|
|
async function toExcelBlob(data: Blob): Promise<Blob> {
|
|
if (data.type === SEWERAGE_AGGREGATE_SUMMARY_EXCEL_MIME) {
|
|
return data;
|
|
}
|
|
return new Blob([data], { type: SEWERAGE_AGGREGATE_SUMMARY_EXCEL_MIME });
|
|
}
|
|
|
|
async function parseBlobErrorMessage(data: Blob): Promise<string> {
|
|
const fallback = "엑셀 내려받기 중 오류가 발생했습니다.";
|
|
try {
|
|
const text = await data.text();
|
|
const json = JSON.parse(text) as { message?: string };
|
|
return json.message?.trim() || fallback;
|
|
} catch {
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
export class StatisticsSewerageAggregateSummaryApiService implements StatisticsSewerageAggregateSummaryService {
|
|
/** 하수도 통계 총괄표 — 통계항목(탭) 목록 */
|
|
public async getAggregateSummaryTabList(): Promise<SewerageAggregateSummaryTabListResponse> {
|
|
const response =
|
|
await stClient.get<SewerageAggregateSummaryTabListResponse>(
|
|
INDICATORS_URL,
|
|
);
|
|
return response.data;
|
|
}
|
|
|
|
/** 하수도 통계 총괄표 — 엑셀 내려받기 (`Content-Type`: xlsx, `Content-Disposition`: 파일명) */
|
|
public async downloadAggregateSummary(
|
|
request: SewerageAggregateSummaryDownloadRequest,
|
|
): Promise<SewerageAggregateSummaryDownloadResult> {
|
|
try {
|
|
const response = await fetch(DOWNLOAD_URL, {
|
|
method: "POST",
|
|
credentials: "include",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Accept: SEWERAGE_AGGREGATE_SUMMARY_EXCEL_MIME,
|
|
},
|
|
body: JSON.stringify(request),
|
|
signal: AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS),
|
|
});
|
|
|
|
const contentType = String(
|
|
response.headers.get("content-type") ?? "",
|
|
);
|
|
const contentDisposition = String(
|
|
response.headers.get("content-disposition") ?? "",
|
|
);
|
|
const data = await response.blob();
|
|
|
|
if (!response.ok) {
|
|
throw new Error(await parseBlobErrorMessage(data));
|
|
}
|
|
|
|
if (contentType.includes("application/json")) {
|
|
throw new Error(await parseBlobErrorMessage(data));
|
|
}
|
|
|
|
return {
|
|
blob: await toExcelBlob(data),
|
|
fileName: parseDownloadFileName(contentDisposition),
|
|
};
|
|
} catch (error) {
|
|
if (
|
|
error instanceof DOMException &&
|
|
error.name === "TimeoutError"
|
|
) {
|
|
throw new Error(
|
|
"엑셀 생성 시간이 초과되었습니다. 선택 항목을 줄이거나 잠시 후 다시 시도해주세요.",
|
|
);
|
|
}
|
|
if (error instanceof Error) {
|
|
throw error;
|
|
}
|
|
throw new Error("엑셀 내려받기 중 오류가 발생했습니다.");
|
|
}
|
|
}
|
|
}
|
|
|
|
export const statisticsSewerageAggregateSummaryApi =
|
|
new StatisticsSewerageAggregateSummaryApiService();
|