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

@8pattern/jmock

v0.2.0

Published

a mock server for JS

Downloads

12

Readme

jMock

A mock server for JS, support HTTP methods and Websocket

Install

npm install @8pattern/jmock

Usage

MockData Difination

The MockData is defined as following:

{
	[Path]: {
		[Method1]: [MockItem1],
		[Method2]: [MockItem2],
	},
	[Path2]: { ... }
}

For example:

{
	'/mock/item/:productName': {
		GET: '@name',
		POST: function(cb, req, param) {
			if (req.id) {
				cb({ id: req.id, name: param.productName })
			} else {
				cb({ desc: 'error' })
			}
		}
	},
	'/mock/item2/.*': {
		PUT: {
			'name|+1': number
		}
	},
	'/ws/mock': {
		WS: (cb, req) => {
		    if (req.id) {
			cb('@name')
		    }
		}
	}
}
  • Path <string>

    1. It can receive an exact, fuzzy or regular expression pattern to match the request url.

      • Exact: the url must match the whole pattern, e.g., "/a/b"
      • Fuzzy: support ":<name>" or "*" to match the url.
      • :<name>: match only a sub route, i.e. "/:url" can match "/a", but can NOT match "/a/b"
      • *: match any routes, i.e., "/route/*" can match "/route/1" and "/route/1/2", but can NOT match "/1/2"
      • Reg: use regular expressions as the pattern, i.e., "/(.*?)/(?<id>.>?)"
      • Be aware NOT define it as RegExp directly, i.e., use 'a/b/.*' rather than /a/b/.*/. (Because object can't receive RegExp as a key.)
    2. The matched string can be found from the third argument of function.

      // URL: /a/b
      // PATTERN: "/:r1/*" or "/(?<r1>.*)/(.*)"
      (cb, req, param) => {
      	console.log(
      		param[0], // "a"
      		param[1], // "b"
      		param.r1, // "a"
      	)
      } 
    3. priority: Exact > Fuzzy > Reg

  • Method<string>

    1. All HTTP methods supported by Express.js also SUPPORTED by us.

    2. Particularly, WS will be used to present the websocket method.

  • MockItem: <string> | <object> | <array> | <function>

    1. string / object / array
    'hello world' | { hello: 'world' } | ['hello', 'world']

    Thanks to mockjs, which provides a wonderful data generator, ALL its template strings will also work well in jMock. For example:

    '@name' => "Sharon Walker"
    { "number|1-100": 100 } => { "number": 201 }
    1. function

      • argument: callback<function>, reqParams<object> routeParams<object>
      • return:
    	function(cb, req, param) {
    		if(req.id === 0) {
    			cb('success')
    		} else if (param.name) {
    			cb('success')
    		} else {
    			setTimeout(() => {
    				cb('fail')
    			}, 200)
    		}
    	}

    The callback function also receive a MockItem (except function) as the only argument, so the grammer of mockjs also works. For example:

     (cb) => {
        cb({ name: '@name' })
     }
     // same as { name: '@name' }

Run as CMD

  1. firstly, you should prepare a mock data file, and export it by CommonJS. For example:
module.exports = {
	'/mock/name': {
        'GET': '@name'
    }
}
  1. then, code the command in the shell.
jmock [--file=./mockdata.js] [--port=3000]

Two arguments should be defined:

  • mock file: --file or -f
    1. e.g., --file=./mockdata.js.
    2. if not asign it, the mock data will be {} by default
  • port: --port or -p
    1. e.g., --port=3001

    2. 3000 by default

Run as Script

const JMock = require('@8pattern/jmock')

const port = 3000
const mockData = {
	'/mock/name': {
        'GET': '@name'
    }
}

const jmock = new JMock(data)
jmock.start(port)

ENJOY YOUR MOCK DATA NOW!