修复 embed 没有 env 时报错、支持 HTTP 400+ 状态码的内容显示 (#1772)

This commit is contained in:
吴多益 2021-04-09 12:40:36 +08:00 committed by GitHub
parent 4062950030
commit 92323f4804
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 37 deletions

View File

@ -108,7 +108,7 @@ export default class PlayGround extends React.Component {
router.push(to);
}
},
fetcher: config => {
fetcher: async config => {
config = {
dataType: 'json',
...config
@ -120,7 +120,31 @@ export default class PlayGround extends React.Component {
config.headers['Content-Type'] = 'application/json';
}
return axios[config.method](config.url, config.data, config);
//
config.validateStatus = function (status) {
return true;
};
const response = await axios[config.method](
config.url,
config.data,
config
);
if (response.status >= 400) {
if (response.data) {
if (response.data.msg) {
throw new Error(response.data.msg);
} else {
throw new Error(
'接口报错:' + JSON.stringify(response.data, null, 2)
);
}
} else {
throw new Error(`接口出错,状态码是 ${response.status}`);
}
}
return response;
},
isCancel: value => axios.isCancel(value),
notify: (type, msg) =>

View File

@ -125,7 +125,7 @@ export function embed(
const responseAdaptor = (api: any) => (value: any) => {
let response = value.data;
// 之前拼写错了,需要兼容
if (env.responseAdpater) {
if (env && env.responseAdpater) {
env.responseAdaptor = env.responseAdpater;
}
if (env && env.responseAdaptor) {
@ -248,10 +248,10 @@ export function embed(
location.href = to;
}
},
fetcher: (api: any) => {
fetcher: async (api: any) => {
let {url, method, data, responseType, config, headers} = api;
config = config || {};
config.withCredentials = true;
// config.withCredentials = true;
responseType && (config.responseType = responseType);
if (config.cancelExecutor) {
@ -277,10 +277,31 @@ export function embed(
config.headers['Content-Type'] = 'application/json';
}
// 支持返回各种报错信息
config.validateStatus = function (status) {
return true;
};
data && (config.data = data);
return axios(url, config)
.then(attachmentAdpator)
.then(responseAdaptor(api));
let response = await axios(url, config);
response = attachmentAdpator(response);
response = responseAdaptor(api)(response);
if (response.status >= 400) {
if (response.data) {
if (response.data.msg) {
throw new Error(response.data.msg);
} else {
throw new Error(
'接口报错:' + JSON.stringify(response.data, null, 2)
);
}
} else {
throw new Error(`接口出错,状态码是 ${response.status}`);
}
}
return response;
},
isCancel: (value: any) => (axios as any).isCancel(value),
copy: (contents: string, options: any = {}) => {

View File

@ -32,7 +32,7 @@ class AMISComponent extends React.Component {
return this.state.schema ? (
<div>
{renderAmis(this.state.schema, this.state.props, {
fetcher: ({
fetcher: async ({
url, // 接口地址
method, // 请求方法 get、post、put、delete
data, // 请求数据
@ -40,39 +40,42 @@ class AMISComponent extends React.Component {
config, // 其他配置
headers // 请求头
}: any) => {
config = config || {};
config.withCredentials = true;
responseType && (config.responseType = responseType);
config = {
dataType: 'json',
...config
};
if (config.cancelExecutor) {
config.cancelToken = new (axios as any).CancelToken(
config.cancelExecutor
);
}
config.headers = headers || {};
if (method !== 'post' && method !== 'put' && method !== 'patch') {
if (data) {
config.params = data;
}
return (axios as any)[method](url, config);
} else if (data && data instanceof FormData) {
config.headers = config.headers || {};
config.headers['Content-Type'] = 'multipart/form-data';
} else if (
data &&
typeof data !== 'string' &&
!(data instanceof Blob) &&
!(data instanceof ArrayBuffer)
) {
data = JSON.stringify(data);
if (config.dataType === 'json' && config.data) {
config.data = JSON.stringify(config.data);
config.headers = config.headers || {};
config.headers['Content-Type'] = 'application/json';
}
return (axios as any)[method](url, data, config);
// 支持返回各种报错信息
config.validateStatus = function (status) {
return true;
};
const response = await axios[config.method](
config.url,
config.data,
config
);
if (response.status >= 400) {
if (response.data) {
if (response.data.msg) {
throw new Error(response.data.msg);
} else {
throw new Error(
'接口报错:' + JSON.stringify(response.data, null, 2)
);
}
} else {
throw new Error(`接口出错,状态码是 ${response.status}`);
}
}
return response;
},
isCancel: (value: any) => (axios as any).isCancel(value),
copy: content => {