1.3.0 • Published 5 months ago

http-mock-json v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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

    KeyExampleDescription
    endpointdata/animals, data/animal/:parameterAPI route
    httpVerbGET, POSTGET, POST, PUT, PATCH, DELETE
    nameResponsesuccess, error, error-401Response name that the mock will use
    responsesA mock can have multiple responses (array), each identified with a name.
    nameResponse name (unique)
    statusCodeHTTP Status Codes
    headersHeaders in json format
    bodyResponse 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
    FlagDefaultDescription
    -p --pathrootIndicates the location of the mocks in a specific folder.
    -m --mocktrueCreate a first mock.
    -s --scripttrueAdd 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
    FlagDefaultDescription
    -p --port3500Indicates the port where the mock will be executed
    -f --pathrootIndicates 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
    FlagDefaultDescription
    -p --pathrootIndicates 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

1.3.0

5 months ago

1.2.9

5 months ago

1.2.8

8 months ago

1.2.7

8 months ago

1.2.6

8 months ago

1.2.5

8 months ago

1.2.4

8 months ago

1.2.3

8 months ago

1.2.2

8 months ago

1.2.1

8 months ago

1.1.1

8 months ago

1.1.0

8 months ago

1.0.0

8 months ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago