@@ -30,6 +30,7 @@ interface ListOptions {
3030 contentType ?: string ;
3131 state ?: string ;
3232 public ?: boolean ;
33+ limit ?: string ;
3334 output ?: string ;
3435}
3536
@@ -817,30 +818,53 @@ export async function listObjects(options: ListOptions) {
817818 try {
818819 const client = getClient ( ) ;
819820
820- // Build query params
821- const queryParams : Record < string , unknown > = {
822- limit : DEFAULT_PAGE_SIZE ,
823- } ;
824- if ( options . name ) {
825- queryParams . name = options . name ;
826- }
827- if ( options . contentType ) {
828- queryParams . content_type = options . contentType ;
829- }
830- if ( options . state ) {
831- queryParams . state = options . state ;
832- }
833- if ( options . public !== undefined ) {
834- queryParams . is_public = options . public ;
835- }
821+ const maxResults = options . limit ? parseInt ( options . limit , 10 ) : Infinity ;
822+ const allObjects : unknown [ ] = [ ] ;
823+ let startingAfter : string | undefined ;
836824
837- // Fetch objects
838- const result = await client . objects . list ( queryParams ) ;
825+ do {
826+ const remaining = maxResults - allObjects . length ;
827+ // Build query params
828+ const queryParams : Record < string , unknown > = {
829+ limit : Math . min ( DEFAULT_PAGE_SIZE , remaining ) ,
830+ } ;
831+ if ( options . name ) {
832+ queryParams . name = options . name ;
833+ }
834+ if ( options . contentType ) {
835+ queryParams . content_type = options . contentType ;
836+ }
837+ if ( options . state ) {
838+ queryParams . state = options . state ;
839+ }
840+ if ( options . public !== undefined ) {
841+ queryParams . is_public = options . public ;
842+ }
843+ if ( startingAfter ) {
844+ queryParams . starting_after = startingAfter ;
845+ }
839846
840- // Extract objects array
841- const objects = result . objects || [ ] ;
847+ // Fetch one page
848+ const result = await client . objects . list ( queryParams ) ;
849+ const pageResult = result as unknown as {
850+ objects ?: { id : string } [ ] ;
851+ has_more ?: boolean ;
852+ } ;
853+ const pageObjects = pageResult . objects || [ ] ;
854+ allObjects . push ( ...pageObjects ) ;
855+
856+ if (
857+ pageResult . has_more &&
858+ pageObjects . length > 0 &&
859+ allObjects . length < maxResults
860+ ) {
861+ startingAfter = pageObjects [ pageObjects . length - 1 ] . id ;
862+ } else {
863+ startingAfter = undefined ;
864+ }
865+ } while ( startingAfter !== undefined ) ;
842866
843- output ( objects , { format : options . output , defaultFormat : "json" } ) ;
867+ output ( allObjects , { format : options . output , defaultFormat : "json" } ) ;
844868 } catch ( error ) {
845869 outputError ( "Failed to list storage objects" , error ) ;
846870 }
0 commit comments