raml-1-mocker

0.1.2 • Public • Published

raml-1-mocker

Node module to create mock API service based on RAML 1.0 definition. Forked from https://github.com/repocho/raml-mocker and remaked.

Using RAML 1.0 mock-service

For single file ...

var ramlMocker = require('raml-1-mocker');
var options = {
    path: 'test/raml'
};
var callback = function (requests){
    console.log(requests);
};
ramlMocker.generate(options, callback);

... or for collection of files

var options = {
    files: ['definition1.raml', 'folder/definition2.raml']
};

The result of execution:

[
    {
        /** URI of the request to mock {string|RegExp}*/
        uri: '/test/:id/objectDef', // or RegExp: /\/test\/\d+/
        /** Method of the request (get, post, ...) */
        method: 'get',
        /** Function by default to return the mock (codes 2XX defined in the RAML). */
        mock: [Function],
        /** If you don't define a 2XX code or want to use randomly other code responses. You can use this function
          * Just use instead of mock(); -> mockByCode(418);
          */
        mockByCode: [Function](code),
        /** Function by default to return the example (codes 2XX defined in the RAML). */
        example: [Function],
        /** The same as mockByCode but applied to examples */
        exampleByCode: [Function](code)
    }
]

Callback for [express] app

var callback = function (requestsToMock){
    _.each(requestsToMock, function(reqToMock){
        app[reqToMock.method](reqToMock.uri, function(req,res){
            var code = 200;
            if (reqToMock.defaultCode) {
                code = reqToMock.defaultCode;
            }
            res.send(code ,reqToMock.mock());
        });
    });
};

Used

History Log

0.0.1
  • Basic functional

Dependencies (6)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i raml-1-mocker

    Weekly Downloads

    0

    Version

    0.1.2

    License

    MIT

    Last publish

    Collaborators

    • artemkaint