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

swagmock

v1.0.0

Published

Mock data generator for swagger api

Downloads

3,429

Readme

swagmock

Mock data generator for swagger api

Install

npm install swagmock

Usage

    var apiPath = 'http://petstore.swagger.io/v2/swagger.json';
    var Swagmock = require('swagmock');
    var mockgen = Swagmock(apiPath);
    var assert = require('assert');

    mockgen.responses({
        path: '/pet/findByStatus',
        operation: 'get',
        response: 200
    }, function (error, mock) {
        assert.ifError(error);

        console.log(mock);
        //This would print:
        // {
        //     "responses": [{
        //         "id": 2530624032210944,
        //         "category": {
        //             "id": 8200505595527168,
        //             "name": "r($vA&"
        //         },
        //         "name": "doggie",
        //         "photoUrls": ["p0x1", "6O)3*kO"],
        //         "tags": [{
        //             "id": 4590764340281344,
        //             "name": "WCTA6f!"
        //         }, {
        //             "id": -4614156653166592,
        //             "name": "e"
        //         }],
        //         "status": "pending"
        //     }]
        // }
    });

    mockgen.parameters({
        path: '/pet/findByStatus',
        operation: 'get'
    }, function (error, mock) {
        assert.ifError(error);

        console.log(mock);
        //This would print:
        // {
        //     "parameters": {
        //         "query": [{
        //             "name": "status",
        //             "value": [ 'available', 'pending' ],
        //             "separator": "multi"
        //         }]
        //     }
        // }
    });

Check Examples for more details on mock generators.

API

Swagmock(apiPath)

  • apiPath - (String) - (required) - The url or local path of the Swagger api.

responses

mockgen.responses(options, callback)

This generates the mock response objects based on the options

  • options - (Object) - (required) - Options to control the mock generation.

  • callback - (Function) - (required) - function (error, mock).

options

  • path - (String) - (optional) - The path for which the response mock need to be generated. For example /pet/findByStatus, /pet etc. If a path is not specified, mock response will be generated for all the paths defined by the swagger api.

  • operation - (String) - (optional) - The operation for which the response mock need to be generated. For example get, post etc. If operation is not specified, mock response will be generated for all the operations defined by the swagger api.

  • response - (String) - (optional) - The response for which the response mock need to be generated. For example 200, 400, default etc. If response is not specified, mock response will be generated for all the responses defined by the swagger api.

parameters

mockgen.parameters(options, callback)

This generates the mock parameters objects based on the options

  • options - (Object) - (required) - Options to control the mock generation.

  • callback - (Function) - (required) - function (error, mock).

options

  • path - (String) - (optional) - The path for which the parameters mock need to be generated. For example /pet/findByStatus, /pet etc. If a path is not specified, mock parameters will be generated for all the paths defined by the swagger api.

  • operation - (String) - (optional) - The operation for which the parameters mock need to be generated. For example get, post etc. If operation is not specified, mock parameters will be generated for all the operations defined by the swagger api.

requests

mockgen.requests(options, callback)

This generates the mock request object based on the options. requests API resolves the parameters mock data to generate the request mock object useful for unit tests.

  • options - (Object) - (required) - Options to control the mock generation.

  • callback - (Function) - (required) - function (error, mock).

options

  • path - (String) - (optional) - The path for which the parameters mock need to be generated. For example /pet/findByStatus, /pet etc. If a path is not specified, mock parameters will be generated for all the paths defined by the swagger api.

  • operation - (String) - (optional) - The operation for which the parameters mock need to be generated. For example get, post etc. If operation is not specified, mock parameters will be generated for all the operations defined by the swagger api.

data

request Object will have following possible properties query, header, pathname, path, formData or body based on the parameters defined for the path and operation.

Mock request Path templates are resolved using path parameters.

    mockgen.requests({
        path: '/pet/findByStatus',
        operation: 'get'
    }, function (error, mock) {
        assert.ifError(error);

        console.log(mock);
        //This would print:
        // {
        //     "request": {
        //         "query": "status=available&status=pending"
        //     }
        // }
    });

Examples

API

Usage

Unit test request mocks

github api express app

slack api hapi app

Mock response data providers

spotify api hapi app

glugbot api express app