Skip to content

kaewberg/cli-api-mocker

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
        }
    }
}

About

CLI wrapper for connect-api-mocker

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%