mock-json-api

0.2.7 • Public • Published

Travis CI - Status

Build Status

mock-json-api

NPM - Mock JSON API

A node module for generating dummy data quickly and mocking an API to deliver that data as json objects defined by the developer

Dependencies:

  • dummy-json
  • json-store
  • validator
  • express

Properties

  • jsonStore: This is the local file location for the actual data that gets generated. The data will be stored in this json file and served up on request by node. This allows us to serve up well-known data vs. generating it everytime, thus simulating a database (of sorts).
  • mockRoutes: An array of routes to mock
    • name: The unique identifier of the service method to be mocked
    • mockRoute: The URL of the route to mock - a regex
    • testScope: The behavior of the route response; success, fail, or error
    • errorBody: The text that will be displayed in the response of any error thrown
    • method: POST, DELETE, GET, PUT, etc
    • testScenario: Determines which JSON template to return in the array when testScope is "success" by either defining the index of the array or the name of the template
    • latency: in milliseconds. Will delay the response by set number of miliseconds. Can be a single number like 3000, a string like '3000' or a range of numbers as a string like '2000-7000'. If a range, it will randomly select a number in that range on each request.
    • jsonTemplate: The actual object to be returned from the mock route. This will also tell node how to construct the dummy data the first time the route is requested. It's simply an array of strings, functions or objects that return string representations of JSON objects laced with dummy JSON notation.
    • data: Your own data to be used with dummy-json.
    • helpers: Custom helpers to be used with dummy-json. Refer to the dummy-json documention for more information.

On The Fly Modification

  • If you need to change scenario, scope or latency on the fly, just GET the route you want to modify and add the property you want to modify to the querystring with a new value.
  • Currently you can modify testScenario, testScope, and latecy values.
  • For example: http://myserver.com/api/route?testScenario=foo will change the scenario of the /api/route route to foo from whatever is set in the mock object.
var express = require('express');
var mock = require('mock-json-api');
 
server = express();
 
var mockapi = mock({
    jsonStore: __dirname + '/data.json',
    mockRoutes: [
        {
        name: 'myFirstRoute',
        mockRoute: '/api/foo',
        testScope: 'success',
        testScenario: 1, //array index OR namedScenario
        latency: 300,
        method: 'POST',
        jsonTemplate: [
            function(){ //Scenario 0
                var myMockObject = {
                    prop1: 'abc',
                    prop2: 123
                }
                return JSON.stringify(myMockObject);
            },
            function() { return //Scenario 1
                '{'+
                    '"people": ['+
                    '{{#repeat 2}} {'+
                        '"id": {{index}},'+
                        '"firstName": "{{firstName}}",'+
                        '"lastName": "{{lastName}}",'+
                        '"email": "{{email}}",'+
                        '"work": "{{company}}",'+
                        '"age": {{int 20 50}},'+
                        '"optedin": {{boolean}}'+
                    '} {{/repeat}}],'+
                    '"images": ['+
                        '{{#repeat 3 6}}'+
                            '"img{{index}}.png"'+
                        '{{/repeat}} ],'+
                    '"revision": {{uniqueIndex}},'+
                    '"tolerance": {{int '0' '2'}},'+
                '}';
            },
            function() { return //Scenario 2
                '{'+
                    '"people": ['+
                        '{{#repeat 300}} {'+
                            '"id": {{index}},'+
                            '"firstName": "{{firstName}}",'+
                            '"lastName": "{{lastName}}",'+
                            '"email": "{{email}}",'+
                            '"work": "{{company}}",'+
                            '"age": {{int 18 35}},'+
                            '"optedin": {{boolean}}'+
                        '} {{/repeat}}],'+
                    '"images": ['+
                        '{{#repeat 6 9}}'+
                            '"img{{index}}.png"'+
                        '{{/repeat}} ],'+
                    '"revision": {{uniqueIndex}},'+
                    '"tolerance": {{int '0' '2'}},'+
                '}';
             };]
        },
        {
          'namedScenario': function() { return //Scenario 3
     '{'+
     '"people": ['+
     '{{#repeat 300}} {'+
     '"id": {{index}},'+
     '"firstName": "{{firstName}}",'+
     '"lastName": "{{lastName}}",'+
     '"email": "{{email}}",'+
     '"work": "{{company}}",'+
     '"age": {{int 18 35}},'+
     '"optedin": {{boolean}}'+
     '} {{/repeat}}],'+
     '"images": ['+
     '{{#repeat 6 9}}'+
     '"img{{index}}.png"'+
     '{{/repeat}} ],'+
     '"revision": {{uniqueIndex}},'+
     '"tolerance": {{int '0' '2'}},'+
     '}';
                 };]
            }
        },
        {
        name: 'anotherRoute',
        mockRoute: '/api/bar',
        testScope: 'fail',
        jsonTemplate: [ function() { return
            '{' +
                '"name": "{{firstName}}",'+
                '"age": {{int 18 65}}'+
            '}';
        };]
        },
        {
        name: 'routeUsingCustomDataAndRegexRoute',
        mockRoute: '\/api\/.*\/customData',
        testScope: 'success',
        latency: '300-7000',
        data: {
            drawers: [
                {name: 'Drawer 1', id: '1'},
                {name: 'Drawer 2', id: '2'},
                {name: 'Drawer 3', id: '3'},
                {name: 'Drawer 4', id: '4'}],
            attributes: [{
                    PropertyType: 0,
                    DisplayName: 'Bool',
                    Id: 'ir:attrdef_1'
                },
                {
                    PropertyType: 3,
                    DisplayName: 'Date',
                    Id: 'ir:attrdef_2'
                },
                {
                    PropertyType: 6,
                    DisplayName: 'String',
                    Id: 'ir:attrdef_3'
                },
                {
                    PropertyType: 6,
                    DisplayName: 'String with choices',
                    Id: 'ir:attrdef_4',
                    Choices: [{
                            DisplayName: 'Choice 1',
                            Value: 'Choice 1'
                        },
                        {
                            DisplayName: 'Choice 2',
                            Value: 'Choice 2'
                        }
                    ]
                },
                {
                    PropertyType: 4,
                    DisplayName: 'Attr Float no min max',
                    Id: 'ir:attrdef_8',
                    MaximumValue: 2147483647,
                    MinimumValue: -2147483648
                },
                {
                    PropertyType: 1,
                    DisplayName: 'Attr User',
                    Id: 'ir:attrdef_11',
                    Choices: [{
                            DisplayName: 'Corey',
                            Value: 'Corey'
                        },
                        {
                            DisplayName: 'Scott',
                            Value: 'Scott'
                        },
                        {
                            DisplayName: 'Derek',
                            Value: 'Derek'
                        }]
                }]
        },
        jsonTemplate: [ function() { return
            '{' +
                '"drawers":[' +
                '       {{#repeat drawers}}' +
                '       {' +
                '       "fileTypes":' +
                '           [' +
                '               {{#repeat 1 5}}' +
                '               {{int 5}}' +
                '               {{/repeat}}' +
                '           ],' +
                '       "id":"{{this.id}}",' +
                '       "name":"{{this.name}}"' +
                '       }{{/repeat}}' +
                '   ],' +
                '"fileTypes":[' +
                '   {{#repeat 5 8}}' +
                '    {' +
                '       "attributes":[' +
                '           {{#repeat attributes}}' +
                '           {' +
                '           {{#if this.Choices}}'+
                '           "Choices": [' +
                '           {{#repeat this.Choices}}' +
                '           {"DisplayName": "{{this.DisplayName}}", "Value": "{{this.Value}}"}' +
                '           {{/repeat}}' +
                '           ],' +
                '           {{else}}' +
                '           "Choices": [],' +
                '           {{/if}}' +
                '           {{#if this.MinimumValue}}' +
                '           "MinimumValue":"{{this.MinimumValue}}",' +
                '           {{/if}}' +
                '           {{#if this.MaximumValue}}' +
                '           "MaximumValue":"{{this.MaximumValue}}",' +
                '           {{/if}}' +
                '           "Id":"{{this.Id}}",' +
                '           "DisplayName":"{{this.DisplayName}}",' +
                '           "PropertyType":{{this.PropertyType}}' +
                '           }' +
                '           {{/repeat}}' +
                '       ],' +
                '       "id": "{{uniqueIndex}}",' +
                '       "name": "{{company}}",' +       
                '    }' +
                '  {{/repeat}}' +
                ']' +
         '}';
        };
           ]
        }
    ]
});
 
server.use(mockapi.registerRoutes);
server.listen(3001);

Readme

Keywords

Package Sidebar

Install

npm i mock-json-api

Weekly Downloads

6

Version

0.2.7

License

MIT

Unpacked Size

55.4 kB

Total Files

18

Last publish

Collaborators

  • bobum
  • flaterje