slanatech / swagger-stats

API Observability. Trace API calls and Monitor API performance, health and usage statistics in Node.js Microservices.
https://swaggerstats.io/
MIT License
892 stars 137 forks source link

Usage with Nest.js #83

Closed MaxVynohradov closed 4 years ago

MaxVynohradov commented 4 years ago

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 .

  const document = SwaggerModule.createDocument(app, options);

document here is JS object - swagger 2.0 .

atjeff commented 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

mghstehr commented 4 years ago

Does calling const document = SwaggerModule.createDocument(app, options); not deliver the data you need ?

MaxVynohradov commented 4 years ago

@atjeff @mghstehr , all is okay, I've just proposed to include this info into docs

magiclen commented 2 years ago

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!.

rudemex commented 2 years ago

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();
muhammadzadeh commented 1 year ago

It's not compatible with NestJs/Fastify adapter?

image

dgastudio commented 1 year ago

It's not compatible with NestJs/Fastify adapter?

image

Same question

muhammadzadeh commented 1 year ago

@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.