@yeepay/client-utils 3.0.1 → 3.0.3
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 +72 -0
- package/dist/index.js +12 -5
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -4,8 +4,80 @@
|
|
|
4
4
|
|
|
5
5
|
> 3.0 版本之后,ESMOnly.
|
|
6
6
|
|
|
7
|
+
# Features
|
|
8
|
+
|
|
7
9
|
- **getQueryObject**: 获取 url 参数
|
|
8
10
|
- **setTokenFromUrl(key)**: 通过 url 中的参数 key 设置 TOKEN, 并存入 cookie 中
|
|
9
11
|
- **getToken**: 从 cookie 中获取 TOKEN
|
|
10
12
|
- **removeToken**: 从 cookie 中删除 TOKEN
|
|
11
13
|
- **serviceFactory**: 请求封装的工厂函数
|
|
14
|
+
|
|
15
|
+
## serviceFactory 介绍
|
|
16
|
+
|
|
17
|
+
### 基本使用
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
import { serviceFactory } from '@yeepay/client-utils'
|
|
21
|
+
|
|
22
|
+
const service = serviceFactory(
|
|
23
|
+
{
|
|
24
|
+
baseUrl: '/xxx-server',
|
|
25
|
+
headers: {},
|
|
26
|
+
},
|
|
27
|
+
successCallback,
|
|
28
|
+
failCallback,
|
|
29
|
+
unauthorizedCallback,
|
|
30
|
+
forbiddenCallback,
|
|
31
|
+
notfoundCallback,
|
|
32
|
+
)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Mock数据
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
import { serviceFactory } from '@yeepay/client-utils'
|
|
39
|
+
|
|
40
|
+
const service = serviceFactory(
|
|
41
|
+
{
|
|
42
|
+
baseUrl: '/xxx-server',
|
|
43
|
+
headers: {},
|
|
44
|
+
// glob mock 数据文件位置
|
|
45
|
+
mockModules: import.meta.glob('./mock/**/*.json', { eager: true }),
|
|
46
|
+
},
|
|
47
|
+
successCallback,
|
|
48
|
+
failCallback,
|
|
49
|
+
unauthorizedCallback,
|
|
50
|
+
forbiddenCallback,
|
|
51
|
+
notfoundCallback,
|
|
52
|
+
)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
比如 mock 数据文件 `./mock/get/user.json` 内容如下:
|
|
56
|
+
|
|
57
|
+
```json
|
|
58
|
+
{
|
|
59
|
+
"code": "000000",
|
|
60
|
+
"data": {
|
|
61
|
+
"name": "John Doe"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
在你调用 uri 为 /get/user 的接口时,mock 数据将会被返回。
|
|
67
|
+
|
|
68
|
+
你还可以使用 js 文件来自定义 status、response、headers:
|
|
69
|
+
|
|
70
|
+
比如 mock 数据文件 `./mock/get/user.ts` 内容如下:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import type { AxiosRequestConfig } from 'axios'
|
|
74
|
+
|
|
75
|
+
export default (config: AxiosRequestConfig) => {
|
|
76
|
+
return [400, {
|
|
77
|
+
code: '000000',
|
|
78
|
+
data: {
|
|
79
|
+
message: 'User not found'
|
|
80
|
+
},
|
|
81
|
+
}, { 'new-header-hello': 'world' }]
|
|
82
|
+
}
|
|
83
|
+
```
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { toArray } from "@imyangyong/utils";
|
|
2
2
|
import axios from "axios";
|
|
3
|
+
import { bold, cyan, gray, green, red, yellow } from "ansis";
|
|
3
4
|
import AxiosMockAdapter from "axios-mock-adapter";
|
|
4
5
|
import Cookie from "js-cookie";
|
|
5
6
|
|
|
6
7
|
//#region src/request/mock.ts
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
+
* 查找数组中最长相同前缀连续子序列
|
|
9
10
|
*/
|
|
10
11
|
function longestCommonPrefix(strs) {
|
|
11
12
|
if (!strs || strs.length === 0) return "";
|
|
@@ -16,9 +17,6 @@ function longestCommonPrefix(strs) {
|
|
|
16
17
|
}
|
|
17
18
|
return prefix;
|
|
18
19
|
}
|
|
19
|
-
function escapeRegExp(string) {
|
|
20
|
-
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
21
|
-
}
|
|
22
20
|
function setupMockAdapter(axiosInstance, mockModules) {
|
|
23
21
|
if (!mockModules) return;
|
|
24
22
|
const mock = new AxiosMockAdapter(axiosInstance);
|
|
@@ -29,7 +27,16 @@ function setupMockAdapter(axiosInstance, mockModules) {
|
|
|
29
27
|
const dotIndex = path.lastIndexOf(".");
|
|
30
28
|
const uri = path.slice(longestPrefixIndex, dotIndex);
|
|
31
29
|
const registerMock = (mod) => {
|
|
32
|
-
|
|
30
|
+
const replyFunc = typeof mod.default === "function" ? mod.default : () => [200, mod.default];
|
|
31
|
+
mock.onAny(new RegExp(uri)).reply((config) => {
|
|
32
|
+
const response = replyFunc(config);
|
|
33
|
+
const status = String(response[0]);
|
|
34
|
+
const statusColor = status.startsWith("2") ? green(status) : status.startsWith("3") ? yellow(status) : red(status);
|
|
35
|
+
const data = response[1] ? JSON.stringify(response[1], null, 2) : "";
|
|
36
|
+
const headers = response[2] ? JSON.stringify(response[2], null, 2) : "";
|
|
37
|
+
console.log(`${gray("Mocked:")} ${bold("URI")}: ${cyan.bold(uri)} \n${bold("Status")}: ${statusColor} \n${bold("Response")}: ${data} \n${headers ? `${bold("Headers:")} ${headers}` : ""}`);
|
|
38
|
+
return response;
|
|
39
|
+
});
|
|
33
40
|
};
|
|
34
41
|
if (typeof mockModules[path] === "function") mockModules[path]().then(registerMock).catch((error) => {
|
|
35
42
|
console.error(`@yeepay/client-utils mock trying to loading ${path} failed`, error);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yeepay/client-utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "3.0.
|
|
4
|
+
"version": "3.0.3",
|
|
5
5
|
"description": "shared utilities for yeepay client packages",
|
|
6
6
|
"author": "Yong Yang",
|
|
7
7
|
"homepage": "http://gitlab.yeepay.com/ued/client-utils#readme",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@imyangyong/utils": "^0.8.0",
|
|
23
|
+
"ansis": "^4.1.0",
|
|
23
24
|
"axios": "^1.7.0",
|
|
24
25
|
"axios-mock-adapter": "^2.1.0",
|
|
25
26
|
"js-cookie": "^3.0.5"
|