nitro icon indicating copy to clipboard operation
nitro copied to clipboard

feat(openAPI): allow hiding routes

Open horvbalint opened this issue 1 year ago โ€ข 8 comments

๐Ÿ”— Linked issue

#2889

โ“ Type of change

  • [ ] ๐Ÿ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • [ ] ๐Ÿž Bug fix (a non-breaking change that fixes an issue)
  • [ ] ๐Ÿ‘Œ Enhancement (improving an existing functionality like performance)
  • [x] โœจ New feature (a non-breaking change that adds functionality)
  • [ ] ๐Ÿงน Chore (updates to the build process or auxiliary tools and libraries)
  • [ ] โš ๏ธ Breaking change (fix or feature that would cause existing functionality to change)

๐Ÿ“š Description

This PR adds a new property openAPIEnabled to 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:

export default defineNitroConfig({
  experimental: {
    openAPI: true,
  },
  routeRules: {
    "**": {
      openAPIEnabled: false,
    },
    "/api/public/**": {
      openAPIEnabled: true,
    },
  },
});

It is also possible to override this rule using the routes meta, like:

// /api/public/exceptionRoute.ts

defineRouteMeta({
  openAPIEnabled: false
})

๐Ÿ“ Checklist

  • [x] I have linked an issue or discussion.
  • [x] I have updated the documentation accordingly.

horvbalint avatar Nov 18 '24 13:11 horvbalint

hey, @horvbalint what's the progress on this? I looked at the checks and the error don't seem to have anything to do with the additon of the property.

xMaxximum avatar Dec 30 '24 22:12 xMaxximum

hey, @horvbalint what's the progress on this? I looked at the checks and the error don't seem to have anything to do with the additon of the property.

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 :/

horvbalint avatar Dec 31 '24 08:12 horvbalint

hey, @horvbalint what's the progress on this? I looked at the checks and the error don't seem to have anything to do with the additon of the property.

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 :/

Hello @pi0, could we get this merged please?

xMaxximum avatar Dec 31 '24 11:12 xMaxximum

Expecting to be able to merge secondary PRs, a lot of the OpenAPI generated by the current code is not needed

CleanShot 2025-05-21 at 19 57 03@2x CleanShot 2025-05-21 at 19 58 21@2x

miantiao-me avatar May 21 '25 11:05 miantiao-me

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];
      });
    }
  });
});

bitbytebit1 avatar Jun 16 '25 12:06 bitbytebit1