vite-plugin-mock-dev-server 0.3.15 → 0.3.17
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 +55 -3
- package/README.zh-CN.md +61 -4
- package/dist/index.cjs +484 -199
- package/dist/index.d.ts +68 -2
- package/dist/index.js +480 -196
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -37,11 +37,20 @@
|
|
|
37
37
|
- ⚖️ Use `server.proxy`
|
|
38
38
|
- 🍕 Support `viteConfig.define` in mock file
|
|
39
39
|
- 📤 Support `multipart` content-type,mock upload file.
|
|
40
|
+
- 🌈 Support `vite preview` mode
|
|
41
|
+
- 🗂 Support for building small mock services that can be deployed independently
|
|
42
|
+
|
|
43
|
+
|
|
40
44
|
## Documentation
|
|
41
45
|
|
|
42
46
|
See the [documentation](https://vite-plugin-mock-dev-server.netlify.app/) to learn more.
|
|
47
|
+
|
|
43
48
|
[](https://app.netlify.com/sites/vite-plugin-mock-dev-server/deploys)
|
|
44
49
|
|
|
50
|
+
## Playground
|
|
51
|
+
|
|
52
|
+
[](https://stackblitz.com/github/pengzhanbo/vite-plugin-mock-dev-server/tree/main/playground)
|
|
53
|
+
|
|
45
54
|
## Usage
|
|
46
55
|
|
|
47
56
|
### Install
|
|
@@ -157,6 +166,29 @@ export default defineConfig({
|
|
|
157
166
|
})
|
|
158
167
|
```
|
|
159
168
|
|
|
169
|
+
- `options.build`
|
|
170
|
+
|
|
171
|
+
When building a small, independently deployable mock service.
|
|
172
|
+
|
|
173
|
+
Type: `boolean | ServerBuildOptions`
|
|
174
|
+
|
|
175
|
+
Default:`false`
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
interface ServerBuildOptions {
|
|
179
|
+
/**
|
|
180
|
+
* server port
|
|
181
|
+
* @default 8080
|
|
182
|
+
*/
|
|
183
|
+
serverPort?: number
|
|
184
|
+
/**
|
|
185
|
+
* build output dir
|
|
186
|
+
@default 'mockServer'
|
|
187
|
+
*/
|
|
188
|
+
dist?: string
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
160
192
|
### defineMock(config)
|
|
161
193
|
|
|
162
194
|
Mock Type Helper
|
|
@@ -221,14 +253,14 @@ export default defineMock({
|
|
|
221
253
|
* configurations and determines which mock configuration
|
|
222
254
|
* is valid according to the validator.
|
|
223
255
|
*
|
|
224
|
-
* @type {
|
|
256
|
+
* @type { headers?: object; body?: object; query?: object; params?: object }
|
|
225
257
|
*
|
|
226
258
|
* If the validator incoming is an object,
|
|
227
259
|
* then the validation method is the comparison of the
|
|
228
260
|
* strict request of interface, headers/body/query/params
|
|
229
261
|
* each `key-value` congruent, congruent check through
|
|
230
262
|
*
|
|
231
|
-
* @type ({
|
|
263
|
+
* @type ({ headers: object; body: object; query: object; params: object }) => boolean
|
|
232
264
|
* If the validator is passed a function,
|
|
233
265
|
* it takes the requested interface-related data as an input,
|
|
234
266
|
* gives it to the consumer for custom validation,
|
|
@@ -260,7 +292,7 @@ export default defineMock({
|
|
|
260
292
|
*
|
|
261
293
|
* @type string | number | array | object
|
|
262
294
|
*
|
|
263
|
-
* @type (request: {
|
|
295
|
+
* @type (request: { headers, query, body, params }) => any | Promise<any>
|
|
264
296
|
*/
|
|
265
297
|
body: {},
|
|
266
298
|
|
|
@@ -468,6 +500,26 @@ export default defineMock({
|
|
|
468
500
|
})
|
|
469
501
|
```
|
|
470
502
|
|
|
503
|
+
## Mock Services
|
|
504
|
+
|
|
505
|
+
In some scenarios, you may need to use the data support provided by the mock service for display purposes, but it is possible that the project has been packaged and deployed as a build and is out of the mock service support provided by `vite` and this plugin. Since this plugin was designed to support the import of `node` modules into mock files, it cannot package mock files inline into client-side build code.
|
|
506
|
+
|
|
507
|
+
To cater for such scenarios, the plugin provides support under `vite preview` as well as the ability to build a small, independently deployable mock service application at `vite build` that can be deployed to the relevant environment. The mock support is then implemented by proxy forwarding to the actual port by another http server such as `nginx`.
|
|
508
|
+
|
|
509
|
+
Build the default output into the `dist/mockServer` directory and generate the following file:
|
|
510
|
+
```sh
|
|
511
|
+
./mockServer
|
|
512
|
+
├── index.js
|
|
513
|
+
├── mock-data.js
|
|
514
|
+
└── package.json
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
In this directory, after `npm install` to install the dependencies, `npm start` can be executed to start the mock server.
|
|
518
|
+
|
|
519
|
+
default port: `8080`。
|
|
520
|
+
|
|
521
|
+
Access the associated `mock` interface via `localhost:8080/`.
|
|
522
|
+
|
|
471
523
|
## Archives
|
|
472
524
|
|
|
473
525
|
[awesome-vite](https://github.com/vitejs/awesome-vite#helpers)
|
package/README.zh-CN.md
CHANGED
|
@@ -38,12 +38,19 @@
|
|
|
38
38
|
- ⚖️ 使用 `server.proxy` 配置
|
|
39
39
|
- 🍕 支持在 mock文件中使用 `viteConfig.define`配置字段
|
|
40
40
|
- 📤 支持 multipart 类型,模拟文件上传
|
|
41
|
+
- 🌈 支持 `vite preview` 模式
|
|
42
|
+
- 🗂 支持构建可独立部署的小型mock服务
|
|
41
43
|
|
|
42
44
|
|
|
43
45
|
## 文档
|
|
44
46
|
|
|
45
47
|
查看 [Documentation](https://vite-plugin-mock-dev-server.netlify.app/) 了解更多。
|
|
48
|
+
|
|
46
49
|
[](https://app.netlify.com/sites/vite-plugin-mock-dev-server/deploys)
|
|
50
|
+
|
|
51
|
+
## Playground
|
|
52
|
+
|
|
53
|
+
[](https://stackblitz.com/github/pengzhanbo/vite-plugin-mock-dev-server/tree/main/playground)
|
|
47
54
|
## 使用
|
|
48
55
|
|
|
49
56
|
### 安装
|
|
@@ -133,7 +140,17 @@ export default defineConfig({
|
|
|
133
140
|
|
|
134
141
|
配置读取 mock文件时,需要排除的文件, 可以是一个 目录、glob、或者一个数组
|
|
135
142
|
|
|
136
|
-
|
|
143
|
+
默认值:
|
|
144
|
+
```ts
|
|
145
|
+
[
|
|
146
|
+
'**/node_modules/**',
|
|
147
|
+
'**/test/**',
|
|
148
|
+
'src/**',
|
|
149
|
+
'**/.vscode/**',
|
|
150
|
+
'**/.git/**',
|
|
151
|
+
'**/dist/**'
|
|
152
|
+
]
|
|
153
|
+
```
|
|
137
154
|
|
|
138
155
|
- `options.formidableOptions`
|
|
139
156
|
|
|
@@ -150,6 +167,28 @@ export default defineConfig({
|
|
|
150
167
|
})
|
|
151
168
|
```
|
|
152
169
|
|
|
170
|
+
- `options.build`
|
|
171
|
+
|
|
172
|
+
构建可独立部署的小型mock服务时配置。
|
|
173
|
+
|
|
174
|
+
类型: `boolean | ServerBuildOptions`
|
|
175
|
+
|
|
176
|
+
默认值:`false`
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
interface ServerBuildOptions {
|
|
180
|
+
/**
|
|
181
|
+
* 服务端口
|
|
182
|
+
* @default 8080
|
|
183
|
+
*/
|
|
184
|
+
serverPort?: number
|
|
185
|
+
/**
|
|
186
|
+
* 构建输出目录
|
|
187
|
+
@default 'mockServer'
|
|
188
|
+
*/
|
|
189
|
+
dist?: string
|
|
190
|
+
}
|
|
191
|
+
```
|
|
153
192
|
|
|
154
193
|
### defineMock(config)
|
|
155
194
|
|
|
@@ -212,13 +251,13 @@ export default defineMock({
|
|
|
212
251
|
* 验证器可以很好的解决这一类问题,将同个 url 分为多个 mock配置,
|
|
213
252
|
* 根据 验证器来判断哪个mock配置生效。
|
|
214
253
|
*
|
|
215
|
-
* @type {
|
|
254
|
+
* @type { headers?: object; body?: object; query?: object; params?: object }
|
|
216
255
|
*
|
|
217
256
|
* 如果 validator 传入的是一个对象,那么验证方式是严格比较 请求的接口
|
|
218
257
|
* 中,headers/body/query/params 的各个`key`的`value`是否全等,
|
|
219
258
|
* 全等则校验通过
|
|
220
259
|
*
|
|
221
|
-
* @type ({
|
|
260
|
+
* @type ({ headers: object; body: object; query: object; params: object }) => boolean
|
|
222
261
|
* 如果 validator 传入的是一个函数,那么会讲 请求的接口相关数据作为入参,提供给使用者进行自定义校验,并返回一个 boolean
|
|
223
262
|
*
|
|
224
263
|
*/
|
|
@@ -250,7 +289,7 @@ export default defineMock({
|
|
|
250
289
|
* @type string | number | array | object
|
|
251
290
|
* 直接返回定义的数据
|
|
252
291
|
*
|
|
253
|
-
* @type (request: {
|
|
292
|
+
* @type (request: { headers, query, body, params }) => any | Promise<any>
|
|
254
293
|
* 如果传入一个函数,那么可以更加灵活的定义返回响应体数据
|
|
255
294
|
*/
|
|
256
295
|
body: {},
|
|
@@ -463,6 +502,24 @@ export default defineMock({
|
|
|
463
502
|
})
|
|
464
503
|
```
|
|
465
504
|
|
|
505
|
+
## 独立部署的小型mock服务
|
|
506
|
+
|
|
507
|
+
在一些场景中,可能会需要使用mock服务提供的数据支持,用于展示,但可能项目已完成打包构建部署,已脱离 `vite` 和本插件提供的 mock服务支持。由于本插件在设计之初,支持在mock文件中引入各种 `node` 模块,所以不能将 mock文件打包内联到客户端构建代码中。
|
|
508
|
+
|
|
509
|
+
为了能够满足这类场景,插件一方面提供了 `vite preview` 下的支持,同时还提供了在 `vite build` 时,也构建一个可独立部署的 小型mock服务应用,可以将这个应用部署到相关的环境,后通过其他http服务器如nginx做代理转发到实际端口实现mock支持。
|
|
510
|
+
|
|
511
|
+
构建默认输出到 `dist/mockServer` 目录中,并生成如下文件:
|
|
512
|
+
```sh
|
|
513
|
+
./mockServer
|
|
514
|
+
├── index.js
|
|
515
|
+
├── mock-data.js
|
|
516
|
+
└── package.json
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
在该目录下,执行 `npm install` 安装依赖后,可执行 `npm start` 即可启动 mock server。
|
|
520
|
+
默认端口为 `8080`。
|
|
521
|
+
可通过 `localhost:8080/` 访问相关的 `mock` 接口。
|
|
522
|
+
|
|
466
523
|
## Archives
|
|
467
524
|
|
|
468
525
|
[awesome-vite](https://github.com/vitejs/awesome-vite#helpers)
|