Skip to content

Commit e54687f

Browse files
committed
feat: add request recording and replay
1 parent d98a760 commit e54687f

35 files changed

Lines changed: 3167 additions & 151 deletions

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- 📤 Support `multipart` content-type, mock upload file.
4949
- 📥 Support mock download file.
5050
- ⚜️ Support `WebSocket Mock` and `Server-Sent Events Mock`
51+
- 📝 Support **recording** and **replay requests**
5152
- 🗂 Support building small independent deployable mock services.
5253

5354
## Documentation
@@ -326,6 +327,108 @@ export default defineMock({
326327
}
327328
```
328329

330+
### record
331+
332+
- **Type:** `false | RecordOptions`
333+
- **Default:** `false`
334+
- **Details:**
335+
336+
Whether to enable the request recording feature. Once enabled, the plugin will record all request data for subsequent request playback.
337+
338+
Based on `vite.server.proxy`, the plugin records request data proxied by `http-proxy`.
339+
After receiving a response, the plugin will record the request data and response data to the specified directory.
340+
341+
```ts
342+
interface RecordOptions {
343+
/**
344+
* Whether to enable the record feature
345+
* - true: Enable, automatically record proxy responses
346+
* - false: Disable (default)
347+
* @default false
348+
*/
349+
enabled?: boolean
350+
/**
351+
* Filter requests to record
352+
* - Function: Custom filter function, return true to record
353+
* - Object: Include/exclude patterns with glob or path-to-regexp mode
354+
* @example
355+
* ```ts
356+
* // Record all requests
357+
* filter: (req) => true
358+
* // Record requests using glob pattern
359+
* filter: { mode: 'glob', include: '/api/**' }
360+
* // Record requests using path-to-regexp pattern
361+
* filter: { mode: 'path-to-regexp', include: '/api/:id' }
362+
* ```
363+
*/
364+
filter?: ((req: RecordedReq) => boolean) | {
365+
/**
366+
* Include the request links that need to be recorded
367+
*
368+
* String: Glob pattern or path-to-regexp pattern
369+
* (Use the mode option to set the mode, default is glob)
370+
*/
371+
include?: string | string[]
372+
/**
373+
* Exclude request links that do not need to be recorded
374+
*
375+
* String: Glob pattern or path-to-regexp pattern
376+
* (Use the mode option to set the mode, default is glob)
377+
*/
378+
exclude?: string | string[]
379+
/**
380+
* Matching mode for include/exclude patterns
381+
* - 'glob': Glob pattern matching (default)
382+
* - 'path-to-regexp': Path-to-regexp pattern matching
383+
*/
384+
mode: 'glob' | 'path-to-regexp'
385+
}
386+
387+
/**
388+
* Directory to store recorded data
389+
* Relative to project root
390+
* @default 'mock/.recordings'
391+
*/
392+
dir?: string
393+
/**
394+
* Whether to overwrite existing recorded data
395+
* - true: Overwrite old data for the same request (default)
396+
* - false: Keep old data, do not record new data
397+
* @default true
398+
*/
399+
overwrite?: boolean
400+
/**
401+
* Expiration time for recorded data in seconds
402+
* - 0: Never expire (default)
403+
* - Positive number: Expire after specified seconds
404+
* @default 0
405+
*/
406+
expires?: number
407+
/**
408+
* Status codes to record
409+
* - Empty array: Record all status codes (default)
410+
* - Specify one or more status codes to filter
411+
* @default []
412+
*/
413+
status?: number | number[]
414+
/**
415+
* Should a .gitignore be added to the recording directory
416+
* - true: Add (default)
417+
* - false: Do not add
418+
* @default true
419+
*/
420+
gitignore?: boolean
421+
}
422+
```
423+
424+
### replay
425+
426+
- **Type:** `boolean`
427+
- **Default:** `false`
428+
- **Details:**
429+
430+
Whether to enable the request playback feature. Once enabled, the plugin will simulate responses based on the recorded request data.
431+
329432
### priority
330433

331434
- **Type:** `MockMatchPriority`

README.zh-CN.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
- 📤 支持 multipart 类型,模拟文件上传
5050
- 📥 支持模拟文件下载
5151
- ⚜️ 支持模拟 `WebSocket``Server-Sent Events`
52+
- 📝 支持 **请求录制****请求回放**
5253
- 🗂 支持构建可独立部署的小型mock服务
5354

5455
## 文档
@@ -325,6 +326,110 @@ export default defineMock({
325326
}
326327
```
327328

329+
### record
330+
331+
- **类型:** `false | RecordOptions`
332+
- **默认值:** `false`
333+
- **详情:**
334+
335+
是否开启请求录制功能。开启后,插件会记录所有请求数据,用于后续的请求回放。
336+
337+
插件在 `vite.server.proxy` 的基础上,记录被 `http-proxy` 代理的请求数据。
338+
在获得响应后,插件会将请求数据和响应数据记录到指定的目录中。
339+
340+
```ts
341+
interface RecordOptions {
342+
/**
343+
* 是否启用录制功能
344+
* - true: 启用,自动录制 proxy 响应
345+
* - false: 禁用(默认)
346+
* @default false
347+
*/
348+
enabled?: boolean
349+
/**
350+
* 过滤要录制的请求
351+
* - 函数:自定义过滤函数,返回 true 表示录制
352+
* - 对象:包含/排除模式,支持 glob 或 path-to-regexp 模式
353+
* @example
354+
* ```ts
355+
* // Record all requests
356+
* filter: (req) => true
357+
* // Record requests using glob pattern
358+
* filter: { mode: 'glob', include: '/api/**' }
359+
* // Record requests using path-to-regexp pattern
360+
* filter: { mode: 'path-to-regexp', include: '/api/:id' }
361+
* ```
362+
*/
363+
filter?: ((req: RecordedReq) => boolean) | {
364+
/**
365+
* 包含需要录制的请求链接
366+
*
367+
* glob 模式或 path-to-regexp 模式
368+
* (使用 mode 选项设置模式,默认为 glob)
369+
*/
370+
include?: string | string[]
371+
/**
372+
* 排除不需要录制的请求链接
373+
*
374+
* glob 模式或 path-to-regexp 模式
375+
* (使用 mode 选项设置模式,默认为 glob)
376+
*/
377+
exclude?: string | string[]
378+
/**
379+
* 包含/排除模式的匹配模式
380+
* - 'glob': glob 模式匹配(默认)
381+
* - 'path-to-regexp': path-to-regexp 模式匹配
382+
*/
383+
mode: 'glob' | 'path-to-regexp'
384+
}
385+
/**
386+
* 录制数据存储目录
387+
* 相对于项目根目录
388+
* @default 'mock/.recordings'
389+
*/
390+
dir?: string
391+
392+
/**
393+
* 是否覆盖已有录制数据
394+
* - true: 相同请求覆盖旧数据(默认)
395+
* - false: 保留旧数据,不录制新数据
396+
* @default true
397+
*/
398+
overwrite?: boolean
399+
/**
400+
* 录制数据过期时间(秒)
401+
* - 0: 永不过期(默认)
402+
* - 正数:指定秒数后过期
403+
* @default 0
404+
*/
405+
expires?: number
406+
407+
/**
408+
* 要录制的状态码
409+
* - 为空数组时记录所有状态码(默认)
410+
* - 指定一个或多个状态码进行过滤
411+
* @default []
412+
*/
413+
status?: number | number[]
414+
415+
/**
416+
* 是否在录制目录中添加 .gitignore
417+
* - true: 添加(默认)
418+
* - false: 不添加
419+
* @default true
420+
*/
421+
gitignore?: boolean
422+
}
423+
```
424+
425+
### replay
426+
427+
- **类型:** `boolean`
428+
- **默认值:** `false`
429+
- **详情:**
430+
431+
是否开启请求回放功能。开启后,插件会根据记录的请求数据,模拟响应。
432+
328433
### priority
329434

330435
- **类型:** `MockMatchPriority`

docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const vitepressConfig: UserConfig<DefaultTheme.Config> = defineConfig({
4848
},
4949
],
5050
search: { provider: 'local' },
51+
outline: [2, 3],
5152
},
5253
vite: {
5354
plugins: [

docs/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export default defineAdditionalConfig({
5858

5959
outline: {
6060
label: 'Page navigation',
61+
level: [2, 3],
6162
},
6263

6364
lastUpdated: {
@@ -142,6 +143,7 @@ function sidebarGuide(): DefaultTheme.SidebarItem[] {
142143
{ text: 'Plugin Configuration', link: 'plugin-config' },
143144
{ text: 'Mock Configuration', link: 'mock-config' },
144145
{ text: 'WebSocket Configuration', link: 'websocket-config' },
146+
{ text: 'Record and Replay', link: 'record-and-replay' },
145147
],
146148
},
147149
{

0 commit comments

Comments
 (0)