48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
/* B03 다중 파일 업로드 API 클라이언트 */
|
|
|
|
import { API_BASE_URL, API_TIMEOUT_MS, AUTH_TOKEN_KEY } from "@config/config_frontend";
|
|
|
|
export interface UploadedFileResult {
|
|
input_file_id: number;
|
|
original_filename: string;
|
|
file_type: string;
|
|
relative_path: string;
|
|
size_bytes: number;
|
|
metadata: Record<string, unknown>;
|
|
}
|
|
|
|
export interface FileUploadResponse {
|
|
status: string;
|
|
project_id: string;
|
|
files: UploadedFileResult[];
|
|
}
|
|
|
|
export async function uploadProjectFiles(
|
|
projectId: string,
|
|
files: readonly File[],
|
|
): Promise<FileUploadResponse> {
|
|
const formData = new FormData();
|
|
for (const file of files) formData.append("files", file, file.name);
|
|
|
|
const controller = new AbortController();
|
|
const timeoutId = window.setTimeout(() => controller.abort(), API_TIMEOUT_MS);
|
|
const token = localStorage.getItem(AUTH_TOKEN_KEY);
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}/projects/${projectId}/files`, {
|
|
method: "POST",
|
|
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
|
body: formData,
|
|
signal: controller.signal,
|
|
});
|
|
const payload = (await response.json()) as Partial<FileUploadResponse> & {
|
|
message?: string;
|
|
};
|
|
if (!response.ok) {
|
|
throw new Error(payload.message ?? `HTTP ${response.status}`);
|
|
}
|
|
return payload as FileUploadResponse;
|
|
} finally {
|
|
window.clearTimeout(timeoutId);
|
|
}
|
|
}
|