cli-mockserver
v1.2.0
Published
create mock server on the go from ui or json schema
Maintainers
Readme
🚀 MockServer
A powerful CLI tool to spin up mock servers instantly with built-in chaos engineering capabilities. Perfect for frontend development when backend APIs aren't ready yet.
✨ Features
- 🎯 Quick Setup - Initialize and start mock servers in seconds
- 📱 Ready-Made Templates - Social media, e-commerce, and basic templates
- 🔥 Chaos Engineering - Test error handling with configurable failure rates
- 📊 Pagination Support - Automatic pagination for large datasets
- 🎨 Pretty Logging - Beautiful, colorful CLI output
- ⚡ Hot Reload - Watch mode for schema changes
- 🌐 Full REST Support - GET, POST, PUT, PATCH, DELETE methods
- 📝 Rich Data Types - 20+ field types powered by Faker.js
📦 Installation
npm install -g cli-mockserverOr use locally in your project:
npm install --save-dev cli-mockserver🚀 Quick Start
1. Initialize a new schema
mockserver initChoose from templates:
- 📱 Social Media (posts, comments, users, likes, etc.)
- 🛒 E-commerce (products, orders, cart)
- 📝 Basic (simple REST endpoints)
2. Start the server
mockserver startYour mock API is now running at http://localhost:9500!
3. Test your endpoints
curl http://localhost:9500/api/users
curl http://localhost:9500/api/posts
curl http://localhost:9500/api/posts/123/comments📖 Commands
start - Start the mock server
mockserver start [options]
Options:
-p, --port <port> Override port from schema
-h, --host <host> Host address (default: localhost)
--no-chaos Disable chaos engineering
-w, --watch Watch schema file for changesinit - Initialize a new schema
mockserver init [options]
Options:
-t, --template <name> Use template (social, ecommerce, basic)
-f, --force Overwrite existing schemavalidate - Validate schema.json
mockserver validate [options]
Options:
-s, --schema <path> Path to schema fileinfo - Show schema information
mockserver infogenerate - Generate static JSON files
mockserver generate [options]
Options:
-o, --output <dir> Output directory (default: ./mock-data)🎨 Field Types
MockChaos supports 20+ field types:
Basic Types
uuid- Unique identifieremail- Email addressusername- UsernamefirstName- First namelastName- Last namesentence- Random sentenceparagraph- Random paragraphword- Random wordavatar- Avatar image URLimage- Random image URLurl- Random URLcity- City nametimezone- Timezone stringboolean- true/false
Date Types
date:past- Past datedate:recent- Recent date (last few days)date:future- Future datedate:recent:nullable- Recent date or null
Advanced Types
integer:min:max- Integer in range (e.g.,integer:0:100)enum:a,b,c- Random from list (e.g.,enum:admin,user,guest)array:type:min:max- Array of items (e.g.,array:image:0:5)image:type- Typed image (e.g.,image:landscape,image:portrait)
Nested Objects
{
"fields": {
"user": {
"id": "uuid",
"name": "firstName",
"email": "email"
}
}
}🔥 Chaos Engineering
Enable chaos mode to test error handling:
{
"chaos": {
"enabled": true,
"globalErrorRate": 0.05,
"scenarios": {
"timeout": 0.02,
"serverError": 0.03,
"networkError": 0.02
}
}
}Set resource-specific error rates:
{
"resources": [
{
"endpoint": "/api/users",
"errorConfig": {
"rate": 0.8,
"code": 503,
"message": "Database Connection Failed"
}
}
]
}📊 Pagination
Enable pagination for large datasets:
{
"resources": [
{
"endpoint": "/api/posts",
"count": 100,
"pagination": {
"enabled": true,
"pageSize": 15
}
}
]
}Query with pagination:
curl http://localhost:9500/api/posts?page=2&limit=10Response includes pagination metadata:
{
"data": [...],
"pagination": {
"page": 2,
"limit": 10,
"total": 100,
"totalPages": 10,
"hasNext": true,
"hasPrev": true
}
}🎯 Example Schema
Here's a complete social media schema:
{
"port": 9500,
"host": "localhost",
"chaos": {
"enabled": true,
"globalErrorRate": 0.05
},
"resources": [
{
"id": "users",
"name": "Users",
"endpoint": "/api/users",
"method": "GET",
"count": 50,
"pagination": {
"enabled": true,
"pageSize": 20
},
"fields": {
"id": "uuid",
"username": "username",
"email": "email",
"avatar": "avatar",
"verified": "boolean",
"followersCount": "integer:0:10000",
"bio": "sentence",
"createdAt": "date:past"
}
}
]
}🛠️ Development
# Clone the repository
git clone https://github.com/himasnhu-at/mockserver.git
cd mockserver
# Install dependencies
npm install
# Build
npm run build
# Development mode
npm run dev start🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
BSD-3-Clause License - feel free to use this in your projects!
🙏 Acknowledgments
- Powered by Faker.js for realistic data generation
- Built with Express and Commander
- Beautiful CLI with Chalk and Ora
Made with ❤️ for frontend developers who are tired of waiting for backend APIs
