Live Demo · Installation · Quick Start · Contributing & Contact · Discussions · 💬 Discord
English · 中文
PocketMocker is an in-page HTTP controller for frontend development.
In simple terms, it lets you decide what your API responses should be—without touching the backend or setting up a mock server.
It provides a visual control panel directly in your browser to intercept, override, and manipulate HTTP responses, helping you build robust UIs faster by seeing changes instantly.
Stop writing mock code. Intercept requests automatically, generate smart data, import API docs with one click.
Edit responses directly in your browser and see UI updates instantly. No tool switching, stay in the flow.
Toggle between Success, Error (500/404), or Empty Data states in one click. verify how your UI handles loading spinners or error toasts without changing a single line of code.
Need to test a long username? A missing avatar? Or a specific price format? Just edit the response JSON directly in the panel and see the UI update instantly.
Simulate network delays (latency), timeout errors, or unauthorized (401) responses to ensure your application handles exceptions gracefully.
https://github.com/user-attachments/assets/e7501191-7ef1-4bd4-bd21-6500585fe4ad
npm install pocket-mocker --save-dev
# or
yarn add pocket-mocker -D
# or
pnpm add pocket-mocker -D
Perfect for individual development or quick experimentation. Simply import and start in your project's entry file:
import { pocketMock } from 'pocket-mocker';
// Only start in development environment
if (process.env.NODE_ENV === 'development') {
pocketMock();
}
After starting your project, you'll see the PocketMock floating panel in the bottom-right corner.
Ideal for production-level projects. The Vite plugin integrates with the file system, saving Mock rules to config files for team sharing.
1. Import and start in your project's entry file:
import { pocketMock } from 'pocket-mocker';
if (process.env.NODE_ENV === 'development') {
pocketMock();
}
2. Configure vite.config.ts
import { defineConfig } from 'vite';
import pocketMockPlugin from 'pocket-mocker/vite-plugin';
export default defineConfig({
plugins: [
pocketMockPlugin()
]
});
3. Start Development
Run npm run dev. PocketMock automatically detects the plugin environment and switches to Server Mode.
PocketMock supports powerful URL patterns to mock complex APIs:
/api/users/:id → matches /api/users/123, /api/users/john/api/* → matches /api/users, /api/users/123/posts/api/:version/users/*/profile → matches /api/v1/users/123/profileAccess captured parameters in mock functions:
(req) => {
const { id, version } = req.params;
const { include } = req.query;
return { id: parseInt(id), version, includeAuthor: include === 'true' };
}
PocketMock includes a powerful Smart Mock Generator that allows you to create realistic test data with simple template syntax.
{
"user": { // → Generate user data
"id": "@guid", // → "550e8400-e29b-41d4-a716-446655440000"
"name": "@name", // → "John"
"username": "@username", // → "brightpanda"
"email": "@email", // → "[email protected]"
"avatar": "@image(100x100)", // → "https://via.placeholder.com/100x100"
"age": "@integer(18,60)", // → 25
"role": "@pick(admin,user)", // → "admin"
"ip": "@ip", // → "192.168.1.1"
"ipv6": "@ip(v6)" // → "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
}
}
| Syntax | Function | Example |
|---|---|---|
@guid |
Unique ID | "f47ac..." |
@name |
Random Name | "John" |
@username |
Random Username | "cool_coder" |
@email |
Email Address | "[email protected]" |
@ip |
Random IP (v4/v6) | @ip → 192.168.1.1 |
@integer(min,max) |
Random Integer | @integer(1,100) → 42 |
@pick(A,B,C) |
Random Choice | @pick(apple,banana) → "apple" |
@image(100x100) |
Placeholder Image | "https://via.placeholder.com/100x100" |
| Category | Syntax | Description |
|---|---|---|
| Basic Types | ||
@float(min,max,decimals) |
Random Float | @float(0,1,2) → 0.57 |
@boolean |
Random Boolean | true |
@string(length) |
Random String | @string(8) → "aX9bK2pQ" |
| Personal | ||
@username(separator, randomDigits, maxLength, dictType) |
Username | @username("-", 2, 20) |
@phone(countryCode) |
Phone Number | @phone(+1) |
| Network | ||
@ip(version) |
IP Address | @ip(v6/v4/6/4) → IPv(x), @ip → IPv4 |
| Date/Time | ||
@date(start,end) |
Random Date | @date(2023-01-01,2024-12-31) |
| Other | ||
@color |
Random Color | "#a3f4c2" |
@text(wordCount) |
Random Text | Generate text with specified word count |
@address(countries) |
Address Object | @address(US,UK) |
@company(industries) |
Company Object | @company(Tech,Finance) |
@url(tlds) |
Random URL | @url(com,io) |
Array Syntax:
{
"users|5": { // Generate 5 users
"id": "@guid",
"name": "@name"
},
"scores|3-5": "@integer(60,100)" // Generate 3 to 5 scores
}
You are not limited to static JSON. You can write JavaScript functions to generate responses dynamically based on request!
(req) => {
// Dynamic response based on Query parameters
if (req.query.id === '1') {
return { id: 1, name: 'Admin', role: 'admin' };
}
// Dynamic response based on Body content
if (req.body?.type === 'error') {
return {
status: 400,
body: { message: 'Invalid Parameter' }
};
}
// Default response
return { id: 2, name: 'Guest' };
}
Import mock rules directly from popular API documentation formats with auto-conversion.
user_id -> @guidavatar -> @image{{baseUrl}}/users -> /usersHow to use: Click the "Import" button in the dashboard header and select your JSON file.
The built-in Network panel logs all network requests (both mocked and real) in real-time, providing powerful debugging capabilities:
window.fetch and extending XMLHttpRequest prototype chain.css: 'injected' strategy to inline all CSS into JS for single-file import experience.Check out our Roadmap to see what's next for PocketMocker and how you can contribute to its future!
We welcome all contributions to PocketMocker! Whether it's reporting bugs, suggesting new features, improving documentation, or submitting code, your help is greatly appreciated.
Please read our Contribution Guidelines for details on how to get started.
If you have any questions, suggestions, or would like to connect, feel free to reach out:
MIT © tianchangNorth
Master HTTP, Master Your Frontend!