From 91779690c77f213b453ff4e47fdb3b903a9189f4 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 28 Jun 2024 14:22:32 +0200 Subject: [PATCH 01/10] feat(open-api): optional production json route --- src/core/config/resolvers/open-api.ts | 26 +++++++++++-- src/types/config.ts | 13 +------ src/types/openapi.ts | 55 +++++++++++++++++++++++++++ test/fixture/nitro.config.ts | 1 + 4 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 src/types/openapi.ts diff --git a/src/core/config/resolvers/open-api.ts b/src/core/config/resolvers/open-api.ts index 88fb464030..880ecda343 100644 --- a/src/core/config/resolvers/open-api.ts +++ b/src/core/config/resolvers/open-api.ts @@ -3,17 +3,35 @@ import type { NitroOptions } from "nitropack/types"; import { join } from "pathe"; export async function resolveOpenAPIOptions(options: NitroOptions) { - if (options.dev && options.experimental.openAPI) { + // Check if the experimental.openAPI option is enabled + if (!options.experimental.openAPI) { + return; + } + + // Add openapi json route + if (options.dev || options.openAPI?.production) { + const shouldPrerender = !options.dev && options.openAPI?.production === 'prerender'; + const route = options.openAPI?.route || "/_nitro/openapi.json" options.handlers.push({ - route: "/_nitro/openapi.json", + route, handler: join(runtimeDir, "internal/routes/openapi"), + env: shouldPrerender ? 'prerender' : undefined /* runtime */ }); + if (shouldPrerender) { + options.prerender ??= {} as any; + options.prerender.routes ??= []; + options.prerender.routes.push(route); + } + } + + // Add /_nitro/scalar and /_nitro/swagger routes in dev mode + if (options.dev) { options.handlers.push({ - route: "/_nitro/scalar", + route: options.openAPI?.ui?.scalar?.route || "/_nitro/scalar", handler: join(runtimeDir, "internal/routes/scalar"), }); options.handlers.push({ - route: "/_nitro/swagger", + route: options.openAPI?.ui?.swagger?.route ?? "/_nitro/swagger", handler: join(runtimeDir, "internal/routes/swagger"), }); } diff --git a/src/types/config.ts b/src/types/config.ts index 686258fe57..35f93b1e81 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -1,5 +1,4 @@ import type { RollupCommonJSOptions } from "@rollup/plugin-commonjs"; -import type { ReferenceConfiguration } from "@scalar/api-reference"; import type { C12InputConfig, ConfigWatcher, ResolvedConfig } from "c12"; import type { WatchConfigOptions } from "c12"; import type { WatchOptions } from "chokidar"; @@ -37,6 +36,7 @@ import type { NitroPreset } from "./preset"; import type { EsbuildOptions, NodeExternalsOptions } from "./rollup"; import type { RollupConfig } from "./rollup"; import type { NitroRouteConfig, NitroRouteRules } from "./route-rules"; +import type { NitroOpenAPIConfig } from "./openapi"; /** * Nitro normalized options (nitro.options) @@ -92,16 +92,7 @@ export interface NitroOptions extends PresetOptions { * @see https://github.com/unjs/unwasm */ wasm?: UnwasmPluginOptions; - openAPI?: { - meta?: { - title?: string; - description?: string; - version?: string; - }; - ui?: { - scalar?: ReferenceConfiguration; - }; - }; + openAPI?: NitroOpenAPIConfig; experimental: { legacyExternals?: boolean; openAPI?: boolean; diff --git a/src/types/openapi.ts b/src/types/openapi.ts new file mode 100644 index 0000000000..184cf684b3 --- /dev/null +++ b/src/types/openapi.ts @@ -0,0 +1,55 @@ +import type { ReferenceConfiguration as ScalarConfig } from "@scalar/api-reference"; + +/** + * Nitro OpenAPI configuration + */ +export interface NitroOpenAPIConfig { + /** + * OpenAPI meta information + */ + meta?: { + title?: string; + description?: string; + version?: string; + }; + + /** + * OpenAPI json route + * + * Default is `/_nitro/openapi.json` + */ + route?: string; + + /** + * Enable OpenAPI generation for production builds + */ + production?: false | "runtime" | "prerender" + + /** + * UI configurations + */ + ui?: { + /** + * Scalar UI configuration + */ + scalar?: ScalarConfig & { + /** + * Scalar UI route + * + * Default is `_nitro/scalar` + */ + route?: string; + }; + /** + * Swagger UI configuration + */ + swagger?: { + /** + * Swagger UI route + * + * Default is `_nitro/swagger` + */ + route?: string; + } + }; +} diff --git a/test/fixture/nitro.config.ts b/test/fixture/nitro.config.ts index e6b9153de2..4a01cb32d4 100644 --- a/test/fixture/nitro.config.ts +++ b/test/fixture/nitro.config.ts @@ -118,6 +118,7 @@ export default defineNitroConfig({ }, }, openAPI: { + production: "prerender", meta: { title: "Nitro Test Fixture", description: "Nitro Test Fixture API", From d4b606402173402bb8af2402ea796c640f419234 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 28 Jun 2024 14:49:50 +0200 Subject: [PATCH 02/10] update --- src/core/config/resolvers/open-api.ts | 55 ++++++++++++++++++--------- src/types/config.ts | 2 +- src/types/openapi.ts | 38 +++++++++--------- test/fixture/middleware/config.ts | 1 + 4 files changed, 61 insertions(+), 35 deletions(-) diff --git a/src/core/config/resolvers/open-api.ts b/src/core/config/resolvers/open-api.ts index 880ecda343..761c9db34a 100644 --- a/src/core/config/resolvers/open-api.ts +++ b/src/core/config/resolvers/open-api.ts @@ -8,31 +8,52 @@ export async function resolveOpenAPIOptions(options: NitroOptions) { return; } - // Add openapi json route - if (options.dev || options.openAPI?.production) { - const shouldPrerender = !options.dev && options.openAPI?.production === 'prerender'; - const route = options.openAPI?.route || "/_nitro/openapi.json" - options.handlers.push({ - route, - handler: join(runtimeDir, "internal/routes/openapi"), - env: shouldPrerender ? 'prerender' : undefined /* runtime */ - }); - if (shouldPrerender) { - options.prerender ??= {} as any; - options.prerender.routes ??= []; - options.prerender.routes.push(route); - } + // Only enable for dev and (opt-in) production + if (!options.dev && !options.openAPI?.production) { + return; } - // Add /_nitro/scalar and /_nitro/swagger routes in dev mode - if (options.dev) { + const shouldPrerender = + !options.dev && options.openAPI?.production === "prerender"; + const handlersEnv = shouldPrerender ? "prerender" : ""; + const prerenderRoutes: string[] = []; + + // Add openapi json route + const jsonRoute = options.openAPI?.route || "/_nitro/openapi.json"; + prerenderRoutes.push(jsonRoute); + options.handlers.push({ + route: jsonRoute, + env: handlersEnv, + handler: join(runtimeDir, "internal/routes/openapi"), + }); + + // Scalar UI + if (options.openAPI?.ui?.scalar !== false) { + const scalarRoute = options.openAPI?.ui?.scalar?.route || "/_nitro/scalar"; + prerenderRoutes.push(scalarRoute); options.handlers.push({ route: options.openAPI?.ui?.scalar?.route || "/_nitro/scalar", + env: handlersEnv, handler: join(runtimeDir, "internal/routes/scalar"), }); + } + + // Swagger UI + if (options.openAPI?.ui?.swagger !== false) { + const swaggerRoute = + options.openAPI?.ui?.swagger?.route || "/_nitro/swagger"; + prerenderRoutes.push(swaggerRoute); options.handlers.push({ - route: options.openAPI?.ui?.swagger?.route ?? "/_nitro/swagger", + route: swaggerRoute, + env: handlersEnv, handler: join(runtimeDir, "internal/routes/swagger"), }); } + + // Prerender + if (shouldPrerender) { + options.prerender ??= {} as any; + options.prerender.routes ??= []; + options.prerender.routes.push(...prerenderRoutes); + } } diff --git a/src/types/config.ts b/src/types/config.ts index 35f93b1e81..c05a520783 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -32,11 +32,11 @@ import type { import type { NitroHooks } from "./hooks"; import type { NitroModuleInput } from "./module"; import type { NitroFrameworkInfo } from "./nitro"; +import type { NitroOpenAPIConfig } from "./openapi"; import type { NitroPreset } from "./preset"; import type { EsbuildOptions, NodeExternalsOptions } from "./rollup"; import type { RollupConfig } from "./rollup"; import type { NitroRouteConfig, NitroRouteRules } from "./route-rules"; -import type { NitroOpenAPIConfig } from "./openapi"; /** * Nitro normalized options (nitro.options) diff --git a/src/types/openapi.ts b/src/types/openapi.ts index 184cf684b3..8d66be78c2 100644 --- a/src/types/openapi.ts +++ b/src/types/openapi.ts @@ -23,7 +23,7 @@ export interface NitroOpenAPIConfig { /** * Enable OpenAPI generation for production builds */ - production?: false | "runtime" | "prerender" + production?: false | "runtime" | "prerender"; /** * UI configurations @@ -32,24 +32,28 @@ export interface NitroOpenAPIConfig { /** * Scalar UI configuration */ - scalar?: ScalarConfig & { - /** - * Scalar UI route - * - * Default is `_nitro/scalar` - */ - route?: string; - }; + scalar?: + | false + | (ScalarConfig & { + /** + * Scalar UI route + * + * Default is `/_nitro/scalar` + */ + route?: string; + }); /** * Swagger UI configuration */ - swagger?: { - /** - * Swagger UI route - * - * Default is `_nitro/swagger` - */ - route?: string; - } + swagger?: + | false + | { + /** + * Swagger UI route + * + * Default is `/_nitro/swagger` + */ + route?: string; + }; }; } diff --git a/test/fixture/middleware/config.ts b/test/fixture/middleware/config.ts index 28158e4dad..4fadbbfd42 100644 --- a/test/fixture/middleware/config.ts +++ b/test/fixture/middleware/config.ts @@ -1,6 +1,7 @@ process.env.NITRO_DYNAMIC = "from-env"; export default eventHandler((event) => { + console.log('>>', event.path) const appConfig = useAppConfig(event); appConfig.dynamic = "from-middleware"; }); From 5c32b22f5f7f21723b2df5dedf450bbd8f297528 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 28 Jun 2024 14:50:01 +0200 Subject: [PATCH 03/10] lint --- test/fixture/middleware/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fixture/middleware/config.ts b/test/fixture/middleware/config.ts index 4fadbbfd42..d5b62de465 100644 --- a/test/fixture/middleware/config.ts +++ b/test/fixture/middleware/config.ts @@ -1,7 +1,7 @@ process.env.NITRO_DYNAMIC = "from-env"; export default eventHandler((event) => { - console.log('>>', event.path) + console.log(">>", event.path); const appConfig = useAppConfig(event); appConfig.dynamic = "from-middleware"; }); From 3c11ca87e9383e975b4baab925b998131639d30d Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 28 Jun 2024 14:51:15 +0200 Subject: [PATCH 04/10] remove log --- test/fixture/middleware/config.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/fixture/middleware/config.ts b/test/fixture/middleware/config.ts index d5b62de465..28158e4dad 100644 --- a/test/fixture/middleware/config.ts +++ b/test/fixture/middleware/config.ts @@ -1,7 +1,6 @@ process.env.NITRO_DYNAMIC = "from-env"; export default eventHandler((event) => { - console.log(">>", event.path); const appConfig = useAppConfig(event); appConfig.dynamic = "from-middleware"; }); From a952fcb7fa53f2bf7085ee55885bee0c29f030a9 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 28 Jun 2024 14:54:46 +0200 Subject: [PATCH 05/10] simplify routes --- src/core/config/resolvers/open-api.ts | 14 ++++++++------ src/runtime/internal/routes/openapi.ts | 2 +- src/runtime/internal/routes/scalar.ts | 4 ++-- src/runtime/internal/routes/swagger.ts | 2 +- src/types/openapi.ts | 6 +++--- test/presets/nitro-dev.test.ts | 4 ++-- 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/core/config/resolvers/open-api.ts b/src/core/config/resolvers/open-api.ts index 761c9db34a..973a92e445 100644 --- a/src/core/config/resolvers/open-api.ts +++ b/src/core/config/resolvers/open-api.ts @@ -15,11 +15,13 @@ export async function resolveOpenAPIOptions(options: NitroOptions) { const shouldPrerender = !options.dev && options.openAPI?.production === "prerender"; - const handlersEnv = shouldPrerender ? "prerender" : ""; - const prerenderRoutes: string[] = []; + + const handlersEnv = shouldPrerender ? "prerender" : ""; + + const prerenderRoutes: string[] = []; // Add openapi json route - const jsonRoute = options.openAPI?.route || "/_nitro/openapi.json"; + const jsonRoute = options.openAPI?.route || "/_openapi.json"; prerenderRoutes.push(jsonRoute); options.handlers.push({ route: jsonRoute, @@ -29,10 +31,10 @@ export async function resolveOpenAPIOptions(options: NitroOptions) { // Scalar UI if (options.openAPI?.ui?.scalar !== false) { - const scalarRoute = options.openAPI?.ui?.scalar?.route || "/_nitro/scalar"; + const scalarRoute = options.openAPI?.ui?.scalar?.route || "/_scalar"; prerenderRoutes.push(scalarRoute); options.handlers.push({ - route: options.openAPI?.ui?.scalar?.route || "/_nitro/scalar", + route: options.openAPI?.ui?.scalar?.route || "/_scalar", env: handlersEnv, handler: join(runtimeDir, "internal/routes/scalar"), }); @@ -41,7 +43,7 @@ export async function resolveOpenAPIOptions(options: NitroOptions) { // Swagger UI if (options.openAPI?.ui?.swagger !== false) { const swaggerRoute = - options.openAPI?.ui?.swagger?.route || "/_nitro/swagger"; + options.openAPI?.ui?.swagger?.route || "/_swagger"; prerenderRoutes.push(swaggerRoute); options.handlers.push({ route: swaggerRoute, diff --git a/src/runtime/internal/routes/openapi.ts b/src/runtime/internal/routes/openapi.ts index b4a9b21b89..8aa4b7f4ae 100644 --- a/src/runtime/internal/routes/openapi.ts +++ b/src/runtime/internal/routes/openapi.ts @@ -10,7 +10,7 @@ import { joinURL } from "ufo"; import { handlersMeta } from "#nitro-internal-virtual/server-handlers-meta"; import { useRuntimeConfig } from "../config"; -// Served as /_nitro/openapi.json +// Served as /_openapi.json export default eventHandler((event) => { const runtimeConfig = useRuntimeConfig(event); diff --git a/src/runtime/internal/routes/scalar.ts b/src/runtime/internal/routes/scalar.ts index f44563ee6a..5060c207a8 100644 --- a/src/runtime/internal/routes/scalar.ts +++ b/src/runtime/internal/routes/scalar.ts @@ -2,7 +2,7 @@ import type { ReferenceConfiguration } from "@scalar/api-reference"; import { eventHandler } from "h3"; import { useRuntimeConfig } from "../config"; -// Served as /_nitro/scalar +// Served as /_scalar export default eventHandler((event) => { const runtimeConfig = useRuntimeConfig(event); @@ -10,7 +10,7 @@ export default eventHandler((event) => { const configuration: ReferenceConfiguration = { ...runtimeConfig.nitro.openAPI?.ui?.scalar, spec: { - url: "/_nitro/openapi.json", + url: "/_openapi.json", ...runtimeConfig.nitro.openAPI?.ui?.scalar?.spec, }, }; diff --git a/src/runtime/internal/routes/swagger.ts b/src/runtime/internal/routes/swagger.ts index fa569b3e30..f89c66528e 100644 --- a/src/runtime/internal/routes/swagger.ts +++ b/src/runtime/internal/routes/swagger.ts @@ -2,7 +2,7 @@ import { eventHandler } from "h3"; // https://github.com/swagger-api/swagger-ui -// Served as /_nitro/swagger +// Served as /_swagger export default eventHandler((event) => { const title = "Nitro Swagger UI"; const CDN_BASE = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@^5"; diff --git a/src/types/openapi.ts b/src/types/openapi.ts index 8d66be78c2..6e232b8c7c 100644 --- a/src/types/openapi.ts +++ b/src/types/openapi.ts @@ -16,7 +16,7 @@ export interface NitroOpenAPIConfig { /** * OpenAPI json route * - * Default is `/_nitro/openapi.json` + * Default is `/_openapi.json` */ route?: string; @@ -38,7 +38,7 @@ export interface NitroOpenAPIConfig { /** * Scalar UI route * - * Default is `/_nitro/scalar` + * Default is `/_scalar` */ route?: string; }); @@ -51,7 +51,7 @@ export interface NitroOpenAPIConfig { /** * Swagger UI route * - * Default is `/_nitro/swagger` + * Default is `/_swagger` */ route?: string; }; diff --git a/test/presets/nitro-dev.test.ts b/test/presets/nitro-dev.test.ts index 0e98ed7c91..ab20fb4d3f 100644 --- a/test/presets/nitro-dev.test.ts +++ b/test/presets/nitro-dev.test.ts @@ -25,8 +25,8 @@ describe.skipIf(isCI)("nitro:preset:nitro-dev", async () => { describe("openAPI", () => { let spec: OpenAPI3; - it("/_nitro/openapi.json", async () => { - spec = ((await callHandler({ url: "/_nitro/openapi.json" })) as any) + it("/_openapi.json", async () => { + spec = ((await callHandler({ url: "/_openapi.json" })) as any) .data; expect(spec.openapi).to.match(/^3\.\d+\.\d+$/); expect(spec.info.title).toBe("Nitro Test Fixture"); From 66b082f0abc654feac72c8982f51742e9fcb3de6 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Fri, 28 Jun 2024 12:55:33 +0000 Subject: [PATCH 06/10] chore: apply automated updates --- src/core/config/resolvers/open-api.ts | 7 +++---- test/presets/nitro-dev.test.ts | 3 +-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/core/config/resolvers/open-api.ts b/src/core/config/resolvers/open-api.ts index 973a92e445..450b0e92cc 100644 --- a/src/core/config/resolvers/open-api.ts +++ b/src/core/config/resolvers/open-api.ts @@ -16,9 +16,9 @@ export async function resolveOpenAPIOptions(options: NitroOptions) { const shouldPrerender = !options.dev && options.openAPI?.production === "prerender"; - const handlersEnv = shouldPrerender ? "prerender" : ""; + const handlersEnv = shouldPrerender ? "prerender" : ""; - const prerenderRoutes: string[] = []; + const prerenderRoutes: string[] = []; // Add openapi json route const jsonRoute = options.openAPI?.route || "/_openapi.json"; @@ -42,8 +42,7 @@ export async function resolveOpenAPIOptions(options: NitroOptions) { // Swagger UI if (options.openAPI?.ui?.swagger !== false) { - const swaggerRoute = - options.openAPI?.ui?.swagger?.route || "/_swagger"; + const swaggerRoute = options.openAPI?.ui?.swagger?.route || "/_swagger"; prerenderRoutes.push(swaggerRoute); options.handlers.push({ route: swaggerRoute, diff --git a/test/presets/nitro-dev.test.ts b/test/presets/nitro-dev.test.ts index ab20fb4d3f..fb58b99510 100644 --- a/test/presets/nitro-dev.test.ts +++ b/test/presets/nitro-dev.test.ts @@ -26,8 +26,7 @@ describe.skipIf(isCI)("nitro:preset:nitro-dev", async () => { describe("openAPI", () => { let spec: OpenAPI3; it("/_openapi.json", async () => { - spec = ((await callHandler({ url: "/_openapi.json" })) as any) - .data; + spec = ((await callHandler({ url: "/_openapi.json" })) as any).data; expect(spec.openapi).to.match(/^3\.\d+\.\d+$/); expect(spec.info.title).toBe("Nitro Test Fixture"); expect(spec.info.description).toBe("Nitro Test Fixture API"); From 9692684501b071665231045b763cc73e6bdafbaf Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 28 Jun 2024 15:00:40 +0200 Subject: [PATCH 07/10] fix type issue --- src/runtime/internal/routes/scalar.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/internal/routes/scalar.ts b/src/runtime/internal/routes/scalar.ts index 5060c207a8..7bb19d5090 100644 --- a/src/runtime/internal/routes/scalar.ts +++ b/src/runtime/internal/routes/scalar.ts @@ -11,7 +11,7 @@ export default eventHandler((event) => { ...runtimeConfig.nitro.openAPI?.ui?.scalar, spec: { url: "/_openapi.json", - ...runtimeConfig.nitro.openAPI?.ui?.scalar?.spec, + ...(runtimeConfig.nitro.openAPI?.ui?.scalar as ReferenceConfiguration)?.spec, }, }; From ea0c8d516fa27c51673e68585e700214542a1e50 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Fri, 28 Jun 2024 13:01:31 +0000 Subject: [PATCH 08/10] chore: apply automated updates --- src/runtime/internal/routes/scalar.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/runtime/internal/routes/scalar.ts b/src/runtime/internal/routes/scalar.ts index 7bb19d5090..9c3eb20da7 100644 --- a/src/runtime/internal/routes/scalar.ts +++ b/src/runtime/internal/routes/scalar.ts @@ -11,7 +11,8 @@ export default eventHandler((event) => { ...runtimeConfig.nitro.openAPI?.ui?.scalar, spec: { url: "/_openapi.json", - ...(runtimeConfig.nitro.openAPI?.ui?.scalar as ReferenceConfiguration)?.spec, + ...(runtimeConfig.nitro.openAPI?.ui?.scalar as ReferenceConfiguration) + ?.spec, }, }; From a25e670d26c6e73db5ba9792cc2a792b7728a39c Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 28 Jun 2024 16:59:51 +0200 Subject: [PATCH 09/10] update ui to respect custom routes --- src/runtime/internal/routes/scalar.ts | 23 ++++++++++++----------- src/runtime/internal/routes/swagger.ts | 14 ++++++++++---- src/types/config.ts | 2 +- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/runtime/internal/routes/scalar.ts b/src/runtime/internal/routes/scalar.ts index 9c3eb20da7..5aeba9eefe 100644 --- a/src/runtime/internal/routes/scalar.ts +++ b/src/runtime/internal/routes/scalar.ts @@ -5,35 +5,36 @@ import { useRuntimeConfig } from "../config"; // Served as /_scalar export default eventHandler((event) => { const runtimeConfig = useRuntimeConfig(event); + const title = runtimeConfig.nitro.openAPI?.meta?.title || "API Reference"; + const description = runtimeConfig.nitro.openAPI?.meta?.description || ""; + const openAPIEndpoint = + runtimeConfig.nitro.openAPI?.route || "./_openapi.json"; // https://github.com/scalar/scalar - const configuration: ReferenceConfiguration = { - ...runtimeConfig.nitro.openAPI?.ui?.scalar, - spec: { - url: "/_openapi.json", - ...(runtimeConfig.nitro.openAPI?.ui?.scalar as ReferenceConfiguration) - ?.spec, - }, + const _config = runtimeConfig.nitro.openAPI?.ui + ?.scalar as ReferenceConfiguration; + const scalarConfig: ReferenceConfiguration = { + ..._config, + spec: { url: openAPIEndpoint, ..._config?.spec }, }; // The default page title - const title = "Nitro Scalar API Reference"; return /* html */ ` - + ${title} diff --git a/src/runtime/internal/routes/swagger.ts b/src/runtime/internal/routes/swagger.ts index f89c66528e..c7b69f9762 100644 --- a/src/runtime/internal/routes/swagger.ts +++ b/src/runtime/internal/routes/swagger.ts @@ -1,17 +1,23 @@ +import type { ReferenceConfiguration } from "@scalar/api-reference"; import { eventHandler } from "h3"; +import { useRuntimeConfig } from "../config"; // https://github.com/swagger-api/swagger-ui -// Served as /_swagger export default eventHandler((event) => { - const title = "Nitro Swagger UI"; + const runtimeConfig = useRuntimeConfig(event); + const title = runtimeConfig.nitro.openAPI?.meta?.title || "API Reference"; + const description = runtimeConfig.nitro.openAPI?.meta?.description || ""; + const openAPIEndpoint = + runtimeConfig.nitro.openAPI?.route || "./_openapi.json"; + const CDN_BASE = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@^5"; return /* html */ ` - + ${title} @@ -25,7 +31,7 @@ export default eventHandler((event) => {