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

mockup-get-server

v1.0.1

Published

Create a mock server for GET requests using a JSON file.

Downloads

7

Readme

Mockup NPM version NPM monthly downloads NPM total downloads

Create a mock server for GET requests using a JSON file.


Run the program as follows: node mockup sample.json

Replace sample.json with the path to the file containing your schema.

Install and use as a package with npm install mockup-get-server

Use it in your program as:

const serve = require('mockup-get-server');

serve(data);

Here, data is an object array or a JSON string of one.


JSON schema

The JSON file, string or object passed must be an array of objects (corresponding to each API endpoint) with the relevant parameters described below.

Parameters

  • path: gives the relative URI path for the API endpoint

  • data: the JSON data to be returned by the endpoint (can be an array)

  • count (default: 10): when returning an array, determines the number of objects returned


Placeholders

Placeholders are of the form {variableName} and can be replaced with URL parameter data. Placeholders placed in strings in the data field of the schema will be replaced with the data passed via URL parameters to that endpoint.

For example, if we have User-{id} in the schema and the value for id passed in the url is 42, then the corresponding response will be User-42.


Data

The API returns JSON data as objects or arrays depending on the data parameter provided in the input JSON file.

Output data

Single object

If the data key contains an object, that object is returned by its corresponding endpoint on every call.

Example
Schema:
{
    "path": "/user",
    "data": {
      "name": "Josin",
      "powerLevel": "9001"
    }
}
Response:
{
    "name": "Josin",
    "powerLevel": "9001"
}

Object array

If data is an array, then the JSON response will be an array populated with the objects in the above mentioned array.

The count parameter will determine the number of objects returned.

If count exceeds the number of objects in the input array, iterative duplication will be used to fill the object count.

Example
Schema:
{
    "path": "/games",
    "count": 3,
    "data": [
      {
        "name": "Mario"
      },
      {
        "name": "Pokemon"
      }
    ]
}
Response:
[
    {
        "name": "Mario"
    },
    {
        "name": "Pokemon"
    },
    {
        "name": "Mario"
    }
]

Input data

Placeholders in the data field in the JSON schema will be replaced with input provided by the user via URL parameters.

Example
Schema:
{
  "path": "/gamers/:num",
  "count": 4,
  "data": [
    {
      "name": "Casual Player",
      "games": ["Mario-{num}", "Poke{num}mon"]
    },
    {
      "name": "NoobMaster {num}",
      "games": [
        {
          "name": "Pukemon",
          "timesPlayed": 6
        },
        {
          "name": "Barfio{num}",
          "timesPlayed": 11
        }
      ]
    }
  ]
}
Query:
http://localhost:9000/gamers/69
Response:
[
    {
        "name": "Casual Player",
        "games": [
            "Mario-69",
            "Poke69mon"
        ]
    },
    {
        "name": "NoobMaster 69",
        "games": [
            {
                "name": "Pukemon",
                "timesPlayed": 6
            },
            {
                "name": "Barfio69",
                "timesPlayed": 11
            }
        ]
    },
    {
        "name": "Casual Player",
        "games": [
            "Mario-69",
            "Poke69mon"
        ]
    },
    {
        "name": "NoobMaster 69",
        "games": [
            {
                "name": "Pukemon",
                "timesPlayed": 6
            },
            {
                "name": "Barfio69",
                "timesPlayed": 11
            }
        ]
    }
]