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

v1.0.0

Published

Simple configuration based Mock server to quickly mock any kind of backend API.

Downloads

19

Readme

Mock Server:

Build Status

This is a general purpose Mock Server which can be used to mock any kind of WebSevice. To mock any service you don't need to write any new code instead all you have to do is add a configuration in rules.json file and drop your response data under "data" directory or you can put rules.json and response data in any custom directory you want and pass these custom path in an object while instantiating mock server (sample code provided in example section below) and that will make mock server use your custom directories for looking up rules and response data.

Here are the detailed steps:

  1. Add a similar json block in rules.json file. Here "api" is an array and it can have n number of objects each representing a service request configuration. This is the file which is parsed to find right response or error depending upon configuration.
{
     "api": [
          {
               "serviceName" : "loanserv",
               "apiName" : "creditDecision",
               "request" : {
                   "customerID" : 12345,
                   "firstName" : "Adam",
                   "lastName" : "Levine",
                   "accountType" : "Business",
                   "homePhoneNumber" : "408-231-6219",
                   "businessPhoneNumber" : "408-123-9234",
                   "dob" : "Aug 10, 1980",
                   "address" : "2145 Campbell St, San Jose, CA 95651"
               },
               "response" : "creditDecisionResponse.json",
               "error" : "error.json"
          }
     ]
}

Note:

  • Make sure your JSON is a valid json. You can verify your JSON at this link: http://jsonlint.com/
  • servicename, apiname, request, response and error are mandatory attributes in rules.json file.
  • You can add any field under request object. It is dynamically picked by Mock Server while parsing the request.
  • Response object can be any valid JSON with any level of nesting inside it.
  1. Add your JSON response file under your root level /data directory or you can have a custom directory for all your mock response. If you chosee to have custom directory then pass the path to your custom directory while instantiating mock server. See examples for reference.

  2. Once above two steps are done, you are ready to use the Mock Server.

Mock Server API exposes two functions:

  • getMockData - This API can only be use to parse flat level requests. So if your request has no nesting which most of the REST APIs are then just use this API. This is optimized for flat level requests. This will throw error if requests are nested.
  • getMockDataForNestedReq - If your requests are nested then use this API. This API is very general purpose and it can parse nested as well as flat level requests. However if your requests are flat then I would recommend to use "getMockData" API.

Use following code as reference to invoke Mock Server:

  • Use default configuration for rules.json and response directories. Note:
    • Default value of rules.json - "rules.json" file under root directory of project same as package.json
    • Default value of response data directory - "data" directory under root of project same as package.json
  • Use deault configuration
var MockServer = require('mockapi');

/* No configuration provided while instantiations means default configuration will be used
 * Default configuration:
 * rules.json must be available under root of project same as package.json
 * Mock response json files must be available under "data" durectory of project root same as package.json
 */
var mockServer = new MockServer();

var apiContext = {
 serviceName: 'userDataService',
 apiName: 'getUserData'
};

// sample request, you can have any number of attributes in your request
var req = {
 accountNumber : '12345',
 city : 'Campbell',
 country : 'US',
 zipCode : 95008
};

var response = mockServer.getMockData(apiContext, req);
  • Use custom configuration for rules.json and response data
var MockServer = require('mockapi');

var customConfiguration = {
    rulesPath: '/myCustomRules/rules', //path where rules.json file is located. This path is relative to your project roor dir
    dataPath: '/mydata/mockResponse' // directory path for response files relative to your package.json file
}

var mockServer = new MockServer(customConfiguration);

var apiContext = {
 serviceName: 'userDataService',
 apiName: 'getUserData'
};

// sample request, you can have any number of attributes in your request
var req = {
 accountNumber : '12345',
 city : 'Campbell',
 country : 'US',
 zipCode : 95008
};

var response = mockServer.getMockData(apiContext, req);
  • Get Mock Data for Nested Request
var MockServer = require('mockapi');

var mockServer = new MockServer();

var apiContext = {
 serviceName: 'userDataService',
 apiName: 'getUserData'
};

// nested request, you can have any number of attributes in your request
var nestedReq = {
customerID: '83467',
productCode: 'lfgns',
subjects: ['English', 'Math'],
address: {
    street: '2121 N 1st St',
    city : 'San Jose'
},
pastCountries : [
    'India',
    'US'
]
};

var response = mockServer.getMockDataForNestedReq(apiContext, req);