@mudit_jain/mockapi-server
v1.0.3
Published
A simple mock API server with delays , failures and dashboard UI
Maintainers
Readme
@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-devUsage
Starting the Mock API Server
You can start the mock API server by running:
mockapi examples/basic.mock.jsonThis 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
GET Requests: Return an array of fake data based on the
schemafield. For example,id: "random.uuid"will generate a random UUID,name: "name.findName"will generate a random name, andemail: "internet.email"will generate a random email address.POST Requests: Accept the request body and return the same payload with a
201 Createdstatus. You can adjust failure rates for POST requests as well.Custom Errors: You can define a
customErrorobject 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.