@@ -33,79 +33,40 @@ function getEnvs() {
3333
3434const apiRoot = 'https://api.netlify.com/api/v1/' ;
3535
36- async function get ( netlifyApiToken , path ) {
36+ async function fetchWithTimeout ( netlifyApiToken , path , method = 'GET' , payload = null , parseAs = 'json' ) {
3737 const controller = new AbortController ( ) ;
3838 const timeout = setTimeout ( ( ) => controller . abort ( ) , 10000 ) ; // 10 second timeout
3939
4040 try {
41- const response = await fetch ( ` ${ apiRoot } ${ path } ` , {
41+ const options = {
4242 signal : controller . signal ,
43+ method,
4344 headers : {
4445 'Content-Type' : 'application/json' ,
4546 Authorization : `Bearer ${ netlifyApiToken } ` ,
4647 } ,
47- } ) . then ( res => res . json ( ) ) ;
48- clearTimeout ( timeout ) ;
49- return response ;
50- } catch ( error ) {
51- clearTimeout ( timeout ) ;
52- if ( error . name === 'AbortError' ) {
53- console . error ( `Netlify API GET timeout after 10s: ${ path } ` ) ;
54- throw new Error ( `Netlify API GET request timeout: ${ path } ` ) ;
48+ } ;
49+
50+ if ( payload ) {
51+ options . body = JSON . stringify ( payload ) ;
5552 }
56- throw error ;
57- }
58- }
59-
60- async function post ( netlifyApiToken , path , payload ) {
61- const controller = new AbortController ( ) ;
62- const timeout = setTimeout ( ( ) => controller . abort ( ) , 10000 ) ; // 10 second timeout
63-
64- try {
65- const response = await fetch ( `${ apiRoot } ${ path } ` , {
66- signal : controller . signal ,
67- method : 'POST' ,
68- headers : {
69- 'Content-Type' : 'application/json' ,
70- Authorization : `Bearer ${ netlifyApiToken } ` ,
71- } ,
72- ...( payload ? { body : JSON . stringify ( payload ) } : { } ) ,
73- } ) . then ( res => res . json ( ) ) ;
53+
54+ const response = await fetch ( `${ apiRoot } ${ path } ` , options ) ;
7455 clearTimeout ( timeout ) ;
75- return response ;
56+
57+ return parseAs === 'json' ? response . json ( ) : response . text ( ) ;
7658 } catch ( error ) {
7759 clearTimeout ( timeout ) ;
7860 if ( error . name === 'AbortError' ) {
79- console . error ( `Netlify API POST timeout after 10s: ${ path } ` ) ;
80- throw new Error ( `Netlify API POST request timeout: ${ path } ` ) ;
61+ console . error ( `Netlify API ${ method } timeout after 10s: ${ path } ` ) ;
62+ throw new Error ( `Netlify API ${ method } request timeout: ${ path } ` ) ;
8163 }
8264 throw error ;
8365 }
8466}
8567
86- async function del ( netlifyApiToken , path ) {
87- const controller = new AbortController ( ) ;
88- const timeout = setTimeout ( ( ) => controller . abort ( ) , 10000 ) ; // 10 second timeout
89-
90- try {
91- const response = await fetch ( `${ apiRoot } ${ path } ` , {
92- signal : controller . signal ,
93- method : 'DELETE' ,
94- headers : {
95- 'Content-Type' : 'application/json' ,
96- Authorization : `Bearer ${ netlifyApiToken } ` ,
97- } ,
98- } ) . then ( res => res . text ( ) ) ;
99- clearTimeout ( timeout ) ;
100- return response ;
101- } catch ( error ) {
102- clearTimeout ( timeout ) ;
103- if ( error . name === 'AbortError' ) {
104- console . error ( `Netlify API DELETE timeout after 10s: ${ path } ` ) ;
105- throw new Error ( `Netlify API DELETE request timeout: ${ path } ` ) ;
106- }
107- throw error ;
108- }
68+ async function post ( netlifyApiToken , path , payload ) {
69+ return fetchWithTimeout ( netlifyApiToken , path , 'POST' , payload , 'json' ) ;
10970}
11071
11172async function createSite ( netlifyApiToken , payload ) {
@@ -130,12 +91,12 @@ async function enableLargeMedia(netlifyApiToken, siteId) {
13091}
13192
13293async function waitForDeploys ( netlifyApiToken , siteId ) {
133- const maxRetries = 5 ; // Reduced from 10 to 5
134- const retryDelayMs = 10 * 1000 ; // Reduced from 30s to 10s
94+ const maxRetries = 5 ;
95+ const retryDelayMs = 10 * 1000 ; // 10 seconds
13596
13697 for ( let i = 0 ; i < maxRetries ; i ++ ) {
13798 try {
138- const deploys = await get ( netlifyApiToken , `sites/${ siteId } /deploys` ) ;
99+ const deploys = await fetchWithTimeout ( netlifyApiToken , `sites/${ siteId } /deploys` , 'GET' , null , 'json' ) ;
139100
140101 if ( deploys && deploys . some ( deploy => deploy . state === 'ready' ) ) {
141102 return ;
@@ -331,7 +292,7 @@ async function teardownGitGateway(taskData) {
331292 const { netlifyApiToken } = getEnvs ( ) ;
332293 const { site_id } = taskData ;
333294 console . log ( 'Deleting Netlify site:' , site_id ) ;
334- await del ( netlifyApiToken , `sites/${ site_id } ` ) ;
295+ await fetchWithTimeout ( netlifyApiToken , `sites/${ site_id } ` , 'DELETE' , null , 'text' ) ;
335296
336297 const result = await methods [ taskData . provider ] . teardown ( taskData ) ;
337298 return result ;
0 commit comments