Skip to content

Instantly share code, notes, and snippets.

@brauliodiez
Last active January 4, 2019 12:13
Show Gist options
  • Save brauliodiez/5a12abc80a944f1a6fd2f0d3b0fd735a to your computer and use it in GitHub Desktop.
Save brauliodiez/5a12abc80a944f1a6fd2f0d3b0fd735a to your computer and use it in GitHub Desktop.
Notes about swagger

Installing swagger globally

npm install -g swagger

Create a new swagger project

swagger project create 

Wizard: choose Express framework.

  • New directory created (NAME_OF_YOUR_PROJECT), cd into that directory and execute:
swagger project start

Starts express server port... you can go to that url in the browser or postman and get results.

  • To get the editor working (command prompt, from the NAME_OF_YOUR_PROJECT folder):
swagger project edit

From browser, you get the text and right hand the friendly documentation.

Swagger, tip in the elements to add:

x-swagger-router-controller

We can create controllers in swagger and add them again

  • How to start swagger in mock mode (automatic mock mode):
swagger project start -m
  • Creating custom mocks

  • Starting using mocked controllers:

    • Under project folder mocks subfolder we can create a new file, named same as x-swagger-router-controller, and functin you can find it in operationId, sample

    ./mocks/GetAllTodos.js

 'use strict'
 
 module.exports = {
  GetAllTodos: GetAllTodos
 }
 
 function GetAllTodos(req, res, next) {
   res.json([
     {
       todo_id: 0,
       todo: "Get some milk",
       author: "Jim",
       //...
     }
   ]);
 }
  • Now let's go one step ahead, we will writer controllers (controller subfolder), same as with mocks but for api endpoints (controlelrs folder).

./controllers/GetAllTodos.js

'use strict'

module.exports = {
}

Notes

Converting json to yaml https://editor.swagger.io/

Visual studio plugin swagger viewer:

https://marketplace.visualstudio.com/items?itemName=Arjun.swagger-viewer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment