rspack-plugin-mock 1.1.0 → 1.2.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/chunk-CUT6urMc.cjs +30 -0
- package/dist/helper.cjs +124 -113
- package/dist/helper.d.cts +92 -104
- package/dist/helper.d.ts +92 -104
- package/dist/helper.js +120 -113
- package/dist/index.cjs +82 -99
- package/dist/index.d.cts +10 -22
- package/dist/index.d.ts +10 -22
- package/dist/index.js +74 -97
- package/dist/json5-loader.cjs +30 -15
- package/dist/logger-C0V8Cvvd.cjs +800 -0
- package/dist/logger-C48_LmdS.js +710 -0
- package/dist/mockWebsocket-DkVHpZCx.d.cts +85 -0
- package/dist/mockWebsocket-qLVAe-RI.d.ts +85 -0
- package/dist/resolvePluginOptions-Da5uqlBx.cjs +506 -0
- package/dist/resolvePluginOptions-DlkIkykz.js +476 -0
- package/dist/rsbuild.cjs +164 -190
- package/dist/rsbuild.d.cts +5 -13
- package/dist/rsbuild.d.ts +5 -13
- package/dist/rsbuild.js +161 -188
- package/dist/server.cjs +9 -18
- package/dist/server.d.cts +21 -25
- package/dist/server.d.ts +21 -25
- package/dist/server.js +3 -18
- package/dist/types-6lajtJPx.d.cts +572 -0
- package/dist/types-DPzh7nJq.d.ts +572 -0
- package/package.json +22 -22
- package/dist/chunk-HTVJXQRM.cjs +0 -906
- package/dist/chunk-HV5L72CY.js +0 -557
- package/dist/chunk-M7F5AAOF.cjs +0 -557
- package/dist/chunk-OGWV5ZGG.js +0 -906
- package/dist/mockWebsocket-DBgZBsdo.d.ts +0 -76
- package/dist/mockWebsocket-Ki_cShTv.d.cts +0 -76
- package/dist/types-Aw0AciTG.d.cts +0 -570
- package/dist/types-Aw0AciTG.d.ts +0 -570
|
@@ -1,570 +0,0 @@
|
|
|
1
|
-
import { Options } from 'co-body';
|
|
2
|
-
import Cookies from 'cookies';
|
|
3
|
-
import { CorsOptions } from 'cors';
|
|
4
|
-
import formidable from 'formidable';
|
|
5
|
-
import { Buffer } from 'node:buffer';
|
|
6
|
-
import http from 'node:http';
|
|
7
|
-
import { Readable } from 'node:stream';
|
|
8
|
-
import { WebSocketServer } from 'ws';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Configure plugin
|
|
12
|
-
*
|
|
13
|
-
* 插件配置项
|
|
14
|
-
*/
|
|
15
|
-
interface MockServerPluginOptions {
|
|
16
|
-
/**
|
|
17
|
-
* To configure the path matching rules for http mock services,
|
|
18
|
-
* any request path starting with prefix will be intercepted and proxied.
|
|
19
|
-
* If the prefix starts with `^`, it will be recognized as a `RegExp`.
|
|
20
|
-
*
|
|
21
|
-
* 为 http mock 服务配置 路径匹配规则,任何请求路径以 prefix 开头的都将被拦截代理。
|
|
22
|
-
* 如果 prefix 以 `^` 开头,将被识别为 `RegExp`。
|
|
23
|
-
* @default []
|
|
24
|
-
* @example ['^/api']
|
|
25
|
-
*/
|
|
26
|
-
prefix?: string | string[];
|
|
27
|
-
/**
|
|
28
|
-
* Configure path matching rules for WebSocket mock service.
|
|
29
|
-
* Any ws/wss requests with a request path starting with wsPrefix
|
|
30
|
-
* will be intercepted by the proxy.
|
|
31
|
-
* If wsPrefix starts with `^`, it will be recognized as a `RegExp`.
|
|
32
|
-
*
|
|
33
|
-
* 为 websocket mock 服务配置 路径匹配规则, 任何请求路径以 wsPrefix 开头的 ws/wss请求,
|
|
34
|
-
* 都将被代理拦截。
|
|
35
|
-
* 如果 wsPrefix 以 `^` 开头,将被识别为 `RegExp`。
|
|
36
|
-
* @default []
|
|
37
|
-
* @example ['/socket.io']
|
|
38
|
-
*/
|
|
39
|
-
wsPrefix?: string | string[];
|
|
40
|
-
/**
|
|
41
|
-
* Configure the matching context for `include` and `exclude`.
|
|
42
|
-
*
|
|
43
|
-
* 配置 `include` 和 `exclude` 的匹配上下文
|
|
44
|
-
*
|
|
45
|
-
* @default process.cwd()
|
|
46
|
-
*/
|
|
47
|
-
cwd?: string;
|
|
48
|
-
/**
|
|
49
|
-
* glob string matching mock includes files
|
|
50
|
-
*
|
|
51
|
-
* glob 字符串匹配 mock 包含的文件
|
|
52
|
-
* @see [picomatch](https://github.com/micromatch/picomatch#globbing-features)
|
|
53
|
-
* @default []
|
|
54
|
-
*/
|
|
55
|
-
include?: string | string[];
|
|
56
|
-
/**
|
|
57
|
-
* glob string matching mock excluded files
|
|
58
|
-
*
|
|
59
|
-
* glob 字符串匹配 mock 排除的文件
|
|
60
|
-
* @see [picomatch](https://github.com/micromatch/picomatch#globbing-features)
|
|
61
|
-
*/
|
|
62
|
-
exclude?: string | string[];
|
|
63
|
-
/**
|
|
64
|
-
* Enable log and configure log level
|
|
65
|
-
*
|
|
66
|
-
* 开启日志,或配置 日志级别
|
|
67
|
-
* @default 'info'
|
|
68
|
-
*/
|
|
69
|
-
log?: boolean | LogLevel;
|
|
70
|
-
/**
|
|
71
|
-
* When the mock resource is hot updated, only the data content is updated,
|
|
72
|
-
* but the page is not refreshed by default.
|
|
73
|
-
* If you want to refresh the page every time you modify a mock file,
|
|
74
|
-
* you can open this option.
|
|
75
|
-
*
|
|
76
|
-
* mock资源热更新时,仅更新了数据内容,但是默认不重新刷新页面。
|
|
77
|
-
* 当你希望每次修改mock文件都刷新页面时,可以打开此选项。
|
|
78
|
-
* @default false
|
|
79
|
-
*/
|
|
80
|
-
reload?: boolean;
|
|
81
|
-
/**
|
|
82
|
-
* Configure to `cors`
|
|
83
|
-
*
|
|
84
|
-
* 配置 `cors`
|
|
85
|
-
* @default true
|
|
86
|
-
* @see [cors](https://github.com/expressjs/cors#configuration-options)
|
|
87
|
-
*/
|
|
88
|
-
cors?: boolean | CorsOptions;
|
|
89
|
-
/**
|
|
90
|
-
* formidable options
|
|
91
|
-
* @see [formidable](https://github.com/node-formidable/formidable#options)
|
|
92
|
-
*/
|
|
93
|
-
formidableOptions?: formidable.Options;
|
|
94
|
-
/**
|
|
95
|
-
* cookies options
|
|
96
|
-
* @see [cookies](https://github.com/pillarjs/cookies#new-cookiesrequest-response--options)
|
|
97
|
-
*/
|
|
98
|
-
cookiesOptions?: Cookies.Option;
|
|
99
|
-
/**
|
|
100
|
-
* Configure to `co-body`
|
|
101
|
-
*
|
|
102
|
-
* 配置 `co-body`
|
|
103
|
-
*
|
|
104
|
-
* @see [co-body](https://github.com/cojs/co-body#options)
|
|
105
|
-
*/
|
|
106
|
-
bodyParserOptions?: BodyParserOptions;
|
|
107
|
-
/**
|
|
108
|
-
* When you need to build a small mock service, you can configure this option.
|
|
109
|
-
*
|
|
110
|
-
* 当需要构建一个小型mock服务时,可配置此项
|
|
111
|
-
* @default false
|
|
112
|
-
*/
|
|
113
|
-
build?: boolean | ServerBuildOption;
|
|
114
|
-
/**
|
|
115
|
-
* Priority sorting for path matching rules is valid only for rules containing dynamic parameters.
|
|
116
|
-
* In most cases, the default sorting rules can meet the needs.
|
|
117
|
-
* However, in some cases where custom sorting rules are required, this option can be used.
|
|
118
|
-
*
|
|
119
|
-
* 路径匹配规则优先级排序,仅对包含动态参数的规则有效。
|
|
120
|
-
* 大部分情况下默认的排序规则都可以满足需求。
|
|
121
|
-
* 但有些情况下,需要自定义排序规则时,可以使用此选项。
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
* ```ts
|
|
125
|
-
* export default {
|
|
126
|
-
* priority: {
|
|
127
|
-
* global: ['/api/:a/b/c', '/api/a/:b/c', '/api/a/b/:c'],
|
|
128
|
-
* special: {
|
|
129
|
-
* '/api/:a/:b/c': {
|
|
130
|
-
* rules: ['/api/a/:b/:c', '/api/a/b/:c'],
|
|
131
|
-
* when: ['/api/a/b/c']
|
|
132
|
-
* }
|
|
133
|
-
* }
|
|
134
|
-
* }
|
|
135
|
-
* }
|
|
136
|
-
* ```
|
|
137
|
-
*/
|
|
138
|
-
priority?: MockMatchPriority;
|
|
139
|
-
}
|
|
140
|
-
interface MockMatchPriority {
|
|
141
|
-
/**
|
|
142
|
-
* The priority of matching rules is global.
|
|
143
|
-
* The rules declared in this option will take priority over the default rules.
|
|
144
|
-
* The higher the position of the rule in the array, the higher the priority.
|
|
145
|
-
*
|
|
146
|
-
* Do not declare general rules in this option, such as /api/(.*),
|
|
147
|
-
* as it will prevent subsequent rules from taking effect.
|
|
148
|
-
* Unless you are clear about the priority of the rules,
|
|
149
|
-
* most of the time you do not need to configure this option.
|
|
150
|
-
*
|
|
151
|
-
* 匹配规则优先级, 全局生效。
|
|
152
|
-
* 声明在该选项中的规则将优先于默认规则生效。
|
|
153
|
-
* 规则在数组越靠前的位置,优先级越高。
|
|
154
|
-
*
|
|
155
|
-
* 不要在此选项中声明通用性的规则,比如 `/api/(.*)`,这将导致后续的规则无法生效。
|
|
156
|
-
* 除非你明确知道规则的优先级,否则大多数情况下都不需要配置该选项。
|
|
157
|
-
* @default []
|
|
158
|
-
*/
|
|
159
|
-
global?: string[];
|
|
160
|
-
/**
|
|
161
|
-
* For some special cases where the priority of certain rules needs to be adjusted,
|
|
162
|
-
* this option can be used. For example, when a request matches both Rule A and Rule B,
|
|
163
|
-
* and Rule A has a higher priority than Rule B, but it is desired for Rule B to take effect.
|
|
164
|
-
*
|
|
165
|
-
* 对于一些特殊情况,需要调整部分规则的优先级,可以使用此选项。
|
|
166
|
-
* 比如一个请求同时命中了规则 A 和 B,且 A 比 B 优先级高, 但期望规则 B 生效时。
|
|
167
|
-
*
|
|
168
|
-
* @example
|
|
169
|
-
* ```ts
|
|
170
|
-
* {
|
|
171
|
-
* special: {
|
|
172
|
-
* // /api/a/:b/c 优先级将提升到 /api/a/b/:c 前面
|
|
173
|
-
* // The /api/a/:b/c priority is promoted to /api/a/b/:c
|
|
174
|
-
* '/api/a/:b/c': ['/api/a/b/:c'],
|
|
175
|
-
* // 仅在请求满足 /api/a/b/c 时生效
|
|
176
|
-
* // Only when the request satisfies /api/a/b/c
|
|
177
|
-
* '/api/:a/b/c': {
|
|
178
|
-
* rules: ['/api/a/:b/c'],
|
|
179
|
-
* when: ['/api/a/b/c']
|
|
180
|
-
* }
|
|
181
|
-
* }
|
|
182
|
-
* }
|
|
183
|
-
* ```
|
|
184
|
-
*/
|
|
185
|
-
special?: MockMatchSpecialPriority;
|
|
186
|
-
}
|
|
187
|
-
interface MockMatchSpecialPriority {
|
|
188
|
-
/**
|
|
189
|
-
* When both A and B or C match, and B or C is at the top of the sort order,
|
|
190
|
-
* insert A into the top position.The `when` option is used to further constrain
|
|
191
|
-
* the priority adjustment to be effective only for certain requests.
|
|
192
|
-
*
|
|
193
|
-
* 当 A 与 B或 C 同时满足匹配,`B` 或 `C` 在排序首位时,将A插入到首位。
|
|
194
|
-
* when 选项用于进一步约束该优先级调整仅针对哪些请求有效。
|
|
195
|
-
*
|
|
196
|
-
* @example
|
|
197
|
-
* ```ts
|
|
198
|
-
* {
|
|
199
|
-
* A: ['B', 'C'],
|
|
200
|
-
* A: { rules: ['B', 'C'], when: ['/api/a/b/c'] }
|
|
201
|
-
* }
|
|
202
|
-
* ```
|
|
203
|
-
*/
|
|
204
|
-
[key: string]: string[] | {
|
|
205
|
-
rules: string[];
|
|
206
|
-
when: string[];
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
type BodyParserOptions = Options & {
|
|
210
|
-
jsonLimit?: string | number;
|
|
211
|
-
formLimit?: string | number;
|
|
212
|
-
textLimit?: string | number;
|
|
213
|
-
};
|
|
214
|
-
interface ServerBuildOption {
|
|
215
|
-
/**
|
|
216
|
-
* Service startup port
|
|
217
|
-
*
|
|
218
|
-
* 服务启动端口
|
|
219
|
-
* @default 8080
|
|
220
|
-
*/
|
|
221
|
-
serverPort?: number;
|
|
222
|
-
/**
|
|
223
|
-
* Service application output directory
|
|
224
|
-
*
|
|
225
|
-
* 服务应用输出目录
|
|
226
|
-
* @default 'dist/mockServer'
|
|
227
|
-
*/
|
|
228
|
-
dist?: string;
|
|
229
|
-
/**
|
|
230
|
-
* Service application log level
|
|
231
|
-
*
|
|
232
|
-
* 服务应用日志级别
|
|
233
|
-
* @default 'error'
|
|
234
|
-
*/
|
|
235
|
-
log?: LogLevel;
|
|
236
|
-
}
|
|
237
|
-
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'TRACE' | 'OPTIONS';
|
|
238
|
-
type Headers = http.IncomingHttpHeaders;
|
|
239
|
-
type ResponseBody = Record<string, any> | any[] | string | number | Readable | Buffer | null;
|
|
240
|
-
/**
|
|
241
|
-
* 扩展 request,添加额外的属性和方法
|
|
242
|
-
*/
|
|
243
|
-
interface ExtraRequest {
|
|
244
|
-
/**
|
|
245
|
-
* The query string located after `?` in the request address has been parsed into JSON.
|
|
246
|
-
*
|
|
247
|
-
* 请求地址中位于 `?` 后面的 queryString,已解析为 json
|
|
248
|
-
*/
|
|
249
|
-
query: Record<string, any>;
|
|
250
|
-
/**
|
|
251
|
-
* The queryString located after `?` in the referer request has been parsed as JSON.
|
|
252
|
-
*
|
|
253
|
-
* 请求 referer 中位于 `?` 后面的 queryString,已解析为 json
|
|
254
|
-
*/
|
|
255
|
-
refererQuery: Record<string, any>;
|
|
256
|
-
/**
|
|
257
|
-
* Body data in the request
|
|
258
|
-
*
|
|
259
|
-
* 请求体中 body 数据
|
|
260
|
-
*/
|
|
261
|
-
body: Record<string, any>;
|
|
262
|
-
/**
|
|
263
|
-
* The params parameter parsed from the `/api/id/:id` in the request address.
|
|
264
|
-
*
|
|
265
|
-
* 请求地址中,`/api/id/:id` 解析后的 params 参数
|
|
266
|
-
*/
|
|
267
|
-
params: Record<string, any>;
|
|
268
|
-
/**
|
|
269
|
-
* headers data in the request
|
|
270
|
-
* 请求体中 headers
|
|
271
|
-
*/
|
|
272
|
-
headers: Headers;
|
|
273
|
-
/**
|
|
274
|
-
* Get the cookie carried in the request.
|
|
275
|
-
*
|
|
276
|
-
* 获取 请求中携带的 cookie
|
|
277
|
-
* @see [cookies](https://github.com/pillarjs/cookies#cookiesgetname--options)
|
|
278
|
-
*/
|
|
279
|
-
getCookie: (name: string, option?: Cookies.GetOption) => string | undefined;
|
|
280
|
-
}
|
|
281
|
-
type MockRequest = http.IncomingMessage & ExtraRequest;
|
|
282
|
-
type MockResponse = http.ServerResponse<http.IncomingMessage> & {
|
|
283
|
-
/**
|
|
284
|
-
* Set cookie in response
|
|
285
|
-
*
|
|
286
|
-
* 向请求响应中设置 cookie
|
|
287
|
-
* @see [cookies](https://github.com/pillarjs/cookies#cookiessetname--values--options)
|
|
288
|
-
*/
|
|
289
|
-
setCookie: (name: string, value?: string | null, option?: Cookies.SetOption) => void;
|
|
290
|
-
};
|
|
291
|
-
type ResponseBodyFn = (request: MockRequest) => ResponseBody | Promise<ResponseBody>;
|
|
292
|
-
type ResponseHeaderFn = (request: MockRequest) => Headers | Promise<Headers>;
|
|
293
|
-
type CookieValue = string | [string, Cookies.SetOption];
|
|
294
|
-
type ResponseCookies = Record<string, CookieValue>;
|
|
295
|
-
type ResponseCookiesFn = (request: MockRequest) => ResponseCookies | Promise<ResponseCookies>;
|
|
296
|
-
interface MockBaseItem {
|
|
297
|
-
/**
|
|
298
|
-
* The interface address that needs to be mocked,
|
|
299
|
-
* supported by `path-to-regexp` for path matching.
|
|
300
|
-
*
|
|
301
|
-
* 需要进行 mock 的接口地址, 由 `path-to-regexp` 提供路径匹配支持
|
|
302
|
-
* @see [path-to-regexp](https://github.com/pillarjs/path-to-regexp)
|
|
303
|
-
* @example
|
|
304
|
-
* ```txt
|
|
305
|
-
* /api/login
|
|
306
|
-
* /api/post/:id
|
|
307
|
-
* /api/post/:id
|
|
308
|
-
* /api/anything/(.*)
|
|
309
|
-
* ```
|
|
310
|
-
*/
|
|
311
|
-
url: string;
|
|
312
|
-
/**
|
|
313
|
-
* Enable WebSocket interface simulation
|
|
314
|
-
*
|
|
315
|
-
* 开启 websocket 接口模拟
|
|
316
|
-
*
|
|
317
|
-
* @default false
|
|
318
|
-
*/
|
|
319
|
-
ws?: boolean;
|
|
320
|
-
/**
|
|
321
|
-
* Whether to enable mock for this interface.
|
|
322
|
-
* In most scenarios, we only need to mock some interfaces instead of all requests that
|
|
323
|
-
* have been configured with mock.
|
|
324
|
-
* Therefore, it is important to be able to configure whether to enable it or not.
|
|
325
|
-
*
|
|
326
|
-
* 是否启动对该接口的mock,在多数场景下,我们仅需要对部分接口进行 mock,
|
|
327
|
-
* 而不是对所有配置了mock的请求进行全量mock,所以是否能够配置是否启用很重要
|
|
328
|
-
* @default true
|
|
329
|
-
*/
|
|
330
|
-
enabled?: boolean;
|
|
331
|
-
/**
|
|
332
|
-
* Enable log and configure log level
|
|
333
|
-
*
|
|
334
|
-
* 开启日志,或配置 日志级别
|
|
335
|
-
* @default 'info'
|
|
336
|
-
*/
|
|
337
|
-
log?: boolean | LogLevel;
|
|
338
|
-
}
|
|
339
|
-
interface MockHttpItem extends MockBaseItem {
|
|
340
|
-
/**
|
|
341
|
-
* The interface allows request methods, and by default allows both GET and POST.
|
|
342
|
-
*
|
|
343
|
-
* 该接口允许的 请求方法,默认同时支持 GET 和 POST
|
|
344
|
-
* @default ['POST','GET']
|
|
345
|
-
*/
|
|
346
|
-
method?: Method | Method[];
|
|
347
|
-
/**
|
|
348
|
-
* Configure the response body headers
|
|
349
|
-
*
|
|
350
|
-
* 配置响应体 headers
|
|
351
|
-
* @default
|
|
352
|
-
* ```json
|
|
353
|
-
* { "Content-Type": "application/json" }
|
|
354
|
-
* ```
|
|
355
|
-
*/
|
|
356
|
-
headers?: Headers | ResponseHeaderFn;
|
|
357
|
-
/**
|
|
358
|
-
* Configure Response Header Status Code
|
|
359
|
-
*
|
|
360
|
-
* 配置 响应头状态码
|
|
361
|
-
* @default 200
|
|
362
|
-
*/
|
|
363
|
-
status?: number;
|
|
364
|
-
/**
|
|
365
|
-
* Configure response header status text
|
|
366
|
-
*
|
|
367
|
-
* 配置响应头状态文本
|
|
368
|
-
* @default 'OK'
|
|
369
|
-
*/
|
|
370
|
-
statusText?: string;
|
|
371
|
-
/**
|
|
372
|
-
* Configure response delay time,
|
|
373
|
-
* If an array is passed in, it represents the range of delay time.
|
|
374
|
-
* unit: `ms`
|
|
375
|
-
*
|
|
376
|
-
* 配置响应延迟时间, 如果传入的是一个数组,则代表延迟时间的范围
|
|
377
|
-
* 单位: `ms`
|
|
378
|
-
* @default 0
|
|
379
|
-
*/
|
|
380
|
-
delay?: number | [number, number];
|
|
381
|
-
/**
|
|
382
|
-
* Configure response body cookies
|
|
383
|
-
*
|
|
384
|
-
* 设置响应体 cookies
|
|
385
|
-
* @example
|
|
386
|
-
* ```ts
|
|
387
|
-
* export default {
|
|
388
|
-
* cookies: {
|
|
389
|
-
* 'token1': '1234567',
|
|
390
|
-
* 'token2': ['1234567', { path: '/' }],
|
|
391
|
-
* },
|
|
392
|
-
* }
|
|
393
|
-
* ```
|
|
394
|
-
* @example
|
|
395
|
-
* ```ts
|
|
396
|
-
* export default {
|
|
397
|
-
* cookies: function (request) {
|
|
398
|
-
* return {
|
|
399
|
-
* 'token1': '1234567',
|
|
400
|
-
* 'token2': ['1234567', { path: '/' }],
|
|
401
|
-
* }
|
|
402
|
-
* },
|
|
403
|
-
* }
|
|
404
|
-
* ```
|
|
405
|
-
*/
|
|
406
|
-
cookies?: ResponseCookies | ResponseCookiesFn;
|
|
407
|
-
/**
|
|
408
|
-
* Response body data type, optional values include `text, json, buffer`.
|
|
409
|
-
*
|
|
410
|
-
* And also support types included in `mime-db`.
|
|
411
|
-
* When the response body returns a file and you are not sure which type to use,
|
|
412
|
-
* you can pass the file name as the value. The plugin will internally search for matching
|
|
413
|
-
* `content-type` based on the file name suffix.
|
|
414
|
-
*
|
|
415
|
-
* However, if it is a TypeScript file such as `a.ts`, it may not be correctly matched
|
|
416
|
-
* as a JavaScript script. You need to modify `a.ts` to `a.js` as the value passed
|
|
417
|
-
* in order to recognize it correctly.
|
|
418
|
-
*
|
|
419
|
-
* 响应体数据类型, 可选值包括 `text, json, buffer`,
|
|
420
|
-
*
|
|
421
|
-
* 还支持`mime-db`中的包含的类型。
|
|
422
|
-
* 当响应体返回的是一个文件,而你不确定应该使用哪个类型时,可以将文件名作为值传入,
|
|
423
|
-
* 插件内部会根据文件名后缀查找匹配的`content-type`。
|
|
424
|
-
*
|
|
425
|
-
* 但如果是 `typescript`文件如 `a.ts`,可能不会被正确匹配为 `javascript`脚本,
|
|
426
|
-
* 你需要将 `a.ts` 修改为 `a.js`作为值传入才能正确识别。
|
|
427
|
-
* @see [mime-db](https://github.com/jshttp/mime-db)
|
|
428
|
-
* @default 'json'
|
|
429
|
-
* @example
|
|
430
|
-
* ```txt
|
|
431
|
-
* json
|
|
432
|
-
* buffer
|
|
433
|
-
* my-app.dmg
|
|
434
|
-
* music.mp4
|
|
435
|
-
* ```
|
|
436
|
-
*/
|
|
437
|
-
type?: 'text' | 'json' | 'buffer' | string;
|
|
438
|
-
/**
|
|
439
|
-
* Configure response body data content
|
|
440
|
-
*
|
|
441
|
-
* 配置响应体数据内容
|
|
442
|
-
* @default ''
|
|
443
|
-
* @example
|
|
444
|
-
* ```ts
|
|
445
|
-
* export default {
|
|
446
|
-
* body: { a: 1 },
|
|
447
|
-
* }
|
|
448
|
-
* ```
|
|
449
|
-
* @example
|
|
450
|
-
* ```ts
|
|
451
|
-
* export default {
|
|
452
|
-
* body: function(request) {
|
|
453
|
-
* return { a: 1, query: request.query }
|
|
454
|
-
* },
|
|
455
|
-
* }
|
|
456
|
-
* ```
|
|
457
|
-
*/
|
|
458
|
-
body?: ResponseBody | ResponseBodyFn;
|
|
459
|
-
/**
|
|
460
|
-
* If you need to set complex response content, you can use the response method,
|
|
461
|
-
* which is a middleware. Here, you can get information such as req
|
|
462
|
-
* and res of the http request,
|
|
463
|
-
* and then return response data through res.write() | res.end().
|
|
464
|
-
* Otherwise, you need to execute next() method.
|
|
465
|
-
* In `req`, you can also get parsed request information such as
|
|
466
|
-
* `query`, `params`, `body` and `refererQuery`.
|
|
467
|
-
*
|
|
468
|
-
* 如果需要设置复杂的响应内容,可以使用 response 方法,
|
|
469
|
-
* 该方法是一个 middleware,你可以在这里拿到 http 请求的 req、res等信息,
|
|
470
|
-
* 然后通过 res.write() | res.end() 返回响应数据, 否则需要执行 next() 方法。
|
|
471
|
-
* 在 `req` 中,还可以拿到 query、params、body, refererQuery 等已解析的请求信息。
|
|
472
|
-
*
|
|
473
|
-
* @see [connect](https://github.com/senchalabs/connect#appusefn)
|
|
474
|
-
* @example
|
|
475
|
-
* ```ts
|
|
476
|
-
* export default {
|
|
477
|
-
* response(req, res) {
|
|
478
|
-
* res.setHeader('Content-Type', 'application/json')
|
|
479
|
-
* res.end(JSON.stringify({ a: 1 }))
|
|
480
|
-
* },
|
|
481
|
-
* }
|
|
482
|
-
* ```
|
|
483
|
-
*
|
|
484
|
-
*/
|
|
485
|
-
response?: (req: MockRequest, res: MockResponse, next: (err?: any) => void) => void | Promise<void>;
|
|
486
|
-
/**
|
|
487
|
-
* Request Validator
|
|
488
|
-
*
|
|
489
|
-
* Sometimes, for the same API request, data needs to be returned based
|
|
490
|
-
* on different request parameters.
|
|
491
|
-
* However, if all of this is written in a single mock's body or response,
|
|
492
|
-
* the content can become cumbersome and difficult to manage.
|
|
493
|
-
* The function of a validator allows you to configure multiple mocks with
|
|
494
|
-
* the same URL simultaneously and determine which mock should be used through validation.
|
|
495
|
-
*
|
|
496
|
-
* 请求验证器
|
|
497
|
-
*
|
|
498
|
-
* 有时候,一个相同的API请求,需要根据不同的请求参数,来决定返回数据,
|
|
499
|
-
* 但全部都在单个 mock中的 body或者 response 中写,内容会很庞杂,不好管理,
|
|
500
|
-
* 验证器的功能,允许你同时配置多条相同url的mock,通过验证器来判断使哪个mock生效。
|
|
501
|
-
* @example
|
|
502
|
-
* ```ts
|
|
503
|
-
* export default {
|
|
504
|
-
* validator: {
|
|
505
|
-
* query: { id: 123 }
|
|
506
|
-
* }
|
|
507
|
-
* }
|
|
508
|
-
* ```
|
|
509
|
-
* @example
|
|
510
|
-
* ```ts
|
|
511
|
-
* export default {
|
|
512
|
-
* validator: function(request) {
|
|
513
|
-
* return request.query.id === 123
|
|
514
|
-
* }
|
|
515
|
-
* }
|
|
516
|
-
* ```
|
|
517
|
-
*/
|
|
518
|
-
validator?: Partial<Omit<ExtraRequest, 'getCookie'>> | ((request: ExtraRequest) => boolean);
|
|
519
|
-
ws?: false;
|
|
520
|
-
}
|
|
521
|
-
interface MockWebsocketItem extends MockBaseItem {
|
|
522
|
-
ws: true;
|
|
523
|
-
/**
|
|
524
|
-
* Configure Websocket Server
|
|
525
|
-
*
|
|
526
|
-
* 配置 Websocket Server
|
|
527
|
-
* @example
|
|
528
|
-
* ```ts
|
|
529
|
-
* export default {
|
|
530
|
-
* ws: true
|
|
531
|
-
* setup: (wss, { onCleanup }) => {
|
|
532
|
-
* wss.on('connection', (ws,req) => {
|
|
533
|
-
* ws.on('message', (raw) => console.log(raw))
|
|
534
|
-
* const timer = setInterval(
|
|
535
|
-
* () => ws.send(JSON.stringify({ type: 'connected' })),
|
|
536
|
-
* 1000,
|
|
537
|
-
* )
|
|
538
|
-
* onCleanup(() => clearInterval(timer))
|
|
539
|
-
* })
|
|
540
|
-
* wss.on('error', (error) => console.error(error))
|
|
541
|
-
* }
|
|
542
|
-
* }
|
|
543
|
-
* ```
|
|
544
|
-
*/
|
|
545
|
-
setup: (wss: WebSocketServer, context: WebSocketSetupContext) => void;
|
|
546
|
-
}
|
|
547
|
-
interface WebSocketSetupContext {
|
|
548
|
-
/**
|
|
549
|
-
* When defining WSS, you may perform some automatic or looping tasks.
|
|
550
|
-
* However, when hot updating, the plugin will re-execute `setup()`,
|
|
551
|
-
* which may result in duplicate registration of listening events and looping tasks
|
|
552
|
-
* such as setTimeout. You can use `onCleanup()` to clear these automatic or looping tasks.
|
|
553
|
-
*
|
|
554
|
-
* 当你在定义 WSS 时,可能会执行一些自动任务或循环任务,
|
|
555
|
-
* 但是当热更新时,插件内部会重新执行 setup() ,
|
|
556
|
-
* 这可能导致出现 重复注册监听事件 和 循环任务如 `setTimeout` 等。
|
|
557
|
-
* 通过 `onCleanup()` 可以来清除这些自动任务或循环任务。
|
|
558
|
-
* @example
|
|
559
|
-
* ``` ts
|
|
560
|
-
* onCleanup(() => clearTimeout(timeId))
|
|
561
|
-
* ```
|
|
562
|
-
*/
|
|
563
|
-
onCleanup: (cleanup: () => void) => void;
|
|
564
|
-
}
|
|
565
|
-
type MockOptions = (MockHttpItem | MockWebsocketItem)[];
|
|
566
|
-
type FormidableFile = formidable.File | formidable.File[];
|
|
567
|
-
type LogType = 'info' | 'warn' | 'error' | 'debug';
|
|
568
|
-
type LogLevel = LogType | 'silent';
|
|
569
|
-
|
|
570
|
-
export type { BodyParserOptions as B, ExtraRequest as E, FormidableFile as F, LogType as L, MockHttpItem as M, ResponseBody as R, ServerBuildOption as S, WebSocketSetupContext as W, MockWebsocketItem as a, MockOptions as b, MockServerPluginOptions as c, MockMatchPriority as d, MockMatchSpecialPriority as e, Method as f, MockRequest as g, MockResponse as h, LogLevel as i };
|