vite-plugin-mock-dev-server 2.0.7 → 2.1.0
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/dist/{types-C8ZwTU-4.d.mts → config-7dnQWJSY.d.ts} +501 -173
- package/dist/helper.d.ts +271 -0
- package/dist/index-x4QPVyiP.d.ts +270 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +48 -0
- package/dist/logger-C-7qpBNh.js +2 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.js +1 -0
- package/dist/types.d.ts +7 -0
- package/package.json +27 -22
- package/dist/helper.d.mts +0 -126
- package/dist/index.d.mts +0 -9
- package/dist/index.mjs +0 -37
- package/dist/server-BEkc6AaK.mjs +0 -1
- package/dist/server-lwszq3fl.d.mts +0 -89
- package/dist/server.d.mts +0 -2
- package/dist/server.mjs +0 -1
- package/dist/types.d.mts +0 -2
- /package/dist/{helper.mjs → helper.js} +0 -0
- /package/dist/{types.mjs → types.js} +0 -0
|
@@ -1,27 +1,15 @@
|
|
|
1
1
|
import { CorsOptions } from "cors";
|
|
2
2
|
import { Options } from "co-body";
|
|
3
3
|
import formidable from "formidable";
|
|
4
|
-
import http
|
|
5
|
-
import crypto from "node:crypto";
|
|
4
|
+
import http from "node:http";
|
|
6
5
|
import { Buffer } from "node:buffer";
|
|
7
6
|
import { WebSocketServer } from "ws";
|
|
8
7
|
import { Readable } from "node:stream";
|
|
9
8
|
import { Connect } from "vite";
|
|
10
9
|
|
|
11
|
-
//#region src/cookies
|
|
12
|
-
declare class Keygrip {
|
|
13
|
-
private algorithm;
|
|
14
|
-
private encoding;
|
|
15
|
-
private keys;
|
|
16
|
-
constructor(keys: string[], algorithm?: string, encoding?: crypto.BinaryToTextEncoding);
|
|
17
|
-
sign(data: string, key?: string): string;
|
|
18
|
-
index(data: string, digest: string): number;
|
|
19
|
-
verify(data: string, digest: string): boolean;
|
|
20
|
-
}
|
|
21
|
-
//#endregion
|
|
22
|
-
//#region src/cookies/types.d.ts
|
|
10
|
+
//#region src/types/cookies.d.ts
|
|
23
11
|
interface CookiesOption {
|
|
24
|
-
keys?: string[]
|
|
12
|
+
keys?: string[];
|
|
25
13
|
secure?: boolean;
|
|
26
14
|
}
|
|
27
15
|
interface SetCookieOption {
|
|
@@ -119,18 +107,422 @@ interface GetCookieOption {
|
|
|
119
107
|
signed: boolean;
|
|
120
108
|
}
|
|
121
109
|
//#endregion
|
|
122
|
-
//#region src/
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
110
|
+
//#region src/types/http.d.ts
|
|
111
|
+
type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "TRACE" | "OPTIONS";
|
|
112
|
+
type Headers = http.IncomingHttpHeaders;
|
|
113
|
+
type ResponseBody = Record<string, any> | any[] | string | number | Readable | Buffer | null;
|
|
114
|
+
/**
|
|
115
|
+
* 扩展 request,添加额外的属性和方法
|
|
116
|
+
*/
|
|
117
|
+
interface ExtraRequest {
|
|
118
|
+
/**
|
|
119
|
+
* The query string located after `?` in the request address has been parsed into JSON.
|
|
120
|
+
*
|
|
121
|
+
* 请求地址中位于 `?` 后面的 queryString,已解析为 json
|
|
122
|
+
*/
|
|
123
|
+
query: Record<string, any>;
|
|
124
|
+
/**
|
|
125
|
+
* The queryString located after `?` in the referer request has been parsed as JSON.
|
|
126
|
+
*
|
|
127
|
+
* 请求 referer 中位于 `?` 后面的 queryString,已解析为 json
|
|
128
|
+
*/
|
|
129
|
+
refererQuery: Record<string, any>;
|
|
130
|
+
/**
|
|
131
|
+
* Body data in the request
|
|
132
|
+
*
|
|
133
|
+
* 请求体中 body 数据
|
|
134
|
+
*/
|
|
135
|
+
body: Record<string, any>;
|
|
136
|
+
/**
|
|
137
|
+
* The params parameter parsed from the `/api/id/:id` in the request address.
|
|
138
|
+
*
|
|
139
|
+
* 请求地址中,`/api/id/:id` 解析后的 params 参数
|
|
140
|
+
*/
|
|
141
|
+
params: Record<string, any>;
|
|
142
|
+
/**
|
|
143
|
+
* headers data in the request
|
|
144
|
+
* 请求体中 headers
|
|
145
|
+
*/
|
|
146
|
+
headers: Headers;
|
|
147
|
+
/**
|
|
148
|
+
* Get the cookie carried in the request.
|
|
149
|
+
*
|
|
150
|
+
* 获取 请求中携带的 cookie
|
|
151
|
+
* @see [cookies](https://github.com/pillarjs/cookies#cookiesgetname--options)
|
|
152
|
+
*/
|
|
153
|
+
getCookie: (name: string, options?: GetCookieOption) => string | void;
|
|
131
154
|
}
|
|
155
|
+
type MockRequest = Connect.IncomingMessage & ExtraRequest;
|
|
156
|
+
type MockResponse = http.ServerResponse<http.IncomingMessage> & {
|
|
157
|
+
/**
|
|
158
|
+
* Set cookie in response
|
|
159
|
+
*
|
|
160
|
+
* 向请求响应中设置 cookie
|
|
161
|
+
* @see [cookies](https://github.com/pillarjs/cookies#cookiessetname--values--options)
|
|
162
|
+
*/
|
|
163
|
+
setCookie: (name: string, value: string, options?: SetCookieOption) => void;
|
|
164
|
+
};
|
|
132
165
|
//#endregion
|
|
133
|
-
//#region src/types.d.ts
|
|
166
|
+
//#region src/types/record.d.ts
|
|
167
|
+
/**
|
|
168
|
+
* Record configuration options
|
|
169
|
+
*
|
|
170
|
+
* 录制配置选项
|
|
171
|
+
*/
|
|
172
|
+
interface RecordOptions {
|
|
173
|
+
/**
|
|
174
|
+
* Whether to enable the record feature
|
|
175
|
+
* - true: Enable, automatically record proxy responses
|
|
176
|
+
* - false: Disable (default)
|
|
177
|
+
*
|
|
178
|
+
* 是否启用录制功能
|
|
179
|
+
* - true: 启用,自动录制 proxy 响应
|
|
180
|
+
* - false: 禁用(默认)
|
|
181
|
+
*
|
|
182
|
+
* @default false
|
|
183
|
+
*/
|
|
184
|
+
enabled?: boolean;
|
|
185
|
+
/**
|
|
186
|
+
* Filter requests to record
|
|
187
|
+
* - Function: Custom filter function, return true to record
|
|
188
|
+
* - Object: Include/exclude patterns with glob or path-to-regexp mode
|
|
189
|
+
*
|
|
190
|
+
* 过滤要录制的请求
|
|
191
|
+
* - 函数:自定义过滤函数,返回 true 表示录制
|
|
192
|
+
* - 对象:包含/排除模式,支持 glob 或 path-to-regexp 模式
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* // Record all requests
|
|
197
|
+
* filter: (req) => true
|
|
198
|
+
* // Record requests using glob pattern
|
|
199
|
+
* filter: { mode: 'glob', include: '/api/**' }
|
|
200
|
+
* // Record requests using path-to-regexp pattern
|
|
201
|
+
* filter: { mode: 'path-to-regexp', include: '/api/:id' }
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
filter?: ((req: RecordedReq) => boolean) | {
|
|
205
|
+
/**
|
|
206
|
+
* Include the request links that need to be recorded
|
|
207
|
+
*
|
|
208
|
+
* String: Glob pattern or path-to-regexp pattern
|
|
209
|
+
* (Use the mode option to set the mode, default is glob)
|
|
210
|
+
*
|
|
211
|
+
* 包含需要录制的请求链接
|
|
212
|
+
*
|
|
213
|
+
* glob 模式或 path-to-regexp 模式
|
|
214
|
+
* (使用 mode 选项设置模式,默认为 glob)
|
|
215
|
+
*/
|
|
216
|
+
include?: string | string[];
|
|
217
|
+
/**
|
|
218
|
+
* Exclude request links that do not need to be recorded
|
|
219
|
+
*
|
|
220
|
+
* String: Glob pattern or path-to-regexp pattern
|
|
221
|
+
* (Use the mode option to set the mode, default is glob)
|
|
222
|
+
*
|
|
223
|
+
* 排除不需要录制的请求链接
|
|
224
|
+
*
|
|
225
|
+
* glob 模式或 path-to-regexp 模式
|
|
226
|
+
* (使用 mode 选项设置模式,默认为 glob)
|
|
227
|
+
*/
|
|
228
|
+
exclude?: string | string[];
|
|
229
|
+
/**
|
|
230
|
+
* Matching mode for include/exclude patterns
|
|
231
|
+
* - 'glob': Glob pattern matching (default)
|
|
232
|
+
* - 'path-to-regexp': Path-to-regexp pattern matching
|
|
233
|
+
*
|
|
234
|
+
* 包含/排除模式的匹配模式
|
|
235
|
+
* - 'glob': glob 模式匹配(默认)
|
|
236
|
+
* - 'path-to-regexp': path-to-regexp 模式匹配
|
|
237
|
+
*/
|
|
238
|
+
mode: "glob" | "path-to-regexp";
|
|
239
|
+
};
|
|
240
|
+
/**
|
|
241
|
+
* Directory to store recorded data
|
|
242
|
+
* Relative to project root
|
|
243
|
+
*
|
|
244
|
+
* 录制数据存储目录
|
|
245
|
+
* 相对于项目根目录
|
|
246
|
+
*
|
|
247
|
+
* @default 'mock/.recordings'
|
|
248
|
+
*/
|
|
249
|
+
dir?: string;
|
|
250
|
+
/**
|
|
251
|
+
* Whether to overwrite existing recorded data
|
|
252
|
+
* - true: Overwrite old data for the same request (default)
|
|
253
|
+
* - false: Keep old data, do not record new data
|
|
254
|
+
*
|
|
255
|
+
* 是否覆盖已有录制数据
|
|
256
|
+
* - true: 相同请求覆盖旧数据(默认)
|
|
257
|
+
* - false: 保留旧数据,不录制新数据
|
|
258
|
+
*
|
|
259
|
+
* @default true
|
|
260
|
+
*/
|
|
261
|
+
overwrite?: boolean;
|
|
262
|
+
/**
|
|
263
|
+
* Expiration time for recorded data in seconds
|
|
264
|
+
* - 0: Never expire (default)
|
|
265
|
+
* - Positive number: Expire after specified seconds
|
|
266
|
+
*
|
|
267
|
+
* 录制数据过期时间(秒)
|
|
268
|
+
* - 0: 永不过期(默认)
|
|
269
|
+
* - 正数:指定秒数后过期
|
|
270
|
+
*
|
|
271
|
+
* @default 0
|
|
272
|
+
*/
|
|
273
|
+
expires?: number;
|
|
274
|
+
/**
|
|
275
|
+
* Status codes to record
|
|
276
|
+
* - Empty array: Record all status codes (default)
|
|
277
|
+
* - Specify one or more status codes to filter
|
|
278
|
+
*
|
|
279
|
+
* 要录制的状态码
|
|
280
|
+
* - 为空数组时记录所有状态码(默认)
|
|
281
|
+
* - 指定一个或多个状态码进行过滤
|
|
282
|
+
*
|
|
283
|
+
* @default []
|
|
284
|
+
*/
|
|
285
|
+
status?: number | number[];
|
|
286
|
+
/**
|
|
287
|
+
* Should a .gitignore be added to the recording directory
|
|
288
|
+
* - true: Add (default)
|
|
289
|
+
* - false: Do not add
|
|
290
|
+
*
|
|
291
|
+
* 是否在录制目录中添加 .gitignore
|
|
292
|
+
* - true: 添加(默认)
|
|
293
|
+
* - false: 不添加
|
|
294
|
+
*
|
|
295
|
+
* @default true
|
|
296
|
+
*/
|
|
297
|
+
gitignore?: boolean;
|
|
298
|
+
}
|
|
299
|
+
interface RecordedMeta {
|
|
300
|
+
/**
|
|
301
|
+
* Recording timestamp
|
|
302
|
+
*
|
|
303
|
+
* 录制数据创建时间戳
|
|
304
|
+
*/
|
|
305
|
+
timestamp: number;
|
|
306
|
+
/**
|
|
307
|
+
* Recorded data create time
|
|
308
|
+
*
|
|
309
|
+
* 录制数据创建时间
|
|
310
|
+
*/
|
|
311
|
+
createAt: string;
|
|
312
|
+
/**
|
|
313
|
+
* Recorded data file path
|
|
314
|
+
*
|
|
315
|
+
* 录制数据文件路径
|
|
316
|
+
*/
|
|
317
|
+
filepath: string;
|
|
318
|
+
/**
|
|
319
|
+
* Reference the source of the original request
|
|
320
|
+
*
|
|
321
|
+
* 对原始请求的来源引用
|
|
322
|
+
*/
|
|
323
|
+
referer?: string;
|
|
324
|
+
}
|
|
325
|
+
interface RecordedReq {
|
|
326
|
+
/**
|
|
327
|
+
* Request method
|
|
328
|
+
*
|
|
329
|
+
* 请求方法
|
|
330
|
+
*/
|
|
331
|
+
method: string;
|
|
332
|
+
/**
|
|
333
|
+
* Request pathname
|
|
334
|
+
*
|
|
335
|
+
* 请求路径
|
|
336
|
+
*/
|
|
337
|
+
pathname: string;
|
|
338
|
+
/**
|
|
339
|
+
* Request query parameters
|
|
340
|
+
*
|
|
341
|
+
* 请求参数
|
|
342
|
+
*/
|
|
343
|
+
query: Record<string, any>;
|
|
344
|
+
/**
|
|
345
|
+
* Request body
|
|
346
|
+
*
|
|
347
|
+
* 请求体
|
|
348
|
+
*/
|
|
349
|
+
body: unknown;
|
|
350
|
+
/**
|
|
351
|
+
* Request body type
|
|
352
|
+
*
|
|
353
|
+
* 请求体类型
|
|
354
|
+
*/
|
|
355
|
+
bodyType: string;
|
|
356
|
+
}
|
|
357
|
+
interface RecordedRes {
|
|
358
|
+
/**
|
|
359
|
+
* Response status code
|
|
360
|
+
*
|
|
361
|
+
* 响应状态码
|
|
362
|
+
*/
|
|
363
|
+
status: number;
|
|
364
|
+
/**
|
|
365
|
+
* Response status text
|
|
366
|
+
*
|
|
367
|
+
* 响应状态文本
|
|
368
|
+
*/
|
|
369
|
+
statusText: string;
|
|
370
|
+
/**
|
|
371
|
+
* Response headers
|
|
372
|
+
*
|
|
373
|
+
* 响应头
|
|
374
|
+
*/
|
|
375
|
+
headers: Record<string, string>;
|
|
376
|
+
/**
|
|
377
|
+
* Response body
|
|
378
|
+
*
|
|
379
|
+
* 响应体
|
|
380
|
+
*/
|
|
381
|
+
body: string;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Recorded request data structure
|
|
385
|
+
*
|
|
386
|
+
* 录制的请求数据结构
|
|
387
|
+
*/
|
|
388
|
+
interface RecordedRequest {
|
|
389
|
+
/**
|
|
390
|
+
* Recorded request metadata
|
|
391
|
+
*
|
|
392
|
+
* 录制请求元数据
|
|
393
|
+
*/
|
|
394
|
+
meta: RecordedMeta;
|
|
395
|
+
/**
|
|
396
|
+
* Recorded request data
|
|
397
|
+
*
|
|
398
|
+
* 录制请求数据
|
|
399
|
+
*/
|
|
400
|
+
req: RecordedReq;
|
|
401
|
+
/**
|
|
402
|
+
* Recorded response data
|
|
403
|
+
*
|
|
404
|
+
* 录制响应数据
|
|
405
|
+
*/
|
|
406
|
+
res: RecordedRes;
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Resolved record options with all fields required
|
|
410
|
+
*
|
|
411
|
+
* 解析后的录制配置选项,所有字段为必填
|
|
412
|
+
*/
|
|
413
|
+
interface ResolvedRecordOptions extends Omit<Required<RecordOptions>, "status"> {
|
|
414
|
+
cwd: string;
|
|
415
|
+
status: number[];
|
|
416
|
+
}
|
|
417
|
+
//#endregion
|
|
418
|
+
//#region src/types/options.d.ts
|
|
419
|
+
type BodyParserOptions = Options & {
|
|
420
|
+
jsonLimit?: string | number;
|
|
421
|
+
formLimit?: string | number;
|
|
422
|
+
textLimit?: string | number;
|
|
423
|
+
};
|
|
424
|
+
type LogType = "info" | "warn" | "error" | "debug";
|
|
425
|
+
type LogLevel = LogType | "silent";
|
|
426
|
+
interface ServerBuildOption {
|
|
427
|
+
/**
|
|
428
|
+
* Service startup port
|
|
429
|
+
*
|
|
430
|
+
* 服务启动端口
|
|
431
|
+
* @default 8080
|
|
432
|
+
*/
|
|
433
|
+
serverPort?: number;
|
|
434
|
+
/**
|
|
435
|
+
* Service application output directory
|
|
436
|
+
*
|
|
437
|
+
* 服务应用输出目录
|
|
438
|
+
* @default 'dist/mockServer'
|
|
439
|
+
*/
|
|
440
|
+
dist?: string;
|
|
441
|
+
/**
|
|
442
|
+
* Service application log level
|
|
443
|
+
*
|
|
444
|
+
* 服务应用日志级别
|
|
445
|
+
* @default 'error'
|
|
446
|
+
*/
|
|
447
|
+
log?: LogLevel;
|
|
448
|
+
/**
|
|
449
|
+
* Whether to include record files in the build output
|
|
450
|
+
*
|
|
451
|
+
* 是否在构建输出中包含录制文件
|
|
452
|
+
*
|
|
453
|
+
* @default true
|
|
454
|
+
*/
|
|
455
|
+
includeRecord?: boolean;
|
|
456
|
+
}
|
|
457
|
+
interface MockMatchPriority {
|
|
458
|
+
/**
|
|
459
|
+
* The priority of matching rules is global.
|
|
460
|
+
* The rules declared in this option will take priority over the default rules.
|
|
461
|
+
* The higher the position of the rule in the array, the higher the priority.
|
|
462
|
+
*
|
|
463
|
+
* Do not declare general rules in this option, such as /api/(.*),
|
|
464
|
+
* as it will prevent subsequent rules from taking effect.
|
|
465
|
+
* Unless you are clear about the priority of the rules,
|
|
466
|
+
* most of the time you do not need to configure this option.
|
|
467
|
+
*
|
|
468
|
+
* 匹配规则优先级, 全局生效。
|
|
469
|
+
* 声明在该选项中的规则将优先于默认规则生效。
|
|
470
|
+
* 规则在数组越靠前的位置,优先级越高。
|
|
471
|
+
*
|
|
472
|
+
* 不要在此选项中声明通用性的规则,比如 `/api/(.*)`,这将导致后续的规则无法生效。
|
|
473
|
+
* 除非你明确知道规则的优先级,否则大多数情况下都不需要配置该选项。
|
|
474
|
+
* @default []
|
|
475
|
+
*/
|
|
476
|
+
global?: string[];
|
|
477
|
+
/**
|
|
478
|
+
* For some special cases where the priority of certain rules needs to be adjusted,
|
|
479
|
+
* this option can be used. For example, when a request matches both Rule A and Rule B,
|
|
480
|
+
* and Rule A has a higher priority than Rule B, but it is desired for Rule B to take effect.
|
|
481
|
+
*
|
|
482
|
+
* 对于一些特殊情况,需要调整部分规则的优先级,可以使用此选项。
|
|
483
|
+
* 比如一个请求同时命中了规则 A 和 B,且 A 比 B 优先级高, 但期望规则 B 生效时。
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* ```ts
|
|
487
|
+
* {
|
|
488
|
+
* special: {
|
|
489
|
+
* // /api/a/:b/c 优先级将提升到 /api/a/b/:c 前面
|
|
490
|
+
* // The /api/a/:b/c priority is promoted to /api/a/b/:c
|
|
491
|
+
* '/api/a/:b/c': ['/api/a/b/:c'],
|
|
492
|
+
* // 仅在请求满足 /api/a/b/c 时生效
|
|
493
|
+
* // Only when the request satisfies /api/a/b/c
|
|
494
|
+
* '/api/:a/b/c': {
|
|
495
|
+
* rules: ['/api/a/:b/c'],
|
|
496
|
+
* when: ['/api/a/b/c']
|
|
497
|
+
* }
|
|
498
|
+
* }
|
|
499
|
+
* }
|
|
500
|
+
* ```
|
|
501
|
+
*/
|
|
502
|
+
special?: MockMatchSpecialPriority;
|
|
503
|
+
}
|
|
504
|
+
interface MockMatchSpecialPriority {
|
|
505
|
+
/**
|
|
506
|
+
* When both A and B or C match, and B or C is at the top of the sort order,
|
|
507
|
+
* insert A into the top position.The `when` option is used to further constrain
|
|
508
|
+
* the priority adjustment to be effective only for certain requests.
|
|
509
|
+
*
|
|
510
|
+
* 当 A 与 B或 C 同时满足匹配,`B` 或 `C` 在排序首位时,将A插入到首位。
|
|
511
|
+
* when 选项用于进一步约束该优先级调整仅针对哪些请求有效。
|
|
512
|
+
*
|
|
513
|
+
* @example
|
|
514
|
+
* ```ts
|
|
515
|
+
* {
|
|
516
|
+
* A: ['B', 'C'],
|
|
517
|
+
* A: { rules: ['B', 'C'], when: ['/api/a/b/c'] }
|
|
518
|
+
* }
|
|
519
|
+
* ```
|
|
520
|
+
*/
|
|
521
|
+
[key: string]: string[] | {
|
|
522
|
+
rules: string[];
|
|
523
|
+
when: string[];
|
|
524
|
+
};
|
|
525
|
+
}
|
|
134
526
|
/**
|
|
135
527
|
* Configure plugin
|
|
136
528
|
*
|
|
@@ -162,6 +554,13 @@ interface MockServerPluginOptions {
|
|
|
162
554
|
*/
|
|
163
555
|
wsPrefix?: string | string[];
|
|
164
556
|
/**
|
|
557
|
+
* Whether to enable mock server
|
|
558
|
+
*
|
|
559
|
+
* 是否开启 mock 服务
|
|
560
|
+
* @default true
|
|
561
|
+
*/
|
|
562
|
+
enabled?: boolean;
|
|
563
|
+
/**
|
|
165
564
|
* Configure the matching context for `include` and `exclude`.
|
|
166
565
|
*
|
|
167
566
|
* 配置 `include` 和 `exclude` 的匹配上下文
|
|
@@ -268,158 +667,38 @@ interface MockServerPluginOptions {
|
|
|
268
667
|
* ```
|
|
269
668
|
*/
|
|
270
669
|
priority?: MockMatchPriority;
|
|
271
|
-
}
|
|
272
|
-
interface MockMatchPriority {
|
|
273
|
-
/**
|
|
274
|
-
* The priority of matching rules is global.
|
|
275
|
-
* The rules declared in this option will take priority over the default rules.
|
|
276
|
-
* The higher the position of the rule in the array, the higher the priority.
|
|
277
|
-
*
|
|
278
|
-
* Do not declare general rules in this option, such as /api/(.*),
|
|
279
|
-
* as it will prevent subsequent rules from taking effect.
|
|
280
|
-
* Unless you are clear about the priority of the rules,
|
|
281
|
-
* most of the time you do not need to configure this option.
|
|
282
|
-
*
|
|
283
|
-
* 匹配规则优先级, 全局生效。
|
|
284
|
-
* 声明在该选项中的规则将优先于默认规则生效。
|
|
285
|
-
* 规则在数组越靠前的位置,优先级越高。
|
|
286
|
-
*
|
|
287
|
-
* 不要在此选项中声明通用性的规则,比如 `/api/(.*)`,这将导致后续的规则无法生效。
|
|
288
|
-
* 除非你明确知道规则的优先级,否则大多数情况下都不需要配置该选项。
|
|
289
|
-
* @default []
|
|
290
|
-
*/
|
|
291
|
-
global?: string[];
|
|
292
670
|
/**
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
* and Rule A has a higher priority than Rule B, but it is desired for Rule B to take effect.
|
|
671
|
+
* Record and replay configuration
|
|
672
|
+
* Can be abbreviated as: record: true
|
|
296
673
|
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
674
|
+
* 录制回放配置
|
|
675
|
+
* 可简写为:record: true
|
|
299
676
|
*
|
|
677
|
+
* @default false
|
|
300
678
|
* @example
|
|
301
679
|
* ```ts
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
* when: ['/api/a/b/c']
|
|
312
|
-
* }
|
|
313
|
-
* }
|
|
680
|
+
* // Enable with default settings
|
|
681
|
+
* record: true
|
|
682
|
+
*
|
|
683
|
+
* // Or with custom configuration
|
|
684
|
+
* record: {
|
|
685
|
+
* enabled: true,
|
|
686
|
+
* dir: 'mock/.recordings',
|
|
687
|
+
* overwrite: true,
|
|
688
|
+
* sensitiveFields: ['password', 'token']
|
|
314
689
|
* }
|
|
315
690
|
* ```
|
|
316
691
|
*/
|
|
317
|
-
|
|
318
|
-
}
|
|
319
|
-
interface MockMatchSpecialPriority {
|
|
692
|
+
record?: boolean | RecordOptions;
|
|
320
693
|
/**
|
|
321
|
-
*
|
|
322
|
-
* insert A into the top position.The `when` option is used to further constrain
|
|
323
|
-
* the priority adjustment to be effective only for certain requests.
|
|
694
|
+
* Replay recorded requests, default enabled when record enabled
|
|
324
695
|
*
|
|
325
|
-
*
|
|
326
|
-
* when 选项用于进一步约束该优先级调整仅针对哪些请求有效。
|
|
327
|
-
*
|
|
328
|
-
* @example
|
|
329
|
-
* ```ts
|
|
330
|
-
* {
|
|
331
|
-
* A: ['B', 'C'],
|
|
332
|
-
* A: { rules: ['B', 'C'], when: ['/api/a/b/c'] }
|
|
333
|
-
* }
|
|
334
|
-
* ```
|
|
696
|
+
* 回放已记录的请求,默认请求录制启用时自动启用回放功能
|
|
335
697
|
*/
|
|
336
|
-
|
|
337
|
-
rules: string[];
|
|
338
|
-
when: string[];
|
|
339
|
-
};
|
|
698
|
+
replay?: boolean;
|
|
340
699
|
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
formLimit?: string | number;
|
|
344
|
-
textLimit?: string | number;
|
|
345
|
-
};
|
|
346
|
-
interface ServerBuildOption {
|
|
347
|
-
/**
|
|
348
|
-
* Service startup port
|
|
349
|
-
*
|
|
350
|
-
* 服务启动端口
|
|
351
|
-
* @default 8080
|
|
352
|
-
*/
|
|
353
|
-
serverPort?: number;
|
|
354
|
-
/**
|
|
355
|
-
* Service application output directory
|
|
356
|
-
*
|
|
357
|
-
* 服务应用输出目录
|
|
358
|
-
* @default 'dist/mockServer'
|
|
359
|
-
*/
|
|
360
|
-
dist?: string;
|
|
361
|
-
/**
|
|
362
|
-
* Service application log level
|
|
363
|
-
*
|
|
364
|
-
* 服务应用日志级别
|
|
365
|
-
* @default 'error'
|
|
366
|
-
*/
|
|
367
|
-
log?: LogLevel;
|
|
368
|
-
}
|
|
369
|
-
type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "TRACE" | "OPTIONS";
|
|
370
|
-
type Headers = http.IncomingHttpHeaders;
|
|
371
|
-
type ResponseBody = Record<string, any> | any[] | string | number | Readable | Buffer | null;
|
|
372
|
-
/**
|
|
373
|
-
* 扩展 request,添加额外的属性和方法
|
|
374
|
-
*/
|
|
375
|
-
interface ExtraRequest {
|
|
376
|
-
/**
|
|
377
|
-
* The query string located after `?` in the request address has been parsed into JSON.
|
|
378
|
-
*
|
|
379
|
-
* 请求地址中位于 `?` 后面的 queryString,已解析为 json
|
|
380
|
-
*/
|
|
381
|
-
query: Record<string, any>;
|
|
382
|
-
/**
|
|
383
|
-
* The queryString located after `?` in the referer request has been parsed as JSON.
|
|
384
|
-
*
|
|
385
|
-
* 请求 referer 中位于 `?` 后面的 queryString,已解析为 json
|
|
386
|
-
*/
|
|
387
|
-
refererQuery: Record<string, any>;
|
|
388
|
-
/**
|
|
389
|
-
* Body data in the request
|
|
390
|
-
*
|
|
391
|
-
* 请求体中 body 数据
|
|
392
|
-
*/
|
|
393
|
-
body: Record<string, any>;
|
|
394
|
-
/**
|
|
395
|
-
* The params parameter parsed from the `/api/id/:id` in the request address.
|
|
396
|
-
*
|
|
397
|
-
* 请求地址中,`/api/id/:id` 解析后的 params 参数
|
|
398
|
-
*/
|
|
399
|
-
params: Record<string, any>;
|
|
400
|
-
/**
|
|
401
|
-
* headers data in the request
|
|
402
|
-
* 请求体中 headers
|
|
403
|
-
*/
|
|
404
|
-
headers: Headers;
|
|
405
|
-
/**
|
|
406
|
-
* Get the cookie carried in the request.
|
|
407
|
-
*
|
|
408
|
-
* 获取 请求中携带的 cookie
|
|
409
|
-
* @see [cookies](https://github.com/pillarjs/cookies#cookiesgetname--options)
|
|
410
|
-
*/
|
|
411
|
-
getCookie: Cookies["get"];
|
|
412
|
-
}
|
|
413
|
-
type MockRequest = Connect.IncomingMessage & ExtraRequest;
|
|
414
|
-
type MockResponse = http.ServerResponse<http.IncomingMessage> & {
|
|
415
|
-
/**
|
|
416
|
-
* Set cookie in response
|
|
417
|
-
*
|
|
418
|
-
* 向请求响应中设置 cookie
|
|
419
|
-
* @see [cookies](https://github.com/pillarjs/cookies#cookiessetname--values--options)
|
|
420
|
-
*/
|
|
421
|
-
setCookie: Cookies["set"];
|
|
422
|
-
};
|
|
700
|
+
//#endregion
|
|
701
|
+
//#region src/types/basicConfig.d.ts
|
|
423
702
|
type ResponseBodyFn = (request: MockRequest) => ResponseBody | Promise<ResponseBody>;
|
|
424
703
|
type ResponseHeaderFn = (request: MockRequest) => Headers | Promise<Headers>;
|
|
425
704
|
type CookieValue = string | [string, SetCookieOption];
|
|
@@ -428,16 +707,16 @@ type ResponseCookiesFn = (request: MockRequest) => ResponseCookies | Promise<Res
|
|
|
428
707
|
interface MockBaseItem {
|
|
429
708
|
/**
|
|
430
709
|
* The interface address that needs to be mocked,
|
|
431
|
-
* supported by `path-to-regexp` for path matching.
|
|
710
|
+
* supported by `path-to-regexp@8.3.0` for path matching.
|
|
432
711
|
*
|
|
433
|
-
* 需要进行 mock 的接口地址, 由 `path-to-regexp` 提供路径匹配支持
|
|
712
|
+
* 需要进行 mock 的接口地址, 由 `path-to-regexp@8.3.0` 提供路径匹配支持
|
|
434
713
|
* @see [path-to-regexp](https://github.com/pillarjs/path-to-regexp)
|
|
435
714
|
* @example
|
|
436
715
|
* ```txt
|
|
437
716
|
* /api/login
|
|
438
717
|
* /api/post/:id
|
|
439
|
-
* /api/
|
|
440
|
-
* /api/
|
|
718
|
+
* /api/users{/:id}
|
|
719
|
+
* /api/files/*path
|
|
441
720
|
* ```
|
|
442
721
|
*/
|
|
443
722
|
url: string;
|
|
@@ -468,6 +747,38 @@ interface MockBaseItem {
|
|
|
468
747
|
*/
|
|
469
748
|
log?: boolean | LogLevel;
|
|
470
749
|
}
|
|
750
|
+
//#endregion
|
|
751
|
+
//#region src/types/httpConfig.d.ts
|
|
752
|
+
interface MockErrorConfig {
|
|
753
|
+
/**
|
|
754
|
+
* Error probability (0-1), default is 0.5
|
|
755
|
+
*
|
|
756
|
+
* 错误概率(0-1),默认 0.5
|
|
757
|
+
* @default 0.5
|
|
758
|
+
*/
|
|
759
|
+
probability?: number;
|
|
760
|
+
/**
|
|
761
|
+
* Error status code, default is 500
|
|
762
|
+
*
|
|
763
|
+
* 错误状态码,默认 500
|
|
764
|
+
* @default 500
|
|
765
|
+
*/
|
|
766
|
+
status?: number;
|
|
767
|
+
/**
|
|
768
|
+
* Error status text
|
|
769
|
+
*
|
|
770
|
+
* 错误状态文本
|
|
771
|
+
*/
|
|
772
|
+
statusText?: string;
|
|
773
|
+
/**
|
|
774
|
+
* Custom error response body, suitable for when the status is 200, but the response body needs to simulate an error scenario
|
|
775
|
+
*
|
|
776
|
+
* 自定义错误响应体,适用于 status 为 200,但响应体需要模拟错误场景
|
|
777
|
+
* @example
|
|
778
|
+
* { code: 500, msg: 'Internal Server Error', result: null }
|
|
779
|
+
*/
|
|
780
|
+
body?: ResponseBody | ResponseBodyFn;
|
|
781
|
+
}
|
|
471
782
|
interface MockHttpItem extends MockBaseItem {
|
|
472
783
|
/**
|
|
473
784
|
* The interface allows request methods, and by default allows both GET and POST.
|
|
@@ -648,8 +959,26 @@ interface MockHttpItem extends MockBaseItem {
|
|
|
648
959
|
* ```
|
|
649
960
|
*/
|
|
650
961
|
validator?: Partial<Omit<ExtraRequest, "getCookie">> | ((request: ExtraRequest) => boolean);
|
|
962
|
+
/**
|
|
963
|
+
* Configure error simulation
|
|
964
|
+
*
|
|
965
|
+
* 配置错误模拟
|
|
966
|
+
* @example
|
|
967
|
+
* ```ts
|
|
968
|
+
* export default {
|
|
969
|
+
* error: {
|
|
970
|
+
* probability: 0.5,
|
|
971
|
+
* status: 500,
|
|
972
|
+
* message: 'Internal Server Error'
|
|
973
|
+
* }
|
|
974
|
+
* }
|
|
975
|
+
* ```
|
|
976
|
+
*/
|
|
977
|
+
error?: MockErrorConfig;
|
|
651
978
|
ws?: false;
|
|
652
979
|
}
|
|
980
|
+
//#endregion
|
|
981
|
+
//#region src/types/wsConfig.d.ts
|
|
653
982
|
interface MockWebsocketItem extends MockBaseItem {
|
|
654
983
|
ws: true;
|
|
655
984
|
/**
|
|
@@ -694,9 +1023,8 @@ interface WebSocketSetupContext {
|
|
|
694
1023
|
*/
|
|
695
1024
|
onCleanup: (cleanup: () => void) => void;
|
|
696
1025
|
}
|
|
1026
|
+
//#endregion
|
|
1027
|
+
//#region src/types/config.d.ts
|
|
697
1028
|
type MockOptions = (MockHttpItem | MockWebsocketItem)[];
|
|
698
|
-
type FormidableFile = formidable.File | formidable.File[];
|
|
699
|
-
type LogType = "info" | "warn" | "error" | "debug";
|
|
700
|
-
type LogLevel = LogType | "silent";
|
|
701
1029
|
//#endregion
|
|
702
|
-
export {
|
|
1030
|
+
export { MockResponse as C, SetCookieOption as D, GetCookieOption as E, MockRequest as S, CookiesOption as T, RecordedRes as _, MockHttpItem as a, Headers as b, LogType as c, MockServerPluginOptions as d, ServerBuildOption as f, RecordedRequest as g, RecordedReq as h, MockErrorConfig as i, MockMatchPriority as l, RecordedMeta as m, MockWebsocketItem as n, BodyParserOptions as o, RecordOptions as p, WebSocketSetupContext as r, LogLevel as s, MockOptions as t, MockMatchSpecialPriority as u, ResolvedRecordOptions as v, ResponseBody as w, Method as x, ExtraRequest as y };
|