@@ -58,12 +58,7 @@ export async function apiRequest<T>(
5858 body,
5959 } )
6060 if ( ! response . ok ) {
61- const text = await response . text ( ) . catch ( ( ) => '' )
62- const message = text || `HTTP ${ response . status } `
63- if ( response . status === 429 || response . status >= 500 ) {
64- throw new Error ( message )
65- }
66- throw new AbortError ( message )
61+ throwHttpStatusError ( response . status , await readResponseTextSafe ( response ) )
6762 }
6863 return ( await response . json ( ) ) as unknown
6964 } ,
@@ -103,12 +98,7 @@ export async function apiRequestForm<T>(
10398 body : args . form ,
10499 } )
105100 if ( ! response . ok ) {
106- const text = await response . text ( ) . catch ( ( ) => '' )
107- const message = text || `HTTP ${ response . status } `
108- if ( response . status === 429 || response . status >= 500 ) {
109- throw new Error ( message )
110- }
111- throw new AbortError ( message )
101+ throwHttpStatusError ( response . status , await readResponseTextSafe ( response ) )
112102 }
113103 return ( await response . json ( ) ) as unknown
114104 } ,
@@ -133,11 +123,7 @@ export async function fetchText(registry: string, args: TextRequestArgs): Promis
133123 const response = await fetchWithTimeout ( url , { method : 'GET' , headers } )
134124 const text = await response . text ( )
135125 if ( ! response . ok ) {
136- const message = text || `HTTP ${ response . status } `
137- if ( response . status === 429 || response . status >= 500 ) {
138- throw new Error ( message )
139- }
140- throw new AbortError ( message )
126+ throwHttpStatusError ( response . status , text )
141127 }
142128 return text
143129 } ,
@@ -157,11 +143,7 @@ export async function downloadZip(registry: string, args: { slug: string; versio
157143
158144 const response = await fetchWithTimeout ( url . toString ( ) , { method : 'GET' } )
159145 if ( ! response . ok ) {
160- const message = ( await response . text ( ) . catch ( ( ) => '' ) ) || `HTTP ${ response . status } `
161- if ( response . status === 429 || response . status >= 500 ) {
162- throw new Error ( message )
163- }
164- throw new AbortError ( message )
146+ throwHttpStatusError ( response . status , await readResponseTextSafe ( response ) )
165147 }
166148 return new Uint8Array ( await response . arrayBuffer ( ) )
167149 } ,
@@ -179,6 +161,18 @@ async function fetchWithTimeout(url: string, init: RequestInit): Promise<Respons
179161 }
180162}
181163
164+ async function readResponseTextSafe ( response : Response ) : Promise < string > {
165+ return await response . text ( ) . catch ( ( ) => '' )
166+ }
167+
168+ function throwHttpStatusError ( status : number , text : string ) : never {
169+ const message = text || `HTTP ${ status } `
170+ if ( status === 429 || status >= 500 ) {
171+ throw new Error ( message )
172+ }
173+ throw new AbortError ( message )
174+ }
175+
182176async function fetchJsonViaCurl ( url : string , args : RequestArgs ) {
183177 const headers = [ '-H' , 'Accept: application/json' ]
184178 if ( args . token ) {
@@ -213,10 +207,7 @@ async function fetchJsonViaCurl(url: string, args: RequestArgs) {
213207 const status = Number ( output . slice ( splitAt + 1 ) . trim ( ) )
214208 if ( ! Number . isFinite ( status ) ) throw new Error ( 'curl response missing status' )
215209 if ( status < 200 || status >= 300 ) {
216- if ( status === 429 || status >= 500 ) {
217- throw new Error ( body || `HTTP ${ status } ` )
218- }
219- throw new AbortError ( body || `HTTP ${ status } ` )
210+ throwHttpStatusError ( status , body )
220211 }
221212 return JSON . parse ( body || 'null' ) as unknown
222213}
@@ -268,10 +259,7 @@ async function fetchJsonFormViaCurl(url: string, args: FormRequestArgs) {
268259 const status = Number ( output . slice ( splitAt + 1 ) . trim ( ) )
269260 if ( ! Number . isFinite ( status ) ) throw new Error ( 'curl response missing status' )
270261 if ( status < 200 || status >= 300 ) {
271- if ( status === 429 || status >= 500 ) {
272- throw new Error ( body || `HTTP ${ status } ` )
273- }
274- throw new AbortError ( body || `HTTP ${ status } ` )
262+ throwHttpStatusError ( status , body )
275263 }
276264 return JSON . parse ( body || 'null' ) as unknown
277265 } finally {
@@ -340,11 +328,7 @@ async function fetchBinaryViaCurl(url: string) {
340328 if ( ! Number . isFinite ( status ) ) throw new Error ( 'curl response missing status' )
341329 if ( status < 200 || status >= 300 ) {
342330 const body = await readFileSafe ( filePath )
343- const message = body ? new TextDecoder ( ) . decode ( body ) : `HTTP ${ status } `
344- if ( status === 429 || status >= 500 ) {
345- throw new Error ( message )
346- }
347- throw new AbortError ( message )
331+ throwHttpStatusError ( status , body ? new TextDecoder ( ) . decode ( body ) : '' )
348332 }
349333 const bytes = await readFileSafe ( filePath )
350334 return bytes ? new Uint8Array ( bytes ) : new Uint8Array ( )
0 commit comments