vite-plugin-mock-dev-server 1.3.4 → 1.4.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/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Connect, Plugin, ResolvedConfig } from 'vite';
2
2
  import { Buffer } from 'node:buffer';
3
- import http from 'node:http';
3
+ import http, { Server } from 'node:http';
4
4
  import { Readable } from 'node:stream';
5
5
  import Cookies from 'cookies';
6
6
  import { CorsOptions } from 'cors';
@@ -8,6 +8,7 @@ import formidable from 'formidable';
8
8
  import { WebSocketServer } from 'ws';
9
9
  import EventEmitter from 'node:events';
10
10
  import chokidar from 'chokidar';
11
+ import { Http2SecureServer } from 'node:http2';
11
12
 
12
13
  /**
13
14
  * Configure plugin
@@ -133,7 +134,7 @@ interface MockMatchPriority {
133
134
  * as it will prevent subsequent rules from taking effect.
134
135
  * Unless you are clear about the priority of the rules,
135
136
  * most of the time you do not need to configure this option.
136
-
137
+ *
137
138
  * 匹配规则优先级, 全局生效。
138
139
  * 声明在该选项中的规则将优先于默认规则生效。
139
140
  * 规则在数组越靠前的位置,优先级越高。
@@ -150,6 +151,23 @@ interface MockMatchPriority {
150
151
  *
151
152
  * 对于一些特殊情况,需要调整部分规则的优先级,可以使用此选项。
152
153
  * 比如一个请求同时命中了规则 A 和 B,且 A 比 B 优先级高, 但期望规则 B 生效时。
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * {
158
+ * special: {
159
+ * // /api/a/:b/c 优先级将提升到 /api/a/b/:c 前面
160
+ * // The /api/a/:b/c priority is promoted to /api/a/b/:c
161
+ * '/api/a/:b/c': ['/api/a/b/:c'],
162
+ * // 仅在请求满足 /api/a/b/c 时生效
163
+ * // Only when the request satisfies /api/a/b/c
164
+ * '/api/:a/b/c': {
165
+ * rules: ['/api/a/:b/c'],
166
+ * when: ['/api/a/b/c']
167
+ * }
168
+ * }
169
+ * }
170
+ * ```
153
171
  */
154
172
  special?: MockMatchSpecialPriority;
155
173
  }
@@ -159,7 +177,7 @@ interface MockMatchSpecialPriority {
159
177
  * insert A into the top position.The `when` option is used to further constrain
160
178
  * the priority adjustment to be effective only for certain requests.
161
179
  *
162
- * 当 A 与 B或 C 同时满足匹配,且 B或 C在排序首位时,将A插入到首位。
180
+ * 当 A 与 B或 C 同时满足匹配,`B``C` 在排序首位时,将A插入到首位。
163
181
  * when 选项用于进一步约束该优先级调整仅针对哪些请求有效。
164
182
  *
165
183
  * @example
@@ -262,6 +280,7 @@ interface MockBaseItem {
262
280
  * /api/login
263
281
  * /api/post/:id
264
282
  * /api/post/:id
283
+ * /api/anything/(.*)
265
284
  * ```
266
285
  */
267
286
  url: string;
@@ -523,13 +542,17 @@ type FormidableFile = formidable.File | formidable.File[];
523
542
  type LogType = 'info' | 'warn' | 'error' | 'debug';
524
543
  type LogLevel = LogType | 'silent';
525
544
 
545
+ /** @deprecated The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details. */
526
546
  declare function mockDevServerPlugin({ prefix, wsPrefix, include, exclude, reload, log, cors, formidableOptions, build, cookiesOptions, priority, }?: MockServerPluginOptions): Plugin[];
527
547
 
528
548
  /**
529
549
  * mock config Type helper
530
550
  *
531
551
  * mock配置 类型帮助函数
532
- * @param config
552
+ * @param config see config docs:
553
+ * {@link https://vite-plugin-mock-dev-server.netlify.app/en/guide/mock-config en-US DOC} |
554
+ * {@link https://vite-plugin-mock-dev-server.netlify.app/guide/mock-config zh-CN DOC}
555
+ *
533
556
  * @example
534
557
  * Mock Http Request
535
558
  * ```ts
@@ -539,6 +562,13 @@ declare function mockDevServerPlugin({ prefix, wsPrefix, include, exclude, reloa
539
562
  * body: { a: 1 },
540
563
  * })
541
564
  * ```
565
+ * ```ts
566
+ * export default defineMock({
567
+ * url: '/api/example',
568
+ * method: 'GET',
569
+ * body: ({ query }) => ({ a: 1, b: query.b }),
570
+ * })
571
+ * ```
542
572
  * @example
543
573
  * Mock WebSocket
544
574
  * ```ts
@@ -585,9 +615,6 @@ type MockData<T = any> = readonly [
585
615
  */
586
616
  (val: T | ((val: T) => T | void)) => void
587
617
  ] & {
588
- /**
589
- * @property getter/setter
590
- */
591
618
  value: T;
592
619
  };
593
620
  declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
@@ -598,6 +625,8 @@ interface Logger {
598
625
  warn(msg: string, level?: boolean | LogLevel): void;
599
626
  error(msg: string, level?: boolean | LogLevel): void;
600
627
  }
628
+ declare const logLevels: Record<LogLevel, number>;
629
+ declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
601
630
 
602
631
  interface MockLoaderOptions {
603
632
  cwd?: string;
@@ -656,7 +685,7 @@ declare function baseMiddleware(mockLoader: MockLoader, { formidableOptions, pro
656
685
 
657
686
  interface MockSocketOptions {
658
687
  loader: MockLoader;
659
- httpServer: http.Server | null;
688
+ httpServer: Server | Http2SecureServer | null;
660
689
  proxies: string[];
661
690
  cookiesOptions: MockServerPluginOptions['cookiesOptions'];
662
691
  logger: Logger;
@@ -666,4 +695,4 @@ declare function mockWebSocket({ loader, httpServer, proxies, cookiesOptions, lo
666
695
  declare function transformMockData(mockList: Map<string, MockHttpItem | MockWebsocketItem | MockOptions> | (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
667
696
  declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
668
697
 
669
- export { BaseMiddlewareOptions, FormidableFile, MockData, MockHttpItem, MockOptions, MockRequest, MockServerPluginOptions, MockSocketOptions, MockWebsocketItem, baseMiddleware, createDefineMock, mockDevServerPlugin as default, defineMock, defineMockData, mockDevServerPlugin, mockWebSocket, sortByValidator, transformMockData };
698
+ export { type BaseMiddlewareOptions, type FormidableFile, type Logger, type MockData, type MockHttpItem, type MockOptions, type MockRequest, type MockServerPluginOptions, type MockSocketOptions, type MockWebsocketItem, baseMiddleware, createDefineMock, createLogger, mockDevServerPlugin as default, defineMock, defineMockData, logLevels, mockDevServerPlugin, mockWebSocket, sortByValidator, transformMockData };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Connect, Plugin, ResolvedConfig } from 'vite';
2
2
  import { Buffer } from 'node:buffer';
3
- import http from 'node:http';
3
+ import http, { Server } from 'node:http';
4
4
  import { Readable } from 'node:stream';
5
5
  import Cookies from 'cookies';
6
6
  import { CorsOptions } from 'cors';
@@ -8,6 +8,7 @@ import formidable from 'formidable';
8
8
  import { WebSocketServer } from 'ws';
9
9
  import EventEmitter from 'node:events';
10
10
  import chokidar from 'chokidar';
11
+ import { Http2SecureServer } from 'node:http2';
11
12
 
12
13
  /**
13
14
  * Configure plugin
@@ -133,7 +134,7 @@ interface MockMatchPriority {
133
134
  * as it will prevent subsequent rules from taking effect.
134
135
  * Unless you are clear about the priority of the rules,
135
136
  * most of the time you do not need to configure this option.
136
-
137
+ *
137
138
  * 匹配规则优先级, 全局生效。
138
139
  * 声明在该选项中的规则将优先于默认规则生效。
139
140
  * 规则在数组越靠前的位置,优先级越高。
@@ -150,6 +151,23 @@ interface MockMatchPriority {
150
151
  *
151
152
  * 对于一些特殊情况,需要调整部分规则的优先级,可以使用此选项。
152
153
  * 比如一个请求同时命中了规则 A 和 B,且 A 比 B 优先级高, 但期望规则 B 生效时。
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * {
158
+ * special: {
159
+ * // /api/a/:b/c 优先级将提升到 /api/a/b/:c 前面
160
+ * // The /api/a/:b/c priority is promoted to /api/a/b/:c
161
+ * '/api/a/:b/c': ['/api/a/b/:c'],
162
+ * // 仅在请求满足 /api/a/b/c 时生效
163
+ * // Only when the request satisfies /api/a/b/c
164
+ * '/api/:a/b/c': {
165
+ * rules: ['/api/a/:b/c'],
166
+ * when: ['/api/a/b/c']
167
+ * }
168
+ * }
169
+ * }
170
+ * ```
153
171
  */
154
172
  special?: MockMatchSpecialPriority;
155
173
  }
@@ -159,7 +177,7 @@ interface MockMatchSpecialPriority {
159
177
  * insert A into the top position.The `when` option is used to further constrain
160
178
  * the priority adjustment to be effective only for certain requests.
161
179
  *
162
- * 当 A 与 B或 C 同时满足匹配,且 B或 C在排序首位时,将A插入到首位。
180
+ * 当 A 与 B或 C 同时满足匹配,`B``C` 在排序首位时,将A插入到首位。
163
181
  * when 选项用于进一步约束该优先级调整仅针对哪些请求有效。
164
182
  *
165
183
  * @example
@@ -262,6 +280,7 @@ interface MockBaseItem {
262
280
  * /api/login
263
281
  * /api/post/:id
264
282
  * /api/post/:id
283
+ * /api/anything/(.*)
265
284
  * ```
266
285
  */
267
286
  url: string;
@@ -529,7 +548,10 @@ declare function mockDevServerPlugin({ prefix, wsPrefix, include, exclude, reloa
529
548
  * mock config Type helper
530
549
  *
531
550
  * mock配置 类型帮助函数
532
- * @param config
551
+ * @param config see config docs:
552
+ * {@link https://vite-plugin-mock-dev-server.netlify.app/en/guide/mock-config en-US DOC} |
553
+ * {@link https://vite-plugin-mock-dev-server.netlify.app/guide/mock-config zh-CN DOC}
554
+ *
533
555
  * @example
534
556
  * Mock Http Request
535
557
  * ```ts
@@ -539,6 +561,13 @@ declare function mockDevServerPlugin({ prefix, wsPrefix, include, exclude, reloa
539
561
  * body: { a: 1 },
540
562
  * })
541
563
  * ```
564
+ * ```ts
565
+ * export default defineMock({
566
+ * url: '/api/example',
567
+ * method: 'GET',
568
+ * body: ({ query }) => ({ a: 1, b: query.b }),
569
+ * })
570
+ * ```
542
571
  * @example
543
572
  * Mock WebSocket
544
573
  * ```ts
@@ -585,9 +614,6 @@ type MockData<T = any> = readonly [
585
614
  */
586
615
  (val: T | ((val: T) => T | void)) => void
587
616
  ] & {
588
- /**
589
- * @property getter/setter
590
- */
591
617
  value: T;
592
618
  };
593
619
  declare function defineMockData<T = any>(key: string, initialData: T): MockData<T>;
@@ -598,6 +624,8 @@ interface Logger {
598
624
  warn(msg: string, level?: boolean | LogLevel): void;
599
625
  error(msg: string, level?: boolean | LogLevel): void;
600
626
  }
627
+ declare const logLevels: Record<LogLevel, number>;
628
+ declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
601
629
 
602
630
  interface MockLoaderOptions {
603
631
  cwd?: string;
@@ -656,7 +684,7 @@ declare function baseMiddleware(mockLoader: MockLoader, { formidableOptions, pro
656
684
 
657
685
  interface MockSocketOptions {
658
686
  loader: MockLoader;
659
- httpServer: http.Server | null;
687
+ httpServer: Server | Http2SecureServer | null;
660
688
  proxies: string[];
661
689
  cookiesOptions: MockServerPluginOptions['cookiesOptions'];
662
690
  logger: Logger;
@@ -666,4 +694,4 @@ declare function mockWebSocket({ loader, httpServer, proxies, cookiesOptions, lo
666
694
  declare function transformMockData(mockList: Map<string, MockHttpItem | MockWebsocketItem | MockOptions> | (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
667
695
  declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
668
696
 
669
- export { BaseMiddlewareOptions, FormidableFile, MockData, MockHttpItem, MockOptions, MockRequest, MockServerPluginOptions, MockSocketOptions, MockWebsocketItem, baseMiddleware, createDefineMock, mockDevServerPlugin as default, defineMock, defineMockData, mockDevServerPlugin, mockWebSocket, sortByValidator, transformMockData };
697
+ export { type BaseMiddlewareOptions, type FormidableFile, type Logger, type MockData, type MockHttpItem, type MockOptions, type MockRequest, type MockServerPluginOptions, type MockSocketOptions, type MockWebsocketItem, baseMiddleware, createDefineMock, createLogger, mockDevServerPlugin as default, defineMock, defineMockData, logLevels, mockDevServerPlugin, mockWebSocket, sortByValidator, transformMockData };