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

ivormock

v1.0.4

Published

A tool to mock REST API, speed up your frontend development

Downloads

1

Readme

ivormock

A tool to mock REST API, speed up your frontend development

Features

  • Decentralization
  • Auto update router
  • Switch response on demand
  • Lightweight
  • Version control
  • Work with multi projects
  • User friendly
  • Easy to integrate with webpack and vite

Install

Globally, used as a single server:


# with npm:

npm install -g ivormock

# or with yarn:

yarn global add ivormock

We also support use ivormock with webpack or vite, which is more efficient:


# for vite:
npm install -g ivormock vite-plugin-ivormock

# for webpack:
npm install -g ivormock webpack-plugin-ivormock

Getting start with webpack

Getting start with vite

Getting Start

1. Create a "ivormock" project

ivormock create <projectName> -b <projectRoot> -mp <mockFileFolderPath> -p <port> -d [description]

Params

  • projectName: Your project name, should be unique globally
  • projectRoot: Your project root path, such as D:\\code\\projects\\someProject
  • mockFileFolderPath: Where your mock files' folder located, relative to projectRoot, default is mock
  • port: Which port should your project's ivormock server started at, default is 6000
  • description: Optional, you can specify a description for your project

2. Start your "ivormock" server

After create your project successfully, simply start a ivormock server via your project name.

ivormock start <projectName>

If configured correctly, you would see a log on your screen, which indicates your ivormock server is started.

3. Write your mock file

Create a new .js file in your mock files folder and name it to get__api_v1_users.js, and paste the code below into your mock file:


module.exports = function(ctx) {
  return {
    message: "Hello, ivormock"
  }
}

Ok, save it!

With the file, we have creat a rest api which url is GET /api/v1/users, and will return a JSON:

{
  "message": "Hello, ivormock"
}

4. Check your server

Let's access our first ivormock api with curl:

curl http://localhost:6000/api/v1/users

Ok, you should have seen Hello, ivormock on your screen.

How to write mock file

Folder

With ivormock, you should specify a mock folder where your mock files locates. Folder name and path can be specified with ivormock command line or in plugin options.

For example, if your mock files locates at <projectRoot>/__mock__, you can tell ivormock via command line:

ivormock config <projectName> -mp __mock__

Or in plugin params:

new IvormockWebpackPlugin({
    mockPath: "__mock__"
})

With that, Ivormock can parse your mock files correctly.

Once you specify your mockPath, sub-folders is supported. You can put your mock files in different folders under mockPath

Filename

In Ivormock, FileName is used as url of a route, so it's important to name your mock files.

Rule of filename like this:

<GET|POST|PUT|PATCH|DELETE|...>__<URL_SLICE>_<URL_SLICE>_[id]

[id] is used to indicate that "id" is a param in route

FileName is composed with Method and Url, with a double underline as separator. if Url includes "/", you should transform it to "_".

Here is an example:

I have a scene to update user's information. The API may like: POST /api/v1/user/:id. I will do this:

  • Create a file under mockPath, named with post__api_v1_user_[id].js.
  • That's all!

A router named /api/v1/user/:id has been created by Ivormock, and it's ready for end users. But currently there's no response yet. Let's move on to define the API's response.

File content

Firstly, I will tell you something important:

  1. Only Commonjs module is supported now
  2. Must export a function, and the function should return an object
  3. You can use any nodejs package, but make sure to install it firstly

With the restriction, Ivormock support three kinds of common usage:

Simply give me a success response

Mock file's code may like this:

// post__api_v1_user_[id].js

module.exports = function() {
    return {
        foo: "sth",
        boo: "sth",
        // anything
    }
}

I want to customize my http code

Ivormock will return a 200 http code default. If you want to get a failed response, you could specify your http code with $$status and $$body:

// post__api_v1_user_[id].js

module.exports = function() {
    return {
        $$status: 400,
        $$body: {
            foo: "sth",
            boo: "sth",
            // anything
        }
    }
}

I want to switch success/fail response on demand

Ivormock support switch response on demand. The feature can free us from annotating code when we want to get a different response.

// post__api_v1_user_[id].js

module.exports = function() {
    return {
        $$type: "SWITCH",     // tell ivormock to switch response on demand
        $$active: 1,          // specify current active response index
        $$options: [          // all possible responses
            {
                $$status: 200,
                $$body: {
                    message: "this is a success response"
                }
            },
            {
                $$status: 500,
                $$body: {
                    message: "this is a failed response"
                }
            }
        ]
    }
}

Interact with request params

Ivormock is written with koa2. Ivormock has already pass the koa context to the function defined in mock file. You can get your param with the context.

For example:

module.exports = function(ctx) {
    // get params in body
    const body = ctx.request.body;
    if (!body.name) {
        return {
            $$status: 400,
            $$body: {
                message: "name is needed!"
            }
        }
    }
    return {
        $$status: 204
    }
}

For more details on koa context, please refer to koa documentation.

Plugin support

With plugin, ivormock is easy to integrate with our current workflow.

Work with vite

In your vite.config.js

const ivormock = require("vite-plugin-ivormock");

export default {
    // ... 
    plugins: [
        //...
        ivormock({
            mockPath: "mock",   // mock file path, relative to project root
            port: 3456          // ivormock server port
        })
    ]
}

Work with webpack

In your webpack.config.js

const IvormockWebpackPlugin = require("webpack-plugin-ivormock");

const isDev = process.env.NODE_ENV === "development";

module.exports = {
    // ... 
    plugins: [
        //...
        // Only used in development envorioment
        isDev && new IvormockWebpackPlugin({
            mockPath: "mock",   // mock file path, relative to project root
            port: 3456,          // ivormock server port
            prefix: "/mock"     // request with "/mock" prefix will pass to ivormock 
        })
    ]
}

TODO

  • Customization: path separator, api prefix...
  • UI
  • Log
  • Generate API document