Skip to content

OpenAPI generation

ts
import { 
createHub
,
routeStore
} from "@duplojs/http";
import {
createHttpServer
} from "@duplojs/http/node";
import {
openApiGeneratorPlugin
} from "@duplojs/http/openApiGenerator";
const
hub
=
createHub
({
environment
: "DEV" })
.
register
(
routeStore
.
getAll
())
.
plug
(
openApiGeneratorPlugin
({
outputFile
: "swagger.json",
routePath
: "/swagger-ui",
}), ); await
createHttpServer
(
hub
,
{
host
: "localhost",
port
: 1506,
}, );

To generate the Swagger configuration for all your routes, use the openApiGeneratorPlugin function from @duplojs/http/openApiGenerator in the Hub and start with the environment variable set to DEV or BUILD.

  • The outputFile parameter lets you define which file the configuration will be written to.
  • The routePath parameter lets you define which path exposes the Swagger UI.
  • ... The remaining parameters define options for the Swagger configuration file.

WARNING

You must have an HTTP server implementation, because the plugin hooks into beforeServerBuildRoutes, which only runs via interface functions like createHttpServer. Run with the Hub configured in BUILD mode so the HTTP server doesn’t start.

Released under the MIT License.