@chatbi-v/mocks 3.0.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -29,8 +29,8 @@ interceptor.register('GET /api/user', {
29
29
  type: 'json',
30
30
  responseSchema: {
31
31
  code: 200,
32
- data: { id: 1, name: '@cname' }
33
- }
32
+ data: { id: 1, name: '@cname' },
33
+ },
34
34
  });
35
35
 
36
36
  // 在请求库中使用拦截器
@@ -73,35 +73,40 @@ export default {
73
73
  responseSchema: {
74
74
  code: SUCCESS_CODE,
75
75
  msg: 'success',
76
- 'data|10': [{ id: '@increment', title: '@ctitle' }]
77
- }
76
+ 'data|10': [{ id: '@increment', title: '@ctitle' }],
77
+ },
78
78
  },
79
79
 
80
80
  // SSE 流式接口
81
81
  chat: {
82
82
  type: 'sse',
83
83
  responseSchema: {
84
- 'data|3-5': [{
85
- event: 'data',
86
- data: { content: '@cparagraph', role: 'assistant' },
87
- delay: '@increment(100)'
88
- }]
89
- }
84
+ 'data|3-5': [
85
+ {
86
+ event: 'data',
87
+ data: { content: '@cparagraph', role: 'assistant' },
88
+ delay: '@increment(100)',
89
+ },
90
+ ],
91
+ },
90
92
  },
91
93
 
92
94
  // SSE 分页接口 (适用于历史记录等场景)
93
95
  getHistory: {
94
96
  type: 'sse-page',
95
97
  responseSchema: {
96
- 'data|8-12': [{ // 随机生成 8-12 条数据
97
- event: 'data',
98
- data: {
99
- id: '@guid',
100
- content: '@csentence',
101
- role: '@pick(["user", "assistant"])'
98
+ 'data|8-12': [
99
+ {
100
+ // 随机生成 8-12 条数据
101
+ event: 'data',
102
+ data: {
103
+ id: '@guid',
104
+ content: '@csentence',
105
+ role: '@pick(["user", "assistant"])',
106
+ },
107
+ delay: 50,
102
108
  },
103
- delay: 50
104
- }]
109
+ ],
105
110
  },
106
111
  // 分页元数据事件(自动计算并在最后推送)
107
112
  pageEvent: {
@@ -110,11 +115,11 @@ export default {
110
115
  pageNo: '{{$query.pageNo}}', // 引用请求参数
111
116
  pageSize: 20,
112
117
  total: 100,
113
- hasNext: true
114
- }
115
- }
116
- }
117
- }
118
+ hasNext: true,
119
+ },
120
+ },
121
+ },
122
+ };
118
123
  ```
119
124
 
120
125
  ## 核心概念
@@ -131,6 +136,7 @@ export default {
131
136
  ### 模板增强功能
132
137
 
133
138
  #### 1. 数组范围生成
139
+
134
140
  支持 Mock.js 的 Key 规则生成指定数量的事件。在 SSE 场景下,生成的数组会被自动展开(flatten),确保每个数组元素作为一个独立的 SSE 事件发送。
135
141
 
136
142
  ```javascript
@@ -142,6 +148,7 @@ export default {
142
148
  ```
143
149
 
144
150
  #### 2. 请求参数引用与计算
151
+
145
152
  在字符串值中可以使用 `{{$query.xxx}}` 语法引用 URL 查询参数。支持简单的 JavaScript 表达式计算。
146
153
 
147
154
  ```javascript
@@ -160,7 +167,7 @@ export default {
160
167
  ```typescript
161
168
  interface MockEvent {
162
169
  event: string; // 事件类型:data, log, error, page, todos
163
- data: any; // 事件载荷
170
+ data: any; // 事件载荷
164
171
  delay?: number; // 延迟毫秒数(相对于流开始时间)
165
172
  }
166
173
  ```
package/dist/index.cjs CHANGED
@@ -30,9 +30,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
30
  ));
31
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
32
 
33
- // ../../node_modules/.pnpm/mockjs@1.1.0/node_modules/mockjs/dist/mock.js
33
+ // ../../../node_modules/.pnpm/mockjs@1.1.0/node_modules/mockjs/dist/mock.js
34
34
  var require_mock = __commonJS({
35
- "../../node_modules/.pnpm/mockjs@1.1.0/node_modules/mockjs/dist/mock.js"(exports, module) {
35
+ "../../../node_modules/.pnpm/mockjs@1.1.0/node_modules/mockjs/dist/mock.js"(exports, module) {
36
36
  "use strict";
37
37
  (function webpackUniversalModuleDefinition(root, factory) {
38
38
  if (typeof exports === "object" && typeof module === "object")
@@ -7553,7 +7553,10 @@ var MockResponseGenerator = class {
7553
7553
  * 标记所有计划为完成
7554
7554
  */
7555
7555
  completePlan() {
7556
- this.currentTodos = this.currentTodos.map((todo) => ({ ...todo, status: "COMPLETED" }));
7556
+ this.currentTodos = this.currentTodos.map((todo) => ({
7557
+ ...todo,
7558
+ status: "COMPLETED"
7559
+ }));
7557
7560
  this.pushTodos();
7558
7561
  return this;
7559
7562
  }
@@ -7764,7 +7767,10 @@ var HistoryStreamStrategy = class {
7764
7767
  const config = schema;
7765
7768
  const generator = new MockResponseGenerator("BI\u52A9\u624B");
7766
7769
  const historyMock = import_mockjs2.default.mock(config.template);
7767
- const fullHistory = [...config.prepend || [], ...historyMock.list || historyMock];
7770
+ const fullHistory = [
7771
+ ...config.prepend || [],
7772
+ ...historyMock.list || historyMock
7773
+ ];
7768
7774
  generator.emitHistory(fullHistory);
7769
7775
  return generator.toString().split("\n\n").map((chunk) => chunk + "\n\n");
7770
7776
  }
@@ -7888,7 +7894,9 @@ var MockAdapter = class _MockAdapter {
7888
7894
  ...typeof schema === "object" ? schema : {}
7889
7895
  };
7890
7896
  if (effectiveConfig.status !== 200) {
7891
- const error = new Error(`Request failed with status code ${effectiveConfig.status}`);
7897
+ const error = new Error(
7898
+ `Request failed with status code ${effectiveConfig.status}`
7899
+ );
7892
7900
  error.response = {
7893
7901
  status: effectiveConfig.status,
7894
7902
  data: import_mockjs3.default.mock(schema),
@@ -7915,9 +7923,15 @@ var MockAdapter = class _MockAdapter {
7915
7923
  } else {
7916
7924
  const type = effectiveConfig.type || "json";
7917
7925
  const strategy = StrategyFactory.getStrategy(type);
7918
- mockData2 = strategy.process(effectiveConfig, config.params || config.data);
7926
+ mockData2 = strategy.process(
7927
+ effectiveConfig,
7928
+ config.params || config.data
7929
+ );
7919
7930
  }
7920
- logger.info(`Request: ${config.method} ${config.url}`, config.data || config.params);
7931
+ logger.info(
7932
+ `Request: ${config.method} ${config.url}`,
7933
+ config.data || config.params
7934
+ );
7921
7935
  logger.info(`Response:`, mockData2);
7922
7936
  resolve(mockData2);
7923
7937
  } catch (error) {
@@ -7979,9 +7993,14 @@ var MockAdapter = class _MockAdapter {
7979
7993
  return;
7980
7994
  }
7981
7995
  }
7982
- const error = new Error(`Stream request failed with status code ${effectiveConfig.status}`);
7996
+ const error = new Error(
7997
+ `Stream request failed with status code ${effectiveConfig.status}`
7998
+ );
7983
7999
  error.response = response;
7984
- logger.error(`[SSE Error] Request failed with status ${effectiveConfig.status}`, error);
8000
+ logger.error(
8001
+ `[SSE Error] Request failed with status ${effectiveConfig.status}`,
8002
+ error
8003
+ );
7985
8004
  if (onError) onError(error);
7986
8005
  reject(error);
7987
8006
  return;
@@ -8029,7 +8048,10 @@ data: ${JSON.stringify(event.data)}
8029
8048
 
8030
8049
  `;
8031
8050
  if (["error", "todos", "page"].includes(event.event)) {
8032
- logger.info(`[SSE Event] Emitting special event: ${event.event}`, event.data);
8051
+ logger.info(
8052
+ `[SSE Event] Emitting special event: ${event.event}`,
8053
+ event.data
8054
+ );
8033
8055
  } else if (eventCount === 0 || eventCount === events.length - 1) {
8034
8056
  logger.info(
8035
8057
  `[SSE Event] Emitting ${eventCount === 0 ? "first" : "last"} data event`,
@@ -8130,13 +8152,16 @@ function installFetchMock(schema) {
8130
8152
  const status = rule.status || 200;
8131
8153
  if (status !== 200) {
8132
8154
  await sleep(rule.delay || 0);
8133
- return new Response(JSON.stringify({
8134
- message: `Mock error status ${status}`,
8135
- code: status
8136
- }), {
8137
- status,
8138
- headers: { "Content-Type": "application/json" }
8139
- });
8155
+ return new Response(
8156
+ JSON.stringify({
8157
+ message: `Mock error status ${status}`,
8158
+ code: status
8159
+ }),
8160
+ {
8161
+ status,
8162
+ headers: { "Content-Type": "application/json" }
8163
+ }
8164
+ );
8140
8165
  }
8141
8166
  await sleep(rule.delay || 0);
8142
8167
  const type = rule.type || "json";
@@ -8186,11 +8211,36 @@ var MOCK_USER = {
8186
8211
  role: "admin"
8187
8212
  };
8188
8213
  var MOCK_SESSIONS = [
8189
- { id: "s_001", title: "2024 Q1 \u9500\u552E\u5206\u6790", date: "2024-03-15", lastMessage: "\u597D\u7684\uFF0C\u6B63\u5728\u4E3A\u60A8\u5206\u6790 Q1 \u9500\u552E\u6570\u636E..." },
8190
- { id: "s_002", title: "\u7528\u6237\u589E\u957F\u8D8B\u52BF", date: "2024-03-10", lastMessage: "\u7528\u6237\u589E\u957F\u66F2\u7EBF\u663E\u793A..." },
8191
- { id: "s_003", title: "\u7ADE\u54C1\u5206\u6790\u62A5\u544A", date: "2024-03-08", lastMessage: "\u4E3B\u8981\u7ADE\u54C1 A \u7684\u5E02\u573A\u4EFD\u989D..." },
8192
- { id: "s_004", title: "\u8425\u9500\u6D3B\u52A8\u590D\u76D8", date: "2024-03-05", lastMessage: "ROI \u63D0\u5347\u4E86 15%..." },
8193
- { id: "s_005", title: "\u8D22\u52A1\u62A5\u8868\u5BA1\u8BA1", date: "2024-03-01", lastMessage: "\u8BF7\u786E\u8BA4\u4EE5\u4E0B\u8D22\u52A1\u6307\u6807..." }
8214
+ {
8215
+ id: "s_001",
8216
+ title: "2024 Q1 \u9500\u552E\u5206\u6790",
8217
+ date: "2024-03-15",
8218
+ lastMessage: "\u597D\u7684\uFF0C\u6B63\u5728\u4E3A\u60A8\u5206\u6790 Q1 \u9500\u552E\u6570\u636E..."
8219
+ },
8220
+ {
8221
+ id: "s_002",
8222
+ title: "\u7528\u6237\u589E\u957F\u8D8B\u52BF",
8223
+ date: "2024-03-10",
8224
+ lastMessage: "\u7528\u6237\u589E\u957F\u66F2\u7EBF\u663E\u793A..."
8225
+ },
8226
+ {
8227
+ id: "s_003",
8228
+ title: "\u7ADE\u54C1\u5206\u6790\u62A5\u544A",
8229
+ date: "2024-03-08",
8230
+ lastMessage: "\u4E3B\u8981\u7ADE\u54C1 A \u7684\u5E02\u573A\u4EFD\u989D..."
8231
+ },
8232
+ {
8233
+ id: "s_004",
8234
+ title: "\u8425\u9500\u6D3B\u52A8\u590D\u76D8",
8235
+ date: "2024-03-05",
8236
+ lastMessage: "ROI \u63D0\u5347\u4E86 15%..."
8237
+ },
8238
+ {
8239
+ id: "s_005",
8240
+ title: "\u8D22\u52A1\u62A5\u8868\u5BA1\u8BA1",
8241
+ date: "2024-03-01",
8242
+ lastMessage: "\u8BF7\u786E\u8BA4\u4EE5\u4E0B\u8D22\u52A1\u6307\u6807..."
8243
+ }
8194
8244
  ];
8195
8245
  var MOCK_FAVORITES = [
8196
8246
  { id: "fav-1", title: "\u5B63\u5EA6\u9500\u552E\u603B\u7ED3", date: "2025/12/15" },
package/dist/index.mjs CHANGED
@@ -24,9 +24,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
24
24
  mod
25
25
  ));
26
26
 
27
- // ../../node_modules/.pnpm/mockjs@1.1.0/node_modules/mockjs/dist/mock.js
27
+ // ../../../node_modules/.pnpm/mockjs@1.1.0/node_modules/mockjs/dist/mock.js
28
28
  var require_mock = __commonJS({
29
- "../../node_modules/.pnpm/mockjs@1.1.0/node_modules/mockjs/dist/mock.js"(exports, module) {
29
+ "../../../node_modules/.pnpm/mockjs@1.1.0/node_modules/mockjs/dist/mock.js"(exports, module) {
30
30
  "use strict";
31
31
  (function webpackUniversalModuleDefinition(root, factory) {
32
32
  if (typeof exports === "object" && typeof module === "object")
@@ -7523,7 +7523,10 @@ var MockResponseGenerator = class {
7523
7523
  * 标记所有计划为完成
7524
7524
  */
7525
7525
  completePlan() {
7526
- this.currentTodos = this.currentTodos.map((todo) => ({ ...todo, status: "COMPLETED" }));
7526
+ this.currentTodos = this.currentTodos.map((todo) => ({
7527
+ ...todo,
7528
+ status: "COMPLETED"
7529
+ }));
7527
7530
  this.pushTodos();
7528
7531
  return this;
7529
7532
  }
@@ -7734,7 +7737,10 @@ var HistoryStreamStrategy = class {
7734
7737
  const config = schema;
7735
7738
  const generator = new MockResponseGenerator("BI\u52A9\u624B");
7736
7739
  const historyMock = import_mockjs2.default.mock(config.template);
7737
- const fullHistory = [...config.prepend || [], ...historyMock.list || historyMock];
7740
+ const fullHistory = [
7741
+ ...config.prepend || [],
7742
+ ...historyMock.list || historyMock
7743
+ ];
7738
7744
  generator.emitHistory(fullHistory);
7739
7745
  return generator.toString().split("\n\n").map((chunk) => chunk + "\n\n");
7740
7746
  }
@@ -7858,7 +7864,9 @@ var MockAdapter = class _MockAdapter {
7858
7864
  ...typeof schema === "object" ? schema : {}
7859
7865
  };
7860
7866
  if (effectiveConfig.status !== 200) {
7861
- const error = new Error(`Request failed with status code ${effectiveConfig.status}`);
7867
+ const error = new Error(
7868
+ `Request failed with status code ${effectiveConfig.status}`
7869
+ );
7862
7870
  error.response = {
7863
7871
  status: effectiveConfig.status,
7864
7872
  data: import_mockjs3.default.mock(schema),
@@ -7885,9 +7893,15 @@ var MockAdapter = class _MockAdapter {
7885
7893
  } else {
7886
7894
  const type = effectiveConfig.type || "json";
7887
7895
  const strategy = StrategyFactory.getStrategy(type);
7888
- mockData2 = strategy.process(effectiveConfig, config.params || config.data);
7896
+ mockData2 = strategy.process(
7897
+ effectiveConfig,
7898
+ config.params || config.data
7899
+ );
7889
7900
  }
7890
- logger.info(`Request: ${config.method} ${config.url}`, config.data || config.params);
7901
+ logger.info(
7902
+ `Request: ${config.method} ${config.url}`,
7903
+ config.data || config.params
7904
+ );
7891
7905
  logger.info(`Response:`, mockData2);
7892
7906
  resolve(mockData2);
7893
7907
  } catch (error) {
@@ -7949,9 +7963,14 @@ var MockAdapter = class _MockAdapter {
7949
7963
  return;
7950
7964
  }
7951
7965
  }
7952
- const error = new Error(`Stream request failed with status code ${effectiveConfig.status}`);
7966
+ const error = new Error(
7967
+ `Stream request failed with status code ${effectiveConfig.status}`
7968
+ );
7953
7969
  error.response = response;
7954
- logger.error(`[SSE Error] Request failed with status ${effectiveConfig.status}`, error);
7970
+ logger.error(
7971
+ `[SSE Error] Request failed with status ${effectiveConfig.status}`,
7972
+ error
7973
+ );
7955
7974
  if (onError) onError(error);
7956
7975
  reject(error);
7957
7976
  return;
@@ -7999,7 +8018,10 @@ data: ${JSON.stringify(event.data)}
7999
8018
 
8000
8019
  `;
8001
8020
  if (["error", "todos", "page"].includes(event.event)) {
8002
- logger.info(`[SSE Event] Emitting special event: ${event.event}`, event.data);
8021
+ logger.info(
8022
+ `[SSE Event] Emitting special event: ${event.event}`,
8023
+ event.data
8024
+ );
8003
8025
  } else if (eventCount === 0 || eventCount === events.length - 1) {
8004
8026
  logger.info(
8005
8027
  `[SSE Event] Emitting ${eventCount === 0 ? "first" : "last"} data event`,
@@ -8100,13 +8122,16 @@ function installFetchMock(schema) {
8100
8122
  const status = rule.status || 200;
8101
8123
  if (status !== 200) {
8102
8124
  await sleep(rule.delay || 0);
8103
- return new Response(JSON.stringify({
8104
- message: `Mock error status ${status}`,
8105
- code: status
8106
- }), {
8107
- status,
8108
- headers: { "Content-Type": "application/json" }
8109
- });
8125
+ return new Response(
8126
+ JSON.stringify({
8127
+ message: `Mock error status ${status}`,
8128
+ code: status
8129
+ }),
8130
+ {
8131
+ status,
8132
+ headers: { "Content-Type": "application/json" }
8133
+ }
8134
+ );
8110
8135
  }
8111
8136
  await sleep(rule.delay || 0);
8112
8137
  const type = rule.type || "json";
@@ -8156,11 +8181,36 @@ var MOCK_USER = {
8156
8181
  role: "admin"
8157
8182
  };
8158
8183
  var MOCK_SESSIONS = [
8159
- { id: "s_001", title: "2024 Q1 \u9500\u552E\u5206\u6790", date: "2024-03-15", lastMessage: "\u597D\u7684\uFF0C\u6B63\u5728\u4E3A\u60A8\u5206\u6790 Q1 \u9500\u552E\u6570\u636E..." },
8160
- { id: "s_002", title: "\u7528\u6237\u589E\u957F\u8D8B\u52BF", date: "2024-03-10", lastMessage: "\u7528\u6237\u589E\u957F\u66F2\u7EBF\u663E\u793A..." },
8161
- { id: "s_003", title: "\u7ADE\u54C1\u5206\u6790\u62A5\u544A", date: "2024-03-08", lastMessage: "\u4E3B\u8981\u7ADE\u54C1 A \u7684\u5E02\u573A\u4EFD\u989D..." },
8162
- { id: "s_004", title: "\u8425\u9500\u6D3B\u52A8\u590D\u76D8", date: "2024-03-05", lastMessage: "ROI \u63D0\u5347\u4E86 15%..." },
8163
- { id: "s_005", title: "\u8D22\u52A1\u62A5\u8868\u5BA1\u8BA1", date: "2024-03-01", lastMessage: "\u8BF7\u786E\u8BA4\u4EE5\u4E0B\u8D22\u52A1\u6307\u6807..." }
8184
+ {
8185
+ id: "s_001",
8186
+ title: "2024 Q1 \u9500\u552E\u5206\u6790",
8187
+ date: "2024-03-15",
8188
+ lastMessage: "\u597D\u7684\uFF0C\u6B63\u5728\u4E3A\u60A8\u5206\u6790 Q1 \u9500\u552E\u6570\u636E..."
8189
+ },
8190
+ {
8191
+ id: "s_002",
8192
+ title: "\u7528\u6237\u589E\u957F\u8D8B\u52BF",
8193
+ date: "2024-03-10",
8194
+ lastMessage: "\u7528\u6237\u589E\u957F\u66F2\u7EBF\u663E\u793A..."
8195
+ },
8196
+ {
8197
+ id: "s_003",
8198
+ title: "\u7ADE\u54C1\u5206\u6790\u62A5\u544A",
8199
+ date: "2024-03-08",
8200
+ lastMessage: "\u4E3B\u8981\u7ADE\u54C1 A \u7684\u5E02\u573A\u4EFD\u989D..."
8201
+ },
8202
+ {
8203
+ id: "s_004",
8204
+ title: "\u8425\u9500\u6D3B\u52A8\u590D\u76D8",
8205
+ date: "2024-03-05",
8206
+ lastMessage: "ROI \u63D0\u5347\u4E86 15%..."
8207
+ },
8208
+ {
8209
+ id: "s_005",
8210
+ title: "\u8D22\u52A1\u62A5\u8868\u5BA1\u8BA1",
8211
+ date: "2024-03-01",
8212
+ lastMessage: "\u8BF7\u786E\u8BA4\u4EE5\u4E0B\u8D22\u52A1\u6307\u6807..."
8213
+ }
8164
8214
  ];
8165
8215
  var MOCK_FAVORITES = [
8166
8216
  { id: "fav-1", title: "\u5B63\u5EA6\u9500\u552E\u603B\u7ED3", date: "2025/12/15" },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chatbi-v/mocks",
3
- "version": "3.0.0",
3
+ "version": "3.1.1",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -22,19 +22,18 @@
22
22
  "dayjs": "^1.11.10",
23
23
  "mockjs": "^1.1.0",
24
24
  "react": "^18.3.1",
25
- "@chatbi-v/core": "3.0.0"
25
+ "@chatbi-v/core": "3.1.1"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/mockjs": "^1.0.10",
29
29
  "@types/node": "^25.2.0",
30
30
  "tsup": "^8.5.1",
31
31
  "vitest": "^1.0.0",
32
- "@chatbi-v/cli": "3.0.0",
33
- "@chatbi-v/config": "3.0.0"
32
+ "@chatbi-v/config": "3.1.1"
34
33
  },
35
34
  "scripts": {
36
35
  "build": "tsup",
37
36
  "dev": "tsup --watch",
38
- "test": "vitest"
37
+ "test": "vitest run"
39
38
  }
40
39
  }