Closed MaxVynohradov closed 4 years ago
@MaxVinogradov I was able to achieve this by querying the swagger json via an api client, and then passing that json result to the library.
const { data } = await axios.get('/swagger-json`)
app.use(swaggerStats.getMiddleware({ swaggerSpec: data}))
So basically you would run @nestjs/swagger and this library together
Does calling
const document = SwaggerModule.createDocument(app, options);
not deliver the data you need ?
@atjeff @mghstehr , all is okay, I've just proposed to include this info into docs
Using swagger-stats
in NestJS
with the express
platform seems okay. But I got no luck with the other platform, fastify
.
The fastify plugin of swagger-stats
needs to be used with fastify-express
. But fastify-express
cannot be registered to NestJS
because the decorator 'use' has already been added!
.
To implement swagger-stats in NestJS, it is not necessary to make a request, just pass the variable containing the document creation and it works.
Demo: Swagger Docs: https://rudemex-nestjs-starter.herokuapp.com/docs/ Swagger Stats: https://rudemex-nestjs-starter.herokuapp.com/swagger-stats/
// ./src/main.ts
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import * as swaggerStats from 'swagger-stats';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const config = new DocumentBuilder()
.setTitle('Cats example')
.setDescription('The cats API description')
.setVersion('1.0')
.addTag('cats')
.build();
const document = SwaggerModule.createDocument(app, config);
app.use(swaggerStats.getMiddleware({ swaggerSpec: document }));
SwaggerModule.setup('api', app, document);
await app.listen(3000);
}
bootstrap();
It's not compatible with NestJs/Fastify adapter?
It's not compatible with NestJs/Fastify adapter?
Same question
@dgastudio the problem was fixed by adding a slash(/) at the end of the address.
example.com/stats
throw error.
example.com/stats/
works fine.
Maybe it will be good to add docs, how to use swagger-stats with Nest.js? It has specific library-plugin for this - https://docs.nestjs.com/recipes/swagger .
document here is JS object - swagger 2.0 .