feat(openAPI): allow hiding routes#2890
Conversation
This comment was marked as off-topic.
This comment was marked as off-topic.
Hi, the PR is ready for review I think. The error in the checks dont seem to come from my changes, but I had no time to investigate it :/ |
This comment was marked as off-topic.
This comment was marked as off-topic.
|
I was also looking for this feature. In the mean time I achieved the same using a nitro plugin: export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook("beforeResponse", (event, { body }) => {
if (event.path === "/openapi.json") {
const entriesToDelete = [
"/openapi.json",
"/_docs/scalar",
"/",
];
entriesToDelete.forEach((entry) => {
delete body.paths[entry];
});
}
});
}); |
|
@pi0 is attempting to deploy a commit to the Nitro Team on Vercel. A member of the Team first needs to authorize it. |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
|
My fully typed version adapted from @bitbytebit1 : import { defineNitroPlugin, useRuntimeConfig } from 'nitropack/runtime'
import { createRouter, toRouteMatcher } from 'radix3'
declare module 'nitropack/types' {
interface NitroOpenAPIConfig {
allow?: string[]
}
}
export default defineNitroPlugin((nitroApp) => {
const openapi = useRuntimeConfig()?.nitro?.openAPI
const endpoint = openapi?.route || '/_openapi.json'
const allow: string[] = Array.isArray(openapi?.allow) ? openapi.allow : []
const router = createRouter({
routes: Object.fromEntries(allow.map(p => [p, { ok: true }]))
})
const matcher = toRouteMatcher(router)
nitroApp.hooks.hook('beforeResponse', (event, response) => {
const body = response.body as { paths?: Record<string, unknown> }
if (event.path !== endpoint) return
if (!body?.paths) return
for (const p of Object.keys(body.paths)) {
// If allowlist is empty => keep everything
if (allow.length && matcher.matchAll(p).length === 0) {
delete body.paths?.[p]
}
}
})
})Usage on export default defineNuxtConfig({
nitro: {
openAPI: {
allow: [
'/api/**'
],
}
}
}) |


🔗 Linked issue
#2889
❓ Type of change
📚 Description
This PR adds a new property
openAPIEnabledto the NitroRouteMeta and NitroRouteConfig types. Using this property one can include/exclude specific routes in the generated_openapi.json.To only include a part of the backend one can do for example:
It is also possible to override this rule using the routes meta, like:
📝 Checklist