A production-ready TypeScript microservice for simulating ONDC (Open Network for Digital Commerce) protocol flows. This service provides mock endpoints for testing buyer and seller interactions in the ONDC ecosystem.
- Flow Management: Create and manage ONDC transaction flows
- Mock Response Generation: Automated payload generation for ONDC protocol actions
- Dual Queue Support: In-memory queue for development, RabbitMQ for production
- Redis Caching: Dual database support for session and configuration management
- Transaction Tracking: Complete API lifecycle tracking and status monitoring
- Schema Validation: JSON Schema validation with AJV
- Custom ESLint Rules: Enforce best practices in response handling
- Comprehensive Logging: Structured logging with Pino and Loki integration
- Health Monitoring: Built-in health checks and metrics
- TypeScript: Full type safety and modern JavaScript features
- Node.js: v18+ recommended
- Redis: v6+ (for caching)
- RabbitMQ: v3+ (optional, for production queue)
- npm or yarn
# Clone the repository
git clone <repository-url>
cd automation-mock-playground-service
# Install dependencies
npm install
# Set up environment variables
cp .env.example .env
# Edit .env with your configurationCreate a .env file in the root directory:
# Application
NODE_ENV=development # development | production
PORT=3000 # Server port
LOG_LEVEL=info # Logging level
# External Services
API_SERVICE_URL=http://api-service.local
# Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_USERNAME=
REDIS_PASSWORD=
REDIS_DB_0=0 # Workbench cache
REDIS_DB_1=1 # Config cache
# RabbitMQ Configuration (Production)
RABBITMQ_URL=amqp://localhost:5672# Using Docker
docker run -d --name redis -p 6379:6379 redis:latest
# Using Homebrew (macOS)
brew install redis
brew services start redis# Using Docker
docker run -d --name rabbitmq \
-p 5672:5672 \
-p 15672:15672 \
rabbitmq:3-management
# Access management UI at http://localhost:15672
# Default credentials: guest/guestnpm run devUses nodemon and tsx for hot-reloading during development.
# Build TypeScript to JavaScript
npm run build
# Start the production server
npm startnpm run build && npm starthttp://localhost:3000/mock/playground
POST /flow/new
Start a new transaction flow.
Request Body:
{
"session_id": "session-123",
"flow_id": "buyer-search-flow",
"transaction_id": "txn-456", // Optional, auto-generated if not provided
"inputs": {
"search_query": "electronics"
}
}Response:
{
"success": true,
"data": {
"transaction_id": "txn-456",
"status": "started"
}
}POST /flow/proceed
Continue an existing transaction flow.
Request Body:
{
"session_id": "session-123",
"transaction_id": "txn-456",
"inputs": {
"selected_items": ["item-1", "item-2"]
}
}GET /flow/current-status?transaction_id=txn-456&session_id=session-123
Retrieve the current status and progress of a flow.
Response:
{
"success": true,
"data": {
"sequence": [
{
"action": "search",
"status": "completed",
"timestamp": "2026-02-03T10:00:00Z"
},
{
"action": "on_search",
"status": "completed",
"timestamp": "2026-02-03T10:00:01Z"
},
{
"action": "select",
"status": "pending"
}
],
"missedSteps": [],
"reference_data": {}
}
}POST /manual/:action
Trigger a specific ONDC action manually.
Example:
POST /mock/playground/manual/searchGET /health
Check service health status.
Response:
{
"success": true,
"data": {
"status": "healthy",
"uptime": 12345,
"timestamp": "2026-02-03T10:00:00Z"
}
}# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:cov
# Run multi-instance tests
npm run test:multi-instance# Check for linting errors
npm run lint
# Auto-fix linting errors
npm run lint:fix# Check code formatting
npm run format:check
# Auto-format code
npm run formatnpm run type-checkautomation-mock-playground-service/
βββ src/
β βββ cache/ # Caching layer (Redis, in-memory)
β βββ config/ # Configuration management
β βββ container/ # Dependency injection container
β βββ controllers/ # Request handlers
β βββ errors/ # Custom error classes
β βββ middlewares/ # Express middlewares
β βββ queue/ # Queue implementations (InMemory, RabbitMQ)
β βββ routes/ # API route definitions
β βββ service/ # Business logic
β β βββ cache/ # Cache services
β β βββ flows/ # Flow processing logic
β β βββ jobs/ # Background job handlers
β βββ types/ # TypeScript type definitions
β βββ utils/ # Utility functions
β βββ validations/ # Schema validation
β βββ index.ts # Application entry point
β βββ server.ts # Express server setup
βββ .env.example # Environment variables template
βββ package.json
βββ tsconfig.json
βββ README.md
Centralized dependency injection for managing services:
const container = ServiceContainer.getInstance();
const queueService = container.getQueueService();
const cacheService = container.getCacheService0();Supports both in-memory (development) and RabbitMQ (production):
- InMemoryQueue: Simple in-process queue
- RabbitMQ: Distributed queue with retries, DLQ, and monitoring
See Queue Service Documentation for details.
Two Redis databases for different purposes:
- DB 0: Workbench cache (sessions, transactions)
- DB 1: Configuration cache (mock runner configs)
Handles ONDC transaction flows:
- Flow initialization
- Step sequencing
- Status tracking
- Missed step detection
This project includes custom ESLint rules in eslint-rules/:
- no-direct-response: Prevents direct use of Express response methods
- Use
sendSuccess()andsendError()fromres-utilsinstead
- Use
Structured JSON logging with Pino:
logger.info('Processing request', { transactionId, sessionId });
logger.error('Error occurred', { error }, err);Prometheus-compatible metrics available at /metrics (if configured).
Built-in health monitoring:
- Service availability
- External dependencies status
- Resource utilization
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]- Set
NODE_ENV=production - Use RabbitMQ instead of in-memory queue
- Configure proper Redis credentials
- Enable production logging
- Set up monitoring and alerting
- Horizontal Scaling: Run multiple instances behind a load balancer
- Queue Workers: Scale RabbitMQ consumers independently
- Redis: Use Redis Cluster for high availability
- Session Affinity: Not required (stateless design)
- Follow the existing code style
- Run linting and tests before committing
- Use meaningful commit messages
- Update documentation for new features
ISC
- extedcouD - Initial work
1. "QueueService not initialized" Error
Ensure InitMainContainer() is called before importing routes.
2. Redis Connection Failed
Check Redis is running and credentials are correct in .env.
3. TypeScript Compilation Errors
Run npm run type-check to identify type issues.
4. Dev Mode Fails but Build Works
This might be a module loading order issue. Try:
npm run build && npm start- Check the Queue Service README
- Review ESLint rules in
eslint-rules/ - Check application logs for detailed error messages