From aff41ae738b1be51580075cb3b0820484bdf1b3a Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Wed, 18 Sep 2024 14:21:31 +0200 Subject: [PATCH] Avoid generic fn names, use simple map functions to set route/namespace deprecated/undocumented status --- src/lib/blueprint.ts | 79 +++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 52 deletions(-) diff --git a/src/lib/blueprint.ts b/src/lib/blueprint.ts index f58126c..6131bf2 100644 --- a/src/lib/blueprint.ts +++ b/src/lib/blueprint.ts @@ -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 => { @@ -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>( - (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,