Giter Club home page Giter Club logo

mgr-swagger-express's Introduction

mgr-swagger-express

Swagger annotations for your express project Medium article

Usage

Install

npm install mgr-swagger-express --save

or

yarn add mgr-swagger-express

Use

app.js:

import express from 'express'
import generateSwagger, { SET_EXPRESS_APP } from 'mgr-swagger-express'

const app = express()
SET_EXPRESS_APP(app)

import MyResource from './resource.service' // Note, the import should happen AFTER the SET_EXPRESS_APP call

const swaggerDocument = generateSwagger({
  name: "My Service Name",
  version: "0.0.1",
  description: "My Service Description",
  host: `localhost:5000`,
  basePath: '/',
})

app.use(
  '/swagger',
  swaggerUI.serve,
  swaggerUI.setup(swaggerDocument));
}

resource.service.js:

import { GET, POST, addSwaggerDefinition } from "mgr-swagger-express"

const ResourceDescription = {
  type: 'object',
  properties: {
    id: 'string',
    name: 'string'
  }
}

const ResourceStatus = {
  type: 'object',
  properties: {
    status: {
      type: 'string',
      enum: ['running', 'failed', 'stopped']
    }
  }
}

export default class BotService {
  constructor() {
    addSwaggerDefinition('ResourceDescription', ResourceDescription)
    addSwaggerDefinition('ResourceStatus', ResourceStatus)
  }

  @GET({
    path: '/resource',
    description: 'Get all resources available',
    tags: ['Resources'],
    success: '#/definitions/ResourceDescription',
  })
  public async getAvailableResources(args, context) {
    return []
  }

  @POST({
    path: '/resource/{resourceId}',
    description: 'Update resource by ID and get its status',
    parameters: [{
      name: 'resourceId',
      description: 'Resource ID',
    }],
    tags: ['Resources'],
    success: '#/definitions/ResourceStatus'
  })
  public async updateResource(args, context) {
    return {
      status: 'failed'
    }
  }
}

mgr-swagger-express's People

Contributors

mgrin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

douglasmariz

mgr-swagger-express's Issues

Example /books/:book_id returns no results

I appreciate what you are doing with decorators and the swagger API. I would like to implement this in my project. I am running the example application. I successfully POST a book with id="1". The GET /books returns my book. However, when I execute GET /books/:book_id, no book is found.

In index.js the line console.log(
${propertyKey} [${method.toUpperCase()} ${expressPath}],
args,
context
);

displays the following when I click swagger "Execute":
getBookById [GET /books/:book_id] { book_id: ':book_id' }

The req parameter value url is /books/:book_id

The correct book JSON is displayed when I explicitly type http://localhost:3000/books/1 into the browser.

Are you able to provide information as to which swagger node module may not be interpreting the path parameter properly?

Thanks for any insight.

Greg

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.