1.0.3 • Published 10 months ago

@mudit_jain/mockapi-server v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

@mudit_jain/mockapi-server

A lightweight Node.js package to quickly mock REST APIs for development and testing. It allows you to simulate various API responses with customizable delay, failure rates, and data generation based on a schema using Faker.js.

Features

  • Dynamic Route Configuration: Define routes and behaviors with custom paths, methods (GET/POST), delay times, and failure rates.
  • Custom Error Handling: Simulate custom error responses with customizable status codes and messages.
  • Fake Data Generation: Automatically generate fake data based on provided schemas using Faker.js.
  • Logging and Dashboard: Track recent API requests with built-in logging and a simple web dashboard.
  • CLI Support: Start the mock API server using a simple command.

Installation

To install @mudit_jain/mockapi-server, use the following npm command:

npm install @mudit_jain/mockapi-server --save-dev

Usage

Starting the Mock API Server

You can start the mock API server by running:

mockapi examples/basic.mock.json

This will start the server using the routes defined in the basic.mock.json configuration file. You can modify this file to suit your needs.

Configuration Example

Create a basic.mock.json file to define the routes and behavior of your mock API. Here's an example of a configuration file:

{
  "routes": [
    {
      "method": "GET",
      "path": "/users",
      "delay": 500,
      "failRate": 0.1,
      "count": 5,
      "schema": {
        "id": "random.uuid",
        "name": "name.findName",
        "email": "internet.email"
      }
    },
    {
      "method": "POST",
      "path": "/users",
      "delay": 300,
      "failRate": 0.2
    }
  ]
}

How It Works

  1. GET Requests: Return an array of fake data based on the schema field. For example, id: "random.uuid" will generate a random UUID, name: "name.findName" will generate a random name, and email: "internet.email" will generate a random email address.
  2. POST Requests: Accept the request body and return the same payload with a 201 Created status. You can adjust failure rates for POST requests as well.

  3. Custom Errors: You can define a customError object in your configuration to simulate errors. For example:

"customError": {
  "status": 500,
  "message": "Simulated server error"
}

API Dashboard

To monitor your mock API server, visit the /__dashboard__ endpoint, where you can view the 50 most recent API requests.

http://localhost:4000/__dashboard__

CLI Command

Once installed, you can run the mock server using the CLI command:

mockapi <config_file>

This command will read the specified JSON configuration file, set up the routes, and start the server on port 4000.

Example Configuration File (basic.mock.json)

{
  "routes": [
    {
      "method": "GET",
      "path": "/users",
      "delay": 500,
      "failRate": 0.1,
      "count": 5,
      "schema": {
        "id": "random.uuid",
        "name": "name.findName",
        "email": "internet.email"
      }
    },
    {
      "method": "POST",
      "path": "/users",
      "delay": 300,
      "failRate": 0.2
    }
  ]
}

Contributing

If you'd like to contribute to this project, feel free to fork the repository, create a new branch, and submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

### Key Updates:

- **Schema**: The schema now supports using Faker functions like `random.uuid`, `name.findName`, and `internet.email` for generating fake data.
- **CLI Command**: You can run the mock API server with the `mockapi` command.
- **Dashboard**: The dashboard will show the 50 most recent API requests.
1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago