moxai

1.1.2 • Public • Published

moxai

Moxai (mɒks-eɪ) - Express Middleware for API mocks using Open API Initiative (OAI) npm TravisCI Code Climate Code Climate Coverage Codecov Codacy Badge bitHound Overall Score bitHound Dependencies Gemnasium Dependency Status VersionEye Dependency Status GitHub Tags GitHub Contributors GitHub Issues Semver license

Moxai (mɒks-eɪ) - Express Middleware for API mocks using Open API Initiative (OAI)

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command.

npm install moxai --save

Usage

Setup as standard third-party middleware which requires Express and mounting on a route, such as '/mocks'.

var express = require('express');
var moxai = require('moxai');
var app = express();
 
app.use('/mocks', moxai());
 
app.listen(8000, function () {
  console.log('Express web server with Moxai listening on port 8000');
})

Options

Options are passed as an object to moxai and all arguments are optional.

var opts = {
 'dir': 'mocks',
 'file': 'api',
 'random': false
};
 
app.use('/mocks', moxai(opts));
 
Option Type Default Argument Description
dir string mocks optional The directory location of OAI files relative to parent directory.
file string api optional The name of OAI JSON file. Must be located within directory location.
random boolean false optional Use random output for regex values in OAI JSON file.

dir

The directory where the files are stored. This is relative to the parent directory and the default is mocks. In most applications, simply add a mocks directory in the root of the application.

/
├── mocks/
├── app.js|index.js
└── package.json

file

The file of the OAI (formerly Swagger) JSON file to use. This is compatible with OAI version 2.0. This must have a .json extension and the default is api, therefore the default file is api.json. The file must be in the designated directory, which defaults to mocks.

/
├── mocks/
    └── api.json
├── app.js|index.js
└── package.json

Mock output should be in the response examples section of the OAI JSON. This should be the exact JSON output expected by the mock API request.

{
  "swagger": "2.0",
  "paths": {
    "/api/": {
      "get": {
        "responses": {
          "200": {
            "examples": {
              "application/json": {
                "key": "value"
              }
            }
          }
        }
      }
    }
  }
}

random

Randomizes the output from the OAI JSON using regular expressions. Only randomizes values that are valid regex and are enclosed in forward slashes (/). Back slashes (\) must be escaped by using double backslash (\\). Ignores any flags such as /i (ignore case) and /g (global). If error with regex, then will output original string.

{
  "swagger": "2.0",
  "paths": {
    "/api/": {
      "get": {
        "responses": {
          "200": {
            "examples": {
              "application/json": {
                "alphanumeric": "/^[a-z0-9]{2,10}$/",
                "titlecase": "/^[A-Z][a-z]{4,16}$/",
                "phonenumber": "/^[1-9]\\d{2}-\\d{3}-\\d{4}/",
                "integer": "/[0-9]{1,7}/",
                "boolean": "/true|false/",
                "ignore": "/[[[ignore/"
              }
            }
          }
        }
      }
    }
  }
}

Testing

Scripts

  • npm test to run Mocha unit tests.
  • npm run coverage for Istanbul code coverage. Results in /coverage folder.
  • npm run lint for ESLint static code analysis. Results in /lint folder.
  • npm run docs to run JSDoc code documentation. Results in /docs folder

Test Data

Dependencies

Modules

Application

Package

Dependency Trackers

Contact

Point of Contact

You can report issues and submit questions by opening a new Issue in GitHub.

Notifications

You can Watch this repo to receive notifications from GitHub when a new issue is posted, when an existing issue’s status is updated, and when a pull request is created.

Contributing

See CONTRIBUTING for additional information.

Acknowledgements

This open source project was developed for the U.S. Forest Service ePermit API project under the General Services Administration (GSA) Technology Transformation Service (TTS) 18F Agile Delivery Services Blanket Purchase Agreement (Agile BPA). Moxai is a testing dependency for the ePermit API which used this package as a temporary, placeholder mock API. Moxai is published as an independent npm package that can be used with any Express application.

License

This project is in the worldwide public domain. As stated in CONTRIBUTING:

This project is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the CC0 1.0 Universal public domain dedication.

All contributions to this project will be released under the CC0 dedication. By submitting a pull request, you are agreeing to comply with this waiver of copyright interest.

Package Sidebar

Install

npm i moxai

Weekly Downloads

0

Version

1.1.2

License

CC0-1.0

Last publish

Collaborators

  • brianfunk