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

mock-middleware-plugin

v1.1.3

Published

mock middleware for webpack-dev-server

Downloads

3

Readme

mock-middleware-plugin

Middleware for mocking data, based on webpack-dev-server.

  • Both support React and Vue.
  • Support hot reloading mock data.
  • Support multiple versions of webpack-dev-server
  • RESTful API mock support
  • Provide built-in mock template files
  • Variable methods to generate mock data(json/js object/function)

Install

npm i mock-middlerware-plugin -D
// if use Mockjs, you should install it either
// npm i mockjs -D

Usage

0. Auto refresh in browser(if you wanna refresh manually, skip this)

Add the code in your root file (e.g. index.js or app.js)
Then webpack will watch mock directory either.
And the browser will reload page after you modify mock files.

// react18
const container = document.getElementById('app');
const root = createRoot(container);
root.render(<App />);
// ...
if (process.env.NODE_ENV === 'development') {
// Don't use "require"! Otherwise webpack will stop when mock file has error
  import('../mock');
}

1. webpack.dev.conf.js(or other config file)

  1. webpack-dev-server(v4.7.0+)
const mockMiddlewarePlugin = require('mock-middlerware-plugin');
//
devServer: {
  // ...
  proxy: {
    // your config
  },
  setupMiddlewares: (middlewares, devServer) => {
    if (!devServer) {
      throw new Error('webpack-dev-server is not defined');
    }
    middlewares.push(
      mockMiddlewarePlugin(path.resolve(process.cwd(), './mock'),{ log: false })
    );
    return middlewares;
  },
}
  1. webpack-dev-server(v4 ~ v4.7.0)
devServer: {
  onAfterSetupMiddleware: function (devServer) {
    if (!devServer) {
      throw new Error('webpack-dev-server is not defined');
    }
    devServer.app.use(mockMiddleware(path.resolve(process.cwd(), './mock')));
  },
}
  1. webpack-dev-server(v3+)
devServer: {
  after: (app) => {
    app.use(mockMiddleware(path.resolve(process.cwd(), './mock')));
  },
}

2. mock data files

Qucik Start

Since v1.1.0, we support some template mock files in the package.
By the util, you can copy template files quickly to your target directory.

// webpack.dev.conf.js
const mockUtil = require('mock-middlerware-plugin/util');
mockUtil.initFiles(baseDir);

// baseDir is optional, if omitted , default is 'process.cwd()'
// it will copy the template files into 'baseDir/mock', default is './mock'
// if baseDir/mock exists, the function will return, and will not your override your files

Custom Define Files

There must be a index.js file in your root mock directory(such as ./mock), the data structure is as follows:

// ./mock/index.js
module.exports = {
  // [route]: [mock data],

  '/list/query' : mockJson,

  'post /list/add': (req, res)  => {
    res.send({ msg : "success" })
  }
};

2.1 @param {string} [route]

route: [method][space][path]  

method: GET/POST/PUT/DELETE; (also can be lowercase); optional, GET method if omitted; 
space: when the method is omitted, the space should be omitted too.
path: the request url; 

2.2 @param {object/function/string} [mock data]

There are variable methods to generate mock data.

  1. use json file
const mockJson = require('./list/query.json');
module.exports = {
  'get /list/query/:id': mockJson,
}
// You can also make it simpler
module.exports = {
  'get /list/query': require('./list/query.json'),
}
  1. use javascript object
module.exports = {
  'get /list/query': {
    code: 0,
    msg: '',
    data: [],
  },
}
  1. use function, just like in Express
 // According to the req.query/body parameter judgment, return different results
module.exports = {
  'get /list/query': (req, res) => {
    res.send({
      code: 0,
      msg: '',
      data: [],
    })
  }
}
  1. All the before methods support Mockjs. Remember to use Mock.mock() to wrap the origin data.
// json file 
{
  "list|1-10": [
    {
      "id|+1": 1
    }
  ]
}
'/list/mock': Mock.mock(require('./list/mock.json'))

// js object or function
'/list/mock': (req, res) => {
  const data = Mock.mock({
    req: req.query,
    'list|1-10': [
      {
        'id|+1': 1,
      },
    ],
  });
  res.send({ data });
},

3. Split root file

When there are too many api to mock, the index.js in root directory may look so huge. So use subfiles to make the root file more compact.

// ./mock/list/index.js
module.exports = {
  'get /list/query': mockJson,
  // ...
}

// ./mock/index.js
const listApi = require('./list/index.js');

module.exports = {
  // ...
  ...listApi,
}

Plugin Option

  1. mockMiddlewarePlugin(dir, [options])
dir: string; // your mock root directory
options: {
  log: boolean; // whether show request log in terminal; default is true!
}
// log demo
// [mock proxy]: GET /list/mock
  1. util.initFiles([baseDir])
baseDir: string; // if omitted, default is current root directory(process.cwd())

Tips

When you need to use req.body in your mock function, you should add 'body-parser' before the mock plugin!

const bodyParser = require('body-parser');
// .webpack.config.js(v5+)
setupMiddlewares: (middlewares, devServer) => {
  middlewares.push(bodyParser.json());
  middlewares.push(bodyParser.urlencoded({ extended: true }));
  middlewares.push(
    mockMiddlewarePlugin(path.resolve(process.cwd(), './mock'),{ log: false })
  );
}

// ./mock/index.js
'post /list/query': (req, res) => {
  res.send({
    reqParams: req.body,
    code: 0,
    msg: '',
    data: [],
  })
}