npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

swagger-controls

v1.0.20

Published

Convert classes to swagger documentation using NestJs decorators

Downloads

9

Readme

swagger-controls

A tool best suited for projects that want Swagger documentation created by use of decorators. Perhaps you have an Express project and you either have considered or are having issues with keeping up with JsDocs, than this project is a great alternative you should consider.

🔧 Use swagger-controls when you know you want those great swagger documentation decorators but your project still needs to maintained in an Express middleware like pattern.

Is this a replacement for NestJs?

Nope. This library simply uses NestJs decorative functionality to assist in generating Swagger files. If anything, this package sets a project up for potential future NestJs adoption.

Install

This project has all the dependencies you need to make robust swagger documentations

npm install swagger-controls @nestjs/platform-express @nestjs/common @nestjs/core @nestjs/swagger reflect-metadata

Example

In this example there are 3 files involved in an Express app generating Swagger docs by decorating classes. Those three files are generate-swagger.ts, index.ts, and controllers.ts

generate-swagger.ts - creates swagger.json file

import writeByControllers from 'swagger-controls';
import { controllers } from './controllers'

writeByControllers(controllers, {
  filePath: `${__dirname}/swagger.json`
})

index.ts - Creates Express app

import { HealthCheck } from './controllers'
import express from 'express'

export const app = express()

app.get('/v1/health-check', new HealthCheck().get)

controllers.ts

import { ApiOperation, ApiProperty, ApiResponse } from '@nestjs/swagger'
import { Controller, Get, HttpStatus } from '@nestjs/common'

@Controller('/v1/health-check')
export class HealthCheck {
  @Get()
  @ApiOperation({
    summary: 'Check on the status or health of API',
    description: 'Acts as a ping and a status indicator of accessible services',
  })
  @ApiResponse({
    status: HttpStatus.OK, type: HealthOutput,
    description: 'Very basic connectivity report',
  })
  @ApiResponse({
    status: HttpStatus.SERVICE_UNAVAILABLE, type: HealthOutput,
    description: 'Connection to additional services is in an invalid state',
  })
  get(req: Express.Request, res: Express.Response): HealthOutput {
    return res.json( new HealthOutput() ) // respond
  }
}

/** Response to /v1/health-check calls */
class HealthOutput {
  @ApiProperty({
    example: 'ok',
    description: 'Basic indicator value of api is active'
  })
  status: 'ok' = 'ok'

  @ApiProperty({
    example: 'ok',
    description: 'Connective state to database. Static value for now'
  })
  db: 'ok' = 'ok'
}

export const controllers = [ HealthCheck ]

Options

Control nuances of the swagger.json generation

interface Options {
  filePath?: string
  servers?: string | ServerObject[] // url dropdown select of servers

  deepScanRoutes?: boolean
  ignoreGlobalPrefix?: boolean

  title?: string
  description?: string
  externalDocs?: [string, string][] // [title, url][]
  version?: string
}

/** Example */

import { version, description, name } from '../package.json'
import writeByControllers from 'swagger-controls'
import { controllers } from './controllers'

const options: Options = {
  version, description, title: name,
  filePath: `${__dirname}/swagger.json`
}

writeByControllers(controllers, options) // generate swagger.json

Manipulate JSON before output

In some cases you may want to alter the swagger.json object before it is written to file in order to make alterations

import * as fs from 'fs'
import { getSwaggerByControllers } from 'swagger-controls'
import controllers from './controllers' // your array of route controllers

const docs = await getSwaggerByControllers(controllers, { ...options... })

docs.components.securitySchemes.SomeApiKeyAuth = {
  type: 'some-auth-type',
  in: 'header',
  name: 'scheduled_job',
  description: 'some custom schema'
}

fs.writeFileSync(filePath, JSON.stringify(docs, null, 2)),

Learn more

❤️ This was crafted out of love for my homey who could use a better approach but not the whole Nest with it. _Acker