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

mimus-mocker

v1.1.1

Published

MimusMocker is a unified API mocking middleware which can be used for providing API mocks quickly i.e even before writing a single line of actual api into your [Routers](https://expressjs.com/en/guide/routing.html).

Downloads

2

Readme

MimusMocker

MimusMocker is a unified API mocking middleware which can be used for providing API mocks quickly i.e even before writing a single line of actual api into your Routers.

Installation

Install via npm.

$ npm install mimus-mocker

or

Install via git clone

$ git clone https://github.com/ajaykumarmeher/mimus-mocker.git
$ npm install

Documentation

  • Supports configuration based mocking.
  • Allows multi-level controls for quick API testing.
  • Supports custom validations per API.

In Node.js

Example 1(with Router)

const mocks = {
  'mocks': [{
    'url': '/user/:id',
    'method': 'GET',
    'responseData': userMockResponse,
    'isEligible': true 
  }, {
    'url': '/posts/:id',
    'method': 'GET',
    'responseData': postMockResponse,
    'additionalValidator': customValidator /* optional */,
    'isEligible': true
  }],
  'isEligible': process.env.NODE_ENV === undefined || process.env.NODE_ENV === 'staging'
};

function customValidator (req, res, next) {
  if (req.params.id) {
    res.status(200).json(postV2MockResponse);
    return next(true, res);
  }
  return next(false, null);
}

const app = require('express')();
const mimusMocker = require('mimus-mocker')(mocks);

app.get('/user/:id', mimusMocker, function (req, res) {
  res.status(200).json({ name: 'tobi' });
});

app.get('/posts/:id', mimusMocker, function (req, res) {
  res.status(200).json({ title: 'some-post' });
});

Example 2 (with Middleware)

const mocks = {
  'mocks': [{
    'url': '/user/:id',
    'method': 'GET',
    'responseData': userMockResponse,
    'isEligible': true 
  }, {
    'url': '/posts/:id',
    'method': 'GET',
    'responseData': postMockResponse,
    'additionalValidator': customValidator /* optional */,
    'isEligible': true
  }],
  'isEligible': process.env.NODE_ENV === undefined || process.env.NODE_ENV === 'staging'
};

function customValidator (req, res, next) {
  if (req.params.id) {
    res.status(200).json(postV2MockResponse);
    return next(true, res);
  }
  return next(false, null);
}

const app = require('express')();
const mimusMocker = require('mimus-mocker')(mocks);
app.use(mimusMocker);

How to define the mock configuration?

url(Mandatory)

MimusMocker uses url-matcher for matching urls.

method(Mandatory)

All the methods like(GET, PUT, POST, DELETE etc.) currently getting supported by Express framework are eligible for validation.

responseData(Mandatory)

The mock contract for the API.

additionalValidator(Optional)

This will be a callback for additional validation. anyFunction(req,res,next)

  • next(isOverrideResult, newResponse) The next function will allow you to process the required action based on your additonalValidator. If isOverrideResult = true then newResponse will be sent. Else the default data defined in responseData will be sent. Use res for sending custom response and pass res Object as the newResponse. For more info please check the examples) given above.
  • req If the filter specified with query params like /test/:id and the requested path is api/test/1234/remainingPath then Mimus will attach the remaining path to req and you can access it via req.remainingPath which will contain the additional path information if any.

isEligible (per mock control)

This is used to control each mock item during development. If true, the url will be matched against the api request.

isEligible (master control)

This is used to control the complete mock feature. eg. the mock should only be enabled for "staging/dev" environment. But, should be disabled for production environments.

License

MIT

Free Software, Hell Yeah!