npm package: @mudit_jain/mockapi-server
Description: A simple mock API server with delays , failures and dashboard UI
Author: Mudit
Latest version: 1.0.3

# @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](https://github.com/marak/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:

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

## Usage

### Starting the Mock API Server

You can start the mock API server by running:

```bash
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:

```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
    }
  ]
}
```

### 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:

```json
"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.

```bash
http://localhost:4000/__dashboard__
```

### CLI Command

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

```bash
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`)

```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](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.