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: Add openAPIEnabled property to route metas and route rules #2890

Open
wants to merge 5 commits into
base: v2
Choose a base branch
from
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 @@ -212,6 +212,7 @@ You can define route handler meta at build-time using `defineRouteMeta` macro in

```ts [/api/test.ts]
defineRouteMeta({
openAPIEnabled: false, // defaults to true
openAPI: {
tags: ["test"],
description: "Test route description",
Expand Down Expand Up @@ -298,6 +299,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 @@ -314,6 +316,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 @@ -379,6 +379,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 @@ -55,6 +56,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 @@ -9,6 +9,7 @@ export interface NitroRouteMeta {
openAPI?: OperationObject & {
$global: Pick<OpenAPI3, "components">;
};
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