-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sort Postman folders based on the "orderOfFolders" configuration
- Loading branch information
Showing
8 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { orderCollectionFolders } from '../../application' | ||
|
||
describe('orderCollectionFolders()', () => { | ||
it('should order the postman request items in folder in the order like defined', () => { | ||
const order = ['Folder D', 'Folder B'] | ||
const obj = { | ||
info: { | ||
name: 'Test Collection', | ||
schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json' | ||
}, | ||
item: [ | ||
{ | ||
name: 'Folder A', | ||
item: [ | ||
{ | ||
name: 'Request A1', | ||
request: { | ||
url: 'https://example.com', | ||
method: 'GET' | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
name: 'Folder B', | ||
item: [ | ||
{ | ||
name: 'Request B1', | ||
request: { | ||
url: 'https://example.com', | ||
method: 'POST' | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
name: 'Folder C', | ||
item: [ | ||
{ | ||
name: 'Request C1', | ||
request: { | ||
url: 'https://example.com', | ||
method: 'PUT' | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
name: 'Folder D', | ||
item: [ | ||
{ | ||
name: 'Request D1', | ||
request: { | ||
url: 'https://example.com', | ||
method: 'DELETE' | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
name: 'Root Request', | ||
request: { | ||
url: 'https://example.com/root', | ||
method: 'HEAD' | ||
} | ||
} | ||
] | ||
} | ||
|
||
const transform = orderCollectionFolders(obj, order) | ||
expect(transform.item[0].name).toBe('Folder D') // First in the desired order | ||
expect(transform.item[1].name).toBe('Folder B') // Second in the desired order | ||
expect(transform.item[2].name).toBe('Folder A') // Original position, not reordered | ||
expect(transform.item[3].name).toBe('Folder C') // Original position, not reordered | ||
expect(transform.item[4].name).toBe('Root Request') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any */ | ||
import { Collection, Item, ItemGroup } from 'postman-collection' | ||
|
||
export const orderCollectionFolders = (obj: any, orderOfFolders: any = []): any => { | ||
// Convert the input object to a Postman Collection instance if it isn't already | ||
const collection = obj instanceof Collection ? obj : new Collection(obj) | ||
|
||
// Arrays to hold folders and non-folder items | ||
const folders: ItemGroup<Item>[] = [] | ||
const nonFolderItems: Item[] = [] | ||
|
||
// Iterate over the collection items to categorize them | ||
collection.items.each(item => { | ||
if (ItemGroup.isItemGroup(item)) { | ||
folders.push(item as ItemGroup<Item>) | ||
} else { | ||
nonFolderItems.push(item as Item) | ||
} | ||
}) | ||
|
||
// Create a map for quick lookup of folder indexes by name | ||
const folderMap = new Map(folders.map((folder, index) => [folder.name, index])) | ||
|
||
// Sort folders based on the order provided | ||
const sortedFolders = orderOfFolders | ||
.map(name => folderMap.get(name)) // get the index of the folder by name | ||
.filter(index => index !== undefined) // filter out undefined (non-existent folders in the order list) | ||
.map(index => folders[index] as ItemGroup<Item>) // map to the actual folder items | ||
|
||
// Append remaining folders that were not in the orderOfFolders list | ||
const remainingFolders = folders.filter(folder => !orderOfFolders.includes(folder.name)) | ||
|
||
// Combine the sorted and remaining folders | ||
const orderedFolders = [...sortedFolders, ...remainingFolders] | ||
|
||
// Clear the collection items | ||
collection.items.clear() | ||
|
||
// Add the sorted folders back to the collection | ||
orderedFolders.forEach(folder => collection.items.add(folder)) | ||
|
||
// Append the non-folder root items back to the collection | ||
nonFolderItems.forEach(item => collection.items.add(item)) | ||
|
||
const collectionObj = JSON.parse(JSON.stringify(collection)) | ||
return collectionObj | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters