diff --git a/src/utils/api.ts b/src/utils/api.ts index a0f1250..c99cf95 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -17,20 +17,37 @@ import { const API_URL = import.meta.env.VITE_API_URL || 'http://40.82.133.44'; +// 크롤링 타임아웃: 5분 +const CRAWL_TIMEOUT = 5 * 60 * 1000; + export async function crawlUrl(url: string): Promise { - const response = await fetch(`${API_URL}/crawling`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ url }), - }); + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), CRAWL_TIMEOUT); - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); + try { + const response = await fetch(`${API_URL}/crawling`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ url }), + signal: controller.signal, + }); + + clearTimeout(timeoutId); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + return response.json(); + } catch (error) { + clearTimeout(timeoutId); + if (error instanceof Error && error.name === 'AbortError') { + throw new Error('크롤링 요청 시간이 초과되었습니다. 다시 시도해주세요.'); + } + throw error; } - - return response.json(); } // 가사 생성 API