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

stubcell

v0.8.4

Published

stub server tool

Downloads

578

Readme

Stubcell Build Status

Stub server to develop client-side project.

Features

Stubcell has the following features.

  • emulate response files
  • response value can be written in JSON5 (JSON file is more readable and human friendly ♡)
  • validate JSON in stub server.
  • Support JSON-RPC (2014/05/14)
  • Support querystring and body (2014/05/21)
  • Support recording json (2014/05/23)
  • Support Command Line tool (2014/05/26)
  • Support Cross-Origin Resource Sharing (2014/07/24)

Difference from other stub tools

Almost Stub servers have some disappointing points.

  • cannot validate JSON.
  • does not use JSON5, so i cannot write comments in the JSON file and cannot write trailing comma.

For example:

  • stubby always launch https server and cannot validate json, so if json is invalid, you should check the value in client side.

  • easymock is so simple, but it depends on sqlite3, and could not write comment in JSON file and could not validate json.

stubcell can check JSON and write comments in your JSON file, so you can write the specification in detail. stubcell is more simple than others and dependent libraries are small.

How to use

Install

$ npm install stubcell

need to write entry yaml

-
  # request content
  request:
    # request url
    # :id is id params, so it matches /test/aaa, /test/1, /test/hello
    url: /test/:id
    # http method, GET, POST, PUT, DELETE
    method: GET
  # response content
  response:
    # status value 200, 404, 500 etc..
    status: 200
    # response body json path.
    file: test/id.json
-
  request:
    url: /test/
    method: get
  response:
    status: 200
    file: test.json

# if response/file is not specified, url and method become the response filepath
# like /abc/abc_get.json
# the algorithm is <url>_<method>.json
# if url is /echo/yosuke/hoge and method is PUT and entry.yaml is /usr/test/stubcell/entry.yaml
# stubcell looks up /usr/test/stubcell/echo/yosuke/hoge_put.json
-
  request:
    url: /abc/abc
    method: get
  response:
    status: 200

# support json-rpc
-
  request:
    url: /jsonrpc
    method: POST
    body:
      # if your server accept jsonrpc, need jsonrpc prop.
      jsonrpc: 2.0
      # need jsonrpc method prop.
      method: sum
  response:
    status: 200
    file: jsonrpc/sum.json

# support querystring and body
-
  request:
    url: /querystringbody
    method: POST
    # can write query
    query:
      q: yosuke
    # can write body
    body:
      test: 123
  response:
    status: 200
    file: querystringbody.json

# support writing body directly
-
  request:
    url: /write/directly
    method: GET
  response:
    status: 200
    body: '{"message": "hello"}'

need to write jsons

test/example.json

{
  // test comment
  // we can write comment in JSON.
  message : "Hello world", // can write trailing comma.
}

jsonrpc/sum.json

// for jsonrpc
// !NOTE! only write result value
// not include id and jsonrpc properties.
{
  // sum [123, 456]
  result: 579
}

sample code

var StubCell = require("../index");
var stubcell = new StubCell();
var http = require("http");
stubcell.loadEntry(__dirname + "/example.yaml");
var app = stubcell.server();
var server = app.listen(3000);

http.get("http://localhost:3000/test/1", function(res){
  var data = '';
  res.on("data", function(chunk) {
    data += chunk;
  });
  res.on("end", function() {
    try {
      // { "message" : "Hello world" }
      console.log(data);
      server.close();
    } catch (e) {
      console.error(e);
    }
  });
});

options

var StubCell = require("../index");
var stubcell = new StubCell();
var http = require("http");
var options = {
  // show more detail information
  debug : true, // default is false
  // json base path, stubcell return json from  basepath + "/" + filepath
  basepath : "", // default is yaml parent dir.
  // request to backend server and record json file.
  record: {
    // show more detail information
    debug : true, // default is false
    // json store base path
    basepath: "", // default is options.basepath
    // request redirectTo.
    target : "http://echo.jsontest.com" // default is http://localhost:3001
  },
  // loose compare request params (headers, query, body) and entry
  looseCompare: true,
  // cors option (default true)
  cors: false,
  //   or set each headers
  cors: {
    "Access-Control-Allow-Origin": "example.com",
    "Access-Control-Allow-Methods": "GET",
    "Access-Control-Allow-Fields": "secret"
  }
};
stubcell.loadEntry(__dirname + "/example.yaml", options);
var app = stubcell.server();
var server = app.listen(3000);

http.get("http://localhost:3000/test/1", function(res){
  var data = '';
  res.on("data", function(chunk) {
    data += chunk;
  });
  res.on("end", function() {
    try {
      // { "message" : "Hello world" }
      console.log(data);
    } catch (e) {
      console.error(e);
    }
  });
});

http.get("http://localhost:3000/hello/world", function(res){
  var data = '';
  res.on("data", function(chunk) {
    data += chunk;
  });
  res.on("end", function() {
    try {
      // { "hello": "world" }
      // and record the json to __dirname/hello/world.json
      console.log(data);
    } catch (e) {
      console.error(e);
    }
  });
});

Use finally routing

you can use finally routing if any request setting matches a current request. set request.url = $finally, it is replaced with * in app's routing.

-
  request:
    url: $finally
    method: ALL
  response:
    status: 200
    body: '{message: "you look me!"}'

How to use in CLI

$ npm install stubcell -g
$ stubcell --port 3000 --entry ./entry.yaml --record_target http://echo.jsontest.com

commandline arguments


  Usage: stubcell [options]

  Options:

      -h, --help                            output usage information
      -V, --version                         output the version number
      --port <n>                            server start port, default is 8090
      --entry [entry filepath]              entry yaml file, default is /Users/furukawa.yosuke/Program/stubcell/entry.yaml
      --record_target [record target server]  record target server, default is null (no record file)
      --silent                              hide detail info, default is false

How to use in some other frontend tools