vite-plugin-mock-dev-server 1.1.4 → 1.1.6

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/index.d.ts CHANGED
@@ -3,21 +3,35 @@ import http from 'node:http';
3
3
  import { Readable } from 'node:stream';
4
4
  import Cookies from 'cookies';
5
5
  import formidable from 'formidable';
6
+ import { WebSocketServer } from 'ws';
6
7
  import EventEmitter from 'node:events';
7
8
  import chokidar from 'chokidar';
8
9
 
9
10
  interface MockServerPluginOptions {
10
11
  /**
11
- * To configure the path matching rules for mock services,
12
+ * To configure the path matching rules for http mock services,
12
13
  * any request path starting with prefix will be intercepted and proxied.
13
14
  * If the prefix starts with `^`, it will be recognized as a `RegExp`.
14
15
  *
15
- * 为 mock 服务配置 路径匹配规则,任何请求路径以 prefix 开头的都将被拦截代理。
16
+ * 为 http mock 服务配置 路径匹配规则,任何请求路径以 prefix 开头的都将被拦截代理。
16
17
  * 如果 prefix 以 `^` 开头,将被识别为 `RegExp`。
17
18
  * @default []
18
19
  * @example ['^/api']
19
20
  */
20
21
  prefix?: string | string[];
22
+ /**
23
+ * Configure path matching rules for WebSocket mock service.
24
+ * Any ws/wss requests with a request path starting with wsPrefix
25
+ * will be intercepted by the proxy.
26
+ * If wsPrefix starts with `^`, it will be recognized as a `RegExp`.
27
+ *
28
+ * 为 websocket mock 服务配置 路径匹配规则, 任何请求路径以 wsPrefix 开头的 ws/wss请求,
29
+ * 都将被代理拦截。
30
+ * 如果 wsPrefix 以 `^` 开头,将被识别为 `RegExp`。
31
+ * @default []
32
+ * @example ['/socket.io']
33
+ */
34
+ wsPrefix?: string | string[];
21
35
  /**
22
36
  * glob string matching mock includes files
23
37
  *
@@ -128,7 +142,7 @@ type ResponseHeaderFn = (request: MockRequest) => Headers | Promise<Headers>;
128
142
  type CookieValue = string | [string, Cookies.SetOption];
129
143
  type ResponseCookies = Record<string, CookieValue>;
130
144
  type ResponseCookiesFn = (request: MockRequest) => ResponseCookies | Promise<ResponseCookies>;
131
- interface MockOptionsItem {
145
+ interface MockBaseItem {
132
146
  /**
133
147
  * The interface address that needs to be mocked,
134
148
  * supported by `path-to-regexp` for path matching.
@@ -144,23 +158,33 @@ interface MockOptionsItem {
144
158
  */
145
159
  url: string;
146
160
  /**
147
- * The interface allows request methods, and by default allows both GET and POST.
161
+ * Enable WebSocket interface simulation
148
162
  *
149
- * 该接口允许的 请求方法,默认同时支持 GET 和 POST
150
- * @default ['POST','GET']
163
+ * 开启 websocket 接口模拟
164
+ *
165
+ * @default false
151
166
  */
152
- method?: Method | Method[];
167
+ ws?: boolean;
153
168
  /**
154
169
  * Whether to enable mock for this interface.
155
170
  * In most scenarios, we only need to mock some interfaces instead of all requests that
156
171
  * have been configured with mock.
157
172
  * Therefore, it is important to be able to configure whether to enable it or not.
158
173
  *
159
- * 是否启动对该接口的mock,在多数场景下,我们进需要对部分接口进行 mock,
174
+ * 是否启动对该接口的mock,在多数场景下,我们仅需要对部分接口进行 mock,
160
175
  * 而不是对所有配置了mock的请求进行全量mock,所以是否能够配置是否启用很重要
161
176
  * @default true
162
177
  */
163
178
  enabled?: boolean;
179
+ }
180
+ interface MockHttpItem extends MockBaseItem {
181
+ /**
182
+ * The interface allows request methods, and by default allows both GET and POST.
183
+ *
184
+ * 该接口允许的 请求方法,默认同时支持 GET 和 POST
185
+ * @default ['POST','GET']
186
+ */
187
+ method?: Method | Method[];
164
188
  /**
165
189
  * Configure the response body headers
166
190
  *
@@ -330,11 +354,35 @@ interface MockOptionsItem {
330
354
  * ```
331
355
  */
332
356
  validator?: Partial<Omit<ExtraRequest, 'getCookie'>> | ((request: ExtraRequest) => boolean);
357
+ ws?: false;
358
+ }
359
+ type MockWebsocketServerDestroy = (() => void) | void;
360
+ interface MockWebsocketItem extends MockBaseItem {
361
+ ws: true;
362
+ /**
363
+ * Configure Websocket Server
364
+ *
365
+ * 配置 Websocket Server
366
+ * @example
367
+ * ```ts
368
+ * export default {
369
+ * ws: true
370
+ * setup: (wss) => {
371
+ * wss.on('connection', (ws,req) => {
372
+ * ws.on('message', (raw) => console.log(raw))
373
+ * ws.send(JSON.stringify({ type: 'connected' }))
374
+ * })
375
+ * wss.on('error', (error) => console.error(error))
376
+ * }
377
+ * }
378
+ * ```
379
+ */
380
+ setup: (wss: WebSocketServer) => MockWebsocketServerDestroy;
333
381
  }
334
- type MockOptions = MockOptionsItem[];
382
+ type MockOptions = (MockHttpItem | MockWebsocketItem)[];
335
383
  type FormidableFile = formidable.File | formidable.File[];
336
384
 
337
- declare function mockDevServerPlugin({ prefix, include, exclude, reload, formidableOptions, build, cookiesOptions, }?: MockServerPluginOptions): Plugin[];
385
+ declare function mockDevServerPlugin({ prefix, wsPrefix, include, exclude, reload, formidableOptions, build, cookiesOptions, }?: MockServerPluginOptions): Plugin[];
338
386
 
339
387
  /**
340
388
  * mock config helper
@@ -351,8 +399,9 @@ declare function mockDevServerPlugin({ prefix, include, exclude, reload, formida
351
399
  * })
352
400
  * ```
353
401
  */
354
- declare function defineMock(config: MockOptionsItem): MockOptionsItem;
402
+ declare function defineMock(config: MockHttpItem): MockHttpItem;
355
403
  declare function defineMock(config: MockOptions): MockOptions;
404
+ declare function defineMock(config: MockWebsocketItem): MockWebsocketItem;
356
405
  /**
357
406
  * 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
358
407
  *
@@ -360,7 +409,7 @@ declare function defineMock(config: MockOptions): MockOptions;
360
409
  *
361
410
  * @param transformer preprocessing function
362
411
  */
363
- declare function createDefineMock(transformer: (mock: MockOptionsItem) => MockOptionsItem | void): typeof defineMock;
412
+ declare function createDefineMock(transformer: (mock: MockHttpItem | MockWebsocketItem) => MockHttpItem | MockWebsocketItem | void): typeof defineMock;
364
413
 
365
414
  interface MockLoaderOptions {
366
415
  cwd?: string;
@@ -374,8 +423,7 @@ interface MockLoaderOptions {
374
423
  */
375
424
  declare class MockLoader extends EventEmitter {
376
425
  options: MockLoaderOptions;
377
- static EXT_JSON: RegExp;
378
- moduleCache: Map<string, MockOptions | MockOptionsItem>;
426
+ moduleCache: Map<string, MockOptions | MockHttpItem | MockWebsocketItem>;
379
427
  moduleDeps: Map<string, Set<string>>;
380
428
  cwd: string;
381
429
  mockWatcher: chokidar.FSWatcher;
@@ -395,8 +443,6 @@ declare class MockLoader extends EventEmitter {
395
443
  private updateMockList;
396
444
  private updateModuleDeps;
397
445
  private loadMock;
398
- private loadJson;
399
- private loadModule;
400
446
  private loadFromCode;
401
447
  private transformWithEsbuild;
402
448
  }
@@ -408,6 +454,8 @@ interface BaseMiddlewareOptions {
408
454
  }
409
455
  declare function baseMiddleware(mockLoader: MockLoader, { formidableOptions, proxies, cookiesOptions }: BaseMiddlewareOptions): Connect.NextHandleFunction;
410
456
 
411
- declare function transformMockData(mockList: Map<string, MockOptionsItem | MockOptions> | (MockOptionsItem | MockOptions)[]): Record<string, MockOptions>;
457
+ declare function mockWebSocket(loader: MockLoader, httpServer: http.Server | null, proxies: string[], cookiesOptions: MockServerPluginOptions['cookiesOptions']): void;
458
+
459
+ declare function transformMockData(mockList: Map<string, MockHttpItem | MockWebsocketItem | MockOptions> | (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
412
460
 
413
- export { BaseMiddlewareOptions, FormidableFile, MockOptions, MockOptionsItem, MockServerPluginOptions, baseMiddleware, createDefineMock, mockDevServerPlugin as default, defineMock, mockDevServerPlugin, transformMockData };
461
+ export { BaseMiddlewareOptions, FormidableFile, MockHttpItem, MockOptions, MockRequest, MockServerPluginOptions, MockWebsocketItem, baseMiddleware, createDefineMock, mockDevServerPlugin as default, defineMock, mockDevServerPlugin, mockWebSocket, transformMockData };