Skip to content

Commit ec144d1

Browse files
authored
fix(server-routes): improve filterByCollection for runtime routes (#538)
1 parent a0d48aa commit ec144d1

File tree

1 file changed

+39
-47
lines changed

1 file changed

+39
-47
lines changed

packages/devtools/client/pages/modules/server-routes.vue

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -57,65 +57,57 @@ const filterByCollection = computed(() => {
5757
collection.routes.push(route)
5858
}
5959
60-
filtered.value.forEach((item) => {
61-
const filepathParts = item.filepath.split('/')
62-
const collectionNames = filepathParts.slice(filepathParts.indexOf('server') + 1)
60+
const findOrCreateCollection = (routeName: string, parentCollection?: ServerRouteInfo) => {
61+
const existingCollection = parentCollection
62+
? parentCollection.routes?.find(r => r.route === routeName)
63+
: collections.find(c => c.route === routeName)
64+
65+
if (existingCollection)
66+
return existingCollection
67+
68+
const newCollection: ServerRouteInfo = {
69+
route: routeName,
70+
filepath: routeName.replace(/\W/g, '-').toLowerCase(),
71+
type: 'collection',
72+
routes: [],
73+
}
6374
64-
if (collectionNames.length > 0 && collectionNames[collectionNames.length - 1].includes('.'))
65-
collectionNames.pop()
75+
if (parentCollection)
76+
addRouteToCollection(parentCollection, newCollection)
6677
67-
let parentCollection: ServerRouteInfo | null = null
68-
collectionNames.forEach((collectionName) => {
69-
const existingCollection = parentCollection
70-
? parentCollection.routes?.find(r => r.route === collectionName)
71-
: collections.find(c => c.route === collectionName)
78+
else
79+
collections.push(newCollection)
7280
73-
if (existingCollection) {
74-
parentCollection = existingCollection
75-
}
76-
else {
77-
const newCollection: ServerRouteInfo = {
78-
route: collectionName,
79-
filepath: collectionName.replace(/\W/g, '-').toLowerCase(),
80-
type: 'collection',
81-
routes: [],
82-
}
83-
84-
if (parentCollection)
85-
addRouteToCollection(parentCollection, newCollection)
81+
return newCollection
82+
}
8683
87-
else
88-
collections.push(newCollection)
84+
filtered.value.forEach((item) => {
85+
let prefix: string | undefined
86+
let parentCollection: ServerRouteInfo | undefined
8987
90-
parentCollection = newCollection
91-
}
92-
})
88+
const filepathParts = item.filepath.split('/')
89+
const collectionNames = filepathParts.slice(filepathParts.indexOf('server') + 1)
9390
9491
if (item.type === 'runtime') {
95-
const runtimeCollection = collections.find(c => c.route === 'runtime')
96-
97-
if (runtimeCollection) {
98-
addRouteToCollection(runtimeCollection, item)
99-
}
100-
else {
101-
const newCollection: ServerRouteInfo = {
102-
route: 'runtime',
103-
filepath: 'runtime',
104-
type: 'collection',
105-
routes: [item],
106-
}
107-
108-
collections.push(newCollection)
92+
collectionNames[0] = 'runtime'
93+
const indexOfDist = filepathParts.indexOf('dist')
94+
if (indexOfDist !== -1) {
95+
prefix = filepathParts[indexOfDist - 1]
96+
prefix && collectionNames.splice(1, 0, prefix)
10997
}
11098
}
11199
112-
else if (parentCollection) {
113-
addRouteToCollection(parentCollection, item)
114-
}
100+
if (collectionNames.length > 0 && collectionNames[collectionNames.length - 1].includes('.'))
101+
collectionNames.pop()
102+
103+
collectionNames.forEach((collectionName) => {
104+
parentCollection = findOrCreateCollection(collectionName, parentCollection)
105+
})
115106
116-
else {
107+
if (parentCollection)
108+
addRouteToCollection(parentCollection, item)
109+
else
117110
collections.push(item)
118-
}
119111
})
120112
121113
return collections

0 commit comments

Comments
 (0)