Skip to content

Commit

Permalink
Add openAPIEnabled property to route metas and route rules
Browse files Browse the repository at this point in the history
  • Loading branch information
horvbalint committed Nov 18, 2024
1 parent 9563e9a commit 704995c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
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

0 comments on commit 704995c

Please sign in to comment.