npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cli-api-mocker

v1.1.1

Published

CLI wrapper for connect-api-mocker

Downloads

462

Readme

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