Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dynamically detect namespace #83

Merged
merged 10 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 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,42 @@ const createRoutes = async (
return Array.from(routeMap.values())
}

const getNamespace = (path: string, paths: OpenapiPaths): string | null => {
// Hold namespace in array to allow nested namespaces
// e.g. namespace for `/foo/bar/baz/get` = `/foo/bar`
const namespace: string[] = []

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

const pathKeys = Object.keys(paths)

for (const [index, part] of pathParts.entries()) {
// Namespaces must be consecutive. If there was a path with an endpoint
// previously, then this part is not in the namespace.
if (namespace.length !== index) {
continue
}

// An endpoint is a route that ends without further paths. i.e., ends in
// a letter (not slash).
const endpoints = pathKeys.filter((key) =>
new RegExp(`^/${[...namespace, part].join('/')}/\\w+$`).test(key),
)

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

if (namespace.length === 0) {
return null
}

return `/${namespace.join('/')}`
}

const createRoute = async (
namespace: string | null,
path: string,
pathItem: OpenapiPathItem,
context: Context,
Expand All @@ -314,7 +350,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
8 changes: 2 additions & 6 deletions test/snapshots/blueprint.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,7 @@ Generated by [AVA](https://avajs.dev).
},
],
name: 'foos',
namespace: {
path: '/foos',
},
namespace: null,
path: '/foos',
subroutes: [],
},
Expand Down Expand Up @@ -836,9 +834,7 @@ Generated by [AVA](https://avajs.dev).
},
],
name: 'foos',
namespace: {
path: '/foos',
},
namespace: null,
path: '/foos',
subroutes: [],
},
Expand Down
Binary file modified test/snapshots/blueprint.test.ts.snap
Binary file not shown.