Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions docs/1.guide/2.routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ You can define route handler meta at build-time using `defineRouteMeta` macro in

```ts [server/api/test.ts]
defineRouteMeta({
openAPIEnabled: false, // defaults to true
openAPI: {
tags: ["test"],
description: "Test route description",
Expand Down Expand Up @@ -294,6 +295,7 @@ export default defineNitroConfig({
'/blog/**': { cache: { /* cache options*/ } },
'/assets/**': { headers: { 'cache-control': 's-maxage=0' } },
'/api/v1/**': { cors: true, headers: { 'access-control-allow-methods': 'GET' } },
'/api/v1/admin/**': { openAPIEnabled: false },
'/old-page': { redirect: '/new-page' },
'/old-page/**': { redirect: '/new-page/**' },
'/proxy/example': { proxy: 'https://example.com' },
Expand All @@ -310,6 +312,7 @@ export default defineNuxtConfig({
'/blog/**': { cache: { /* cache options*/ } },
'/assets/**': { headers: { 'cache-control': 's-maxage=0' } },
'/api/v1/**': { cors: true, headers: { 'access-control-allow-methods': 'GET' } },
'/api/v1/admin/**': { openAPIEnabled: false },
'/old-page': { redirect: '/new-page' },
'/old-page/**': { redirect: '/new-page/**' },
'/proxy/example': { proxy: 'https://example.com' },
Expand Down
1 change: 1 addition & 0 deletions docs/3.config/0.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ routeRules: {
'/blog/**': { cache: { /* cache options*/ } },
'/assets/**': { headers: { 'cache-control': 's-maxage=0' } },
'/api/v1/**': { cors: true, headers: { 'access-control-allow-methods': 'GET' } },
'/api/v1/admin/**': { openAPIEnabled: false },
'/old-page': { redirect: '/new-page' }, // uses status code 307 (Temporary Redirect)
'/old-page2': { redirect: { to:'/new-page2', statusCode: 301 } },
'/old-page/**': { redirect: '/new-page/**' },
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/internal/routes/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { joinURL } from "ufo";
import { defu } from "defu";
import { handlersMeta } from "#nitro-internal-virtual/server-handlers-meta";
import { useRuntimeConfig } from "../config";
import { getRouteRulesForPath } from "../route-rules";

// Served as /_openapi.json
export default eventHandler((event) => {
Expand Down Expand Up @@ -63,6 +64,12 @@ function getHandlersMeta(): {
let globals: OpenAPIGlobals = {};

for (const h of handlersMeta) {
const enabledInRouteMeta = h.meta && h.meta.openAPIEnabled;
const enabledInRouteRules =
h.route && getRouteRulesForPath(h.route).openAPIEnabled;
const openAPIEnabled = enabledInRouteMeta ?? enabledInRouteRules;
if (openAPIEnabled === false) continue;

const { route, parameters } = normalizeRoute(h.route || "");
const tags = defaultTags(h.route || "");
const method = (h.method || "get").toLowerCase() as Lowercase<HTTPMethod>;
Expand Down
1 change: 1 addition & 0 deletions src/types/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface NitroRouteMeta {
openAPI?: OperationObject & {
$global?: Pick<OpenAPI3, "components"> & Extensable;
};
openAPIEnabled?: boolean;
}

export interface NitroEventHandler {
Expand Down
1 change: 1 addition & 0 deletions src/types/route-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface NitroRouteConfig {
prerender?: boolean;
proxy?: string | ({ to: string } & ProxyOptions);
isr?: number /* expiration */ | boolean | VercelISRConfig;
openAPIEnabled?: boolean;

// Shortcuts
cors?: boolean;
Expand Down
Loading