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

demock

v0.4.3

Published

Declarative mocking

Downloads

24

Readme

demock

Build Status

A library-agnostic API mocking library.

It's extremely lightweight, flexible and follows UMD.

bower install --save-dev demock

Or:

npm install --save-dev demock

The why and what

Backend-less UI Development

How it works

  1. Hook into your application's HTTP transport to intercept requests.
  2. Pass a request through demock, which in turn passes it through all the configured request filters.
  3. Pass the filtered request down to the HTTP transport to make the actual HTTP request.
  4. On receiving the response, pass the response along with the original request through demock, which in turn passes it through all the configured response filters.
  5. Pass the fitered response up to the application.

Usage

  1. Include Demock
  2. Include a transport adaptor
  3. Enable some filters

Two transport adaptors are readily available:

  • jQuery bower install --save-dev demock-jquery
  • Angular bower install --save-dev angular-demock

API

Get a new demock instance:

var demock = new Demock();

Methods

.addRequestFilter(filter)

Adds a filter to the request filter chain. A filter is a function that takes in a request object:

demock.appendRequestFilter(function (request) {
    // manipulate the request
});

.addResponseFilter(filter)

Adds a filter to the response filter chain. A filter is a function that takes in a request and a response object:

demock.appendResponseFilter(function (request, response) {
    // manipulate the request and response
});

.filterRequest(request)

Runs all request filters on an abstract request object:

demock.filterRequest(request);

.filterResponse(request, response)

Runs all response filters on an abstract response object. The abstract request object that's used for .filterRequest() is used again:

demock.filterResponse(request, response);

Objects

request

An abstract representation of an HTTP request. Has the following properties:

Example
{
    method: 'GET',
    url: '/api/users',
    params: { id: 1, sortKey: 'name' },
    headers: { 'X-Custom': 'foo' }
}

response

An abtract representation of an HTTP response. Has the following properties:

Example
{
    statusCode: 200,
    statusText: 'OK',
    data: [{ name: 'John' }, { name: 'Jane' }],
    headers: { 'Content-Type': 'application/json' }
}

Stock request filters

Demock comes with stock request filters as properties of the Demock.requestFilters object.

method

  • Changes the method of non-GET requests to GET
  • Appends the original method name to the path
  • Includes the original request parameters as "X-Request-*" HTTP headers

Example

demock.appendRequestFilter(Demock.requestFilters.method());

var request = {
    method: 'POST',
    url: '/api/foo',
    params: {
        a: 1,
        b: 2
    }
};

demock.filterRequest(request);

/* request becomes:
{
    method: 'GET',
    url: '/api/foo/POST',
    params: {
        a: 1,
        b: 2
    }
    headers: {
        'X-Request-Param-a': 1,
        'X-Request-Param-b': 2
    }
}
*/

defaultDocument

Appends a default document to the path. This is useful when you don't want to configure your static web server with a custom default document (i.e. something other than index.html, etc.)

The filter instance expects a configuration object with the defaultDocument property.

Example

demock.appendRequestFilter(Demock.requestFilters.defaultDocument({
    defaultDocument: 'data.json'
});

var request = {
    method: 'GET',
    url: '/api/foo'
};

demock.filterRequest(request);

/* request becomes:
{
    method: 'GET',
    url: '/api/foo/data.json'
}
*/

Built-in response filtering

The only response filtering that is built-in is the data replacement filter. Response filters can make use of this filter for nested application of response filters on response data.

Example

var response = {
    data: {
        $data: {
            a: 1
        }
    }
};

demock.filterResponse(request, response);

/* response becomes:
{
    data: {
        a: 1
    }
}
*/

Stock response filters

Demock comes with stock response filters as properties of the Demock.responseFilters object.

delay

Sets the delay property of the response object to simulate latency.

Example

demock.appendResponseFilter(Demock.responseFilters.delay());

var response = {
    data: {
        $delay: 250,
        $data: {
            a: 1
        }
    }
};

demock.filterResponse(request, response);

/* response becomes:
{
    delay: 500,
    data: {
        a: 1
    }
}
*/

status

Overrides the response status code and text.

Example

demock.appendResponseFilter(Demock.responseFilters.status());

var response = {
    statusCode: 200,
    statusText: 'OK',
    data: {
        $status: {
            code: 500,
            text: 'Internal Server Error'
        },
        $data: {
            a: 1
        }
    }
};

demock.filterResponse(request, response);

/* response becomes:
{
    statusCode: 500,
    statusText: 'Internal Server Error',
    data: {
        a: 1
    }
}
*/

timeout

switch

Transport adaptors

A typical transport adaptor would do:

// Intercept request
// ...

// Compose abstract request object from original request configuration:
var request = {
    method: httpConfig.method,
    url: httpConfig.url,
    params: httpConfig.params,
    headers: httpConfig.headers
};

// Run request filters:
demock.filterRequest(request);

// Convey changes from the abstract request to the real request configuration and perform the request:
httpConfig.method = request.method;
httpConfig.url = request.url;
httpConfig.params = request.params;
httpConfig.headers = request.headers;

// Perform request
// ...

// Compose abstract response object from original response object:
var response = {
    statusCode: httpResponse.statusCode,
    statusText: httpResponse.statusText,
    headers: httpResponse.headers,
    data: httpResponse.data
};

// Run response filters:
demock.filterResponse(request, response);

// Convey changes from the abstract response to the real response:
httpResponse.statusCode = response.statusCode;
httpResponse.statusText = response.statusText;
httpResponse.headers = response.headers;
httpResponse.data = response.data;

// Return modified response
// ...