chore: jssdk 的 responseAdaptor 扩充参数,支持拿 response 整个对象,包括返回的 headers 和… (#4229)

* chore: jssdk 的 responseAdpator 扩充参数,支持拿 response 整个对象,包括返回的 headers 和状态码信息

* chore: jssdk 的 responseAdpator 扩充参数,支持拿 response 整个对象,包括返回的 headers 和状态码信息
This commit is contained in:
liaoxuezhi 2022-05-09 10:43:45 +08:00 committed by GitHub
parent 0e317b8f97
commit 773700cf68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 15 deletions

View File

@ -156,8 +156,8 @@ let amisScoped = amis.embed(
//
// 全局 api 适配器。
// 另外在 amis 配置项中的 api 也可以配置适配器,针对某个特定接口单独处理。
// responseAdaptor(api, response, query, request) {
// return response;
// responseAdaptor(api, payload, query, request, response) {
// return payload;
// }
//
// 用来接管页面跳转,比如用 location.href 或 window.open或者自己实现 amis 配置更新

View File

@ -58,35 +58,35 @@ export function embed(
return request;
};
const responseAdaptor = (api: any) => (value: any) => {
let response = value.data || {}; // blob 下可能会返回内容为空?
const responseAdaptor = (api: any) => (response: any) => {
let payload = response.data || {}; // blob 下可能会返回内容为空?
// 之前拼写错了,需要兼容
if (env && env.responseAdpater) {
env.responseAdaptor = env.responseAdpater;
}
if (env && env.responseAdaptor) {
const url = api.url;
const idx = api.url.indexOf('?');
const query = ~idx ? qs.parse(api.url.substring(idx)) : {};
const idx = url.indexOf('?');
const query = ~idx ? qs.parse(url.substring(idx)) : {};
const request = {
...api,
query: query,
body: api.data
};
response = env.responseAdaptor(api, response, query, request);
payload = env.responseAdaptor(api, payload, query, request, response);
} else {
if (response.hasOwnProperty('errno')) {
response.status = response.errno;
response.msg = response.errmsg;
} else if (response.hasOwnProperty('no')) {
response.status = response.no;
response.msg = response.error;
if (payload.hasOwnProperty('errno')) {
payload.status = payload.errno;
payload.msg = payload.errmsg;
} else if (payload.hasOwnProperty('no')) {
payload.status = payload.no;
payload.msg = payload.error;
}
}
const result = {
...value,
data: response
...response,
data: payload
};
return result;
};