11import { API_BASE } from "@/lib/api" ;
22
3+ async function fetchWithTimeout ( input : RequestInfo | URL , init : RequestInit = { } , timeoutMs = 60000 ) : Promise < Response > {
4+ const controller = new AbortController ( ) ;
5+ const id = setTimeout ( ( ) => controller . abort ( ) , timeoutMs ) ;
6+ try {
7+ const res = await fetch ( input , { ...init , signal : controller . signal } ) ;
8+ return res ;
9+ } finally {
10+ clearTimeout ( id ) ;
11+ }
12+ }
13+
314export async function createProcessingJob ( file : File , fast = false ) : Promise < string > {
415 const form = new FormData ( ) ;
516 form . append ( "video" , file ) ;
617 form . append ( "fast" , String ( fast ) ) ;
7- const res = await fetch ( `${ API_BASE ? API_BASE : "" } /api/jobs` , {
8- method : "POST" ,
9- body : form ,
10- } ) ;
18+ // Try absolute backend URL first; on network error, fall back to relative (Vercel rewrite)
19+ let res : Response ;
20+ try {
21+ res = await fetchWithTimeout ( `${ API_BASE ? API_BASE : "" } /api/jobs` , { method : "POST" , body : form } , 120000 ) ;
22+ } catch ( _ ) {
23+ res = await fetchWithTimeout ( `/api/jobs` , { method : "POST" , body : form } , 120000 ) ;
24+ }
1125 if ( ! res . ok ) throw new Error ( await res . text ( ) ) ;
1226 const data = await res . json ( ) ;
1327 return data . job_id as string ;
@@ -16,7 +30,12 @@ export async function createProcessingJob(file: File, fast = false): Promise<str
1630export async function pollJob ( jobId : string , { intervalMs = 3000 , timeoutMs = 15 * 60 * 1000 } = { } ) : Promise < void > {
1731 const start = Date . now ( ) ;
1832 while ( true ) {
19- const res = await fetch ( `${ API_BASE ? API_BASE : "" } /api/jobs/${ jobId } /status` ) ;
33+ let res : Response ;
34+ try {
35+ res = await fetchWithTimeout ( `${ API_BASE ? API_BASE : "" } /api/jobs/${ jobId } /status` , { } , 30000 ) ;
36+ } catch ( _ ) {
37+ res = await fetchWithTimeout ( `/api/jobs/${ jobId } /status` , { } , 30000 ) ;
38+ }
2039 if ( ! res . ok ) throw new Error ( await res . text ( ) ) ;
2140 const data = await res . json ( ) ;
2241 if ( data . status === "done" ) return ;
@@ -27,7 +46,12 @@ export async function pollJob(jobId: string, { intervalMs = 3000, timeoutMs = 15
2746}
2847
2948export async function fetchJobResult ( jobId : string ) : Promise < Blob > {
30- const res = await fetch ( `${ API_BASE ? API_BASE : "" } /api/jobs/${ jobId } /result` ) ;
49+ let res : Response ;
50+ try {
51+ res = await fetchWithTimeout ( `${ API_BASE ? API_BASE : "" } /api/jobs/${ jobId } /result` , { } , 60000 ) ;
52+ } catch ( _ ) {
53+ res = await fetchWithTimeout ( `/api/jobs/${ jobId } /result` , { } , 60000 ) ;
54+ }
3155 if ( res . status === 202 ) throw new Error ( "Job not complete yet" ) ;
3256 if ( ! res . ok ) throw new Error ( await res . text ( ) ) ;
3357 const contentType = res . headers . get ( "content-type" ) || "" ;
0 commit comments