Giter Site home page Giter Site logo

swagger-docs-example's Introduction

Install

  1. $ yarn
  2. $ yarn start or start debugger in vscode
  3. go to localhost:4000/docs

Content

1. Swagger doc

We can still use our swagger 2.0 definitions for now, although swagger-jsdoc supports openapi 3.0, we can do the upgrade step by step

Folder structure

├── definitions
├── parameters   <--- for defining reusable parameters like objectId
│   ├── body
│   └── path
└── paths
    ├── apis
    └── cms

Referencing definitions

Should reference global definitions instead of file referencing in this context

before:

responses:
  "200":
    description: Success response
    schema:
      $ref: "./definitions/somedefintion.yaml"

now:

responses:
  "200":
    description: Success response
    schema:
      $ref: "#/definitions/somedefintion" <-- if your definition is correct, swagger-jsdoc will resolve the definition for you automatically without writing definition at the bottom of the same file

Consumming the yaml files

const swaggerJSDoc = require('swagger-jsdoc');

const swaggerDefinition = {
  info: {
    title: 'REST API for my App',
    version: '1.0.0',
    description: 'This is the REST API for my product',
  },
  host: 'localhost:4000',
  basePath: '/api',
};

const options = {
  // import swaggerDefinitions
  swaggerDefinition,
  // path to the API docs
  apis: ['./swagger/docs/**/*.yaml'], <-- everything under this folder will be combined into the swagger json without losing reference
};
// initialize swagger-jsdoc
module.exports = swaggerJSDoc(options);

In swaggerConfigGenerator.js

const definition = require('.')

module.exports = async (extraMiddlewares = []) => {
  return {
    definition: definition,
    extraMiddlewares,
    controllers: path.join(__dirname, '..', 'controllers')
  }
}

2. Validations

Rely on swagger-tools/middleware/swagger-validator

For example:

definition: ./swagger/docs/parameters/path/ObjectId.yaml

parameters:
  ObjectId:
    name: id
    in: path
    description: object id
    required: true
    type: string
    pattern: '^[a-f\d]{24}$'   <--- define validation pattern

consumer: ./swagger/docs/paths/apis/users.yaml

paths:
  /users/{id}:
    x-swagger-router-controller: PetController
    get:
      tags:
        - User
      summary: get a user
      operationId: getUserById
      produces:
      - application/json
      parameters:
        - $ref: '#/parameters/ObjectId'  <-- reference the parameter

then handle the error in error handler instead of controller

3. Parsing request query based on definitions (optional)

In reality, swagger already did the parsing job for us, however it's a bit hard to access the valus inside the swagger object, we can use a middleware to extract the values

Middleware

./swagger/middlewares/parseQuery.js

Usage

/* with middleware */
const query = req.parsedQuery
const { page, pageSize } = query

/* without middleware */
const page = req.swagger.params.page.value
const pageSize = req.swagger.params.pageSize.value

4. Normalize reponse based on definitions

This middleware help to normalize the response object according to definitions like string to number, number to string, string boolean to boolean, etc, and remove excess attribute if not defined in response object

Middleware

./swagger/middlewares/responseNormalizer.js

Assume the response object looks like

{ 
  "id": "5e2b016b37f7805e2efc7d59",
  "name": "Hello Kitty",
  "species": "cat",
  "isSick": "false",
  "age": 1
}

after the middleware

{ 
  "id": "5e2b016b37f7805e2efc7d59",
  "name": "Hello Kitty",
  "species": "cat",
  "isSick": false,
}

Try this request (http://localhost:4000/api/pets/5e2b016b37f7805e2efc7d59)

swagger-docs-example's People

Contributors

cwizard2011 avatar ivanyiphk01 avatar

Watchers

 avatar

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.