Skip to content

Commit

Permalink
Avoid generic fn names, use simple map functions to set route/namespa…
Browse files Browse the repository at this point in the history
…ce deprecated/undocumented status
  • Loading branch information
andrii-balitskyi committed Sep 18, 2024
1 parent f3bff56 commit aff41ae
Showing 1 changed file with 27 additions and 52 deletions.
79 changes: 27 additions & 52 deletions src/lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,12 @@ const createRoutes = async (
}
}

let routes = Array.from(routeMap.values())

routes = updateRoutesStatus(routes)
routes = updateNamespaceStatus(routes)
const routes = Array.from(routeMap.values())

return routes
.map(addIsDeprecatedToRoute)
.map(addIsUndocumentedToRoute)
.map(addNamespaceStatusToRoute(routes))
}

const getNamespace = (path: string, paths: OpenapiPaths): string | null => {
Expand Down Expand Up @@ -386,62 +386,37 @@ const createRoute = async (
}
}

const updateRoutesStatus = (routes: Route[]): Route[] => {
return routes.map((route) => {
const isRouteDeprecated = route.endpoints.every(
(endpoint) => endpoint.isDeprecated,
)
const isRouteUndocumented = route.endpoints.every(
(endpoint) => endpoint.isUndocumented,
)

return {
...route,
isDeprecated: isRouteDeprecated,
isUndocumented: isRouteUndocumented,
}
})
}

const updateNamespaceStatus = (routes: Route[]): Route[] => {
// Group routes by namespace
const namespaceGroups = routes.reduce<Record<string, Route[]>>(
(acc, route) => {
if (route.namespace?.path == null) return acc

const namespacePath = route.namespace.path

if (acc[namespacePath] == null) {
acc[namespacePath] = []
}
const addIsDeprecatedToRoute = (route: Route): Route => ({
...route,
isDeprecated: route.endpoints.every((endpoint) => endpoint.isDeprecated),
})

acc[namespacePath].push(route)
const addIsUndocumentedToRoute = (route: Route): Route => ({
...route,
isUndocumented: route.endpoints.every((endpoint) => endpoint.isUndocumented),
})

return acc
},
{},
)
const addNamespaceStatusToRoute =
(routes: Route[]) =>
(route: Route): Route => {
if (route.namespace == null) return route

// Update namespace status based on routes
for (const routesInNamespace of Object.values(namespaceGroups)) {
const isNamespaceDeprecated = routesInNamespace.every(
(route) => route.isDeprecated,
)
const isNamespaceUndocumented = routesInNamespace.every(
(route) => route.isUndocumented,
const namespaceRoutes = routes.filter(
(r) => r.namespace?.path === route.namespace?.path,
)
const isDeprecated = namespaceRoutes.every((r) => r.isDeprecated)
const isUndocumented = namespaceRoutes.every((r) => r.isUndocumented)

for (const route of routesInNamespace) {
if (route.namespace != null) {
route.namespace.isDeprecated = isNamespaceDeprecated
route.namespace.isUndocumented = isNamespaceUndocumented
}
return {
...route,
namespace: {
...route.namespace,
isDeprecated,
isUndocumented,
},
}
}

return routes
}

const createEndpoints = async (
path: string,
pathItem: OpenapiPathItem,
Expand Down

0 comments on commit aff41ae

Please sign in to comment.