@hzab/data-model 0.1.1 → 0.2.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 +10 -1
- package/package.json +1 -1
- package/src/data-model.js +22 -8
- package/src/hooks.js +7 -0
- package/src/index.js +1 -0
package/README.md
CHANGED
|
@@ -9,7 +9,16 @@
|
|
|
9
9
|
## 示例
|
|
10
10
|
|
|
11
11
|
```jsx
|
|
12
|
-
import DataModel
|
|
12
|
+
import DataModel, {
|
|
13
|
+
// 设置 token 到 localStorage 及 cookie,可以设置 opt.hasCookie(setToken(token, { hasCookie: false })) 关闭设置 token 到 cookie 上
|
|
14
|
+
setToken,
|
|
15
|
+
// 从 cookie 或 localStorage 获取 token
|
|
16
|
+
getToken,
|
|
17
|
+
// 设置默认兜底的错误信息
|
|
18
|
+
setDefaultErrMsg,
|
|
19
|
+
// 设置 DataModel 中默认的 axios
|
|
20
|
+
setDefaultAxios,
|
|
21
|
+
} from "@hzab/data-model";
|
|
13
22
|
|
|
14
23
|
const listDM = new DataModel({
|
|
15
24
|
// query 参数
|
package/package.json
CHANGED
package/src/data-model.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import _ from "lodash";
|
|
2
2
|
import axios from "axios";
|
|
3
3
|
|
|
4
|
-
const
|
|
4
|
+
const DEFAULT_MSG = "未知错误";
|
|
5
|
+
const _$Temp = {
|
|
6
|
+
defaultMsg: DEFAULT_MSG,
|
|
7
|
+
};
|
|
5
8
|
|
|
6
9
|
class DataModel {
|
|
7
10
|
constructor(params) {
|
|
@@ -281,16 +284,17 @@ class DataModel {
|
|
|
281
284
|
let _data = data ?? {};
|
|
282
285
|
if (_.isObject(_data) && _data.message === undefined) {
|
|
283
286
|
// 前缀 _ 避免与 data 里已有的 message 冲突
|
|
284
|
-
_data._message = message || msg;
|
|
285
287
|
_data._msg = message || msg;
|
|
288
|
+
_data._message = _data._msg;
|
|
286
289
|
}
|
|
287
290
|
resolve(_data);
|
|
288
291
|
} else {
|
|
289
|
-
const
|
|
292
|
+
const _msg = message || msg || _$Temp.defaultMsg;
|
|
293
|
+
const error = new Error(_msg);
|
|
290
294
|
error.code = code;
|
|
291
295
|
error.response = response;
|
|
292
|
-
error.
|
|
293
|
-
error.
|
|
296
|
+
error._msg = _msg;
|
|
297
|
+
error._message = _msg;
|
|
294
298
|
reject(error);
|
|
295
299
|
}
|
|
296
300
|
}
|
|
@@ -298,8 +302,12 @@ class DataModel {
|
|
|
298
302
|
errorHandler(err, reject) {
|
|
299
303
|
const response = err.response || err;
|
|
300
304
|
if (response) {
|
|
301
|
-
const message =
|
|
302
|
-
|
|
305
|
+
const message =
|
|
306
|
+
(response.data && (response.data.message || response.data.msg)) ||
|
|
307
|
+
response.msg ||
|
|
308
|
+
response.statusText ||
|
|
309
|
+
_$Temp.defaultMsg;
|
|
310
|
+
const error = new Error(message);
|
|
303
311
|
error.code = response.status;
|
|
304
312
|
error.response = response;
|
|
305
313
|
if (message) {
|
|
@@ -309,7 +317,7 @@ class DataModel {
|
|
|
309
317
|
}
|
|
310
318
|
return reject(error);
|
|
311
319
|
}
|
|
312
|
-
return reject(err);
|
|
320
|
+
return reject(err || { _msg: _$Temp.defaultMsg });
|
|
313
321
|
}
|
|
314
322
|
}
|
|
315
323
|
|
|
@@ -319,6 +327,12 @@ function setDefaultAxios(ax) {
|
|
|
319
327
|
}
|
|
320
328
|
}
|
|
321
329
|
|
|
330
|
+
export function setDefaultErrMsg(msg) {
|
|
331
|
+
if (msg) {
|
|
332
|
+
_$Temp.defaultMsg = msg;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
322
336
|
export { axios, DataModel, setDefaultAxios };
|
|
323
337
|
|
|
324
338
|
export default DataModel;
|
package/src/hooks.js
ADDED