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

connect-api-mocker-adv-v2

v0.2.1

Published

Middleware to mock connect request

Downloads

7

Readme

Advanced API mocker

This is a connect.js middleware to mock http request with manual body, headers and status code. Used to mock REST API.

Usage

Add this this middleware to your config. Example of configuring gulp-connect with API root '/api' and mock location in directory `mocks'

gulpConnect.connect.server({
middleware: function (connect, opt) {
  var mocker = require('connect-api-mocker-adv'),
    options = {
      urlRoot: '/api',
      pathRoot: 'mocks'
    };

  return [mocker(options)];
}
});

If don't want use gulp, you can use without

var connect = require('connect');
var http = require('http');
var mocker = require('connect-api-mocker-adv')
 
var app = connect();

 var   options = {
      urlRoot: '/api',
      pathRoot: 'mocks',
      ignoreQuery: false
    };
    
app.use(mocker(options));

http.createServer(app).listen(3000);

Firstly mocks will be served, than other middleware.

Structure

Mock file will be searched from pathRoot according to request url and request method. For example if pathRoot is 'mocks', method is 'GET' and url is '/api/collection mock files will be searched under './mocks/api/collection/GET.yaml'. Filename int the upper case.

Disable subtree

Adding file disabled without content or with true will disable all subtree from current path

Mock file structure

Mock file is a yaml config file with next keys:

  • status (number) - Response status code, default is 200

  • headers (object) - Headers which will be appended to default headers

  • disabled (boolean) - Is this mock disabled

  • body (object|string) - Response body. Can be an object, that will be serialized to json or simple text (use | or > for multiline text)

Example

status: 200
disabled: false
headers:
  X-Header: header-value
body:
  prop: val

Or only text body

body: |
  This is text body line breaks.
  Use > to disable line breaks

Or with json

body: |
  {
    "key": "value",
    "subKey": {
      "subObjectKey":"subObjectValue"
    }
  }

API

mocker(options)

options.urlRoot

Required

Type: String

URL root for api. If url equals to * then it will try to mock all requests

options.pathRoot

Required

Type: String

Root location of mock files

options.headers

Type: Object

Default: {'Content-Type': 'application/json; charset=utf-8'}

Response headers for all requests

options.forced

Type: Boolean

Default: false

If true all disabled mocks will be served

options.mockAll

Type: Boolean

Default: false

All request will be mocked. If mock not found or disabled response with status 500 will be returned

options.urlMangler

Type: Function

Modify request url. Arguments:

  • url - request url without query string
  • request - request object

options.speedLimit

Type: Number

Default: 0 (unlimited)

Limit speed of response in KB

options.ignoreQuery

Type: Boolean

Default: true

If true querystring is ignored.

  • If false - each mock folder will be checked for custom query string.

  • Format of subfolders with query: #[parameterName[=parameterValue]]