Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

swagger-mock-api

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swagger-mock-api

Creates a connect middleware mock API from a Swagger 2.0 YAML file

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
49
decreased by-15.52%
Maintainers
1
Weekly downloads
 
Created
Source

#mock-api

This module is a connect-compatible middleware generating function that generates an API based on a Swagger 2.0 compatible YAML file. All data returned is generated by chance.

installation

The primary use case is to be run as a middleware for grunt-contrib-connect. Run the following command:

npm install swagger-mock-api

Using the middleware

Assuming the API is to be run from a grunt task, ensure the following module has also been installed via npm:

  • grunt-contrib-connect

If you're not auto-loading grunt tasks, find in your Gruntfile where the tasks are being loaded and add the line:

grunt.loadNpmTasks('grunt-contrib-connect');

Then, configure a task like so:


var path = require('path');
var mockApi = require('path to mockApi');

grunt.initConfig({
  connect: {
    server: {
      options: {
        middleware: [
          mockApi({
                yamlPath: path.join(__dirname, 'relative path to swagger yaml file')
            })
        ],
      },
    },
  },
});

Specifying custom Chance options

Swagger specifies only a few primitive types; for scenarios where specific chance methods are needed, use the x-chance-type field.

...
definitions:
  NewPet:
    properties:
      name:
        type: string
        x-chance-type: name
      tag:
        type: string
        x-chance-type: guid

Most of the chance methods allow some fine-tuning of the returned data. For example, the integer method allows specification of minimum and maximum output values. These options can be configured in the Swagger YAML file with the x-chance-options block:

...
definitions:
  Pet:
    allOf:
      - $ref: '#/definitions/NewPet'
      - required:
        - id
        properties:
          id:
            type: integer
            format: int64
            x-type-options:
              min: 1
              max: 1000

Todos:

  • Test with more complex swagger files
  • Actual unit tests
  • Convert code to utilize ES6 features and clean up codebase

FAQs

Package last updated on 11 Jun 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc