@@ -62,22 +62,42 @@ function token() {
6262 return `${ signingInput } .${ base64url ( signature ) } ` ;
6363}
6464
65- async function request ( method , route , body ) {
66- const response = await fetch ( `https://api.appstoreconnect.apple.com${ route } ` , {
67- method,
68- headers : {
69- Authorization : `Bearer ${ token ( ) } ` ,
70- "Content-Type" : "application/json"
71- } ,
72- body : body ? JSON . stringify ( body ) : undefined
73- } ) ;
65+ function sleep ( ms ) {
66+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
67+ }
68+
69+ async function request ( method , route , body , options = { } ) {
70+ const attempts = options . attempts || 1 ;
71+ let lastError ;
72+
73+ for ( let attempt = 1 ; attempt <= attempts ; attempt += 1 ) {
74+ const response = await fetch ( `https://api.appstoreconnect.apple.com${ route } ` , {
75+ method,
76+ headers : {
77+ Authorization : `Bearer ${ token ( ) } ` ,
78+ "Content-Type" : "application/json"
79+ } ,
80+ body : body ? JSON . stringify ( body ) : undefined
81+ } ) ;
7482
75- const text = await response . text ( ) ;
76- const json = text ? JSON . parse ( text ) : { } ;
77- if ( ! response . ok ) {
78- throw new Error ( `App Store Connect API ${ method } ${ route } failed (${ response . status } ): ${ text } ` ) ;
83+ const text = await response . text ( ) ;
84+ const json = text ? JSON . parse ( text ) : { } ;
85+ if ( ! response . ok ) {
86+ const message = `App Store Connect API ${ method } ${ route } failed (${ response . status } ): ${ text } ` ;
87+ lastError = new Error ( message ) ;
88+ if ( response . status >= 500 && attempt < attempts ) {
89+ const delayMs = attempt * 15000 ;
90+ console . warn ( `${ message } \nRetrying in ${ delayMs / 1000 } s (${ attempt + 1 } /${ attempts } )...` ) ;
91+ await sleep ( delayMs ) ;
92+ continue ;
93+ }
94+ throw lastError ;
95+ }
96+
97+ return json ;
7998 }
80- return json ;
99+
100+ throw lastError ;
81101}
82102
83103async function bundleIdFor ( identifier , name ) {
@@ -187,7 +207,7 @@ async function createProfile({ name, bundleId, certificateId }) {
187207 }
188208 }
189209 }
190- } ) ;
210+ } , { attempts : 4 } ) ;
191211
192212 return {
193213 uuid : response . data . attributes . uuid ,
0 commit comments