Skip to content

Commit

Permalink
feat: dynamically detect namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewuu committed Sep 10, 2024
1 parent ad52671 commit d764c1a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 361 deletions.
35 changes: 33 additions & 2 deletions src/lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ const createRoutes = async (
)

for (const [path, pathItem] of pathEntries) {
const route = await createRoute(path, pathItem, context)
const namespace = getNamespace(path, paths)
const route = await createRoute(namespace, path, pathItem, context)

const existingRoute = routeMap.get(route.path)
if (existingRoute != null) {
Expand All @@ -299,7 +300,37 @@ const createRoutes = async (
return Array.from(routeMap.values())
}

const getNamespace = (path: string, paths: OpenapiPaths): string | null => {
const namespace: string[] = []

const pathParts = path.split('/').filter((path) => Boolean(path))

const pathKeys = Object.keys(paths)
// console.log('keys: ', pathKeys)

for (const [index, part] of pathParts.entries()) {
if (namespace.length !== index) {
continue
}

const children = pathKeys.filter((key) =>
new RegExp(`^/${[...namespace, part].join('/')}/\\w+$`).test(key),
)

if (children.length === 0) {
namespace.push(part)
}
}

if (namespace.length > 0) {
return `/${namespace.join('/')}`
}

return null
}

const createRoute = async (
namespace: string | null,
path: string,
pathItem: OpenapiPathItem,
context: Context,
Expand All @@ -314,7 +345,7 @@ const createRoute = async (
return {
path: routePath,
name,
namespace: { path: `/${pathParts[1]}` },
namespace: namespace != null ? { path: namespace } : null,
endpoints: await createEndpoints(path, pathItem, context),
subroutes: [],
}
Expand Down
Loading

0 comments on commit d764c1a

Please sign in to comment.