@@ -14,22 +14,44 @@ export const plugin: PluginDefinition = {
1414 ctx . httpRequest . list ( ) ,
1515 ] ) ;
1616
17- // Build a set of all folder IDs that are descendants of the target folder
18- const folderIds = new Set < string > ( [ targetFolder . id ] ) ;
19- const addDescendants = ( parentId : string ) => {
20- for ( const folder of allFolders ) {
21- if ( folder . folderId === parentId && ! folderIds . has ( folder . id ) ) {
22- folderIds . add ( folder . id ) ;
23- addDescendants ( folder . id ) ;
24- }
17+ // Build the send order to match tree ordering:
18+ // sort siblings by sortPriority then updatedAt, and traverse folders depth-first.
19+ const compareByOrder = (
20+ a : Pick < typeof allFolders [ number ] , 'sortPriority' | 'updatedAt' > ,
21+ b : Pick < typeof allFolders [ number ] , 'sortPriority' | 'updatedAt' > ,
22+ ) => {
23+ if ( a . sortPriority === b . sortPriority ) {
24+ return a . updatedAt > b . updatedAt ? 1 : - 1 ;
2525 }
26+ return a . sortPriority - b . sortPriority ;
2627 } ;
27- addDescendants ( targetFolder . id ) ;
2828
29- // Filter HTTP requests to those in the target folder or its descendants
30- const requestsToSend = allRequests . filter (
31- ( req ) => req . folderId != null && folderIds . has ( req . folderId ) ,
32- ) ;
29+ const childrenByFolderId = new Map < string , Array < typeof allFolders [ number ] | typeof allRequests [ number ] > > ( ) ;
30+ for ( const folder of allFolders ) {
31+ if ( folder . folderId == null ) continue ;
32+ const children = childrenByFolderId . get ( folder . folderId ) ?? [ ] ;
33+ children . push ( folder ) ;
34+ childrenByFolderId . set ( folder . folderId , children ) ;
35+ }
36+ for ( const request of allRequests ) {
37+ if ( request . folderId == null ) continue ;
38+ const children = childrenByFolderId . get ( request . folderId ) ?? [ ] ;
39+ children . push ( request ) ;
40+ childrenByFolderId . set ( request . folderId , children ) ;
41+ }
42+
43+ const requestsToSend : typeof allRequests = [ ] ;
44+ const collectRequests = ( folderId : string ) => {
45+ const children = ( childrenByFolderId . get ( folderId ) ?? [ ] ) . slice ( ) . sort ( compareByOrder ) ;
46+ for ( const child of children ) {
47+ if ( child . model === 'folder' ) {
48+ collectRequests ( child . id ) ;
49+ } else if ( child . model === 'http_request' ) {
50+ requestsToSend . push ( child ) ;
51+ }
52+ }
53+ } ;
54+ collectRequests ( targetFolder . id ) ;
3355
3456 if ( requestsToSend . length === 0 ) {
3557 await ctx . toast . show ( {
@@ -40,7 +62,7 @@ export const plugin: PluginDefinition = {
4062 return ;
4163 }
4264
43- // Send each request sequentially
65+ // Send requests sequentially in the calculated folder order.
4466 let successCount = 0 ;
4567 let errorCount = 0 ;
4668
0 commit comments