https://github.com/akinoccc/jsonx-mock
https://github.com/akinoccc/jsonx-mock
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/akinoccc/jsonx-mock
- Owner: akinoccc
- License: apache-2.0
- Created: 2025-02-13T15:13:07.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-02-21T13:04:31.000Z (3 months ago)
- Last Synced: 2025-03-29T04:42:55.831Z (about 2 months ago)
- Language: TypeScript
- Size: 213 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Jsonx Mock
A TypeScript-based Mock REST API Server with integrated authentication and validation. Perfect for rapid API prototyping and development.
## Features
- 🚀 **Auto-generated REST endpoints**
- 🔒 **JWT Authentication middleware**
- ✅ **Data validation** with Joi schemas
- ⚡ **Dual usage mode** (CLI & Programmatic)
- 📊 **Built-in pagination**
- ⏱️ **Configurable response delays**
- 🛠️ **Custom route support**## Installation
```bash
npm install jsonx-mock --save-dev
# or
yarn add jsonx-mock -D
# or
pnpm add jsonx-mock -D
```## CLI Usage
### Quick Start
```bash
jsonx-mock --port 3000 --db-storage ./data/db.json --db-model ./models
```### Command Options
| Option | Description | Default Value |
|----------------------|--------------------------------------|-------------------|
| `-p, --port ` | Set server port | 3000 |
| `-d, --delay ` | Add response delay in milliseconds | 0 |
| `--db-storage `| Path to database storage file | Required |
| `--db-model ` | Path to model definitions directory | Required |
| `--help` | Show help menu | - |### Configuration File
Supports multiple configuration file formats (loaded by priority):
```ts
// mock.config.ts
export default {
port: 3000,
dbStoragePath: './data/db.json',
dbModelPath: './models',
auth: {
enabled: true,
secret: 'your-secret-key',
expiresIn: '1h'
}
}
```## Programmatic Usage
### Basic Setup
```ts
import MockServer from 'jsonx-mock'const server = new MockServer({
port: 3000,
dbStoragePath: './data/db.json',
dbModelPath: './models',
auth: {
enabled: true,
secret: 'your-secret-key'
}
})server.start()
```### Advanced Configuration
```ts
interface Config {
port?: number // Server port (default: 3000)
delay?: number // Response delay in milliseconds
prefix?: string // API path prefix
dbStoragePath: string // Path to database storage file
dbModelPath: string // Path to model definitions
auth?: {
enabled: boolean // Enable JWT authentication
secret: string // JWT signing secret
expiresIn?: string // Token expiration time
excludePaths?: string[]// Public endpoints
}
}
```### Custom Extensions
```ts
// Add validation rules
server.addValidation('users', {
name: Joi.string().required(),
email: Joi.string().email().required()
})// Add custom routes
server.addCustomRoute('get', '/system/health', (req, res) => {
res.json({ status: 'ok', timestamp: Date.now() })
})// Add middleware
server.pre((req, res, next) => {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.path}`)
next()
})
```## Core Functionality
### Auto-generated Endpoints
| Method | Endpoint | Description |
|--------|-----------------------|----------------------|
| GET | /api/:resource | List resources |
| GET | /api/:resource/:id | Get single resource |
| POST | /api/:resource | Create resource |
| PUT | /api/:resource/:id | Update resource |
| DELETE | /api/:resource/:id | Delete resource |### Authentication Flow
```ts
// Generate access token
app.post('/auth/login', (req, res) => {
const token = server.generateToken({
userId: 123,
role: 'admin'
})
res.json({ token })
})// Protected endpoint example
app.get('/user/profile', (req, res) => {
const user = req.user // Parsed from JWT
res.json({
id: user.id,
name: 'Test User'
})
})
```## License
[Apache-2.0](./LICENSE)