3

i don't understand, how to use swagger-ui with routing-controller.

i have tried:

@Controller('/api-docs')
@UseBefore(swagger.serve) //ERROR No overload matches this call
class SwaggerController {
  
  @Get('/')
  @UseBefore(swagger.setup(swaggerJson))
  public swaggerUi(): void {}
}

but i got Error (No overload matches this call) for swagger.serve.

Now, i made it so:

// server.ts
const app = express();

useExpressServer(app, {/* controllers, middlewares, options */});

app.use('/api-docs', swagger.serve, swagger.setup(swaggerJson));

it works, but this is incorrect implementation (i think)

How use existing middlewares for that cases?

Alex4answer
  • 485
  • 1
  • 5
  • 15

1 Answers1

2

This seems to be the right implementation, as recommanded on swagger-ui-express documentation.

As decribed in routing-controllers documentation, you can pre-configure your Express app with common Express middlwares, then setup routing-controllers using useExpressServer(app, ...

No need to create any dedicated Controller, simply add it as a function middleware

I'm using Routing-Controller / Express typescript project, and I configured Swagger this way (using YML openAPI file, but could be JSON as well), except that I configure express BEFORE routing-controllers setup:

import * as swaggerUi from "swagger-ui-express";
import * as YAML from "yaml";
...

{
    const app = express();
    ...
    // load YML Swagger spec
    const buf = await fs.promises.readFile(path.join(__dirname, "../install/openapi.yml"));
    const openApi = YAML.parse(buf.toString());
    // setup swaggerUI as Express middleware
    app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(openApi));

    // then setup routing-controller
    useExpressServer(app, { ...
    ...
}
Harty911
  • 69
  • 6