mocksw 1.0.0 → 1.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/README.md CHANGED
@@ -14,6 +14,8 @@
14
14
 
15
15
  ```sh
16
16
  npm install mocksw -D
17
+ # or
18
+ npm install mocksw -D
17
19
  ```
18
20
 
19
21
  ## 🚀 快速开始
@@ -24,6 +26,8 @@ npm install mocksw -D
24
26
 
25
27
  ```sh
26
28
  npx mocksw init public
29
+ # or
30
+ pnpm exec mocksw init public
27
31
  ```
28
32
 
29
33
  这将在你的公共目录下生成 `swMockWorker.js` worker环境。
@@ -63,7 +67,20 @@ httpRequest.init('www.vadmin.test.com').then(() => {
63
67
  console.log('🚀 完美 Mock 环境已就绪');
64
68
  // 挂载 Vue 实例 推荐在 init 后挂载
65
69
  app.mount('#app');
70
+ // 现在这个接口调用会被拦截 并返回模拟响应
71
+ login();
66
72
  });
73
+
74
+ function login() {
75
+ axios
76
+ .post('http://www.vadmin.test.com/user/login', {
77
+ username: 'admin',
78
+ password: '123456',
79
+ })
80
+ .then(res => {
81
+ console.log(res.data);
82
+ });
83
+ }
67
84
  ```
68
85
 
69
86
  启动项目,即可在浏览器中使用 Mock API。
@@ -87,6 +104,7 @@ httpRequest.init('www.vadmin.test.com').then(() => {
87
104
  - `query`:URL 查询参数对象
88
105
  - `headers`:原生请求头
89
106
  - `method`:请求方法(如 GET、POST 等)
107
+ - `headers`:原生请求头
90
108
 
91
109
  `MockResponse` (回调第二个参数)
92
110
 
package/dist/index.js CHANGED
@@ -92,19 +92,14 @@ var MockClient = class {
92
92
  });
93
93
  navigator.serviceWorker.addEventListener("message", async (event) => {
94
94
  if (event.data.type === "MSW_SIMULATE_REQUEST") {
95
- const { method, url, body, query } = event.data;
95
+ const { method, url } = event.data.data;
96
96
  const port = event.ports[0];
97
97
  const handlerKey = `${method}:${url}`;
98
98
  const handler = this.handlers.get(handlerKey);
99
99
  if (!port) return;
100
100
  if (handler) {
101
101
  const res = new ResponseActions(port);
102
- await handler({
103
- url,
104
- method,
105
- body,
106
- query
107
- }, res);
102
+ await handler(Object.assign({}, event.data.data), res);
108
103
  } else port.postMessage({
109
104
  body: { error: "Not Found" },
110
105
  status: 404
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "mocksw",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "@zstings",
7
7
  "description": "一个基于 Service Worker 的轻量级、零侵入 API 模拟工具",
8
8
  "bin": {
9
- "mocksw": "./dist/cli.mjs"
9
+ "mocksw": "dist/cli.mjs"
10
10
  },
11
11
  "files": [
12
12
  "dist",
@@ -25,7 +25,6 @@ self.addEventListener('fetch', event => {
25
25
  status: status || 200,
26
26
  headers: {
27
27
  'Content-Type': 'application/json',
28
- 'Content-Length': 100000, // 显式设置大小
29
28
  ...headers,
30
29
  },
31
30
  }),
@@ -37,10 +36,13 @@ self.addEventListener('fetch', event => {
37
36
  allClients[0].postMessage(
38
37
  {
39
38
  type: 'MSW_SIMULATE_REQUEST',
40
- url: url.pathname,
41
- method: event.request.method,
42
- body: requestBody,
43
- query: Object.fromEntries(url.searchParams.entries()),
39
+ data: {
40
+ url: url.pathname,
41
+ method: event.request.method,
42
+ body: requestBody,
43
+ query: Object.fromEntries(url.searchParams.entries()),
44
+ headers: Object.fromEntries(event.request.headers.entries()),
45
+ },
44
46
  },
45
47
  [channel.port2],
46
48
  );