Code Monkey home page Code Monkey logo

cli-api-mocker's Introduction

cli-api-mocker

CLI wrapper for connect-api-mocker

Installation

npm i -g cli-api-mocker

Usage

After you created your mock files like defined in connect-api-mocker documents you can simply start your mock server with running mockit command inside the root folder of your mocks:

$ mockit

That command will start to serve your mocks on port 9090 by default.

Arguments

mockit command has 4 available arguments.

[-p | --port]     with default value `9090`,
[-f | --fromPath] with default value `/`,
[-t | --toPath]   with default value ``
[-c | --capture]   with default value `false`
[-v | --verbose]   with default value `false`
[-d | --disable-mocks] with default value `false`

These arguments are optional. You can use mockit command with any one of them or any combination of them.

You can see usage examples below:

mockit --port=8989 or mockit -p 8989 for running on port 8989 instead of default port 9090 mockit --fromPath=/api or mockit -f '/api' for running listening paths from /api instead from default path `` mockit --toPath=/mapi or `mockit -t '/mapi'` for forwarding to path `/api` instead of forwarding to default path `/`

Or you can combine any of them like:

mockit --port=8989 --fromPath=/api --toPath=/mapi

Or

mockit -p 8989 -f '/api -t '/mapi'

Note: In next title you will notice config file. If there is a config file, config file will be active. But command line arguments are stronger. So if you use both of them together, command line arguments will override config file.

Configuration with a config file

You can set your configuration with file mock.config.js file in the root of your project directory.

module.exports = {
    port: 9090,
    map: {
        '/api': 'mocks/api'
    }
}

Configuration above will create a mocking server on running port 9090 and serve an api that defined with files in the mocks/api folder with a base url of '/api'. So after running your mockit command in the same folder with that configuration, if you make a request to http://localhost:9090/api/users, api mocker will respond request with file(if exists) in mocks/api/users/GET.json.

Using a proxy

If you also want to use a proxy for requests that you didn't have a mock file, you can define your mock config like that:

module.exports = {
    port: 9090,
    map: {
        '/api': {
            target: 'mocks/api',
            proxy: 'https://api.yourdomain.com'
        }
    }
}

Proxy definition object is a http-proxy-middleware options object. So you can take advantage of all options of http-proxy-middleware library here. Here a more detailed proxy definition example:

module.exports = {
    port: 9090,
    map: {
        '/api': {
            target: 'mocks/api',
            proxy: {
                target: 'https://api.yourdomain.com',
                pathRewrite: {
                    '^/api': ''
                },
                changeOrigin: true,
                secure: false
            }
        }
    }
}

Capture mode

With capture mode, you can automatically create mock files from your api origin responses over proxy. You can enable capture mode by --capture (or -c) command line parameter or capture property in config file:

module.exports = {
    port: 9090,
    map: {
        '/api': {
            target: 'mocks/api',
            proxy: 'https://api.yourdomain.com',
            capture: true
        }
    }
}

When capture mode enabled, if you don't have a mock file for a request and if you have a proxy definition, a mock file will automatically generated for you for successful responses from your origin.

Disabling mocks

You can also disable mocks to just use it as proxy via --disable-mocks cli parameter or disableMocks property in config file.

module.exports = {
    port: 9090,
    map: {
        '/api': {
            target: 'mocks/api',
            proxy: 'https://api.yourdomain.com',
            disableMocks: true
        }
    }
}

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.