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

http-mock-json

v1.2.8

Published

Allows to create a mock server and test the frontend without depending on the backend.

Downloads

888

Readme

HTTP MOCK JSON

Allows to create a mock server and test the frontend without depending on the backend.

Installation and use 🔧

  1. Install library.

    npm install http-mock-json --save-dev
  2. Run the command.

    mock-server init
  3. Follow the instructions as in the example image.

  4. A mocks folder will be created that will contain a first endpoint.

    Mock structure

    | Key | Example | Description | |--------------|------------------------------------------|----------------------------------------------------------------------------| | endpoint | data/animals, data/animal/:parameter | API route | | httpVerb | GET, POST | GET, POST, PUT, PATCH, DELETE | | nameResponse | success, error, error-401 | Response name that the mock will use | | responses | | A mock can have multiple responses (array), each identified with a name. | | name | | Response name (unique) | | statusCode | | HTTP Status Codes | | body | | Response in json format |

  5. Add the responses you want to simulate in the body. (You can change the mock response by changing the nameResponse).

  6. Execute command

    mock-server start

Commands ⚙️

  1. init

    Create the folder that will contain the mocks.

    mock-server init

    | Flag | Default | Description | |-------------|---------|-----------------------------------------------------------| | -p --path | root | Indicates the location of the mocks in a specific folder. | | -m --mock | true | Create a first mock. | | -s --script | true | Add script to start the mock in the package.json file. |

    Example:

    mock-server init --path apps/folder1 --mock false --script false
  2. start

    Start mock server.

    mock-server start

    | Flag | Default | Description | |-----------|---------|-----------------------------------------------------------| | -p --port | 3500 | Indicates the port where the mock will be executed | | -f --path | root | Indicates the location of the mocks in a specific folder. |

    Example:

    mock-server start --port 3001 --path apps/folder1
  3. add

    Create a mock.

    mock-server add

    | Flag | Default | Description | |-----------|---------|-----------------------------------------------------------| | -p --path | root | Indicates the location of the mocks in a specific folder. |

    Example:

    mock-server add --path apps/folder1

Recommendations 📋

  • Review the advanced examples.
  • A single json file can contain many mocks.
  • There can be many json files each with their respective mocks.

Advanced examples

{
   "data/animals": {
      "GET": {
         "nameResponse": "AnimalsError",
         "responses": [
            {
               "name": "AnimalsList",
               "statusCode": "200",
               "body": {
                 "example": "data"
               }
            },
            {
               "name": "AnimalsError",
               "statusCode": "404",
               "body": {
                 "example-error": "error"
               }
            }
         ]
      },
      "POST": {
         "nameResponse": "AnimalsError",
         "responses": [
            {
               "name": "AnimalsSave",
               "statusCode": "201",
               "body": {
                 "example": "data"
               }
            },
            {
               "name": "AnimalsError",
               "statusCode": "404",
               "body": {
                 "example-error": "error"
               }
            }
         ]
      }
   }
}
{
   "data/animals/:id": {
      "GET": {
         "nameResponse": "AnimalsList",
         "responses": [
            {
               "name": "AnimalsList",
               "statusCode": "200",
               "body": {
                 "example": "data"
               }
            },
            {
               "name": "AnimalsError",
               "statusCode": "404",
               "body": {
                 "example-error": "error"
               }
            }
         ]
      },
      "POST": {
         "nameResponse": "AnimalsSave",
         "responses": [
            {
               "name": "AnimalsSave",
               "statusCode": "201",
               "body": {
                 "example": "data"
               }
            },
            {
               "name": "AnimalsError",
               "statusCode": "404",
               "body": {
                 "example-error": "error"
               }
            }
         ]
      }
   },
   "data/brands": {
      "GET": {
         "nameResponse": "BrandsList3",
         "responses": [
            {
               "name": "BrandsList",
               "statusCode": "200",
               "body": {
                 "example": "data1"
               }
            },
            {
               "name": "BrandsList2",
               "statusCode": "200",
               "body": {
                 "example": "data2"
               }
            },
            {
               "name": "BrandsList3",
               "statusCode": "200",
               "body": {
                 "example": "data3"
               }
            }
         ]
      }
   }
   
}

License 📖

http-mock-json is MIT licensed.

Author ✒️

Alejandro Rodriguez Romero