@@ -248,11 +248,18 @@ export class VariablesController {
248248
249249 fetch : async < T = unknown > ( path : string ) : Promise < T > => {
250250 const url = new URL ( path , baseUrl ) ;
251+
251252 const response = await fetch ( url . toString ( ) , {
252253 headers : buildHeaders ( ) ,
253254 } ) ;
254- if ( ! response . ok ) throw new Error ( `HTTP ${ response . status } ` ) ;
255- return response . json ( ) ;
255+
256+ if ( ! response . ok ) {
257+ const errorText = await response . text ( ) ;
258+ throw new Error ( `HTTP ${ response . status } : ${ errorText } ` ) ;
259+ }
260+
261+ const data = await response . json ( ) ;
262+ return data as T ;
256263 } ,
257264
258265 fetchAllPages : async < T = unknown > ( path : string ) : Promise < T [ ] > => {
@@ -268,10 +275,30 @@ export class VariablesController {
268275 const response = await fetch ( url . toString ( ) , {
269276 headers : buildHeaders ( ) ,
270277 } ) ;
271- if ( ! response . ok ) throw new Error ( `HTTP ${ response . status } ` ) ;
272278
273- const items : T [ ] = await response . json ( ) ;
274- if ( ! Array . isArray ( items ) || items . length === 0 ) break ;
279+ if ( ! response . ok ) {
280+ const errorText = await response . text ( ) ;
281+ throw new Error ( `HTTP ${ response . status } : ${ errorText } ` ) ;
282+ }
283+
284+ const data = await response . json ( ) ;
285+
286+ // Handle both direct array responses and wrapped responses
287+ // e.g., some APIs return { items: [...] } instead of [...]
288+ let items : T [ ] ;
289+ if ( Array . isArray ( data ) ) {
290+ items = data ;
291+ } else if ( data && typeof data === 'object' ) {
292+ // Find the first array property in the response
293+ const arrayValue = Object . values ( data ) . find ( ( v ) =>
294+ Array . isArray ( v ) ,
295+ ) as T [ ] | undefined ;
296+ items = arrayValue ?? [ ] ;
297+ } else {
298+ items = [ ] ;
299+ }
300+
301+ if ( items . length === 0 ) break ;
275302
276303 allItems . push ( ...items ) ;
277304 if ( items . length < perPage ) break ;
0 commit comments