-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
129 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { get as lodashGet } from 'lodash'; | ||
|
||
const defaultGetter = { | ||
codePath: 'code', | ||
successCode: 0, | ||
dataPath: 'data', | ||
totalPath: 'total', | ||
listPath: 'list', | ||
messagePath: 'msg', | ||
}; | ||
|
||
/** | ||
* 从 API Response 中获取相关字段,如是否成功、total、data 等 | ||
* https://github.com/vipshop/ams/pull/125 | ||
* | ||
* @param {Object} response : api response data | ||
* @param {String} action : read/update/list/delete 增删改查 | ||
* @param {Object} actionGetter : 适配更多接口的返回值 | ||
* | ||
* ams 默认接口数据结构: https://vipshop.github.io/ams/api/api.html#%E9%80%9A%E7%94%A8%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84 | ||
* | ||
* -------常规请求---------- | ||
* { | ||
[codePath]: [successCode], | ||
[dataPath]: String, | ||
[messagePath]: String | ||
* } | ||
* | ||
* | ||
* ------- 列表请求---------- | ||
* { | ||
[codePath]: [successCode], | ||
[dataPath]: { | ||
[dataPath.listPath]: [], | ||
[dataPath.totalPath]: Number, | ||
}, | ||
[messagePath]: String | ||
* } | ||
* | ||
* | ||
*/ | ||
export function responseHandler(response, action, actionGetter) { | ||
const configGetter = this.getConfig(`resource.api.${action}.getter`); | ||
const getter = { | ||
...defaultGetter, | ||
...actionGetter, | ||
...configGetter | ||
}; | ||
|
||
const code = lodashGet(response.data, getter.codePath); // api response(success/result)对应的值 | ||
// eslint-disable-next-line | ||
const isSuccess = code == getter.successCode; | ||
return { | ||
code, | ||
getter, | ||
isSuccess, | ||
data: lodashGet(response.data, getter.dataPath), | ||
total: lodashGet(response.data, getter.totalPath), | ||
message: lodashGet(response.data, getter.messagePath), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters