Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jun 28, 2024
1 parent 9177969 commit d4b6064
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 35 deletions.
55 changes: 38 additions & 17 deletions src/core/config/resolvers/open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 21 additions & 17 deletions src/types/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface NitroOpenAPIConfig {
/**
* Enable OpenAPI generation for production builds
*/
production?: false | "runtime" | "prerender"
production?: false | "runtime" | "prerender";

/**
* UI configurations
Expand All @@ -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;
};
};
}
1 change: 1 addition & 0 deletions test/fixture/middleware/config.ts
Original file line number Diff line number Diff line change
@@ -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";
});

0 comments on commit d4b6064

Please sign in to comment.