sdk 的 fetcher 方法调整

This commit is contained in:
liaoxuezhi 2019-09-09 09:45:37 +08:00
parent f33e585064
commit 5b1e36cd61
1 changed files with 19 additions and 13 deletions

View File

@ -156,24 +156,29 @@ export function embed(container: string | HTMLElement, schema: any, data: any, e
location.href = to; location.href = to;
} }
}, },
fetcher: (api:any) => { fetcher: (api: any) => {
let { let {
url, url,
method, method,
data, data,
config responseType,
config,
headers
} = api; } = api;
config = config || {}; config = config || {};
config.withCredentials = true; config.withCredentials = true;
responseType && (config.responseType = responseType);
if (method !== 'post' && method !== 'put' && method !== 'patch') {
if (data) { if (config.cancelExecutor) {
config.params = data; config.cancelToken = new (axios as any).CancelToken(config.cancelExecutor);
} }
return (axios as any)[method](url, config).then(responseAdpater(api)); config.headers = headers || {};
config.method = method;
if (method === 'get' && data) {
config.params = data;
} else if (data && data instanceof FormData) { } else if (data && data instanceof FormData) {
// config.headers = config.headers || {};
// config.headers['Content-Type'] = 'multipart/form-data'; // config.headers['Content-Type'] = 'multipart/form-data';
} else if (data } else if (data
&& typeof data !== 'string' && typeof data !== 'string'
@ -181,11 +186,12 @@ export function embed(container: string | HTMLElement, schema: any, data: any, e
&& !(data instanceof ArrayBuffer) && !(data instanceof ArrayBuffer)
) { ) {
data = JSON.stringify(data); data = JSON.stringify(data);
config.headers = config.headers || {};
config.headers['Content-Type'] = 'application/json'; config.headers['Content-Type'] = 'application/json';
} }
return (axios as any)[method](url, data, config).then(responseAdpater(api)); data && (config.data = data);
return axios(url, config)
.then(responseAdpater(api));
}, },
isCancel: (value: any) => (axios as any).isCancel(value), isCancel: (value: any) => (axios as any).isCancel(value),
copy: (contents: string, options: any = {}) => { copy: (contents: string, options: any = {}) => {