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 2 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
5 changes: 4 additions & 1 deletion docs/1.guide/2.routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,14 @@ Returning anything from a middleware will close the request and should be avoide

### Route Meta

You can define route handler meta at build-time using `defineRouteMeta` micro in the event handler files.
You can define route handler meta at build-time using `defineRouteMeta` macro in the event handler files.

> [!IMPORTANT]
> 🚧 This feature is currently experimental.

```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 @@ -361,6 +361,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
20 changes: 18 additions & 2 deletions src/runtime/internal/routes/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import type {
import { joinURL } from "ufo";
import { handlersMeta } from "#nitro-internal-virtual/server-handlers-meta";
import { useRuntimeConfig } from "../config";
import { createRouter as createRadixRouter, toRouteMatcher } from "radix3";
import type { NitroRouteRules, NitroRuntimeConfig } from "nitropack/types";
import defu from "defu";

// Served as /_openapi.json
export default eventHandler((event) => {
Expand Down Expand Up @@ -36,14 +39,27 @@ export default eventHandler((event) => {
variables: {},
},
],
paths: getPaths(),
paths: getPaths(runtimeConfig),
};
});

function getPaths(): PathsObject {
function getPaths(runtimeConfig: NitroRuntimeConfig): PathsObject {
const paths: PathsObject = {};

const _routeRulesMatcher = toRouteMatcher(
createRadixRouter({ routes: runtimeConfig.nitro.routeRules })
);

const _getRouteRules = (path: string) =>
defu({}, ..._routeRulesMatcher.matchAll(path).reverse()) as NitroRouteRules;

for (const h of handlersMeta) {
const enabledInRouteMeta = h.meta && h.meta.openAPIEnabled;
const enabledInRouteRules =
h.route && _getRouteRules(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 @@ -7,6 +7,7 @@ type MaybeArray<T> = T | T[];
/** @exprerimental */
export interface NitroRouteMeta {
openAPI?: OperationObject;
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